You are here

class WorkspaceRequestSubscriber in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/workspaces/src/EventSubscriber/WorkspaceRequestSubscriber.php \Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber

Provides a event subscriber for setting workspace-specific cache keys.

Hierarchy

  • class \Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of WorkspaceRequestSubscriber

1 file declares its use of WorkspaceRequestSubscriber
WorkspaceRequestSubscriberTest.php in core/modules/workspaces/tests/src/Unit/WorkspaceRequestSubscriberTest.php
1 string reference to 'WorkspaceRequestSubscriber'
workspaces.services.yml in core/modules/workspaces/workspaces.services.yml
core/modules/workspaces/workspaces.services.yml
1 service uses WorkspaceRequestSubscriber
workspaces.workspace_subscriber in core/modules/workspaces/workspaces.services.yml
Drupal\workspaces\EventSubscriber\WorkspaceRequestSubscriber

File

core/modules/workspaces/src/EventSubscriber/WorkspaceRequestSubscriber.php, line 18

Namespace

Drupal\workspaces\EventSubscriber
View source
class WorkspaceRequestSubscriber implements EventSubscriberInterface {

  /**
   * The alias manager that caches alias lookups based on the request.
   *
   * @var \Drupal\path_alias\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * The current path.
   *
   * @var \Drupal\Core\Path\CurrentPathStack
   */
  protected $currentPath;

  /**
   * The route provider to load routes by name.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;

  /**
   * The workspace manager.
   *
   * @var \Drupal\workspaces\WorkspaceManagerInterface
   */
  protected $workspaceManager;

  /**
   * Constructs a new WorkspaceRequestSubscriber instance.
   *
   * @param \Drupal\path_alias\AliasManagerInterface $alias_manager
   *   The alias manager.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Drupal\workspaces\WorkspaceManagerInterface $workspace_manager
   *   The workspace manager.
   */
  public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path, RouteProviderInterface $route_provider, WorkspaceManagerInterface $workspace_manager) {
    $this->aliasManager = $alias_manager;
    $this->currentPath = $current_path;
    $this->routeProvider = $route_provider;
    $this->workspaceManager = $workspace_manager;
  }

  /**
   * Sets the cache key on the alias manager cache decorator.
   *
   * KernelEvents::CONTROLLER is used in order to be executed after routing.
   *
   * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
   *   The Event to process.
   */
  public function onKernelController(FilterControllerEvent $event) {

    // Set the cache key on the alias manager cache decorator.
    if ($event
      ->isMasterRequest() && $this->workspaceManager
      ->hasActiveWorkspace()) {
      $cache_key = $this->workspaceManager
        ->getActiveWorkspace()
        ->id() . ':' . rtrim($this->currentPath
        ->getPath($event
        ->getRequest()), '/');
      $this->aliasManager
        ->setCacheKey($cache_key);
    }
  }

  /**
   * Adds the active workspace as a cache key part to the route provider.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   An event object.
   */
  public function onKernelRequest(GetResponseEvent $event) {
    if ($this->workspaceManager
      ->hasActiveWorkspace() && $this->routeProvider instanceof CacheableRouteProviderInterface) {
      $this->routeProvider
        ->addExtraCacheKeyPart('workspace', $this->workspaceManager
        ->getActiveWorkspace()
        ->id());
    }
  }

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

    // Use a priority of 190 in order to run after the generic core subscriber.
    // @see \Drupal\Core\EventSubscriber\PathSubscriber::getSubscribedEvents()
    $events[KernelEvents::CONTROLLER][] = [
      'onKernelController',
      190,
    ];

    // Use a priority of 33 in order to run before Symfony's router listener.
    // @see \Symfony\Component\HttpKernel\EventListener\RouterListener::getSubscribedEvents()
    $events[KernelEvents::REQUEST][] = [
      'onKernelRequest',
      33,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
WorkspaceRequestSubscriber::$aliasManager protected property The alias manager that caches alias lookups based on the request.
WorkspaceRequestSubscriber::$currentPath protected property The current path.
WorkspaceRequestSubscriber::$routeProvider protected property The route provider to load routes by name.
WorkspaceRequestSubscriber::$workspaceManager protected property The workspace manager.
WorkspaceRequestSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
WorkspaceRequestSubscriber::onKernelController public function Sets the cache key on the alias manager cache decorator.
WorkspaceRequestSubscriber::onKernelRequest public function Adds the active workspace as a cache key part to the route provider.
WorkspaceRequestSubscriber::__construct public function Constructs a new WorkspaceRequestSubscriber instance.