1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Annotations\Builder; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Service; |
6
|
|
|
use BrainExe\Annotations\Builder\ServiceDefinition; |
7
|
|
|
use BrainExe\Core\Annotations\Guest; |
8
|
|
|
use BrainExe\Core\Annotations\Role; |
9
|
|
|
use BrainExe\Core\Annotations\Route; |
10
|
|
|
use BrainExe\Core\DependencyInjection\CompilerPass\ControllerCompilerPass; |
11
|
|
|
use BrainExe\Core\Annotations\Controller as ControllerAnnotation; |
12
|
|
|
use ReflectionClass; |
13
|
|
|
use ReflectionMethod; |
14
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
15
|
|
|
|
16
|
|
|
class Controller extends ServiceDefinition |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param ReflectionClass $reflectionClass |
21
|
|
|
* @param ControllerAnnotation|Service $annotation |
22
|
|
|
* @param Definition $definition |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
1 |
|
public function build(ReflectionClass $reflectionClass, Service $annotation, Definition $definition) |
26
|
|
|
{ |
27
|
|
|
/** @var Definition $definition */ |
28
|
1 |
|
list ($serviceId, $definition) = parent::build( |
|
|
|
|
29
|
|
|
$reflectionClass, |
30
|
|
|
$annotation, |
31
|
|
|
$definition |
32
|
|
|
); |
33
|
1 |
|
$serviceId = $reflectionClass->getName(); |
34
|
|
|
|
35
|
1 |
|
$definition->addTag(ControllerCompilerPass::CONTROLLER_TAG); |
36
|
1 |
|
$definition->setPublic(true); |
37
|
1 |
|
$definition->setShared(false); |
38
|
|
|
|
39
|
1 |
|
foreach ($reflectionClass->getMethods() as $method) { |
40
|
|
|
/** @var Route $routeAnnotation */ |
41
|
1 |
|
$routeAnnotation = $this->reader->getMethodAnnotation( |
42
|
|
|
$method, |
43
|
1 |
|
Route::class |
44
|
|
|
); |
45
|
|
|
|
46
|
1 |
|
if ($routeAnnotation) { |
47
|
1 |
|
$this->handleRouteAnnotation( |
48
|
|
|
$annotation, |
|
|
|
|
49
|
|
|
$method, |
50
|
|
|
$routeAnnotation, |
51
|
|
|
$serviceId, |
52
|
|
|
$definition |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
return [$serviceId, $definition]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Route $routeAnnotation |
63
|
|
|
* @param ReflectionMethod $method |
64
|
|
|
* @param string $serviceId |
65
|
|
|
* @param Guest $guestAnnotation |
66
|
|
|
* @param Role $roleAnnotation |
67
|
|
|
*/ |
68
|
1 |
|
protected function setDefaults( |
69
|
|
|
Route $routeAnnotation, |
70
|
|
|
ReflectionMethod $method, |
71
|
|
|
string $serviceId, |
72
|
|
|
Guest $guestAnnotation = null, |
73
|
|
|
Role $roleAnnotation = null |
74
|
|
|
) { |
75
|
1 |
|
$defaults = $routeAnnotation->getDefaults(); |
76
|
1 |
|
$defaults['_controller'] = [ |
77
|
1 |
|
$serviceId, |
78
|
1 |
|
$method->getName() |
79
|
|
|
]; |
80
|
|
|
|
81
|
1 |
|
if ($guestAnnotation) { |
82
|
1 |
|
$defaults['_guest'] = true; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
if ($roleAnnotation) { |
86
|
|
|
$defaults['_role'] = $roleAnnotation->role; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$routeAnnotation->setDefaults($defaults); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ControllerAnnotation $annotation |
94
|
|
|
* @param ReflectionMethod $method |
95
|
|
|
* @param Route $routeAnnotation |
96
|
|
|
* @param string $serviceId |
97
|
|
|
* @param Definition $definition |
98
|
|
|
*/ |
99
|
1 |
|
private function handleRouteAnnotation( |
100
|
|
|
ControllerAnnotation $annotation, |
101
|
|
|
ReflectionMethod$method, |
102
|
|
|
Route $routeAnnotation, |
103
|
|
|
string $serviceId, |
104
|
|
|
Definition $definition |
105
|
|
|
) { |
106
|
|
|
/** @var Guest $guestAnnotation */ |
107
|
1 |
|
$guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class); |
108
|
|
|
|
109
|
|
|
/** @var Role $roleAnnotation */ |
110
|
1 |
|
$roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class); |
111
|
|
|
|
112
|
1 |
|
$this->setDefaults($routeAnnotation, $method, $serviceId, $guestAnnotation, $roleAnnotation); |
113
|
|
|
|
114
|
1 |
|
if ($annotation->requirements) { |
|
|
|
|
115
|
|
|
$routeAnnotation->setRequirements( |
116
|
|
|
array_merge( |
117
|
|
|
$routeAnnotation->getRequirements(), |
118
|
|
|
$annotation->requirements |
119
|
|
|
) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
$definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]); |
124
|
1 |
|
} |
125
|
|
|
} |
126
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.