@@ -12,10 +12,10 @@ |
||
12 | 12 | public function __construct( |
13 | 13 | public readonly object $value, |
14 | 14 | public readonly bool $singleton = false, |
15 | - ) {} |
|
15 | + ){} |
|
16 | 16 | |
17 | 17 | public function __toString(): string |
18 | 18 | { |
19 | - return 'Shared object of class ' . $this->value::class; |
|
19 | + return 'Shared object of class '.$this->value::class; |
|
20 | 20 | } |
21 | 21 | } |
@@ -12,7 +12,8 @@ |
||
12 | 12 | public function __construct( |
13 | 13 | public readonly object $value, |
14 | 14 | public readonly bool $singleton = false, |
15 | - ) {} |
|
15 | + ) { |
|
16 | +} |
|
16 | 17 | |
17 | 18 | public function __toString(): string |
18 | 19 | { |
@@ -68,19 +68,19 @@ discard block |
||
68 | 68 | * |
69 | 69 | * @throws \Throwable |
70 | 70 | */ |
71 | - public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
|
71 | + public function make(string $alias, array $parameters = [], Stringable | string | null $context = null): mixed |
|
72 | 72 | { |
73 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
73 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)){ |
|
74 | 74 | return $this->state->singletons[$alias]; |
75 | 75 | } |
76 | 76 | |
77 | 77 | $binding = $this->state->bindings[$alias] ?? null; |
78 | 78 | |
79 | - if ($binding === null) { |
|
79 | + if ($binding === null){ |
|
80 | 80 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
81 | 81 | } |
82 | 82 | |
83 | - try { |
|
83 | + try{ |
|
84 | 84 | $this->tracer->push( |
85 | 85 | false, |
86 | 86 | action: 'resolve from binding', |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
111 | 111 | default => $binding, |
112 | 112 | }; |
113 | - } finally { |
|
113 | + }finally{ |
|
114 | 114 | $this->state->bindings[$alias] ??= $binding; |
115 | 115 | $this->tracer->pop(true); |
116 | 116 | $this->tracer->pop(false); |
@@ -124,18 +124,18 @@ discard block |
||
124 | 124 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
125 | 125 | { |
126 | 126 | $context = $ctx->context; |
127 | - try { |
|
127 | + try{ |
|
128 | 128 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
129 | - } catch (\ReflectionException $e) { |
|
129 | + }catch (\ReflectionException $e){ |
|
130 | 130 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
131 | 131 | } |
132 | 132 | |
133 | 133 | $injector = $binding->injector; |
134 | 134 | |
135 | - try { |
|
135 | + try{ |
|
136 | 136 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
137 | 137 | |
138 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
138 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
139 | 139 | throw new InjectionException( |
140 | 140 | \sprintf( |
141 | 141 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -162,7 +162,7 @@ discard block |
||
162 | 162 | default => (string)$context, |
163 | 163 | }); |
164 | 164 | |
165 | - if (!$reflection->isInstance($instance)) { |
|
165 | + if (!$reflection->isInstance($instance)){ |
|
166 | 166 | throw new InjectionException( |
167 | 167 | \sprintf( |
168 | 168 | "Invalid injection response for '%s'.", |
@@ -172,7 +172,7 @@ discard block |
||
172 | 172 | } |
173 | 173 | |
174 | 174 | return $instance; |
175 | - } finally { |
|
175 | + }finally{ |
|
176 | 176 | $this->state->bindings[$ctx->class] ??= $binding; |
177 | 177 | } |
178 | 178 | } |
@@ -180,27 +180,27 @@ discard block |
||
180 | 180 | private function resolveAlias( |
181 | 181 | Config\Alias $binding, |
182 | 182 | string $alias, |
183 | - Stringable|string|null $context, |
|
183 | + Stringable | string | null $context, |
|
184 | 184 | array $arguments, |
185 | 185 | ): mixed { |
186 | 186 | $result = $binding->alias === $alias |
187 | 187 | ? $this->autowire( |
188 | - new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton), |
|
188 | + new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton), |
|
189 | 189 | $arguments, |
190 | 190 | ) |
191 | 191 | //Binding is pointing to something else |
192 | 192 | : $this->make($binding->alias, $arguments, $context); |
193 | 193 | |
194 | - if ($binding->singleton && $arguments === []) { |
|
194 | + if ($binding->singleton && $arguments === []){ |
|
195 | 195 | $this->state->singletons[$alias] = $result; |
196 | 196 | } |
197 | 197 | |
198 | 198 | return $result; |
199 | 199 | } |
200 | 200 | |
201 | - private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed |
|
201 | + private function resolveProxy(Config\Proxy $binding, string $alias, Stringable | string | null $context): mixed |
|
202 | 202 | { |
203 | - if ($context instanceof RetryContext) { |
|
203 | + if ($context instanceof RetryContext){ |
|
204 | 204 | return $binding->fallbackFactory === null |
205 | 205 | ? throw new RecursiveProxyException( |
206 | 206 | $alias, |
@@ -211,7 +211,7 @@ discard block |
||
211 | 211 | |
212 | 212 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
213 | 213 | |
214 | - if ($binding->singleton) { |
|
214 | + if ($binding->singleton){ |
|
215 | 215 | $this->state->singletons[$alias] = $result; |
216 | 216 | } |
217 | 217 | |
@@ -221,19 +221,19 @@ discard block |
||
221 | 221 | private function resolveShared( |
222 | 222 | Config\Shared $binding, |
223 | 223 | string $alias, |
224 | - Stringable|string|null $context, |
|
224 | + Stringable | string | null $context, |
|
225 | 225 | array $arguments, |
226 | 226 | ): object { |
227 | 227 | $avoidCache = $arguments !== []; |
228 | 228 | |
229 | - if ($avoidCache) { |
|
229 | + if ($avoidCache){ |
|
230 | 230 | return $this->createInstance( |
231 | 231 | new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
232 | 232 | $arguments, |
233 | 233 | ); |
234 | 234 | } |
235 | 235 | |
236 | - if ($binding->singleton) { |
|
236 | + if ($binding->singleton){ |
|
237 | 237 | $this->state->singletons[$alias] = $binding->value; |
238 | 238 | } |
239 | 239 | |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | private function resolveAutowire( |
244 | 244 | Config\Autowire $binding, |
245 | 245 | string $alias, |
246 | - Stringable|string|null $context, |
|
246 | + Stringable | string | null $context, |
|
247 | 247 | array $arguments, |
248 | 248 | ): mixed { |
249 | 249 | $target = $binding->autowire->alias; |
@@ -257,17 +257,17 @@ discard block |
||
257 | 257 | } |
258 | 258 | |
259 | 259 | private function resolveFactory( |
260 | - Config\Factory|Config\DeferredFactory $binding, |
|
260 | + Config\Factory | Config\DeferredFactory $binding, |
|
261 | 261 | string $alias, |
262 | - Stringable|string|null $context, |
|
262 | + Stringable | string | null $context, |
|
263 | 263 | array $arguments, |
264 | 264 | ): mixed { |
265 | 265 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
266 | - try { |
|
266 | + try{ |
|
267 | 267 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
268 | 268 | ? ($binding->factory)() |
269 | 269 | : $this->invoker->invoke($binding->factory, $arguments); |
270 | - } catch (NotCallableException $e) { |
|
270 | + }catch (NotCallableException $e){ |
|
271 | 271 | throw new ContainerException( |
272 | 272 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
273 | 273 | $e->getCode(), |
@@ -281,24 +281,24 @@ discard block |
||
281 | 281 | private function resolveWeakReference( |
282 | 282 | Config\WeakReference $binding, |
283 | 283 | string $alias, |
284 | - Stringable|string|null $context, |
|
284 | + Stringable | string | null $context, |
|
285 | 285 | array $arguments, |
286 | 286 | ): ?object { |
287 | 287 | $avoidCache = $arguments !== []; |
288 | 288 | |
289 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
290 | - try { |
|
289 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
290 | + try{ |
|
291 | 291 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
292 | 292 | |
293 | 293 | $object = $this->createInstance( |
294 | 294 | new Ctx(alias: $alias, class: $alias, context: $context), |
295 | 295 | $arguments, |
296 | 296 | ); |
297 | - if ($avoidCache) { |
|
297 | + if ($avoidCache){ |
|
298 | 298 | return $object; |
299 | 299 | } |
300 | 300 | $binding->reference = WeakReference::create($object); |
301 | - } catch (\Throwable) { |
|
301 | + }catch (\Throwable){ |
|
302 | 302 | throw new ContainerException( |
303 | 303 | $this->tracer->combineTraceMessage( |
304 | 304 | \sprintf( |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | ) |
309 | 309 | ) |
310 | 310 | ); |
311 | - } finally { |
|
311 | + }finally{ |
|
312 | 312 | $this->tracer->pop(); |
313 | 313 | } |
314 | 314 | } |
@@ -319,23 +319,23 @@ discard block |
||
319 | 319 | private function resolveWithoutBinding( |
320 | 320 | string $alias, |
321 | 321 | array $parameters = [], |
322 | - Stringable|string|null $context = null |
|
322 | + Stringable | string | null $context = null |
|
323 | 323 | ): mixed { |
324 | 324 | $parent = $this->scope->getParentFactory(); |
325 | 325 | |
326 | - if ($parent !== null) { |
|
327 | - try { |
|
326 | + if ($parent !== null){ |
|
327 | + try{ |
|
328 | 328 | $this->tracer->push(false, ...[ |
329 | 329 | 'current scope' => $this->scope->getScopeName(), |
330 | 330 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
331 | 331 | ]); |
332 | 332 | /** @psalm-suppress TooManyArguments */ |
333 | 333 | return $parent->make($alias, $parameters, $context); |
334 | - } catch (BadScopeException $e) { |
|
335 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
334 | + }catch (BadScopeException $e){ |
|
335 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
336 | 336 | throw $e; |
337 | 337 | } |
338 | - } catch (ContainerExceptionInterface $e) { |
|
338 | + }catch (ContainerExceptionInterface $e){ |
|
339 | 339 | $className = match (true) { |
340 | 340 | $e instanceof NotFoundException => NotFoundException::class, |
341 | 341 | $e instanceof RecursiveProxyException => throw $e, |
@@ -345,19 +345,19 @@ discard block |
||
345 | 345 | 'Can\'t resolve `%s`.', |
346 | 346 | $alias, |
347 | 347 | )), previous: $e); |
348 | - } finally { |
|
348 | + }finally{ |
|
349 | 349 | $this->tracer->pop(false); |
350 | 350 | } |
351 | 351 | } |
352 | 352 | |
353 | 353 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
354 | - try { |
|
354 | + try{ |
|
355 | 355 | //No direct instructions how to construct class, make is automatically |
356 | 356 | return $this->autowire( |
357 | 357 | new Ctx(alias: $alias, class: $alias, context: $context), |
358 | 358 | $parameters, |
359 | 359 | ); |
360 | - } finally { |
|
360 | + }finally{ |
|
361 | 361 | $this->tracer->pop(false); |
362 | 362 | } |
363 | 363 | } |
@@ -378,7 +378,7 @@ discard block |
||
378 | 378 | && |
379 | 379 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
380 | 380 | )) |
381 | - ) { |
|
381 | + ){ |
|
382 | 382 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
383 | 383 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
384 | 384 | $this->tracer->getRootAlias(), |
@@ -404,13 +404,13 @@ discard block |
||
404 | 404 | Ctx $ctx, |
405 | 405 | array $arguments, |
406 | 406 | ): object { |
407 | - if ($this->options->checkScope) { |
|
407 | + if ($this->options->checkScope){ |
|
408 | 408 | // Check scope name |
409 | 409 | $ctx->reflection = new \ReflectionClass($instance); |
410 | 410 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
411 | - if ($scopeName !== null) { |
|
411 | + if ($scopeName !== null){ |
|
412 | 412 | $scope = $this->scope; |
413 | - while ($scope->getScopeName() !== $scopeName) { |
|
413 | + while ($scope->getScopeName() !== $scopeName){ |
|
414 | 414 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
415 | 415 | } |
416 | 416 | } |
@@ -439,26 +439,26 @@ discard block |
||
439 | 439 | array $arguments, |
440 | 440 | ): object { |
441 | 441 | $class = $ctx->class; |
442 | - try { |
|
442 | + try{ |
|
443 | 443 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
444 | - } catch (\ReflectionException $e) { |
|
444 | + }catch (\ReflectionException $e){ |
|
445 | 445 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
446 | 446 | } |
447 | 447 | |
448 | 448 | // Check scope name |
449 | - if ($this->options->checkScope) { |
|
449 | + if ($this->options->checkScope){ |
|
450 | 450 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
451 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
451 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
452 | 452 | throw new BadScopeException($scope, $class); |
453 | 453 | } |
454 | 454 | } |
455 | 455 | |
456 | 456 | // We have to construct class using external injector when we know the exact context |
457 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
457 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
458 | 458 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
459 | 459 | } |
460 | 460 | |
461 | - if (!$reflection->isInstantiable()) { |
|
461 | + if (!$reflection->isInstantiable()){ |
|
462 | 462 | $itIs = match (true) { |
463 | 463 | $reflection->isEnum() => 'Enum', |
464 | 464 | $reflection->isAbstract() => 'Abstract class', |
@@ -471,12 +471,12 @@ discard block |
||
471 | 471 | |
472 | 472 | $constructor = $reflection->getConstructor(); |
473 | 473 | |
474 | - if ($constructor !== null) { |
|
475 | - try { |
|
474 | + if ($constructor !== null){ |
|
475 | + try{ |
|
476 | 476 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
477 | 477 | $this->tracer->push(true); |
478 | 478 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
479 | - } catch (ValidationException $e) { |
|
479 | + }catch (ValidationException $e){ |
|
480 | 480 | throw new ContainerException( |
481 | 481 | $this->tracer->combineTraceMessage( |
482 | 482 | \sprintf( |
@@ -486,22 +486,22 @@ discard block |
||
486 | 486 | ) |
487 | 487 | ), |
488 | 488 | ); |
489 | - } finally { |
|
489 | + }finally{ |
|
490 | 490 | $this->tracer->pop(true); |
491 | 491 | $this->tracer->pop(false); |
492 | 492 | } |
493 | - try { |
|
493 | + try{ |
|
494 | 494 | // Using constructor with resolved arguments |
495 | 495 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
496 | 496 | $this->tracer->push(true); |
497 | 497 | $instance = new $class(...$args); |
498 | - } catch (\TypeError $e) { |
|
498 | + }catch (\TypeError $e){ |
|
499 | 499 | throw new WrongTypeException($constructor, $e); |
500 | - } finally { |
|
500 | + }finally{ |
|
501 | 501 | $this->tracer->pop(true); |
502 | 502 | $this->tracer->pop(false); |
503 | 503 | } |
504 | - } else { |
|
504 | + }else{ |
|
505 | 505 | // No constructor specified |
506 | 506 | $instance = $reflection->newInstance(); |
507 | 507 | } |
@@ -519,13 +519,13 @@ discard block |
||
519 | 519 | $instance = $this->runInflector($instance); |
520 | 520 | |
521 | 521 | //Declarative singletons |
522 | - if ($this->isSingleton($ctx)) { |
|
522 | + if ($this->isSingleton($ctx)){ |
|
523 | 523 | $this->state->singletons[$ctx->alias] = $instance; |
524 | 524 | } |
525 | 525 | |
526 | 526 | // Register finalizer |
527 | 527 | $finalizer = $this->getFinalizer($ctx, $instance); |
528 | - if ($finalizer !== null) { |
|
528 | + if ($finalizer !== null){ |
|
529 | 529 | $this->state->finalizers[] = $finalizer; |
530 | 530 | } |
531 | 531 | |
@@ -537,12 +537,12 @@ discard block |
||
537 | 537 | */ |
538 | 538 | private function isSingleton(Ctx $ctx): bool |
539 | 539 | { |
540 | - if ($ctx->singleton === true) { |
|
540 | + if ($ctx->singleton === true){ |
|
541 | 541 | return true; |
542 | 542 | } |
543 | 543 | |
544 | 544 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
545 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
545 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
546 | 546 | return true; |
547 | 547 | } |
548 | 548 | |
@@ -556,7 +556,7 @@ discard block |
||
556 | 556 | * @var Attribute\Finalize|null $attribute |
557 | 557 | */ |
558 | 558 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
559 | - if ($attribute === null) { |
|
559 | + if ($attribute === null){ |
|
560 | 560 | return null; |
561 | 561 | } |
562 | 562 | |
@@ -570,10 +570,10 @@ discard block |
||
570 | 570 | { |
571 | 571 | $scope = $this->scope; |
572 | 572 | |
573 | - while ($scope !== null) { |
|
574 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
575 | - if ($instance instanceof $class) { |
|
576 | - foreach ($inflectors as $inflector) { |
|
573 | + while ($scope !== null){ |
|
574 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
575 | + if ($instance instanceof $class){ |
|
576 | + foreach ($inflectors as $inflector){ |
|
577 | 577 | $instance = $inflector->getParametersCount() > 1 |
578 | 578 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
579 | 579 | : ($inflector->inflector)($instance); |
@@ -589,9 +589,9 @@ discard block |
||
589 | 589 | |
590 | 590 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
591 | 591 | { |
592 | - try { |
|
592 | + try{ |
|
593 | 593 | $this->resolver->validateArguments($reflection, $arguments); |
594 | - } catch (\Throwable) { |
|
594 | + }catch (\Throwable){ |
|
595 | 595 | return false; |
596 | 596 | } |
597 | 597 |
@@ -70,17 +70,20 @@ discard block |
||
70 | 70 | */ |
71 | 71 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
72 | 72 | { |
73 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
73 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
74 | + { |
|
74 | 75 | return $this->state->singletons[$alias]; |
75 | 76 | } |
76 | 77 | |
77 | 78 | $binding = $this->state->bindings[$alias] ?? null; |
78 | 79 | |
79 | - if ($binding === null) { |
|
80 | + if ($binding === null) |
|
81 | + { |
|
80 | 82 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
81 | 83 | } |
82 | 84 | |
83 | - try { |
|
85 | + try |
|
86 | + { |
|
84 | 87 | $this->tracer->push( |
85 | 88 | false, |
86 | 89 | action: 'resolve from binding', |
@@ -110,7 +113,9 @@ discard block |
||
110 | 113 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
111 | 114 | default => $binding, |
112 | 115 | }; |
113 | - } finally { |
|
116 | + } |
|
117 | + finally |
|
118 | + { |
|
114 | 119 | $this->state->bindings[$alias] ??= $binding; |
115 | 120 | $this->tracer->pop(true); |
116 | 121 | $this->tracer->pop(false); |
@@ -124,18 +129,23 @@ discard block |
||
124 | 129 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
125 | 130 | { |
126 | 131 | $context = $ctx->context; |
127 | - try { |
|
132 | + try |
|
133 | + { |
|
128 | 134 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
129 | - } catch (\ReflectionException $e) { |
|
135 | + } |
|
136 | + catch (\ReflectionException $e) |
|
137 | + { |
|
130 | 138 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
131 | 139 | } |
132 | 140 | |
133 | 141 | $injector = $binding->injector; |
134 | 142 | |
135 | - try { |
|
143 | + try |
|
144 | + { |
|
136 | 145 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
137 | 146 | |
138 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
147 | + if (!$injectorInstance instanceof InjectorInterface) |
|
148 | + { |
|
139 | 149 | throw new InjectionException( |
140 | 150 | \sprintf( |
141 | 151 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -162,7 +172,8 @@ discard block |
||
162 | 172 | default => (string)$context, |
163 | 173 | }); |
164 | 174 | |
165 | - if (!$reflection->isInstance($instance)) { |
|
175 | + if (!$reflection->isInstance($instance)) |
|
176 | + { |
|
166 | 177 | throw new InjectionException( |
167 | 178 | \sprintf( |
168 | 179 | "Invalid injection response for '%s'.", |
@@ -172,7 +183,9 @@ discard block |
||
172 | 183 | } |
173 | 184 | |
174 | 185 | return $instance; |
175 | - } finally { |
|
186 | + } |
|
187 | + finally |
|
188 | + { |
|
176 | 189 | $this->state->bindings[$ctx->class] ??= $binding; |
177 | 190 | } |
178 | 191 | } |
@@ -191,7 +204,8 @@ discard block |
||
191 | 204 | //Binding is pointing to something else |
192 | 205 | : $this->make($binding->alias, $arguments, $context); |
193 | 206 | |
194 | - if ($binding->singleton && $arguments === []) { |
|
207 | + if ($binding->singleton && $arguments === []) |
|
208 | + { |
|
195 | 209 | $this->state->singletons[$alias] = $result; |
196 | 210 | } |
197 | 211 | |
@@ -200,7 +214,8 @@ discard block |
||
200 | 214 | |
201 | 215 | private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed |
202 | 216 | { |
203 | - if ($context instanceof RetryContext) { |
|
217 | + if ($context instanceof RetryContext) |
|
218 | + { |
|
204 | 219 | return $binding->fallbackFactory === null |
205 | 220 | ? throw new RecursiveProxyException( |
206 | 221 | $alias, |
@@ -211,7 +226,8 @@ discard block |
||
211 | 226 | |
212 | 227 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
213 | 228 | |
214 | - if ($binding->singleton) { |
|
229 | + if ($binding->singleton) |
|
230 | + { |
|
215 | 231 | $this->state->singletons[$alias] = $result; |
216 | 232 | } |
217 | 233 | |
@@ -226,14 +242,16 @@ discard block |
||
226 | 242 | ): object { |
227 | 243 | $avoidCache = $arguments !== []; |
228 | 244 | |
229 | - if ($avoidCache) { |
|
245 | + if ($avoidCache) |
|
246 | + { |
|
230 | 247 | return $this->createInstance( |
231 | 248 | new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
232 | 249 | $arguments, |
233 | 250 | ); |
234 | 251 | } |
235 | 252 | |
236 | - if ($binding->singleton) { |
|
253 | + if ($binding->singleton) |
|
254 | + { |
|
237 | 255 | $this->state->singletons[$alias] = $binding->value; |
238 | 256 | } |
239 | 257 | |
@@ -263,11 +281,14 @@ discard block |
||
263 | 281 | array $arguments, |
264 | 282 | ): mixed { |
265 | 283 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
266 | - try { |
|
284 | + try |
|
285 | + { |
|
267 | 286 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
268 | 287 | ? ($binding->factory)() |
269 | 288 | : $this->invoker->invoke($binding->factory, $arguments); |
270 | - } catch (NotCallableException $e) { |
|
289 | + } |
|
290 | + catch (NotCallableException $e) |
|
291 | + { |
|
271 | 292 | throw new ContainerException( |
272 | 293 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
273 | 294 | $e->getCode(), |
@@ -286,19 +307,24 @@ discard block |
||
286 | 307 | ): ?object { |
287 | 308 | $avoidCache = $arguments !== []; |
288 | 309 | |
289 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
290 | - try { |
|
310 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
311 | + { |
|
312 | + try |
|
313 | + { |
|
291 | 314 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
292 | 315 | |
293 | 316 | $object = $this->createInstance( |
294 | 317 | new Ctx(alias: $alias, class: $alias, context: $context), |
295 | 318 | $arguments, |
296 | 319 | ); |
297 | - if ($avoidCache) { |
|
320 | + if ($avoidCache) |
|
321 | + { |
|
298 | 322 | return $object; |
299 | 323 | } |
300 | 324 | $binding->reference = WeakReference::create($object); |
301 | - } catch (\Throwable) { |
|
325 | + } |
|
326 | + catch (\Throwable) |
|
327 | + { |
|
302 | 328 | throw new ContainerException( |
303 | 329 | $this->tracer->combineTraceMessage( |
304 | 330 | \sprintf( |
@@ -308,7 +334,9 @@ discard block |
||
308 | 334 | ) |
309 | 335 | ) |
310 | 336 | ); |
311 | - } finally { |
|
337 | + } |
|
338 | + finally |
|
339 | + { |
|
312 | 340 | $this->tracer->pop(); |
313 | 341 | } |
314 | 342 | } |
@@ -323,19 +351,26 @@ discard block |
||
323 | 351 | ): mixed { |
324 | 352 | $parent = $this->scope->getParentFactory(); |
325 | 353 | |
326 | - if ($parent !== null) { |
|
327 | - try { |
|
354 | + if ($parent !== null) |
|
355 | + { |
|
356 | + try |
|
357 | + { |
|
328 | 358 | $this->tracer->push(false, ...[ |
329 | 359 | 'current scope' => $this->scope->getScopeName(), |
330 | 360 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
331 | 361 | ]); |
332 | 362 | /** @psalm-suppress TooManyArguments */ |
333 | 363 | return $parent->make($alias, $parameters, $context); |
334 | - } catch (BadScopeException $e) { |
|
335 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
364 | + } |
|
365 | + catch (BadScopeException $e) |
|
366 | + { |
|
367 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
368 | + { |
|
336 | 369 | throw $e; |
337 | 370 | } |
338 | - } catch (ContainerExceptionInterface $e) { |
|
371 | + } |
|
372 | + catch (ContainerExceptionInterface $e) |
|
373 | + { |
|
339 | 374 | $className = match (true) { |
340 | 375 | $e instanceof NotFoundException => NotFoundException::class, |
341 | 376 | $e instanceof RecursiveProxyException => throw $e, |
@@ -345,19 +380,24 @@ discard block |
||
345 | 380 | 'Can\'t resolve `%s`.', |
346 | 381 | $alias, |
347 | 382 | )), previous: $e); |
348 | - } finally { |
|
383 | + } |
|
384 | + finally |
|
385 | + { |
|
349 | 386 | $this->tracer->pop(false); |
350 | 387 | } |
351 | 388 | } |
352 | 389 | |
353 | 390 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
354 | - try { |
|
391 | + try |
|
392 | + { |
|
355 | 393 | //No direct instructions how to construct class, make is automatically |
356 | 394 | return $this->autowire( |
357 | 395 | new Ctx(alias: $alias, class: $alias, context: $context), |
358 | 396 | $parameters, |
359 | 397 | ); |
360 | - } finally { |
|
398 | + } |
|
399 | + finally |
|
400 | + { |
|
361 | 401 | $this->tracer->pop(false); |
362 | 402 | } |
363 | 403 | } |
@@ -404,13 +444,16 @@ discard block |
||
404 | 444 | Ctx $ctx, |
405 | 445 | array $arguments, |
406 | 446 | ): object { |
407 | - if ($this->options->checkScope) { |
|
447 | + if ($this->options->checkScope) |
|
448 | + { |
|
408 | 449 | // Check scope name |
409 | 450 | $ctx->reflection = new \ReflectionClass($instance); |
410 | 451 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
411 | - if ($scopeName !== null) { |
|
452 | + if ($scopeName !== null) |
|
453 | + { |
|
412 | 454 | $scope = $this->scope; |
413 | - while ($scope->getScopeName() !== $scopeName) { |
|
455 | + while ($scope->getScopeName() !== $scopeName) |
|
456 | + { |
|
414 | 457 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
415 | 458 | } |
416 | 459 | } |
@@ -439,26 +482,33 @@ discard block |
||
439 | 482 | array $arguments, |
440 | 483 | ): object { |
441 | 484 | $class = $ctx->class; |
442 | - try { |
|
485 | + try |
|
486 | + { |
|
443 | 487 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
444 | - } catch (\ReflectionException $e) { |
|
488 | + } |
|
489 | + catch (\ReflectionException $e) |
|
490 | + { |
|
445 | 491 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
446 | 492 | } |
447 | 493 | |
448 | 494 | // Check scope name |
449 | - if ($this->options->checkScope) { |
|
495 | + if ($this->options->checkScope) |
|
496 | + { |
|
450 | 497 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
451 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
498 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
499 | + { |
|
452 | 500 | throw new BadScopeException($scope, $class); |
453 | 501 | } |
454 | 502 | } |
455 | 503 | |
456 | 504 | // We have to construct class using external injector when we know the exact context |
457 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
505 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
506 | + { |
|
458 | 507 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
459 | 508 | } |
460 | 509 | |
461 | - if (!$reflection->isInstantiable()) { |
|
510 | + if (!$reflection->isInstantiable()) |
|
511 | + { |
|
462 | 512 | $itIs = match (true) { |
463 | 513 | $reflection->isEnum() => 'Enum', |
464 | 514 | $reflection->isAbstract() => 'Abstract class', |
@@ -471,12 +521,16 @@ discard block |
||
471 | 521 | |
472 | 522 | $constructor = $reflection->getConstructor(); |
473 | 523 | |
474 | - if ($constructor !== null) { |
|
475 | - try { |
|
524 | + if ($constructor !== null) |
|
525 | + { |
|
526 | + try |
|
527 | + { |
|
476 | 528 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
477 | 529 | $this->tracer->push(true); |
478 | 530 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
479 | - } catch (ValidationException $e) { |
|
531 | + } |
|
532 | + catch (ValidationException $e) |
|
533 | + { |
|
480 | 534 | throw new ContainerException( |
481 | 535 | $this->tracer->combineTraceMessage( |
482 | 536 | \sprintf( |
@@ -486,22 +540,31 @@ discard block |
||
486 | 540 | ) |
487 | 541 | ), |
488 | 542 | ); |
489 | - } finally { |
|
543 | + } |
|
544 | + finally |
|
545 | + { |
|
490 | 546 | $this->tracer->pop(true); |
491 | 547 | $this->tracer->pop(false); |
492 | 548 | } |
493 | - try { |
|
549 | + try |
|
550 | + { |
|
494 | 551 | // Using constructor with resolved arguments |
495 | 552 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
496 | 553 | $this->tracer->push(true); |
497 | 554 | $instance = new $class(...$args); |
498 | - } catch (\TypeError $e) { |
|
555 | + } |
|
556 | + catch (\TypeError $e) |
|
557 | + { |
|
499 | 558 | throw new WrongTypeException($constructor, $e); |
500 | - } finally { |
|
559 | + } |
|
560 | + finally |
|
561 | + { |
|
501 | 562 | $this->tracer->pop(true); |
502 | 563 | $this->tracer->pop(false); |
503 | 564 | } |
504 | - } else { |
|
565 | + } |
|
566 | + else |
|
567 | + { |
|
505 | 568 | // No constructor specified |
506 | 569 | $instance = $reflection->newInstance(); |
507 | 570 | } |
@@ -519,13 +582,15 @@ discard block |
||
519 | 582 | $instance = $this->runInflector($instance); |
520 | 583 | |
521 | 584 | //Declarative singletons |
522 | - if ($this->isSingleton($ctx)) { |
|
585 | + if ($this->isSingleton($ctx)) |
|
586 | + { |
|
523 | 587 | $this->state->singletons[$ctx->alias] = $instance; |
524 | 588 | } |
525 | 589 | |
526 | 590 | // Register finalizer |
527 | 591 | $finalizer = $this->getFinalizer($ctx, $instance); |
528 | - if ($finalizer !== null) { |
|
592 | + if ($finalizer !== null) |
|
593 | + { |
|
529 | 594 | $this->state->finalizers[] = $finalizer; |
530 | 595 | } |
531 | 596 | |
@@ -537,12 +602,14 @@ discard block |
||
537 | 602 | */ |
538 | 603 | private function isSingleton(Ctx $ctx): bool |
539 | 604 | { |
540 | - if ($ctx->singleton === true) { |
|
605 | + if ($ctx->singleton === true) |
|
606 | + { |
|
541 | 607 | return true; |
542 | 608 | } |
543 | 609 | |
544 | 610 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
545 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
611 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
612 | + { |
|
546 | 613 | return true; |
547 | 614 | } |
548 | 615 | |
@@ -556,7 +623,8 @@ discard block |
||
556 | 623 | * @var Attribute\Finalize|null $attribute |
557 | 624 | */ |
558 | 625 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
559 | - if ($attribute === null) { |
|
626 | + if ($attribute === null) |
|
627 | + { |
|
560 | 628 | return null; |
561 | 629 | } |
562 | 630 | |
@@ -570,10 +638,14 @@ discard block |
||
570 | 638 | { |
571 | 639 | $scope = $this->scope; |
572 | 640 | |
573 | - while ($scope !== null) { |
|
574 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
575 | - if ($instance instanceof $class) { |
|
576 | - foreach ($inflectors as $inflector) { |
|
641 | + while ($scope !== null) |
|
642 | + { |
|
643 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
644 | + { |
|
645 | + if ($instance instanceof $class) |
|
646 | + { |
|
647 | + foreach ($inflectors as $inflector) |
|
648 | + { |
|
577 | 649 | $instance = $inflector->getParametersCount() > 1 |
578 | 650 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
579 | 651 | : ($inflector->inflector)($instance); |
@@ -589,9 +661,12 @@ discard block |
||
589 | 661 | |
590 | 662 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
591 | 663 | { |
592 | - try { |
|
664 | + try |
|
665 | + { |
|
593 | 666 | $this->resolver->validateArguments($reflection, $arguments); |
594 | - } catch (\Throwable) { |
|
667 | + } |
|
668 | + catch (\Throwable) |
|
669 | + { |
|
595 | 670 | return false; |
596 | 671 | } |
597 | 672 |
@@ -48,11 +48,11 @@ discard block |
||
48 | 48 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
49 | 49 | |
50 | 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; |
|
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 | 56 | private Internal\Scope $scope; |
57 | 57 | |
58 | 58 | /** |
@@ -60,11 +60,11 @@ discard block |
||
60 | 60 | */ |
61 | 61 | public function __construct( |
62 | 62 | private Config $config = new Config(), |
63 | - string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
|
63 | + string | \BackedEnum | null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
|
64 | 64 | private Options $options = new Options(), |
65 | - ) { |
|
66 | - if (\is_object($scopeName)) { |
|
67 | - $scopeName = (string) $scopeName->value; |
|
65 | + ){ |
|
66 | + if (\is_object($scopeName)){ |
|
67 | + $scopeName = (string)$scopeName->value; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | $this->initServices($this, $scopeName); |
@@ -118,11 +118,11 @@ discard block |
||
118 | 118 | * @throws \Throwable |
119 | 119 | * @psalm-suppress TooManyArguments |
120 | 120 | */ |
121 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
121 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
122 | 122 | { |
123 | 123 | return ContainerScope::getContainer() === $this |
124 | 124 | ? $this->factory->make($alias, $parameters, $context) |
125 | - : ContainerScope::runScope($this, fn (): mixed => $this->factory->make($alias, $parameters, $context)); |
|
125 | + : ContainerScope::runScope($this, fn () : mixed => $this->factory->make($alias, $parameters, $context)); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | /** |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | * @throws \Throwable |
145 | 145 | * @psalm-suppress TooManyArguments |
146 | 146 | */ |
147 | - public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
147 | + public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed |
|
148 | 148 | { |
149 | 149 | return ContainerScope::getContainer() === $this |
150 | 150 | ? $this->container->get($id, $context) |
@@ -164,9 +164,9 @@ discard block |
||
164 | 164 | * If {@see string}, the default binder for the given scope will be returned. Default bindings won't affect |
165 | 165 | * already created Container instances except the case with the root one. |
166 | 166 | */ |
167 | - public function getBinder(string|\BackedEnum|null $scope = null): BinderInterface |
|
167 | + public function getBinder(string | \BackedEnum | null $scope = null): BinderInterface |
|
168 | 168 | { |
169 | - $scope = \is_object($scope) ? (string) $scope->value : $scope; |
|
169 | + $scope = \is_object($scope) ? (string)$scope->value : $scope; |
|
170 | 170 | |
171 | 171 | return $scope === null |
172 | 172 | ? $this->binder |
@@ -176,25 +176,25 @@ discard block |
||
176 | 176 | /** |
177 | 177 | * @throws \Throwable |
178 | 178 | */ |
179 | - public function runScope(Scope|array $bindings, callable $scope): mixed |
|
179 | + public function runScope(Scope | array $bindings, callable $scope): mixed |
|
180 | 180 | { |
181 | - if (!\is_array($bindings)) { |
|
181 | + if (!\is_array($bindings)){ |
|
182 | 182 | return $this->runIsolatedScope($bindings, $scope); |
183 | 183 | } |
184 | 184 | |
185 | 185 | $binds = &$this->state->bindings; |
186 | 186 | $singletons = &$this->state->singletons; |
187 | 187 | $cleanup = $previous = $prevSin = []; |
188 | - foreach ($bindings as $alias => $resolver) { |
|
188 | + foreach ($bindings as $alias => $resolver){ |
|
189 | 189 | // Store previous bindings |
190 | - if (isset($binds[$alias])) { |
|
190 | + if (isset($binds[$alias])){ |
|
191 | 191 | $previous[$alias] = $binds[$alias]; |
192 | - } else { |
|
192 | + }else{ |
|
193 | 193 | // Store bindings to be removed |
194 | 194 | $cleanup[] = $alias; |
195 | 195 | } |
196 | 196 | // Store previous singletons |
197 | - if (isset($singletons[$alias])) { |
|
197 | + if (isset($singletons[$alias])){ |
|
198 | 198 | $prevSin[$alias] = $singletons[$alias]; |
199 | 199 | unset($singletons[$alias]); |
200 | 200 | } |
@@ -202,21 +202,21 @@ discard block |
||
202 | 202 | $this->binder->bind($alias, $resolver); |
203 | 203 | } |
204 | 204 | |
205 | - try { |
|
205 | + try{ |
|
206 | 206 | return ContainerScope::getContainer() !== $this |
207 | 207 | ? ContainerScope::runScope($this, $scope) |
208 | 208 | : $scope($this); |
209 | - } finally { |
|
209 | + }finally{ |
|
210 | 210 | // Remove new bindings |
211 | - foreach ($cleanup as $alias) { |
|
211 | + foreach ($cleanup as $alias){ |
|
212 | 212 | unset($binds[$alias], $singletons[$alias]); |
213 | 213 | } |
214 | 214 | // Restore previous bindings |
215 | - foreach ($previous as $alias => $resolver) { |
|
215 | + foreach ($previous as $alias => $resolver){ |
|
216 | 216 | $binds[$alias] = $resolver; |
217 | 217 | } |
218 | 218 | // Restore singletons |
219 | - foreach ($prevSin as $alias => $instance) { |
|
219 | + foreach ($prevSin as $alias => $instance){ |
|
220 | 220 | $singletons[$alias] = $instance; |
221 | 221 | } |
222 | 222 | } |
@@ -264,9 +264,9 @@ discard block |
||
264 | 264 | * If the value is null, option {@see Options::$allowSingletonsRebinding} will be used. |
265 | 265 | * @throws SingletonOverloadException |
266 | 266 | */ |
267 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
|
267 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, ?bool $force = null): void |
|
268 | 268 | { |
269 | - if ($force ?? $this->options->allowSingletonsRebinding) { |
|
269 | + if ($force ?? $this->options->allowSingletonsRebinding){ |
|
270 | 270 | $this->binder->removeBinding($alias); |
271 | 271 | } |
272 | 272 | |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | { |
294 | 294 | return ContainerScope::getContainer() === $this |
295 | 295 | ? $this->invoker->invoke($target, $parameters) |
296 | - : ContainerScope::runScope($this, fn (): mixed => $this->invoker->invoke($target, $parameters)); |
|
296 | + : ContainerScope::runScope($this, fn () : mixed => $this->invoker->invoke($target, $parameters)); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | /** |
@@ -337,8 +337,8 @@ discard block |
||
337 | 337 | ], $this->options); |
338 | 338 | |
339 | 339 | // Create container services |
340 | - foreach ($container->config as $property => $class) { |
|
341 | - if (\property_exists($container, $property)) { |
|
340 | + foreach ($container->config as $property => $class){ |
|
341 | + if (\property_exists($container, $property)){ |
|
342 | 342 | $container->$property = $constructor->get($property, $class); |
343 | 343 | } |
344 | 344 | } |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | private function closeScope(): void |
353 | 353 | { |
354 | 354 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
355 | - if (!isset($this->scope)) { |
|
355 | + if (!isset($this->scope)){ |
|
356 | 356 | $this->destruct(); |
357 | 357 | return; |
358 | 358 | } |
@@ -361,10 +361,10 @@ discard block |
||
361 | 361 | |
362 | 362 | // Run finalizers |
363 | 363 | $errors = []; |
364 | - foreach ($this->state->finalizers as $finalizer) { |
|
365 | - try { |
|
364 | + foreach ($this->state->finalizers as $finalizer){ |
|
365 | + try{ |
|
366 | 366 | $this->invoker->invoke($finalizer); |
367 | - } catch (\Throwable $e) { |
|
367 | + }catch (\Throwable $e){ |
|
368 | 368 | $errors[] = $e; |
369 | 369 | } |
370 | 370 | } |
@@ -373,7 +373,7 @@ discard block |
||
373 | 373 | $this->destruct(); |
374 | 374 | |
375 | 375 | // Throw collected errors |
376 | - if ($errors !== []) { |
|
376 | + if ($errors !== []){ |
|
377 | 377 | throw new FinalizersException($scopeName, $errors); |
378 | 378 | } |
379 | 379 | } |
@@ -395,18 +395,18 @@ discard block |
||
395 | 395 | $container->scope->setParent($this, $this->scope, $this->factory); |
396 | 396 | |
397 | 397 | // Add specific bindings |
398 | - foreach ($config->bindings as $alias => $resolver) { |
|
398 | + foreach ($config->bindings as $alias => $resolver){ |
|
399 | 399 | $container->binder->bind($alias, $resolver); |
400 | 400 | } |
401 | 401 | |
402 | 402 | return ContainerScope::runScope( |
403 | 403 | $container, |
404 | 404 | static function (self $container) use ($config, $closure): mixed { |
405 | - try { |
|
405 | + try{ |
|
406 | 406 | return $config->autowire |
407 | 407 | ? $container->invoke($closure) |
408 | 408 | : $closure($container); |
409 | - } finally { |
|
409 | + }finally{ |
|
410 | 410 | $container->closeScope(); |
411 | 411 | } |
412 | 412 | } |
@@ -63,7 +63,8 @@ discard block |
||
63 | 63 | string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
64 | 64 | private Options $options = new Options(), |
65 | 65 | ) { |
66 | - if (\is_object($scopeName)) { |
|
66 | + if (\is_object($scopeName)) |
|
67 | + { |
|
67 | 68 | $scopeName = (string) $scopeName->value; |
68 | 69 | } |
69 | 70 | |
@@ -178,23 +179,29 @@ discard block |
||
178 | 179 | */ |
179 | 180 | public function runScope(Scope|array $bindings, callable $scope): mixed |
180 | 181 | { |
181 | - if (!\is_array($bindings)) { |
|
182 | + if (!\is_array($bindings)) |
|
183 | + { |
|
182 | 184 | return $this->runIsolatedScope($bindings, $scope); |
183 | 185 | } |
184 | 186 | |
185 | 187 | $binds = &$this->state->bindings; |
186 | 188 | $singletons = &$this->state->singletons; |
187 | 189 | $cleanup = $previous = $prevSin = []; |
188 | - foreach ($bindings as $alias => $resolver) { |
|
190 | + foreach ($bindings as $alias => $resolver) |
|
191 | + { |
|
189 | 192 | // Store previous bindings |
190 | - if (isset($binds[$alias])) { |
|
193 | + if (isset($binds[$alias])) |
|
194 | + { |
|
191 | 195 | $previous[$alias] = $binds[$alias]; |
192 | - } else { |
|
196 | + } |
|
197 | + else |
|
198 | + { |
|
193 | 199 | // Store bindings to be removed |
194 | 200 | $cleanup[] = $alias; |
195 | 201 | } |
196 | 202 | // Store previous singletons |
197 | - if (isset($singletons[$alias])) { |
|
203 | + if (isset($singletons[$alias])) |
|
204 | + { |
|
198 | 205 | $prevSin[$alias] = $singletons[$alias]; |
199 | 206 | unset($singletons[$alias]); |
200 | 207 | } |
@@ -202,21 +209,27 @@ discard block |
||
202 | 209 | $this->binder->bind($alias, $resolver); |
203 | 210 | } |
204 | 211 | |
205 | - try { |
|
212 | + try |
|
213 | + { |
|
206 | 214 | return ContainerScope::getContainer() !== $this |
207 | 215 | ? ContainerScope::runScope($this, $scope) |
208 | 216 | : $scope($this); |
209 | - } finally { |
|
217 | + } |
|
218 | + finally |
|
219 | + { |
|
210 | 220 | // Remove new bindings |
211 | - foreach ($cleanup as $alias) { |
|
221 | + foreach ($cleanup as $alias) |
|
222 | + { |
|
212 | 223 | unset($binds[$alias], $singletons[$alias]); |
213 | 224 | } |
214 | 225 | // Restore previous bindings |
215 | - foreach ($previous as $alias => $resolver) { |
|
226 | + foreach ($previous as $alias => $resolver) |
|
227 | + { |
|
216 | 228 | $binds[$alias] = $resolver; |
217 | 229 | } |
218 | 230 | // Restore singletons |
219 | - foreach ($prevSin as $alias => $instance) { |
|
231 | + foreach ($prevSin as $alias => $instance) |
|
232 | + { |
|
220 | 233 | $singletons[$alias] = $instance; |
221 | 234 | } |
222 | 235 | } |
@@ -266,7 +279,8 @@ discard block |
||
266 | 279 | */ |
267 | 280 | public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
268 | 281 | { |
269 | - if ($force ?? $this->options->allowSingletonsRebinding) { |
|
282 | + if ($force ?? $this->options->allowSingletonsRebinding) |
|
283 | + { |
|
270 | 284 | $this->binder->removeBinding($alias); |
271 | 285 | } |
272 | 286 | |
@@ -337,8 +351,10 @@ discard block |
||
337 | 351 | ], $this->options); |
338 | 352 | |
339 | 353 | // Create container services |
340 | - foreach ($container->config as $property => $class) { |
|
341 | - if (\property_exists($container, $property)) { |
|
354 | + foreach ($container->config as $property => $class) |
|
355 | + { |
|
356 | + if (\property_exists($container, $property)) |
|
357 | + { |
|
342 | 358 | $container->$property = $constructor->get($property, $class); |
343 | 359 | } |
344 | 360 | } |
@@ -352,7 +368,8 @@ discard block |
||
352 | 368 | private function closeScope(): void |
353 | 369 | { |
354 | 370 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
355 | - if (!isset($this->scope)) { |
|
371 | + if (!isset($this->scope)) |
|
372 | + { |
|
356 | 373 | $this->destruct(); |
357 | 374 | return; |
358 | 375 | } |
@@ -361,10 +378,14 @@ discard block |
||
361 | 378 | |
362 | 379 | // Run finalizers |
363 | 380 | $errors = []; |
364 | - foreach ($this->state->finalizers as $finalizer) { |
|
365 | - try { |
|
381 | + foreach ($this->state->finalizers as $finalizer) |
|
382 | + { |
|
383 | + try |
|
384 | + { |
|
366 | 385 | $this->invoker->invoke($finalizer); |
367 | - } catch (\Throwable $e) { |
|
386 | + } |
|
387 | + catch (\Throwable $e) |
|
388 | + { |
|
368 | 389 | $errors[] = $e; |
369 | 390 | } |
370 | 391 | } |
@@ -373,7 +394,8 @@ discard block |
||
373 | 394 | $this->destruct(); |
374 | 395 | |
375 | 396 | // Throw collected errors |
376 | - if ($errors !== []) { |
|
397 | + if ($errors !== []) |
|
398 | + { |
|
377 | 399 | throw new FinalizersException($scopeName, $errors); |
378 | 400 | } |
379 | 401 | } |
@@ -395,18 +417,22 @@ discard block |
||
395 | 417 | $container->scope->setParent($this, $this->scope, $this->factory); |
396 | 418 | |
397 | 419 | // Add specific bindings |
398 | - foreach ($config->bindings as $alias => $resolver) { |
|
420 | + foreach ($config->bindings as $alias => $resolver) |
|
421 | + { |
|
399 | 422 | $container->binder->bind($alias, $resolver); |
400 | 423 | } |
401 | 424 | |
402 | 425 | return ContainerScope::runScope( |
403 | 426 | $container, |
404 | 427 | static function (self $container) use ($config, $closure): mixed { |
405 | - try { |
|
428 | + try |
|
429 | + { |
|
406 | 430 | return $config->autowire |
407 | 431 | ? $container->invoke($closure) |
408 | 432 | : $closure($container); |
409 | - } finally { |
|
433 | + } |
|
434 | + finally |
|
435 | + { |
|
410 | 436 | $container->closeScope(); |
411 | 437 | } |
412 | 438 | } |