|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Core; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use ReflectionFunctionAbstract as ContextFunction; |
|
9
|
|
|
use Spiral\Core\Config\Alias; |
|
10
|
|
|
use Spiral\Core\Config\WeakReference; |
|
11
|
|
|
use Spiral\Core\Container\Autowire; |
|
12
|
|
|
use Spiral\Core\Container\InjectableInterface; |
|
13
|
|
|
use Spiral\Core\Container\SingletonInterface; |
|
14
|
|
|
use Spiral\Core\Exception\Binder\SingletonOverloadException; |
|
15
|
|
|
use Spiral\Core\Exception\Container\ContainerException; |
|
16
|
|
|
use Spiral\Core\Exception\LogicException; |
|
17
|
|
|
use Spiral\Core\Exception\Scope\FinalizersException; |
|
18
|
|
|
use Spiral\Core\Internal\Common\DestructorTrait; |
|
19
|
|
|
use Spiral\Core\Internal\Config\StateBinder; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Auto-wiring container: declarative singletons, contextual injections, parent container |
|
23
|
|
|
* delegation and ability to lazy wire. |
|
24
|
|
|
* |
|
25
|
|
|
* Container does not support setter injections, private properties, etc. Normally it will work |
|
26
|
|
|
* with classes only to be as much invisible as possible. Attention, this is hungry implementation |
|
27
|
|
|
* of container, meaning it WILL try to resolve dependency unless you specified custom lazy |
|
28
|
|
|
* factory. |
|
29
|
|
|
* |
|
30
|
|
|
* You can use injectors to delegate class resolution to external container. |
|
31
|
|
|
* |
|
32
|
|
|
* @see InjectableInterface |
|
33
|
|
|
* @see SingletonInterface |
|
34
|
|
|
* |
|
35
|
|
|
* @psalm-import-type TResolver from BinderInterface |
|
36
|
|
|
* @psalm-import-type TInvokable from InvokerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
final class Container implements |
|
39
|
|
|
ContainerInterface, |
|
40
|
|
|
BinderInterface, |
|
41
|
|
|
FactoryInterface, |
|
42
|
|
|
ResolverInterface, |
|
43
|
|
|
InvokerInterface, |
|
44
|
|
|
ScopeInterface |
|
45
|
|
|
{ |
|
46
|
|
|
use DestructorTrait; |
|
47
|
|
|
|
|
48
|
|
|
public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
|
49
|
|
|
|
|
50
|
|
|
private Internal\State $state; |
|
51
|
|
|
private ResolverInterface|Internal\Resolver $resolver; |
|
52
|
|
|
private FactoryInterface|Internal\Factory $factory; |
|
53
|
|
|
private ContainerInterface|Internal\Container $container; |
|
54
|
|
|
private BinderInterface|Internal\Binder $binder; |
|
55
|
|
|
private InvokerInterface|Internal\Invoker $invoker; |
|
56
|
|
|
private Internal\Scope $scope; |
|
57
|
|
|
private Internal\Actor $actor; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Container constructor. |
|
61
|
|
|
*/ |
|
62
|
1350 |
|
public function __construct( |
|
63
|
|
|
private Config $config = new Config(), |
|
64
|
|
|
string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
|
65
|
|
|
private Options $options = new Options(), |
|
66
|
|
|
) { |
|
67
|
1350 |
|
if (\is_object($scopeName)) { |
|
68
|
103 |
|
$scopeName = (string) $scopeName->value; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1350 |
|
$this->initServices($this, $scopeName); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */ |
|
74
|
1350 |
|
\assert(isset($this->state)); |
|
75
|
|
|
|
|
76
|
|
|
// Bind himself |
|
77
|
1350 |
|
$shared = new Alias(self::class); |
|
78
|
1350 |
|
$this->state->bindings = \array_merge($this->state->bindings, [ |
|
79
|
1350 |
|
self::class => new WeakReference(\WeakReference::create($this)), |
|
80
|
1350 |
|
ContainerInterface::class => $shared, |
|
81
|
1350 |
|
BinderInterface::class => $shared, |
|
82
|
1350 |
|
FactoryInterface::class => $shared, |
|
83
|
1350 |
|
ScopeInterface::class => $shared, |
|
84
|
1350 |
|
ResolverInterface::class => $shared, |
|
85
|
1350 |
|
InvokerInterface::class => $shared, |
|
86
|
1350 |
|
]); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
607 |
|
public function resolveArguments( |
|
90
|
|
|
ContextFunction $reflection, |
|
91
|
|
|
array $parameters = [], |
|
92
|
|
|
bool $validate = true, |
|
93
|
|
|
): array { |
|
94
|
607 |
|
return $this->resolver->resolveArguments($reflection, $parameters, $validate); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function validateArguments(ContextFunction $reflection, array $arguments = []): void |
|
98
|
|
|
{ |
|
99
|
|
|
$this->resolver->validateArguments($reflection, $arguments); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param \Stringable|string|null $context Related to parameter caused injection if any. |
|
104
|
|
|
* |
|
105
|
|
|
* @throws ContainerException |
|
106
|
|
|
* @throws \Throwable |
|
107
|
|
|
* @psalm-suppress TooManyArguments |
|
108
|
|
|
*/ |
|
109
|
657 |
|
public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
110
|
|
|
{ |
|
111
|
657 |
|
return ContainerScope::getContainer() === $this |
|
112
|
473 |
|
? $this->factory->make($alias, $parameters, $context) |
|
113
|
657 |
|
: ContainerScope::runScope($this, fn(): mixed => $this->factory->make($alias, $parameters, $context)); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Context parameter will be passed to class injectors, which makes possible to use this method |
|
118
|
|
|
* as: |
|
119
|
|
|
* |
|
120
|
|
|
* $this->container->get(DatabaseInterface::class, 'default'); |
|
121
|
|
|
* |
|
122
|
|
|
* Attention, context ignored when outer container has instance by alias. |
|
123
|
|
|
* |
|
124
|
|
|
* @template T |
|
125
|
|
|
* |
|
126
|
|
|
* @param class-string<T>|string|Autowire $id |
|
|
|
|
|
|
127
|
|
|
* @param \Stringable|string|null $context Call context. |
|
128
|
|
|
* |
|
129
|
|
|
* @return ($id is class-string ? T : mixed) |
|
|
|
|
|
|
130
|
|
|
* |
|
131
|
|
|
* @throws ContainerException |
|
132
|
|
|
* @throws \Throwable |
|
133
|
|
|
* @psalm-suppress TooManyArguments |
|
134
|
|
|
*/ |
|
135
|
1116 |
|
public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
136
|
|
|
{ |
|
137
|
1116 |
|
return ContainerScope::getContainer() === $this |
|
138
|
768 |
|
? $this->container->get($id, $context) |
|
139
|
1115 |
|
: ContainerScope::runScope($this, fn() => $this->container->get($id, $context)); |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
682 |
|
public function has(string $id): bool |
|
143
|
|
|
{ |
|
144
|
682 |
|
return $this->container->has($id); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Make a Binder proxy to configure bindings for a specific scope. |
|
149
|
|
|
* |
|
150
|
|
|
* @param null|\BackedEnum|string $scope Scope name. |
|
151
|
|
|
* If {@see null}, binder for the current working scope will be returned. |
|
152
|
|
|
* If {@see string}, the default binder for the given scope will be returned. Default bindings won't affect |
|
153
|
|
|
* already created Container instances except the case with the root one. |
|
154
|
|
|
*/ |
|
155
|
628 |
|
public function getBinder(string|\BackedEnum|null $scope = null): BinderInterface |
|
156
|
|
|
{ |
|
157
|
628 |
|
$scope = \is_object($scope) ? (string) $scope->value : $scope; |
|
|
|
|
|
|
158
|
|
|
|
|
159
|
628 |
|
return $scope === null |
|
160
|
5 |
|
? $this->binder |
|
161
|
628 |
|
: new StateBinder($this->config->scopedBindings->getState($scope)); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @throws \Throwable |
|
166
|
|
|
*/ |
|
167
|
772 |
|
public function runScope(Scope|array $bindings, callable $scope): mixed |
|
168
|
|
|
{ |
|
169
|
772 |
|
if (!\is_array($bindings)) { |
|
|
|
|
|
|
170
|
321 |
|
return $this->runIsolatedScope($bindings, $scope); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
706 |
|
$binds = &$this->state->bindings; |
|
174
|
706 |
|
$singletons = &$this->state->singletons; |
|
175
|
706 |
|
$cleanup = $previous = $prevSin = []; |
|
176
|
706 |
|
foreach ($bindings as $alias => $resolver) { |
|
177
|
|
|
// Store previous bindings |
|
178
|
705 |
|
if (isset($binds[$alias])) { |
|
179
|
641 |
|
$previous[$alias] = $binds[$alias]; |
|
180
|
|
|
} else { |
|
181
|
|
|
// Store bindings to be removed |
|
182
|
704 |
|
$cleanup[] = $alias; |
|
183
|
|
|
} |
|
184
|
|
|
// Store previous singletons |
|
185
|
705 |
|
if (isset($singletons[$alias])) { |
|
186
|
1 |
|
$prevSin[$alias] = $singletons[$alias]; |
|
187
|
1 |
|
unset($singletons[$alias]); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
705 |
|
$this->binder->bind($alias, $resolver); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
try { |
|
194
|
706 |
|
return ContainerScope::getContainer() !== $this |
|
195
|
705 |
|
? ContainerScope::runScope($this, $scope) |
|
196
|
681 |
|
: $scope($this); |
|
197
|
|
|
} finally { |
|
198
|
|
|
// Remove new bindings |
|
199
|
706 |
|
foreach ($cleanup as $alias) { |
|
200
|
704 |
|
unset($binds[$alias], $singletons[$alias]); |
|
201
|
|
|
} |
|
202
|
|
|
// Restore previous bindings |
|
203
|
706 |
|
foreach ($previous as $alias => $resolver) { |
|
204
|
641 |
|
$binds[$alias] = $resolver; |
|
205
|
|
|
} |
|
206
|
|
|
// Restore singletons |
|
207
|
706 |
|
foreach ($prevSin as $alias => $instance) { |
|
208
|
1 |
|
$singletons[$alias] = $instance; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Invoke given closure or function withing specific IoC scope. |
|
215
|
|
|
* |
|
216
|
|
|
* @template TReturn |
|
217
|
|
|
* |
|
218
|
|
|
* @param callable(mixed ...$params): TReturn $closure |
|
219
|
|
|
* @param array<non-empty-string, TResolver> $bindings Custom bindings for the new scope. |
|
|
|
|
|
|
220
|
|
|
* @param null|string $name Scope name. Named scopes can have individual bindings and constrains. |
|
221
|
|
|
* @param bool $autowire If {@see false}, closure will be invoked with just only the passed Container as an |
|
222
|
|
|
* argument. Otherwise, {@see InvokerInterface::invoke()} will be used to invoke the closure. |
|
223
|
|
|
* |
|
224
|
|
|
* @return TReturn |
|
225
|
|
|
* @throws \Throwable |
|
226
|
|
|
* |
|
227
|
|
|
* @deprecated Use {@see runScope()} with the {@see Scope} as the first argument. |
|
228
|
|
|
* @internal Used in tests only |
|
229
|
|
|
*/ |
|
230
|
26 |
|
public function runScoped(callable $closure, array $bindings = [], ?string $name = null, bool $autowire = true): mixed |
|
231
|
|
|
{ |
|
232
|
26 |
|
return $this->runIsolatedScope(new Scope($name, $bindings, $autowire), $closure); |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Bind value resolver to container alias. Resolver can be class name (will be constructed |
|
237
|
|
|
* for each method call), function array or Closure (executed every call). Only object resolvers |
|
238
|
|
|
* supported by this method. |
|
239
|
|
|
*/ |
|
240
|
1057 |
|
public function bind(string $alias, mixed $resolver): void |
|
241
|
|
|
{ |
|
242
|
1057 |
|
$this->binder->bind($alias, $resolver); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Bind value resolver to container alias to be executed as cached. Resolver can be class name |
|
247
|
|
|
* (will be constructed only once), function array or Closure (executed only once call). |
|
248
|
|
|
* |
|
249
|
|
|
* @psalm-param TResolver $resolver |
|
250
|
|
|
* @param bool|null $force If the value is false, an exception will be thrown when attempting |
|
251
|
|
|
* to bind an already constructed singleton. |
|
252
|
|
|
* If the value is null, option {@see Options::$allowSingletonsRebinding} will be used. |
|
253
|
|
|
* @throws SingletonOverloadException |
|
254
|
|
|
*/ |
|
255
|
953 |
|
public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
|
256
|
|
|
{ |
|
257
|
953 |
|
if ($force ?? $this->options->allowSingletonsRebinding) { |
|
258
|
952 |
|
$this->binder->removeBinding($alias); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
953 |
|
$this->binder->bindSingleton($alias, $resolver); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Check if alias points to constructed instance (singleton). |
|
266
|
|
|
*/ |
|
267
|
14 |
|
public function hasInstance(string $alias): bool |
|
268
|
|
|
{ |
|
269
|
14 |
|
return $this->binder->hasInstance($alias); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Check if the alias has is bound. |
|
274
|
|
|
*/ |
|
275
|
99 |
|
public function hasBinding(string $alias): bool |
|
276
|
|
|
{ |
|
277
|
99 |
|
return $this->binder->hasBinding($alias); |
|
|
|
|
|
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
452 |
|
public function removeBinding(string $alias): void |
|
281
|
|
|
{ |
|
282
|
452 |
|
$this->binder->removeBinding($alias); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @psalm-param TInvokable $target |
|
287
|
|
|
*/ |
|
288
|
795 |
|
public function invoke(mixed $target, array $parameters = []): mixed |
|
289
|
|
|
{ |
|
290
|
795 |
|
return ContainerScope::getContainer() === $this |
|
291
|
758 |
|
? $this->invoker->invoke($target, $parameters) |
|
292
|
773 |
|
: ContainerScope::runScope($this, fn(): mixed => $this->invoker->invoke($target, $parameters)); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Bind class or class interface to the injector source (InjectorInterface). |
|
297
|
|
|
*/ |
|
298
|
555 |
|
public function bindInjector(string $class, string $injector): void |
|
299
|
|
|
{ |
|
300
|
555 |
|
$this->binder->bindInjector($class, $injector); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function removeInjector(string $class): void |
|
304
|
|
|
{ |
|
305
|
|
|
$this->binder->removeInjector($class); |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
10 |
|
public function hasInjector(string $class): bool |
|
309
|
|
|
{ |
|
310
|
10 |
|
return $this->binder->hasInjector($class); |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Container can not be cloned. |
|
315
|
|
|
*/ |
|
316
|
1 |
|
public function __clone() |
|
317
|
|
|
{ |
|
318
|
1 |
|
throw new LogicException('Container is not cloneable.'); |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
610 |
|
public function __destruct() |
|
322
|
|
|
{ |
|
323
|
610 |
|
$this->closeScope(); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/** |
|
327
|
|
|
* Init internal container services. |
|
328
|
|
|
*/ |
|
329
|
1350 |
|
private function initServices( |
|
330
|
|
|
self $container, |
|
331
|
|
|
?string $scopeName, |
|
332
|
|
|
): void { |
|
333
|
1350 |
|
$isRoot = $container->config->lockRoot(); |
|
334
|
|
|
|
|
335
|
|
|
// Get named scope or create anonymous one |
|
336
|
1350 |
|
$state = match (true) { |
|
337
|
217 |
|
$scopeName === null => new Internal\State(), |
|
338
|
|
|
// Only root container can make default bindings directly |
|
339
|
1350 |
|
$isRoot => $container->config->scopedBindings->getState($scopeName), |
|
|
|
|
|
|
340
|
302 |
|
default => clone $container->config->scopedBindings->getState($scopeName), |
|
341
|
1350 |
|
}; |
|
342
|
|
|
|
|
343
|
1350 |
|
$constructor = new Internal\Common\Registry($container->config, [ |
|
344
|
1350 |
|
'state' => $state, |
|
345
|
1350 |
|
'scope' => new Internal\Scope($scopeName), |
|
346
|
1350 |
|
], $this->options); |
|
347
|
|
|
|
|
348
|
|
|
// Create container services |
|
349
|
1350 |
|
foreach ($container->config as $property => $class) { |
|
350
|
1350 |
|
if (\property_exists($container, $property)) { |
|
351
|
1350 |
|
$container->$property = $constructor->get($property, $class); |
|
352
|
|
|
} |
|
353
|
|
|
} |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* Execute finalizers and destruct the container. |
|
358
|
|
|
* |
|
359
|
|
|
* @throws FinalizersException |
|
360
|
|
|
*/ |
|
361
|
610 |
|
private function closeScope(): void |
|
362
|
|
|
{ |
|
363
|
|
|
/** @psalm-suppress RedundantPropertyInitializationCheck */ |
|
364
|
610 |
|
if (!isset($this->scope)) { |
|
365
|
347 |
|
$this->destruct(); |
|
366
|
347 |
|
return; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
610 |
|
$scopeName = $this->scope->getScopeName(); |
|
370
|
|
|
|
|
371
|
|
|
// Run finalizers |
|
372
|
610 |
|
$errors = []; |
|
373
|
610 |
|
foreach ($this->state->finalizers as $finalizer) { |
|
374
|
|
|
try { |
|
375
|
4 |
|
$this->invoker->invoke($finalizer); |
|
376
|
|
|
} catch (\Throwable $e) { |
|
377
|
|
|
$errors[] = $e; |
|
378
|
|
|
} |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
// Destroy the container |
|
382
|
610 |
|
$this->destruct(); |
|
383
|
|
|
|
|
384
|
|
|
// Throw collected errors |
|
385
|
610 |
|
if ($errors !== []) { |
|
386
|
|
|
throw new FinalizersException($scopeName, $errors); |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
/** |
|
391
|
|
|
* @template TReturn |
|
392
|
|
|
* |
|
393
|
|
|
* @param callable(mixed ...$params): TReturn $closure |
|
394
|
|
|
* |
|
395
|
|
|
* @return TReturn |
|
|
|
|
|
|
396
|
|
|
* @throws \Throwable |
|
397
|
|
|
*/ |
|
398
|
347 |
|
private function runIsolatedScope(Scope $config, callable $closure): mixed |
|
399
|
|
|
{ |
|
400
|
|
|
// Open scope |
|
401
|
347 |
|
$container = new self($this->config, $config->name, $this->options); |
|
402
|
|
|
|
|
403
|
|
|
// Configure scope |
|
404
|
347 |
|
$container->scope->setParent($this, $this->scope, $this->actor); |
|
405
|
|
|
|
|
406
|
|
|
// Add specific bindings |
|
407
|
347 |
|
foreach ($config->bindings as $alias => $resolver) { |
|
408
|
210 |
|
$container->binder->bind($alias, $resolver); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
347 |
|
return ContainerScope::runScope( |
|
412
|
347 |
|
$container, |
|
413
|
347 |
|
static function (self $container) use ($config, $closure): mixed { |
|
414
|
|
|
try { |
|
415
|
347 |
|
return $config->autowire |
|
416
|
347 |
|
? $container->invoke($closure) |
|
417
|
336 |
|
: $closure($container); |
|
418
|
|
|
} finally { |
|
419
|
347 |
|
$container->closeScope(); |
|
420
|
|
|
} |
|
421
|
347 |
|
}, |
|
422
|
347 |
|
); |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
|