class PathSubscriber

Access subscriber for controller requests.

Hierarchy

Expanded class hierarchy of PathSubscriber

1 string reference to 'PathSubscriber'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml

File

drupal/core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php, line 22
Definition of Drupal\Core\EventSubscriber\PathSubscriber.

Namespace

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

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

  /**
   * A path processor manager for resolving the system path.
   *
   * @var \Drupal\Core\PathProcessor\InboundPathProcessorInterface
   */
  protected $pathProcessor;
  public function __construct(AliasManagerCacheDecorator $alias_manager, InboundPathProcessorInterface $path_processor) {
    $this->aliasManager = $alias_manager;
    $this->pathProcessor = $path_processor;
  }

  /**
   * Converts the request path to a system path.
   *
   * @param Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The Event to process.
   */
  public function onKernelRequestConvertPath(GetResponseEvent $event) {
    $request = $event
      ->getRequest();
    $path = trim($request
      ->getPathInfo(), '/');
    $path = $this->pathProcessor
      ->processInbound($path, $request);
    $request->attributes
      ->set('system_path', $path);

    // Also set an attribute that indicates whether we are using clean URLs.
    $clean_urls = TRUE;
    $base_url = $request
      ->getBaseUrl();
    if (!empty($base_url) && strpos($base_url, $request
      ->getScriptName()) !== FALSE) {
      $clean_urls = FALSE;
    }
    $request->attributes
      ->set('clean_urls', $clean_urls);

    // Set the cache key on the alias manager cache decorator.
    if ($event
      ->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
      $this->aliasManager
        ->setCacheKey($path);
    }
  }

  /**
   * 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::REQUEST][] = array(
      'onKernelRequestConvertPath',
      200,
    );
    $events[KernelEvents::TERMINATE][] = array(
      'onKernelTerminate',
      200,
    );
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PathListenerBase::extractPath public function
PathListenerBase::setPath public function
PathSubscriber::$aliasManager protected property The alias manager that caches alias lookups based on the request.
PathSubscriber::$pathProcessor protected property A path processor manager for resolving the system path.
PathSubscriber::getSubscribedEvents static function Registers the methods in this class that should be listeners. Overrides EventSubscriberInterface::getSubscribedEvents
PathSubscriber::onKernelRequestConvertPath public function Converts the request path to a system path.
PathSubscriber::onKernelTerminate public function Ensures system paths for the request get cached.
PathSubscriber::__construct public function