1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Routing\RouteBuildEvent; |
6
|
|
|
use Drupal\Core\Routing\RoutingEvents; |
7
|
|
|
use Drupal\controller_annotations\Configuration\ConfigurationLoader; |
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
|
9 |
|
public function __construct(AnnotationDirectoryLoader $annotationDirectoryLoader, $rootPath) |
29
|
|
|
{ |
30
|
9 |
|
ConfigurationLoader::load(); |
31
|
|
|
|
32
|
9 |
|
$this->annotationDirectoryLoader = $annotationDirectoryLoader; |
33
|
9 |
|
$this->rootPath = $rootPath; |
34
|
9 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param RouteBuildEvent $event |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
9 |
|
public function onRoutes(RouteBuildEvent $event) |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var $route Route |
44
|
|
|
*/ |
45
|
9 |
|
foreach ($event->getRouteCollection() as $name => $route) { |
46
|
8 |
|
if ($route->hasOption('type') |
47
|
8 |
|
&& $route->getOption('type') === 'annotation' |
48
|
|
|
) { |
49
|
7 |
|
$routeCollection = $this->annotationDirectoryLoader->load($this->rootPath . $this->getRoutePath($route)); |
50
|
6 |
|
$routeCollection->addPrefix($route->getPath()); |
51
|
|
|
|
52
|
6 |
|
$event->getRouteCollection()->addCollection($routeCollection); |
53
|
7 |
|
$event->getRouteCollection()->remove($name); |
54
|
|
|
} |
55
|
|
|
} |
56
|
8 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
5 |
|
public static function getSubscribedEvents() |
62
|
|
|
{ |
63
|
|
|
return [ |
64
|
|
|
RoutingEvents::DYNAMIC => [ |
65
|
|
|
['onRoutes', 0], |
66
|
|
|
] |
67
|
5 |
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Route $route |
72
|
|
|
* @return string |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
7 |
|
public function getRoutePath(Route $route) |
76
|
|
|
{ |
77
|
7 |
|
if ($route->hasOption('path')) { |
78
|
1 |
|
$path = $route->getOption('path'); |
79
|
6 |
|
} elseif ($route->hasOption('module')) { |
80
|
5 |
|
$path = sprintf('/%s/src/Controller', drupal_get_path('module', $route->getOption('module'))); |
81
|
|
|
} else { |
82
|
1 |
|
throw new \Exception( |
83
|
1 |
|
'Either the "resource" or "module" option is required to load from annotations' |
84
|
|
|
); |
85
|
|
|
} |
86
|
6 |
|
return $path; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|