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