1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Annotations\Builder; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Builder\ServiceDefinition; |
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
|
|
|
* @todo private! |
|
|
|
|
20
|
2 |
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $serviceId; |
23
|
2 |
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function build(ReflectionClass $reflectionClass, $annotation) |
28
|
2 |
|
{ |
29
|
|
|
/** @var Definition $definition */ |
30
|
|
|
list($serviceId, $definition) = parent::build( |
31
|
2 |
|
$reflectionClass, |
32
|
2 |
|
$annotation |
33
|
|
|
); |
34
|
|
|
$this->serviceId = $serviceId = sprintf('__controller.%s', $serviceId); |
35
|
|
|
|
36
|
|
|
$definition->addTag(ControllerCompilerPass::CONTROLLER_TAG); |
37
|
|
|
$definition->setPublic(true); |
38
|
|
|
$definition->setShared(false); |
39
|
|
|
|
40
|
2 |
|
foreach ($reflectionClass->getMethods() as $method) { |
41
|
|
|
/** @var Route $routeAnnotation */ |
42
|
2 |
|
$routeAnnotation = $this->reader->getMethodAnnotation($method, Route::class); |
43
|
|
|
|
44
|
2 |
|
if ($routeAnnotation) { |
45
|
|
|
/** @var Guest $guestAnnotation */ |
46
|
2 |
|
$guestAnnotation = $this->reader->getMethodAnnotation($method, Guest::class); |
47
|
|
|
|
48
|
2 |
|
/** @var Role $roleAnnotation */ |
49
|
|
|
$roleAnnotation = $this->reader->getMethodAnnotation($method, Role::class); |
50
|
2 |
|
|
51
|
|
|
$this->setDefaults( |
52
|
|
|
$routeAnnotation, |
53
|
2 |
|
$method, |
54
|
|
|
$annotation, |
55
|
2 |
|
$guestAnnotation, |
56
|
|
|
$roleAnnotation |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$definition->addTag(ControllerCompilerPass::ROUTE_TAG, [$routeAnnotation]); |
60
|
|
|
} |
61
|
|
|
} |
62
|
2 |
|
|
63
|
|
|
return [$serviceId, $definition]; |
64
|
|
|
} |
65
|
2 |
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Route $routeAnnotation |
69
|
|
|
* @param ReflectionMethod $method |
70
|
2 |
|
* @param ControllerAnnotation $controller |
71
|
|
|
* @param Guest $guestAnnotation |
72
|
2 |
|
* @param Role $roleAnnotation |
73
|
2 |
|
*/ |
74
|
2 |
|
protected function setDefaults( |
75
|
|
|
Route $routeAnnotation, |
76
|
|
|
ReflectionMethod $method, |
77
|
|
|
ControllerAnnotation $controller, |
78
|
|
|
Guest $guestAnnotation = null, |
79
|
|
|
Role $roleAnnotation = null |
80
|
|
|
) { |
81
|
|
|
$defaults = $routeAnnotation->getDefaults(); |
82
|
|
|
$defaults['_controller'] = [ |
83
|
|
|
$this->serviceId, |
84
|
2 |
|
$method->getName() |
85
|
|
|
]; |
86
|
|
|
if ($guestAnnotation) { |
87
|
|
|
$defaults['_guest'] = true; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
if ($roleAnnotation) { |
91
|
2 |
|
$defaults['_role'] = $roleAnnotation->role; |
92
|
2 |
|
} |
93
|
2 |
|
|
94
|
|
|
|
95
|
2 |
|
if ($controller->requirements) { |
|
|
|
|
96
|
2 |
|
$routeAnnotation->setRequirements( |
97
|
|
|
array_merge($routeAnnotation->getRequirements(), $controller->requirements) |
98
|
|
|
); |
99
|
2 |
|
} |
100
|
|
|
$routeAnnotation->setDefaults($defaults); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks
TODO
comments that have been left in the code.``TODO``s show that something is left unfinished and should be attended to.