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'
1 service uses CacheableResponseSubscriber
File
- src/
EventSubscriber/ CacheableResponseSubscriber.php, line 14
Namespace
Drupal\purge\EventSubscriberView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheableResponseSubscriber:: |
protected | property | The tagsheaders service for iterating the available header plugins. | |
CacheableResponseSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
CacheableResponseSubscriber:: |
public | function | Add cache tags headers on cacheable responses. | |
CacheableResponseSubscriber:: |
public | function | Construct a CacheableResponseSubscriber object. |