1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\controller_annotations\Configuration\Method; |
6
|
|
|
use Drupal\controller_annotations\Configuration\RouteModifierClassInterface; |
7
|
|
|
use Drupal\controller_annotations\Configuration\RouteModifierMethodInterface; |
8
|
|
|
use Symfony\Component\Routing\Loader\AnnotationClassLoader; |
9
|
|
|
use Symfony\Component\Routing\Route; |
10
|
|
|
|
11
|
|
|
class AnnotatedRouteControllerLoader extends AnnotationClassLoader |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param Route $route |
15
|
|
|
* @param \ReflectionClass $class |
16
|
|
|
* @param \ReflectionMethod $method |
17
|
|
|
* @param mixed $annotation |
18
|
|
|
*/ |
19
|
8 |
|
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annotation) |
20
|
|
|
{ |
21
|
8 |
|
$this->setController($route, $class, $method); |
22
|
8 |
|
$this->configureClassAnnotations($route, $class, $method); |
23
|
8 |
|
$this->configureMethodAnnotations($route, $class, $method); |
24
|
8 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \ReflectionClass $class |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
7 |
|
protected function getGlobals(\ReflectionClass $class): array |
31
|
|
|
{ |
32
|
7 |
|
$globals = parent::getGlobals($class); |
33
|
|
|
|
34
|
7 |
|
foreach ($this->reader->getClassAnnotations($class) as $configuration) { |
35
|
7 |
|
if ($configuration instanceof Method) { |
36
|
7 |
|
$globals['methods'] = array_merge($globals['methods'], $configuration->getMethods()); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
7 |
|
return $globals; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Route $route |
45
|
|
|
* @param \ReflectionClass $class |
46
|
|
|
* @param \ReflectionMethod $method |
47
|
|
|
*/ |
48
|
8 |
|
protected function configureClassAnnotations(Route $route, \ReflectionClass $class, \ReflectionMethod $method) |
49
|
|
|
{ |
50
|
8 |
|
foreach ($this->reader->getClassAnnotations($class) as $configuration) { |
51
|
7 |
|
if ($configuration instanceof RouteModifierClassInterface) { |
52
|
7 |
|
$configuration->modifyRouteClass($route, $class, $method); |
53
|
|
|
} |
54
|
|
|
} |
55
|
8 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Route $route |
59
|
|
|
* @param \ReflectionClass $class |
60
|
|
|
* @param \ReflectionMethod $method |
61
|
|
|
*/ |
62
|
8 |
|
protected function configureMethodAnnotations(Route $route, \ReflectionClass $class, \ReflectionMethod $method) |
63
|
|
|
{ |
64
|
8 |
|
foreach ($this->reader->getMethodAnnotations($method) as $configuration) { |
65
|
8 |
|
if ($configuration instanceof RouteModifierMethodInterface) { |
66
|
8 |
|
$configuration->modifyRouteMethod($route, $class, $method); |
67
|
|
|
} |
68
|
|
|
} |
69
|
8 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Route $route |
73
|
|
|
* @param \ReflectionClass $class |
74
|
|
|
* @param \ReflectionMethod $method |
75
|
|
|
*/ |
76
|
8 |
|
protected function setController(Route $route, \ReflectionClass $class, \ReflectionMethod $method) |
77
|
|
|
{ |
78
|
8 |
|
$route->setDefault('_controller', $this->getControllerName($class, $method)); |
79
|
8 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \ReflectionClass $class |
83
|
|
|
* @param \ReflectionMethod $method |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
8 |
|
protected function getControllerName(\ReflectionClass $class, \ReflectionMethod $method) |
87
|
|
|
{ |
88
|
8 |
|
$annotation = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass); |
89
|
8 |
|
if ($annotation instanceof \Drupal\controller_annotations\Configuration\Route && $service = $annotation->getService( |
90
|
|
|
)) { |
91
|
7 |
|
return sprintf('%s:%s', $service, $method->getName()); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
8 |
|
return sprintf('%s::%s', $class->getName(), $method->getName()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|