You are here

class PathSubscriber in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php \Drupal\Core\EventSubscriber\PathSubscriber

Provides a path subscriber that converts path aliases.

Hierarchy

Expanded class hierarchy of PathSubscriber

1 string reference to 'PathSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses PathSubscriber
path_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\PathSubscriber

File

core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php, line 21
Contains \Drupal\Core\EventSubscriber\PathSubscriber.

Namespace

Drupal\Core\EventSubscriber
View source
class PathSubscriber implements EventSubscriberInterface {

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

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

  /**
   * Constructs a new PathSubscriber instance.
   *
   * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
   *   The alias manager.
   * @param \Drupal\Core\Path\CurrentPathStack $current_path
   *   The current path.
   */
  public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
    $this->aliasManager = $alias_manager;
    $this->currentPath = $current_path;
  }

  /**
   * 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
      ->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
      $this->aliasManager
        ->setCacheKey(rtrim($this->currentPath
        ->getPath($event
        ->getRequest()), '/'));
    }
  }

  /**
   * Ensures system paths for the request get cached.
   */
  public function onKernelTerminate(PostResponseEvent $event) {
    $this->aliasManager
      ->writeCache();
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::CONTROLLER][] = array(
      'onKernelController',
      200,
    );
    $events[KernelEvents::TERMINATE][] = array(
      'onKernelTerminate',
      200,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathSubscriber::$aliasManager protected property The alias manager that caches alias lookups based on the request.
PathSubscriber::$currentPath protected property The current path.
PathSubscriber::getSubscribedEvents static function Registers the methods in this class that should be listeners. Overrides EventSubscriberInterface::getSubscribedEvents
PathSubscriber::onKernelController public function Sets the cache key on the alias manager cache decorator.
PathSubscriber::onKernelTerminate public function Ensures system paths for the request get cached.
PathSubscriber::__construct public function Constructs a new PathSubscriber instance.