3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Path\AliasManagerInterface;
6 use Drupal\Core\Path\CurrentPathStack;
7 use Symfony\Component\HttpKernel\HttpKernelInterface;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
10 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
11 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14 * Provides a path subscriber that converts path aliases.
16 class PathSubscriber implements EventSubscriberInterface {
19 * The alias manager that caches alias lookups based on the request.
21 * @var \Drupal\Core\Path\AliasManagerInterface
23 protected $aliasManager;
28 * @var \Drupal\Core\Path\CurrentPathStack
30 protected $currentPath;
33 * Constructs a new PathSubscriber instance.
35 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
37 * @param \Drupal\Core\Path\CurrentPathStack $current_path
40 public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
41 $this->aliasManager = $alias_manager;
42 $this->currentPath = $current_path;
46 * Sets the cache key on the alias manager cache decorator.
48 * KernelEvents::CONTROLLER is used in order to be executed after routing.
50 * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
51 * The Event to process.
53 public function onKernelController(FilterControllerEvent $event) {
54 // Set the cache key on the alias manager cache decorator.
55 if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) {
56 $this->aliasManager->setCacheKey(rtrim($this->currentPath->getPath($event->getRequest()), '/'));
61 * Ensures system paths for the request get cached.
63 public function onKernelTerminate(PostResponseEvent $event) {
64 $this->aliasManager->writeCache();
68 * Registers the methods in this class that should be listeners.
71 * An array of event listener definitions.
73 public static function getSubscribedEvents() {
74 $events[KernelEvents::CONTROLLER][] = ['onKernelController', 200];
75 $events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 200];