1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\controller_annotations\Routing; |
4
|
|
|
|
5
|
|
|
use Drupal\controller_annotations\Configuration\Method; |
6
|
|
|
use Drupal\controller_annotations\Configuration\Security; |
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route as FrameworkExtraBundleRoute; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader as BaseAnnotatedRouteControllerLoader; |
9
|
|
|
use Symfony\Component\Routing\Route; |
10
|
|
|
|
11
|
|
|
class AnnotatedRouteControllerLoader extends BaseAnnotatedRouteControllerLoader |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param Route $route |
15
|
|
|
* @param \ReflectionClass $class |
16
|
|
|
* @param \ReflectionMethod $method |
17
|
|
|
* @param mixed $annot |
18
|
|
|
*/ |
19
|
3 |
|
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot) |
20
|
|
|
{ |
21
|
3 |
|
$this->setController($route, $class, $method); |
22
|
|
|
|
23
|
3 |
|
foreach ($this->reader->getMethodAnnotations($method) as $configuration) { |
24
|
3 |
|
if ($configuration instanceof Method) { |
25
|
3 |
|
$this->setMethodConfiguration($route, $configuration); |
26
|
|
|
} elseif ($configuration instanceof Security) { |
27
|
3 |
|
$this->setSecurityConfiguration($route, $configuration); |
28
|
3 |
|
} elseif ($configuration instanceof FrameworkExtraBundleRoute && $configuration->getService()) { |
29
|
3 |
|
throw new \LogicException('The service option can only be specified at class level.'); |
30
|
|
|
} |
31
|
|
|
} |
32
|
3 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Route $route |
36
|
|
|
* @param \ReflectionClass $class |
37
|
|
|
* @param \ReflectionMethod $method |
38
|
|
|
*/ |
39
|
3 |
|
private function setController(Route $route, \ReflectionClass $class, \ReflectionMethod $method) |
40
|
|
|
{ |
41
|
3 |
|
$classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass); |
42
|
3 |
|
if ($classAnnot instanceof FrameworkExtraBundleRoute && $service = $classAnnot->getService()) { |
43
|
3 |
|
$route->setDefault('_controller', $service.':'.$method->getName()); |
|
|
|
|
44
|
|
|
} else { |
45
|
3 |
|
$route->setDefault('_controller', $class->getName().'::'.$method->getName()); |
|
|
|
|
46
|
|
|
} |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* we need to make sure this is an array instead of a string which is different in Symfony Framework |
51
|
|
|
* otherwise the support for defining an array of methods will not work as expected |
52
|
|
|
* |
53
|
|
|
* @param Route $route |
54
|
|
|
* @param Method $method |
55
|
|
|
*/ |
56
|
3 |
|
private function setMethodConfiguration(Route $route, Method $method) |
57
|
|
|
{ |
58
|
3 |
|
$route->setMethods($method->getMethods()); |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Route $route |
63
|
|
|
* @param Security $security |
64
|
|
|
*/ |
65
|
3 |
|
private function setSecurityConfiguration(Route $route, Security $security) |
66
|
|
|
{ |
67
|
3 |
|
if ($security->isAccess()) { |
68
|
3 |
|
$route->setRequirement('_access', 'TRUE'); |
69
|
|
|
} |
70
|
3 |
|
if ($security->hasPermission()) { |
71
|
3 |
|
$route->setRequirement('_permission', $security->getPermission()); |
72
|
|
|
} |
73
|
3 |
|
if ($security->hasRole()) { |
74
|
3 |
|
$route->setRequirement('_role', $security->getRole()); |
75
|
|
|
} |
76
|
3 |
|
if ($security->hasAuth()) { |
77
|
|
|
$route->setRequirement('_auth', $security->getAuth()); |
78
|
|
|
} |
79
|
3 |
|
if ($security->hasCsrf()) { |
80
|
3 |
|
$route->setRequirement('_csrf_token', 'TRUE'); |
81
|
|
|
} |
82
|
3 |
|
if ($security->hasCustom()) { |
83
|
3 |
|
$route->setRequirement('_custom_access', $security->getCustom()); |
84
|
|
|
} |
85
|
3 |
|
} |
86
|
|
|
} |
87
|
|
|
|