1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\App\Bootloader; |
13
|
|
|
|
14
|
|
|
use Psr\Container\ContainerInterface; |
15
|
|
|
use Spiral\App\Checker\MyChecker; |
16
|
|
|
use Spiral\App\Condition\MyCondition; |
17
|
|
|
use Spiral\App\Controller\AuthController; |
18
|
|
|
use Spiral\App\Controller\InterceptedController; |
19
|
|
|
use Spiral\App\Controller\TestController; |
20
|
|
|
use Spiral\App\Interceptor; |
21
|
|
|
use Spiral\App\User\UserRepository; |
22
|
|
|
use Spiral\App\ViewEngine\TestEngine; |
23
|
|
|
use Spiral\Bootloader\DomainBootloader; |
24
|
|
|
use Spiral\Bootloader\Http\JsonPayloadsBootloader; |
25
|
|
|
use Spiral\Bootloader\Security\ValidationBootloader; |
26
|
|
|
use Spiral\Bootloader\Views\ViewsBootloader; |
27
|
|
|
use Spiral\Core\Core; |
28
|
|
|
use Spiral\Core\CoreInterface; |
29
|
|
|
use Spiral\Core\InterceptableCore; |
30
|
|
|
use Spiral\Domain\CycleInterceptor; |
31
|
|
|
use Spiral\Domain\FilterInterceptor; |
32
|
|
|
use Spiral\Domain\GuardInterceptor; |
33
|
|
|
use Spiral\Domain\PipelineInterceptor; |
34
|
|
|
use Spiral\Router\Route; |
35
|
|
|
use Spiral\Router\RouterInterface; |
36
|
|
|
use Spiral\Router\Target\Action; |
37
|
|
|
use Spiral\Router\Target\Controller; |
38
|
|
|
use Spiral\Security\PermissionsInterface; |
39
|
|
|
|
40
|
|
|
class AppBootloader extends DomainBootloader |
41
|
|
|
{ |
42
|
|
|
protected const SINGLETONS = [ |
43
|
|
|
CoreInterface::class => [self::class, 'domainCore'] |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
protected const INTERCEPTORS = [ |
47
|
|
|
CycleInterceptor::class, |
48
|
|
|
GuardInterceptor::class, |
49
|
|
|
FilterInterceptor::class |
50
|
|
|
]; |
51
|
|
|
/** @var ContainerInterface */ |
52
|
|
|
private $container; |
53
|
|
|
|
54
|
|
|
/** @var Core */ |
55
|
|
|
private $core; |
56
|
|
|
|
57
|
|
|
public function __construct(ContainerInterface $container, Core $core) |
58
|
|
|
{ |
59
|
|
|
$this->container = $container; |
60
|
|
|
$this->core = $core; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function boot( |
64
|
|
|
\Spiral\Bootloader\Auth\AuthBootloader $authBootloader, |
65
|
|
|
RouterInterface $router, |
66
|
|
|
PermissionsInterface $rbac, |
67
|
|
|
ViewsBootloader $views, |
68
|
|
|
ValidationBootloader $validation, |
69
|
|
|
JsonPayloadsBootloader $json, |
70
|
|
|
PipelineInterceptor $pipelineInterceptor |
71
|
|
|
): void { |
72
|
|
|
$authBootloader->addActorProvider(UserRepository::class); |
73
|
|
|
|
74
|
|
|
$rbac->addRole('user'); |
75
|
|
|
$rbac->associate('user', '*'); |
76
|
|
|
|
77
|
|
|
$rbac->addRole('demo'); |
78
|
|
|
$rbac->associate('demo', 'demo.*'); |
79
|
|
|
|
80
|
|
|
$route = new Route( |
81
|
|
|
'/<action>[/<name>]', |
82
|
|
|
new Controller(TestController::class) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$router->setDefault($route->withDefaults(['name' => 'Dave'])); |
86
|
|
|
|
87
|
|
|
$router->setRoute( |
88
|
|
|
'auth', |
89
|
|
|
new Route('/auth/<action>', new Controller(AuthController::class)) |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
$views->addDirectory('custom', __DIR__ . '/../../views/custom/'); |
93
|
|
|
$views->addEngine(TestEngine::class); |
94
|
|
|
|
95
|
|
|
$validation->addAlias('aliased', 'notEmpty'); |
96
|
|
|
$validation->addChecker('my', MyChecker::class); |
97
|
|
|
$validation->addCondition('cond', MyCondition::class); |
98
|
|
|
|
99
|
|
|
$json->addContentType('application/vnd.api+json'); |
100
|
|
|
|
101
|
|
|
$this->registerInterceptedRoute( |
102
|
|
|
$router, |
103
|
|
|
'without', |
104
|
|
|
[ |
105
|
|
|
new Interceptor\Append('one'), |
106
|
|
|
new Interceptor\Append('two'), |
107
|
|
|
new Interceptor\Append('three'), |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
$this->registerInterceptedRoute( |
112
|
|
|
$router, |
113
|
|
|
'with', |
114
|
|
|
[ |
115
|
|
|
$pipelineInterceptor, |
116
|
|
|
] |
117
|
|
|
); |
118
|
|
|
$this->registerInterceptedRoute( |
119
|
|
|
$router, |
120
|
|
|
'mix', |
121
|
|
|
[ |
122
|
|
|
new Interceptor\Append('four'), |
123
|
|
|
new Interceptor\Append('five'), |
124
|
|
|
$pipelineInterceptor, |
125
|
|
|
new Interceptor\Append('six'), |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
$this->registerInterceptedRoute( |
129
|
|
|
$router, |
130
|
|
|
'dup', |
131
|
|
|
[ |
132
|
|
|
$pipelineInterceptor, |
133
|
|
|
new Interceptor\Append('one'), |
134
|
|
|
new Interceptor\Append('two'), |
135
|
|
|
new Interceptor\Append('three'), |
136
|
|
|
] |
137
|
|
|
); |
138
|
|
|
$this->registerInterceptedRoute( |
139
|
|
|
$router, |
140
|
|
|
'skip', |
141
|
|
|
[ |
142
|
|
|
new Interceptor\Append('one'), |
143
|
|
|
$pipelineInterceptor, |
144
|
|
|
new Interceptor\Append('two'), |
145
|
|
|
new Interceptor\Append('three'), |
146
|
|
|
] |
147
|
|
|
); |
148
|
|
|
$this->registerInterceptedRoute( |
149
|
|
|
$router, |
150
|
|
|
'first', |
151
|
|
|
[ |
152
|
|
|
$pipelineInterceptor, |
153
|
|
|
new Interceptor\Append('four'), |
154
|
|
|
new Interceptor\Append('five'), |
155
|
|
|
new Interceptor\Append('six'), |
156
|
|
|
] |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function registerInterceptedRoute(RouterInterface $router, string $action, array $interceptors): void |
161
|
|
|
{ |
162
|
|
|
$target = new Action(InterceptedController::class, $action); |
163
|
|
|
$router->setRoute( |
164
|
|
|
"intercepted:$action", |
165
|
|
|
new Route( |
166
|
|
|
"/intercepted/$action", |
167
|
|
|
$target->withCore($this->getInterceptedCore($interceptors)) |
168
|
|
|
) |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
private function getInterceptedCore(array $interceptors = []): InterceptableCore |
173
|
|
|
{ |
174
|
|
|
$core = new InterceptableCore($this->core); |
175
|
|
|
|
176
|
|
|
foreach ($interceptors as $interceptor) { |
177
|
|
|
$core->addInterceptor(is_object($interceptor) ? $interceptor : $this->container->get($interceptor)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $core; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|