1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Annotations\Builder; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\Annotations\Service; |
6
|
|
|
use BrainExe\Core\Annotations\Guest; |
7
|
|
|
use BrainExe\Core\Annotations\Role; |
8
|
|
|
use BrainExe\Core\Annotations\Route; |
9
|
|
|
use BrainExe\Core\DependencyInjection\CompilerPass\ControllerCompilerPass; |
10
|
|
|
use BrainExe\Core\Annotations\Controller as ControllerAnnotation; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionMethod; |
13
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
14
|
|
|
|
15
|
|
|
class Controller extends ServiceDefinition |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param ReflectionClass $reflectionClass |
20
|
|
|
* @param ControllerAnnotation|Service $annotation |
21
|
|
|
* @param Definition $definition |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
1 |
|
public function build(ReflectionClass $reflectionClass, Service $annotation, Definition $definition) |
25
|
|
|
{ |
26
|
|
|
/** @var Definition $definition */ |
27
|
1 |
|
[, $definition] = parent::build( |
28
|
|
|
$reflectionClass, |
29
|
|
|
$annotation, |
30
|
1 |
|
$definition |
31
|
|
|
); |
32
|
1 |
|
$serviceId = $reflectionClass->getName(); |
33
|
|
|
|
34
|
1 |
|
$definition->addTag(ControllerCompilerPass::CONTROLLER_TAG); |
35
|
1 |
|
$definition->setPublic(false); |
36
|
1 |
|
$definition->setShared(false); |
37
|
|
|
|
38
|
1 |
|
foreach ($reflectionClass->getMethods() as $method) { |
39
|
|
|
/** @var Route $routeAnnotation */ |
40
|
1 |
|
$routeAnnotation = $this->reader->getMethodAnnotation( |
41
|
|
|
$method, |
42
|
1 |
|
Route::class |
43
|
|
|
); |
44
|
|
|
|
45
|
1 |
|
if ($routeAnnotation) { |
46
|
1 |
|
$this->handleRouteAnnotation( |
47
|
1 |
|
$annotation, |
|
|
|
|
48
|
1 |
|
$method, |
49
|
1 |
|
$routeAnnotation, |
50
|
1 |
|
$serviceId, |
51
|
1 |
|
$definition |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return [$serviceId, $definition]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Route $routeAnnotation |
62
|
|
|
* @param ReflectionMethod $method |
63
|
|
|
* @param string $serviceId |
64
|
|
|
* @param Guest $guestAnnotation |
65
|
|
|
* @param Role $roleAnnotation |
66
|
|
|
*/ |
67
|
1 |
|
protected function setDefaults( |
68
|
|
|
Route $routeAnnotation, |
69
|
|
|
ReflectionMethod $method, |
70
|
|
|
string $serviceId, |
71
|
|
|
Guest $guestAnnotation = null, |
72
|
|
|
Role $roleAnnotation = null |
73
|
|
|
) { |
74
|
1 |
|
$defaults = $routeAnnotation->getDefaults(); |
75
|
1 |
|
$defaults['_controller'] = [ |
76
|
1 |
|
$serviceId, |
77
|
1 |
|
$method->getName() |
78
|
|
|
]; |
79
|
|
|
|
80
|
1 |
|
if ($guestAnnotation) { |
81
|
1 |
|
$defaults['_guest'] = true; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
if ($roleAnnotation) { |
85
|
|
|
$defaults['_role'] = $roleAnnotation->role; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
$routeAnnotation->setDefaults($defaults); |
89
|
1 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param ControllerAnnotation $annotation |
93
|
|
|
* @param ReflectionMethod $method |
94
|
|
|
* @param Route $routeAnnotation |
95
|
|
|
* @param string $serviceId |
96
|
|
|
* @param Definition $definition |
97
|
|
|
*/ |
98
|
1 |
|
private function handleRouteAnnotation( |
99
|
|
|
ControllerAnnotation $annotation, |
100
|
|
|
ReflectionMethod$method, |
101
|
|
|
Route $routeAnnotation, |
102
|
|
|
string $serviceId, |
103
|
|
|
Definition $definition |
104
|
|
|
) { |
105
|
|
|
/** @var Guest $guestAnnotation */ |
106
|
1 |
|
$guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class); |
107
|
|
|
|
108
|
|
|
/** @var Role $roleAnnotation */ |
109
|
1 |
|
$roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class); |
110
|
|
|
|
111
|
1 |
|
$this->setDefaults($routeAnnotation, $method, $serviceId, $guestAnnotation, $roleAnnotation); |
112
|
|
|
|
113
|
1 |
|
if ($annotation->requirements) { |
|
|
|
|
114
|
|
|
$routeAnnotation->setRequirements( |
115
|
|
|
array_merge( |
116
|
|
|
$routeAnnotation->getRequirements(), |
117
|
|
|
$annotation->requirements |
118
|
|
|
) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]); |
123
|
1 |
|
} |
124
|
|
|
} |
125
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.