You are here

class CacheableResponseSubscriber in Purge 8.3

Add cache tags headers on cacheable responses, for external caching systems.

Hierarchy

  • class \Drupal\purge\EventSubscriber\CacheableResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of CacheableResponseSubscriber

1 string reference to 'CacheableResponseSubscriber'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses CacheableResponseSubscriber
purge.tagsheaders.cacheable_response_subscriber in ./purge.services.yml
Drupal\purge\EventSubscriber\CacheableResponseSubscriber

File

src/EventSubscriber/CacheableResponseSubscriber.php, line 14

Namespace

Drupal\purge\EventSubscriber
View source
class CacheableResponseSubscriber implements EventSubscriberInterface {

  /**
   * The tagsheaders service for iterating the available header plugins.
   *
   * @var \Drupal\purge\Plugin\Purge\TagsHeader\TagsHeadersServiceInterface
   */
  protected $purgeTagsHeaders;

  /**
   * Construct a CacheableResponseSubscriber object.
   *
   * @param \Drupal\purge\Plugin\Purge\TagsHeader\TagsHeadersServiceInterface $purge_tagsheaders
   *   The tagsheaders service for iterating the available header plugins.
   */
  public function __construct(TagsHeadersServiceInterface $purge_tagsheaders) {
    $this->purgeTagsHeaders = $purge_tagsheaders;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::RESPONSE][] = [
      'onRespond',
    ];
    return $events;
  }

  /**
   * Add cache tags headers on cacheable responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }

    // Only set any headers when this is a cacheable response.
    $response = $event
      ->getResponse();
    if ($response instanceof CacheableResponseInterface) {

      // Iterate all tagsheader plugins and add a header for each plugin.
      $tags = $response
        ->getCacheableMetadata()
        ->getCacheTags();
      foreach ($this->purgeTagsHeaders as $header) {
        if ($header
          ->isEnabled()) {

          // Retrieve the header name and perform a few simple sanity checks.
          $name = $header
            ->getHeaderName();
          if (!is_string($name) || empty(trim($name))) {
            $plugin_id = $header
              ->getPluginId();
            throw new \LogicException("Header plugin '{$plugin_id}' should return a non-empty string on ::getHeaderName()!");
          }
          $response->headers
            ->set($name, $header
            ->getValue($tags));
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableResponseSubscriber::$purgeTagsHeaders protected property The tagsheaders service for iterating the available header plugins.
CacheableResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CacheableResponseSubscriber::onRespond public function Add cache tags headers on cacheable responses.
CacheableResponseSubscriber::__construct public function Construct a CacheableResponseSubscriber object.