|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Boot\BootloadManager; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Spiral\Boot\Attribute\BootloadConfig; |
|
9
|
|
|
use Spiral\Boot\Bootloader\BootloaderInterface; |
|
10
|
|
|
use Spiral\Boot\Bootloader\DependedInterface; |
|
11
|
|
|
use Spiral\Boot\BootloadManager\Checker\BootloaderChecker; |
|
12
|
|
|
use Spiral\Boot\BootloadManager\Checker\BootloaderCheckerInterface; |
|
13
|
|
|
use Spiral\Boot\BootloadManager\Checker\CanBootedChecker; |
|
14
|
|
|
use Spiral\Boot\BootloadManager\Checker\CheckerRegistry; |
|
15
|
|
|
use Spiral\Boot\BootloadManager\Checker\ClassExistsChecker; |
|
16
|
|
|
use Spiral\Boot\BootloadManager\Checker\ConfigChecker; |
|
17
|
|
|
use Spiral\Boot\BootloadManagerInterface; |
|
18
|
|
|
use Spiral\Core\BinderInterface; |
|
19
|
|
|
use Spiral\Core\Container; |
|
20
|
|
|
use Spiral\Core\ResolverInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @internal |
|
24
|
|
|
* @psalm-import-type TClass from BootloadManagerInterface |
|
25
|
|
|
* @psalm-import-type TFullBinding from BootloaderInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
class Initializer implements InitializerInterface, Container\SingletonInterface |
|
28
|
|
|
{ |
|
29
|
|
|
protected ?BootloaderCheckerInterface $checker = null; |
|
30
|
|
|
|
|
31
|
526 |
|
public function __construct( |
|
32
|
|
|
protected readonly ContainerInterface $container, |
|
33
|
|
|
protected readonly BinderInterface $binder, |
|
34
|
|
|
protected readonly ClassesRegistry $bootloaders = new ClassesRegistry(), |
|
35
|
|
|
?BootloaderCheckerInterface $checker = null, |
|
|
|
|
|
|
36
|
|
|
) { |
|
37
|
526 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Instantiate bootloader objects and resolve dependencies |
|
41
|
|
|
* |
|
42
|
|
|
* @param TClass[]|array<TClass, array<string,mixed>> $classes |
|
43
|
|
|
*/ |
|
44
|
517 |
|
public function init(array $classes, bool $useConfig = true): \Generator |
|
45
|
|
|
{ |
|
46
|
517 |
|
$this->checker ??= $this->initDefaultChecker(); |
|
47
|
|
|
|
|
48
|
517 |
|
foreach ($classes as $bootloader => $options) { |
|
49
|
|
|
// default bootload syntax as simple array |
|
50
|
517 |
|
if (\is_string($options) || $options instanceof BootloaderInterface) { |
|
51
|
500 |
|
$bootloader = $options; |
|
52
|
500 |
|
$options = []; |
|
53
|
|
|
} |
|
54
|
517 |
|
$options = $useConfig ? $this->getBootloadConfig($bootloader, $options) : []; |
|
55
|
|
|
|
|
56
|
517 |
|
if (!$this->checker->canInitialize($bootloader, $useConfig ? $options : null)) { |
|
|
|
|
|
|
57
|
390 |
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
496 |
|
$this->bootloaders->register($bootloader instanceof BootloaderInterface ? $bootloader::class : $bootloader); |
|
61
|
|
|
|
|
62
|
496 |
|
if (!$bootloader instanceof BootloaderInterface) { |
|
63
|
495 |
|
$bootloader = $this->container->get($bootloader); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var BootloaderInterface $bootloader */ |
|
67
|
496 |
|
yield from $this->initBootloader($bootloader); |
|
68
|
496 |
|
yield $bootloader::class => [ |
|
69
|
496 |
|
'bootloader' => $bootloader, |
|
70
|
496 |
|
'options' => $options instanceof BootloadConfig ? $options->args : $options, |
|
71
|
496 |
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
9 |
|
public function getRegistry(): ClassesRegistry |
|
76
|
|
|
{ |
|
77
|
9 |
|
return $this->bootloaders; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Resolve all bootloader dependencies and init bindings |
|
82
|
|
|
*/ |
|
83
|
496 |
|
protected function initBootloader(BootloaderInterface $bootloader): iterable |
|
84
|
|
|
{ |
|
85
|
496 |
|
if ($bootloader instanceof DependedInterface) { |
|
86
|
496 |
|
yield from $this->init($this->getDependencies($bootloader)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
496 |
|
$this->initBindings( |
|
90
|
496 |
|
$bootloader->defineBindings(), |
|
91
|
496 |
|
$bootloader->defineSingletons() |
|
92
|
496 |
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Bind declared bindings. |
|
97
|
|
|
* |
|
98
|
|
|
* @param TFullBinding $bindings |
|
|
|
|
|
|
99
|
|
|
* @param TFullBinding $singletons |
|
100
|
|
|
*/ |
|
101
|
496 |
|
protected function initBindings(array $bindings, array $singletons): void |
|
102
|
|
|
{ |
|
103
|
496 |
|
foreach ($bindings as $aliases => $resolver) { |
|
104
|
428 |
|
$this->binder->bind($aliases, $resolver); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
496 |
|
foreach ($singletons as $aliases => $resolver) { |
|
108
|
473 |
|
$this->binder->bindSingleton($aliases, $resolver); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
496 |
|
protected function getDependencies(DependedInterface $bootloader): array |
|
113
|
|
|
{ |
|
114
|
496 |
|
$deps = $bootloader->defineDependencies(); |
|
115
|
|
|
|
|
116
|
496 |
|
$reflectionClass = new \ReflectionClass($bootloader); |
|
117
|
|
|
|
|
118
|
496 |
|
$methodsDeps = []; |
|
119
|
|
|
|
|
120
|
496 |
|
foreach (Methods::cases() as $method) { |
|
121
|
496 |
|
if ($reflectionClass->hasMethod($method->value)) { |
|
122
|
473 |
|
$methodsDeps[] = $this->findBootloaderClassesInMethod( |
|
123
|
473 |
|
$reflectionClass->getMethod($method->value) |
|
124
|
473 |
|
); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
496 |
|
return \array_values(\array_unique(\array_merge($deps, ...$methodsDeps))); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
473 |
|
protected function findBootloaderClassesInMethod(\ReflectionMethod $method): array |
|
132
|
|
|
{ |
|
133
|
473 |
|
$args = []; |
|
134
|
473 |
|
foreach ($method->getParameters() as $parameter) { |
|
135
|
473 |
|
$type = $parameter->getType(); |
|
136
|
473 |
|
if ($type instanceof \ReflectionNamedType && $this->shouldBeBooted($type)) { |
|
137
|
412 |
|
$args[] = $type->getName(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
473 |
|
return $args; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
473 |
|
protected function shouldBeBooted(\ReflectionNamedType $type): bool |
|
145
|
|
|
{ |
|
146
|
|
|
/** @var TClass $class */ |
|
147
|
473 |
|
$class = $type->getName(); |
|
148
|
|
|
|
|
149
|
473 |
|
return $this->isBootloader($class) |
|
150
|
473 |
|
&& !$this->bootloaders->isBooted($class); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @psalm-pure |
|
155
|
|
|
* @psalm-assert-if-true TClass $class |
|
156
|
|
|
*/ |
|
157
|
473 |
|
protected function isBootloader(string|object $class): bool |
|
158
|
|
|
{ |
|
159
|
473 |
|
return \is_subclass_of($class, BootloaderInterface::class); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
517 |
|
protected function initDefaultChecker(): BootloaderCheckerInterface |
|
163
|
|
|
{ |
|
164
|
517 |
|
$registry = new CheckerRegistry(); |
|
165
|
517 |
|
$registry->register($this->container->get(ConfigChecker::class)); |
|
166
|
517 |
|
$registry->register(new ClassExistsChecker()); |
|
167
|
517 |
|
$registry->register(new CanBootedChecker($this->bootloaders)); |
|
168
|
|
|
|
|
169
|
517 |
|
return new BootloaderChecker($registry); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns merged config. Attribute config have lower priority. |
|
174
|
|
|
* |
|
175
|
|
|
* @param class-string<BootloaderInterface>|BootloaderInterface $bootloader |
|
|
|
|
|
|
176
|
|
|
*/ |
|
177
|
519 |
|
private function getBootloadConfig( |
|
178
|
|
|
string|BootloaderInterface $bootloader, |
|
179
|
|
|
array|callable|BootloadConfig $config |
|
180
|
|
|
): BootloadConfig { |
|
181
|
519 |
|
if ($config instanceof \Closure) { |
|
|
|
|
|
|
182
|
2 |
|
$config = $this->container instanceof ResolverInterface |
|
183
|
2 |
|
? $config(...$this->container->resolveArguments(new \ReflectionFunction($config))) |
|
184
|
|
|
: $config(); |
|
185
|
|
|
} |
|
186
|
519 |
|
$attr = $this->getBootloadConfigAttribute($bootloader); |
|
187
|
|
|
|
|
188
|
519 |
|
$getArgument = static function (string $key, bool $override, mixed $default = []) use ($config, $attr): mixed { |
|
189
|
519 |
|
return match (true) { |
|
190
|
519 |
|
$config instanceof BootloadConfig && $override => $config->{$key}, |
|
191
|
519 |
|
$config instanceof BootloadConfig && !$override && \is_array($default) => |
|
192
|
519 |
|
$config->{$key} + ($attr->{$key} ?? []), |
|
193
|
519 |
|
$config instanceof BootloadConfig && !$override && \is_bool($default) => $config->{$key}, |
|
194
|
519 |
|
\is_array($config) && $config !== [] && $key === 'args' => $config, |
|
195
|
519 |
|
default => $attr->{$key} ?? $default, |
|
196
|
519 |
|
}; |
|
197
|
519 |
|
}; |
|
198
|
|
|
|
|
199
|
519 |
|
$override = $config instanceof BootloadConfig ? $config->override : true; |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
519 |
|
return new BootloadConfig( |
|
202
|
519 |
|
args: $getArgument('args', $override), |
|
203
|
519 |
|
enabled: $getArgument('enabled', $override, true), |
|
204
|
519 |
|
allowEnv: $getArgument('allowEnv', $override), |
|
205
|
519 |
|
denyEnv: $getArgument('denyEnv', $override), |
|
206
|
519 |
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param class-string<BootloaderInterface>|BootloaderInterface $bootloader |
|
|
|
|
|
|
211
|
|
|
*/ |
|
212
|
519 |
|
private function getBootloadConfigAttribute(string|BootloaderInterface $bootloader): ?BootloadConfig |
|
213
|
|
|
{ |
|
214
|
519 |
|
$attribute = null; |
|
215
|
519 |
|
if ($bootloader instanceof BootloaderInterface || \class_exists($bootloader)) { |
|
216
|
518 |
|
$ref = new \ReflectionClass($bootloader); |
|
217
|
518 |
|
$attribute = $ref->getAttributes(BootloadConfig::class)[0] ?? null; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
519 |
|
if ($attribute === null) { |
|
221
|
500 |
|
return null; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
22 |
|
return $attribute->newInstance(); |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.