comparison core/lib/Drupal/Core/EventSubscriber/PathSubscriber.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
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;
12
13 /**
14 * Provides a path subscriber that converts path aliases.
15 */
16 class PathSubscriber implements EventSubscriberInterface {
17
18 /**
19 * The alias manager that caches alias lookups based on the request.
20 *
21 * @var \Drupal\Core\Path\AliasManagerInterface
22 */
23 protected $aliasManager;
24
25 /**
26 * The current path.
27 *
28 * @var \Drupal\Core\Path\CurrentPathStack
29 */
30 protected $currentPath;
31
32 /**
33 * Constructs a new PathSubscriber instance.
34 *
35 * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
36 * The alias manager.
37 * @param \Drupal\Core\Path\CurrentPathStack $current_path
38 * The current path.
39 */
40 public function __construct(AliasManagerInterface $alias_manager, CurrentPathStack $current_path) {
41 $this->aliasManager = $alias_manager;
42 $this->currentPath = $current_path;
43 }
44
45 /**
46 * Sets the cache key on the alias manager cache decorator.
47 *
48 * KernelEvents::CONTROLLER is used in order to be executed after routing.
49 *
50 * @param \Symfony\Component\HttpKernel\Event\FilterControllerEvent $event
51 * The Event to process.
52 */
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()), '/'));
57 }
58 }
59
60 /**
61 * Ensures system paths for the request get cached.
62 */
63 public function onKernelTerminate(PostResponseEvent $event) {
64 $this->aliasManager->writeCache();
65 }
66
67 /**
68 * Registers the methods in this class that should be listeners.
69 *
70 * @return array
71 * An array of event listener definitions.
72 */
73 public static function getSubscribedEvents() {
74 $events[KernelEvents::CONTROLLER][] = ['onKernelController', 200];
75 $events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 200];
76 return $events;
77 }
78
79 }