1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
6
|
|
|
use Drupal\Core\Routing\RouteBuildEvent; |
7
|
|
|
use Drupal\Core\Routing\RoutingEvents; |
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
9
|
|
|
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader; |
10
|
|
|
use Symfony\Component\Routing\Route; |
11
|
|
|
|
12
|
|
|
class RouteEventSubscriber implements EventSubscriberInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var AnnotationDirectoryLoader |
16
|
|
|
*/ |
17
|
|
|
private $annotationDirectoryLoader; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $rootPath; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param AnnotationDirectoryLoader $annotationDirectoryLoader |
26
|
|
|
* @param string $rootPath |
27
|
|
|
*/ |
28
|
11 |
|
public function __construct(AnnotationDirectoryLoader $annotationDirectoryLoader, string $rootPath) |
29
|
|
|
{ |
30
|
11 |
|
$this->registerAnnotations(); |
31
|
11 |
|
$this->annotationDirectoryLoader = $annotationDirectoryLoader; |
32
|
11 |
|
$this->rootPath = $rootPath; |
33
|
11 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Configure the annotation registry to make routing annotations available |
37
|
|
|
*/ |
38
|
11 |
|
private function registerAnnotations() |
39
|
|
|
{ |
40
|
11 |
|
AnnotationRegistry::registerLoader('class_exists'); |
|
|
|
|
41
|
11 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param RouteBuildEvent $event |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
11 |
|
public function onRoutes(RouteBuildEvent $event) |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* @var $route Route |
51
|
|
|
*/ |
52
|
11 |
|
foreach ($event->getRouteCollection() as $name => $route) { |
53
|
10 |
|
if ($route->hasOption('type') |
54
|
10 |
|
&& $route->getOption('type') === 'annotation' |
55
|
|
|
) { |
56
|
9 |
|
$routeCollection = $this->annotationDirectoryLoader->load($this->rootPath.$this->getRoutePath($route)); |
57
|
8 |
|
$routeCollection->addPrefix($route->getPath()); |
58
|
|
|
|
59
|
8 |
|
$event->getRouteCollection()->addCollection($routeCollection); |
|
|
|
|
60
|
9 |
|
$event->getRouteCollection()->remove($name); |
61
|
|
|
} |
62
|
|
|
} |
63
|
10 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
7 |
|
public static function getSubscribedEvents() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
7 |
|
RoutingEvents::DYNAMIC => [ |
72
|
|
|
['onRoutes', 0], |
73
|
|
|
], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Route $route |
79
|
|
|
* @return string |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
9 |
|
protected function getRoutePath(Route $route) |
83
|
|
|
{ |
84
|
9 |
|
if ($route->hasOption('path')) { |
85
|
1 |
|
$path = $route->getOption('path'); |
86
|
8 |
|
} elseif ($route->hasOption('module')) { |
87
|
7 |
|
$path = sprintf('/%s/src/Controller', drupal_get_path('module', $route->getOption('module'))); |
88
|
|
|
} else { |
89
|
1 |
|
throw new \Exception( |
90
|
1 |
|
'Either the "resource" or "module" option is required to load from annotations' |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
8 |
|
return $path; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.