|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Boot\BootloadManager; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerExceptionInterface; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
|
10
|
|
|
use Spiral\Boot\Attribute\BootloadConfig; |
|
11
|
|
|
use Spiral\Boot\Attribute\BootMethod; |
|
12
|
|
|
use Spiral\Boot\Attribute\InitMethod; |
|
13
|
|
|
use Spiral\Boot\Bootloader\BootloaderInterface; |
|
14
|
|
|
use Spiral\Boot\Bootloader\DependedInterface; |
|
15
|
|
|
use Spiral\Boot\BootloadManager\Checker\BootloaderChecker; |
|
16
|
|
|
use Spiral\Boot\BootloadManager\Checker\BootloaderCheckerInterface; |
|
17
|
|
|
use Spiral\Boot\BootloadManager\Checker\CanBootedChecker; |
|
18
|
|
|
use Spiral\Boot\BootloadManager\Checker\CheckerRegistry; |
|
19
|
|
|
use Spiral\Boot\BootloadManager\Checker\ClassExistsChecker; |
|
20
|
|
|
use Spiral\Boot\BootloadManager\Checker\ConfigChecker; |
|
21
|
|
|
use Spiral\Boot\BootloadManagerInterface; |
|
22
|
|
|
use Spiral\Core\Attribute\Singleton; |
|
23
|
|
|
use Spiral\Core\BinderInterface; |
|
24
|
|
|
use Spiral\Core\ResolverInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @internal |
|
28
|
|
|
* @psalm-import-type TClass from BootloadManagerInterface |
|
29
|
|
|
* @psalm-import-type TFullBinding from BootloaderInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
#[Singleton] |
|
32
|
631 |
|
class Initializer implements InitializerInterface |
|
33
|
|
|
{ |
|
34
|
|
|
protected ?BootloaderCheckerInterface $checker = null; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
protected readonly ContainerInterface $container, |
|
38
|
631 |
|
protected readonly BinderInterface $binder, |
|
39
|
|
|
protected readonly ClassesRegistry $bootloaders = new ClassesRegistry(), |
|
40
|
|
|
?BootloaderCheckerInterface $checker = null, |
|
41
|
|
|
) {} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Instantiate bootloader objects and resolve dependencies |
|
45
|
622 |
|
* |
|
46
|
|
|
* @param TClass[]|array<class-string<BootloaderInterface>, array<string,mixed>> $classes |
|
|
|
|
|
|
47
|
622 |
|
* @throws ContainerExceptionInterface |
|
48
|
|
|
* @throws NotFoundExceptionInterface |
|
49
|
622 |
|
* @throws \ReflectionException |
|
50
|
|
|
*/ |
|
51
|
622 |
|
public function init(array $classes, bool $useConfig = true): \Generator |
|
52
|
605 |
|
{ |
|
53
|
605 |
|
$this->checker ??= $this->initDefaultChecker(); |
|
54
|
|
|
|
|
55
|
622 |
|
foreach ($classes as $bootloader => $options) { |
|
56
|
|
|
// default bootload syntax as simple array |
|
57
|
622 |
|
if (\is_string($options) || $options instanceof BootloaderInterface) { |
|
58
|
458 |
|
$bootloader = $options; |
|
59
|
|
|
$options = []; |
|
60
|
|
|
} |
|
61
|
601 |
|
$options = $useConfig ? $this->getBootloadConfig($bootloader, $options) : []; |
|
62
|
|
|
|
|
63
|
601 |
|
if (!$this->checker->canInitialize($bootloader, $useConfig ? $options : null)) { |
|
|
|
|
|
|
64
|
600 |
|
continue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->bootloaders->register($bootloader instanceof BootloaderInterface ? $bootloader::class : $bootloader); |
|
68
|
601 |
|
|
|
69
|
601 |
|
if (!$bootloader instanceof BootloaderInterface) { |
|
70
|
601 |
|
$bootloader = $this->container->get($bootloader); |
|
71
|
601 |
|
} |
|
72
|
601 |
|
|
|
73
|
|
|
$initMethods = $this->findMethodsWithPriority( |
|
74
|
|
|
$bootloader, |
|
75
|
|
|
[0 => [Methods::INIT->value]], |
|
76
|
9 |
|
InitMethod::class, |
|
77
|
|
|
); |
|
78
|
9 |
|
|
|
79
|
|
|
$bootMethods = $this->findMethodsWithPriority( |
|
80
|
|
|
$bootloader, |
|
81
|
|
|
[0 => [Methods::BOOT->value]], |
|
82
|
|
|
BootMethod::class, |
|
83
|
|
|
); |
|
84
|
601 |
|
|
|
85
|
|
|
yield from $this->resolveDependencies($bootloader, \array_unique([...$initMethods, ...$bootMethods])); |
|
86
|
601 |
|
|
|
87
|
601 |
|
$this->initBootloader($bootloader); |
|
88
|
|
|
yield $bootloader::class => [ |
|
89
|
|
|
'bootloader' => $bootloader, |
|
90
|
601 |
|
'options' => $options instanceof BootloadConfig ? $options->args : $options, |
|
91
|
601 |
|
'init_methods' => $initMethods, |
|
92
|
601 |
|
'boot_methods' => $bootMethods, |
|
93
|
601 |
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getRegistry(): ClassesRegistry |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->bootloaders; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
601 |
|
/** |
|
103
|
|
|
* Resolve all bootloader dependencies and init bindings |
|
104
|
601 |
|
*/ |
|
105
|
557 |
|
private function initBootloader(BootloaderInterface $bootloader): void |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($bootloader->defineBindings() as $alias => $resolver) { |
|
108
|
601 |
|
$this->binder->bind($alias, $resolver); |
|
109
|
578 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach ($bootloader->defineSingletons() as $alias => $resolver) { |
|
112
|
|
|
$this->binder->bindSingleton($alias, $resolver); |
|
113
|
601 |
|
} |
|
114
|
|
|
|
|
115
|
601 |
|
$this->resolveAttributeBindings($bootloader); |
|
116
|
|
|
} |
|
117
|
601 |
|
|
|
118
|
|
|
protected function shouldBeBooted(\ReflectionNamedType $type): bool |
|
119
|
601 |
|
{ |
|
120
|
|
|
/** @var TClass $class */ |
|
121
|
601 |
|
$class = $type->getName(); |
|
122
|
601 |
|
|
|
123
|
578 |
|
return $this->isBootloader($class) |
|
124
|
578 |
|
&& !$this->bootloaders->isBooted($class); |
|
125
|
578 |
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @psalm-pure |
|
129
|
601 |
|
* @psalm-assert-if-true TClass $class |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function isBootloader(string|object $class): bool |
|
132
|
578 |
|
{ |
|
133
|
|
|
return \is_subclass_of($class, BootloaderInterface::class); |
|
134
|
578 |
|
} |
|
135
|
578 |
|
|
|
136
|
578 |
|
protected function initDefaultChecker(): BootloaderCheckerInterface |
|
137
|
578 |
|
{ |
|
138
|
461 |
|
$registry = new CheckerRegistry(); |
|
139
|
|
|
$registry->register($this->container->get(ConfigChecker::class)); |
|
140
|
|
|
$registry->register(new ClassExistsChecker()); |
|
141
|
|
|
$registry->register(new CanBootedChecker($this->bootloaders)); |
|
142
|
578 |
|
|
|
143
|
|
|
return new BootloaderChecker($registry); |
|
144
|
|
|
} |
|
145
|
578 |
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Returns merged config. Attribute config has lower priority. |
|
148
|
578 |
|
* |
|
149
|
|
|
* @param class-string<BootloaderInterface>|BootloaderInterface $bootloader |
|
|
|
|
|
|
150
|
578 |
|
* @throws \ReflectionException |
|
151
|
578 |
|
*/ |
|
152
|
|
|
private function getBootloadConfig( |
|
153
|
|
|
string|BootloaderInterface $bootloader, |
|
154
|
|
|
array|callable|BootloadConfig $config, |
|
155
|
|
|
): BootloadConfig { |
|
156
|
|
|
if ($config instanceof \Closure) { |
|
|
|
|
|
|
157
|
|
|
$config = $this->container instanceof ResolverInterface |
|
158
|
578 |
|
? $config(...$this->container->resolveArguments(new \ReflectionFunction($config))) |
|
159
|
|
|
: $config(); |
|
160
|
578 |
|
} |
|
161
|
|
|
|
|
162
|
|
|
$attr = $this->getBootloadConfigAttribute($bootloader); |
|
163
|
622 |
|
|
|
164
|
|
|
$getArgument = static fn(string $key, bool $override, mixed $default = []): mixed => match (true) { |
|
165
|
622 |
|
$config instanceof BootloadConfig && $override => $config->{$key}, |
|
166
|
622 |
|
$config instanceof BootloadConfig && !$override && \is_array($default) => |
|
167
|
622 |
|
$config->{$key} + ($attr->{$key} ?? []), |
|
168
|
622 |
|
$config instanceof BootloadConfig && !$override && \is_bool($default) => $config->{$key}, |
|
169
|
|
|
\is_array($config) && $config !== [] && $key === 'args' => $config, |
|
170
|
622 |
|
default => $attr->{$key} ?? $default, |
|
171
|
|
|
}; |
|
172
|
|
|
|
|
173
|
|
|
$override = $config instanceof BootloadConfig ? $config->override : true; |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
return new BootloadConfig( |
|
176
|
|
|
args: $getArgument('args', $override), |
|
177
|
|
|
enabled: $getArgument('enabled', $override, true), |
|
178
|
624 |
|
allowEnv: $getArgument('allowEnv', $override), |
|
179
|
|
|
denyEnv: $getArgument('denyEnv', $override), |
|
180
|
|
|
); |
|
181
|
|
|
} |
|
182
|
624 |
|
|
|
183
|
2 |
|
/** |
|
184
|
2 |
|
* This method is used to find and instantiate BootloadConfig attribute. |
|
185
|
|
|
* |
|
186
|
|
|
* @param class-string<BootloaderInterface>|BootloaderInterface $bootloader |
|
|
|
|
|
|
187
|
624 |
|
* @throws \ReflectionException |
|
188
|
|
|
*/ |
|
189
|
624 |
|
private function getBootloadConfigAttribute(string|BootloaderInterface $bootloader): ?BootloadConfig |
|
190
|
624 |
|
{ |
|
191
|
606 |
|
$attribute = null; |
|
192
|
3 |
|
if ($bootloader instanceof BootloaderInterface || \class_exists($bootloader)) { |
|
193
|
606 |
|
$ref = new \ReflectionClass($bootloader); |
|
194
|
603 |
|
$attribute = $ref->getAttributes(BootloadConfig::class)[0] ?? null; |
|
195
|
624 |
|
} |
|
196
|
606 |
|
|
|
197
|
|
|
if ($attribute === null) { |
|
198
|
624 |
|
return null; |
|
199
|
|
|
} |
|
200
|
624 |
|
|
|
201
|
624 |
|
return $attribute->newInstance(); |
|
202
|
624 |
|
} |
|
203
|
624 |
|
|
|
204
|
624 |
|
/** |
|
205
|
624 |
|
* This method is used to find methods with InitMethod or BootMethod attributes. |
|
206
|
|
|
* |
|
207
|
|
|
* @param class-string<InitMethod|BootMethod> $attribute |
|
|
|
|
|
|
208
|
|
|
* @param list<non-empty-string[]> $initialMethods |
|
209
|
|
|
* @return list<non-empty-string> |
|
210
|
|
|
*/ |
|
211
|
624 |
|
private function findMethodsWithPriority( |
|
212
|
|
|
BootloaderInterface $bootloader, |
|
213
|
624 |
|
array $initialMethods, |
|
214
|
624 |
|
string $attribute, |
|
215
|
623 |
|
): array { |
|
216
|
623 |
|
$methods = $initialMethods; |
|
217
|
|
|
|
|
218
|
|
|
$refl = new \ReflectionClass($bootloader); |
|
219
|
624 |
|
foreach ($refl->getMethods() as $method) { |
|
220
|
605 |
|
if ($method->isStatic()) { |
|
221
|
|
|
continue; |
|
222
|
|
|
} |
|
223
|
22 |
|
|
|
224
|
|
|
$attrs = $method->getAttributes($attribute); |
|
225
|
|
|
if (\count($attrs) === 0) { |
|
226
|
|
|
continue; |
|
227
|
|
|
} |
|
228
|
|
|
/** @var InitMethod|BootMethod $attr */ |
|
229
|
|
|
$attr = $attrs[0]->newInstance(); |
|
230
|
|
|
$methods[$attr->priority][] = $method->getName(); |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
\ksort($methods); |
|
234
|
|
|
|
|
235
|
|
|
return \array_merge(...$methods); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* This method is used to resolve bindings from attributes. |
|
240
|
|
|
* |
|
241
|
|
|
* @throws \ReflectionException |
|
242
|
|
|
*/ |
|
243
|
|
|
private function resolveAttributeBindings(BootloaderInterface $bootloader): void |
|
244
|
|
|
{ |
|
245
|
|
|
if (!$this->container->has(AttributeResolver::class)) { |
|
246
|
|
|
return; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** @var AttributeResolver $attributeResolver */ |
|
250
|
|
|
$attributeResolver = $this->container->get(AttributeResolver::class); |
|
251
|
|
|
|
|
252
|
|
|
$availableAttributes = $attributeResolver->getResolvers(); |
|
253
|
|
|
|
|
254
|
|
|
$refl = new \ReflectionClass($bootloader); |
|
255
|
|
|
foreach ($refl->getMethods() as $method) { |
|
256
|
|
|
if ($method->isStatic()) { |
|
257
|
|
|
continue; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
foreach ($availableAttributes as $attributeClass) { |
|
261
|
|
|
$attrs = $method->getAttributes($attributeClass); |
|
262
|
|
|
foreach ($attrs as $attr) { |
|
263
|
|
|
$instance = $attr->newInstance(); |
|
264
|
|
|
$attributeResolver->resolve($instance, $bootloader, $method); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param non-empty-string[] $bootloaderMethods |
|
|
|
|
|
|
272
|
|
|
* @throws ContainerExceptionInterface |
|
273
|
|
|
* @throws NotFoundExceptionInterface |
|
274
|
|
|
* @throws \ReflectionException |
|
275
|
|
|
*/ |
|
276
|
|
|
private function resolveDependencies(BootloaderInterface $bootloader, array $bootloaderMethods): iterable |
|
277
|
|
|
{ |
|
278
|
|
|
$deps = $this->findDependenciesInMethods($bootloader, $bootloaderMethods); |
|
279
|
|
|
if ($bootloader instanceof DependedInterface) { |
|
280
|
|
|
$deps = [...$deps, ...$bootloader->defineDependencies()]; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
yield from $this->init(\array_values(\array_unique($deps))); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param non-empty-string[] $methods |
|
|
|
|
|
|
288
|
|
|
* @return class-string[] |
|
|
|
|
|
|
289
|
|
|
*/ |
|
290
|
|
|
private function findDependenciesInMethods(BootloaderInterface $bootloader, array $methods): array |
|
291
|
|
|
{ |
|
292
|
|
|
$reflectionClass = new \ReflectionClass($bootloader); |
|
293
|
|
|
|
|
294
|
|
|
$methodsDeps = []; |
|
295
|
|
|
|
|
296
|
|
|
foreach ($methods as $method) { |
|
297
|
|
|
if ($reflectionClass->hasMethod($method)) { |
|
298
|
|
|
$methodsDeps[] = $this->findBootloaderClassesInMethod( |
|
299
|
|
|
$reflectionClass->getMethod($method), |
|
300
|
|
|
); |
|
301
|
|
|
} |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
return \array_merge(...$methodsDeps); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @return class-string[] |
|
|
|
|
|
|
309
|
|
|
*/ |
|
310
|
|
|
private function findBootloaderClassesInMethod(\ReflectionMethod $method): array |
|
311
|
|
|
{ |
|
312
|
|
|
$args = []; |
|
313
|
|
|
foreach ($method->getParameters() as $parameter) { |
|
314
|
|
|
$type = $parameter->getType(); |
|
315
|
|
|
if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) { |
|
316
|
|
|
$args[] = $type->getName(); |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
return $args; |
|
321
|
|
|
} |
|
322
|
|
|
} |
|
323
|
|
|
|