annotate core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\Path\AliasManagerInterface;
Chris@0 6 use Drupal\Core\Path\CurrentPathStack;
Chris@0 7 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 8 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 9 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
Chris@0 10 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
Chris@0 11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Provides a path subscriber that converts path aliases.
Chris@0 15 */
Chris@0 16 class PathSubscriber implements EventSubscriberInterface {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The alias manager that caches alias lookups based on the request.
Chris@0 20 *
Chris@0 21 * @var \Drupal\Core\Path\AliasManagerInterface
Chris@0 22 */
Chris@0 23 protected $aliasManager;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The current path.
Chris@0 27 *
Chris@0 28 * @var \Drupal\Core\Path\CurrentPathStack
Chris@0 29 */
Chris@0 30 protected $currentPath;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * Constructs a new PathSubscriber instance.
Chris@0 34 *
Chris@0 35 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
Chris@0 36 * The alias manager.
Chris@0 37 * @param \Drupal\Core\Path\CurrentPathStack $current_path
Chris@0 38 * The current path.
Chris@0 39 */
Chris@0 40 public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
Chris@0 41 $this->aliasManager = $alias_manager;
Chris@0 42 $this->currentPath = $current_path;
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Sets the cache key on the alias manager cache decorator.
Chris@0 47 *
Chris@0 48 * KernelEvents::CONTROLLER is used in order to be executed after routing.
Chris@0 49 *
Chris@0 50 * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
Chris@0 51 * The Event to process.
Chris@0 52 */
Chris@0 53 public function onKernelController(FilterControllerEvent $event) {
Chris@0 54 // Set the cache key on the alias manager cache decorator.
Chris@0 55 if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
Chris@0 56 $this->aliasManager->setCacheKey(rtrim($this->currentPath->getPath($event->getRequest()), '/'));
Chris@0 57 }
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Ensures system paths for the request get cached.
Chris@0 62 */
Chris@0 63 public function onKernelTerminate(PostResponseEvent $event) {
Chris@0 64 $this->aliasManager->writeCache();
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Registers the methods in this class that should be listeners.
Chris@0 69 *
Chris@0 70 * @return array
Chris@0 71 * An array of event listener definitions.
Chris@0 72 */
Chris@0 73 public static function getSubscribedEvents() {
Chris@0 74 $events[KernelEvents::CONTROLLER][] = ['onKernelController', 200];
Chris@0 75 $events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 200];
Chris@0 76 return $events;
Chris@0 77 }
Chris@0 78
Chris@0 79 }