annotate core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php @ 19:fa3358dc1485 tip

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