You are here

class CacheableResponseSubscriber in Akamai 8.3

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

Hierarchy

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

Expanded class hierarchy of CacheableResponseSubscriber

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

File

src/EventSubscriber/CacheableResponseSubscriber.php, line 17

Namespace

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

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Cache tag formatter.
   *
   * @var \Drupal\akamai\Helper\CacheTagFormatter
   */
  protected $tagFormatter;

  /**
   * The event dispatcher.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Constructs a new CacheableResponseSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\akamai\Helper\CacheTagFormatter $formatter
   *   The cache tag formatter.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   The event dispatcher.
   */
  public function __construct(ConfigFactoryInterface $config_factory, CacheTagFormatter $formatter, EventDispatcherInterface $event_dispatcher) {
    $this->configFactory = $config_factory;
    $this->tagFormatter = $formatter;
    $this->eventDispatcher = $event_dispatcher;
  }

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

  /**
   * Add cache tags header on cacheable responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onRespond(FilterResponseEvent $event) {
    if (!$event
      ->isMasterRequest()) {
      return;
    }
    $response = $event
      ->getResponse();
    $config = $this->configFactory
      ->get('akamai.settings');
    $header = $config
      ->get('edge_cache_tag_header');

    // Send headers if response is cacheable and the setting is enabled.
    if ($header && $response instanceof CacheableResponseInterface) {
      $tags = $response
        ->getCacheableMetadata()
        ->getCacheTags();
      $blacklist = $config
        ->get('edge_cache_tag_header_blacklist');
      $blacklist = is_array($blacklist) ? $blacklist : [];
      $tags = array_filter($tags, function ($tag) use ($blacklist) {
        foreach ($blacklist as $prefix) {
          if (strpos($tag, $prefix) !== FALSE) {
            return FALSE;
          }
        }
        return TRUE;
      });

      // Instantiate our event.
      $event = new AkamaiHeaderEvents($tags);
      $this->eventDispatcher
        ->dispatch(AkamaiHeaderEvents::HEADER_CREATION, $event);
      $tags = $event->data;
      foreach ($tags as &$tag) {
        $tag = $this->tagFormatter
          ->format($tag);
      }
      $response->headers
        ->set('Edge-Cache-Tag', implode(',', $tags));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableResponseSubscriber::$configFactory protected property The config factory.
CacheableResponseSubscriber::$eventDispatcher protected property The event dispatcher.
CacheableResponseSubscriber::$tagFormatter protected property Cache tag formatter.
CacheableResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
CacheableResponseSubscriber::onRespond public function Add cache tags header on cacheable responses.
CacheableResponseSubscriber::__construct public function Constructs a new CacheableResponseSubscriber.