You are here

class RouteNameResponseSubscriber in Page Manager 8

Same name and namespace in other branches
  1. 8.4 src/EventSubscriber/RouteNameResponseSubscriber.php \Drupal\page_manager\EventSubscriber\RouteNameResponseSubscriber

Adds the route name as a cache tag to all cacheable responses.

Hierarchy

  • class \Drupal\page_manager\EventSubscriber\RouteNameResponseSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RouteNameResponseSubscriber

1 file declares its use of RouteNameResponseSubscriber
RouteNameResponseSubscriberTest.php in tests/src/Unit/RouteNameResponseSubscriberTest.php
Contains \Drupal\Tests\page_manager\Unit\RouteNameResponseSubscriberTest.
1 string reference to 'RouteNameResponseSubscriber'
page_manager.services.yml in ./page_manager.services.yml
page_manager.services.yml
1 service uses RouteNameResponseSubscriber
page_manager.route_name_response_subscriber in ./page_manager.services.yml
Drupal\page_manager\EventSubscriber\RouteNameResponseSubscriber

File

src/EventSubscriber/RouteNameResponseSubscriber.php, line 19
Contains \Drupal\page_manager\EventSubscriber\RouteNameResponseSubscriber.

Namespace

Drupal\page_manager\EventSubscriber
View source
class RouteNameResponseSubscriber implements EventSubscriberInterface {

  /**
   * The master route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs a new RouteNameResponseSubscriber.
   *
   * @param \Drupal\Core\Routing\StackedRouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(StackedRouteMatchInterface $route_match) {
    $this->routeMatch = $route_match
      ->getMasterRouteMatch();
  }

  /**
   * Adds the route name as a cache tag to all cacheable responses.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
   *   The event to process.
   */
  public function onResponse(FilterResponseEvent $event) {
    $response = $event
      ->getResponse();
    if ($response instanceof CacheableResponseInterface) {
      $cacheability_metadata = $response
        ->getCacheableMetadata();

      // If the route specifies a 'base route name', use that. Otherwise fall
      // back to the route name. The 'base route name' is specified in
      // \Drupal\page_manager\Routing\PageManagerRoutes.
      $route_name = $this->routeMatch
        ->getParameter('base_route_name') ?: $this->routeMatch
        ->getRouteName();
      $cacheability_metadata
        ->addCacheTags([
        'page_manager_route_name:' . $route_name,
      ]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Run before dynamic_page_cache_subscriber:onResponse.
    $events[KernelEvents::RESPONSE][] = [
      'onResponse',
      101,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RouteNameResponseSubscriber::$routeMatch protected property The master route match.
RouteNameResponseSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RouteNameResponseSubscriber::onResponse public function Adds the route name as a cache tag to all cacheable responses.
RouteNameResponseSubscriber::__construct public function Constructs a new RouteNameResponseSubscriber.