@@ -33,8 +33,10 @@ discard block |
||
33 | 33 | { |
34 | 34 | $root = new Container(); |
35 | 35 | |
36 | - $root->runScoped(static function (Container $c1) { |
|
37 | - $c1->runScoped(static function (Container $c2) use ($c1) { |
|
36 | + $root->runScoped(static function (Container $c1) |
|
37 | + { |
|
38 | + $c1->runScoped(static function (Container $c2) use ($c1) |
|
39 | + { |
|
38 | 40 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
39 | 41 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
40 | 42 | |
@@ -48,8 +50,10 @@ discard block |
||
48 | 50 | $root = new Container(); |
49 | 51 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
50 | 52 | |
51 | - $root->runScoped(static function (Container $fooScope) { |
|
52 | - $fooScope->runScoped(static function (Container $container) { |
|
53 | + $root->runScoped(static function (Container $fooScope) |
|
54 | + { |
|
55 | + $fooScope->runScoped(static function (Container $container) |
|
56 | + { |
|
53 | 57 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
54 | 58 | }, name: 'bar'); |
55 | 59 | }, name: 'foo'); |
@@ -63,8 +67,10 @@ discard block |
||
63 | 67 | $root = new Container(); |
64 | 68 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
65 | 69 | |
66 | - $root->runScoped(static function (Container $fooScope) { |
|
67 | - $fooScope->runScoped(static function (Container $container) { |
|
70 | + $root->runScoped(static function (Container $fooScope) |
|
71 | + { |
|
72 | + $fooScope->runScoped(static function (Container $container) |
|
73 | + { |
|
68 | 74 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
69 | 75 | }, name: 'bar'); |
70 | 76 | }, name: 'baz'); |
@@ -83,8 +89,10 @@ discard block |
||
83 | 89 | $root = new Container(); |
84 | 90 | $root->bind('foo', self::makeFooScopeObject(...)); |
85 | 91 | |
86 | - $root->runScoped(static function (Container $c1) { |
|
87 | - $c1->runScoped(static function (Container $c2) { |
|
92 | + $root->runScoped(static function (Container $c1) |
|
93 | + { |
|
94 | + $c1->runScoped(static function (Container $c2) |
|
95 | + { |
|
88 | 96 | $c2->get('foo'); |
89 | 97 | }, name: 'foo'); |
90 | 98 | }); |
@@ -102,8 +110,10 @@ discard block |
||
102 | 110 | $root = new Container(); |
103 | 111 | $root->bind('foo', self::makeFooScopeObject(...)); |
104 | 112 | |
105 | - $root->runScoped(static function (Container $c1) { |
|
106 | - $c1->runScoped(static function (Container $c2) { |
|
113 | + $root->runScoped(static function (Container $c1) |
|
114 | + { |
|
115 | + $c1->runScoped(static function (Container $c2) |
|
116 | + { |
|
107 | 117 | $c2->get('foo'); |
108 | 118 | }); |
109 | 119 | }); |
@@ -121,15 +131,21 @@ discard block |
||
121 | 131 | |
122 | 132 | $root = new Container(); |
123 | 133 | |
124 | - try { |
|
125 | - $root->runScoped(static function (Container $c1) { |
|
126 | - $c1->runScoped(static function (Container $c2) { |
|
127 | - $c2->runScoped(static function (Container $c3) { |
|
134 | + try |
|
135 | + { |
|
136 | + $root->runScoped(static function (Container $c1) |
|
137 | + { |
|
138 | + $c1->runScoped(static function (Container $c2) |
|
139 | + { |
|
140 | + $c2->runScoped(static function (Container $c3) |
|
141 | + { |
|
128 | 142 | // do nothing |
129 | 143 | }, name: 'root'); |
130 | 144 | }); |
131 | 145 | }); |
132 | - } catch (NamedScopeDuplicationException $e) { |
|
146 | + } |
|
147 | + catch (NamedScopeDuplicationException $e) |
|
148 | + { |
|
133 | 149 | self::assertSame('root', $e->getScope()); |
134 | 150 | throw $e; |
135 | 151 | } |
@@ -144,14 +160,19 @@ discard block |
||
144 | 160 | { |
145 | 161 | self::expectException(BadScopeException::class); |
146 | 162 | |
147 | - try { |
|
163 | + try |
|
164 | + { |
|
148 | 165 | $root = new Container(); |
149 | - $root->runScoped(static function (Container $c1) { |
|
150 | - $c1->runScoped(static function (Container $c2) { |
|
166 | + $root->runScoped(static function (Container $c1) |
|
167 | + { |
|
168 | + $c1->runScoped(static function (Container $c2) |
|
169 | + { |
|
151 | 170 | $c2->get(AttrScopeFoo::class); |
152 | 171 | }); |
153 | 172 | }, name: 'bar'); |
154 | - } catch (BadScopeException $e) { |
|
173 | + } |
|
174 | + catch (BadScopeException $e) |
|
175 | + { |
|
155 | 176 | self::assertSame('foo', $e->getScope()); |
156 | 177 | throw $e; |
157 | 178 | } |
@@ -65,17 +65,20 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
67 | 67 | { |
68 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
68 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
69 | + { |
|
69 | 70 | return $this->state->singletons[$alias]; |
70 | 71 | } |
71 | 72 | |
72 | 73 | $binding = $this->state->bindings[$alias] ?? null; |
73 | 74 | |
74 | - if ($binding === null) { |
|
75 | + if ($binding === null) |
|
76 | + { |
|
75 | 77 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
76 | 78 | } |
77 | 79 | |
78 | - try { |
|
80 | + try |
|
81 | + { |
|
79 | 82 | $this->tracer->push( |
80 | 83 | false, |
81 | 84 | action: 'resolve from binding', |
@@ -105,7 +108,9 @@ discard block |
||
105 | 108 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
106 | 109 | default => $binding, |
107 | 110 | }; |
108 | - } finally { |
|
111 | + } |
|
112 | + finally |
|
113 | + { |
|
109 | 114 | $this->state->bindings[$alias] ??= $binding; |
110 | 115 | $this->tracer->pop(true); |
111 | 116 | $this->tracer->pop(false); |
@@ -119,18 +124,23 @@ discard block |
||
119 | 124 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
120 | 125 | { |
121 | 126 | $context = $ctx->context; |
122 | - try { |
|
127 | + try |
|
128 | + { |
|
123 | 129 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
124 | - } catch (\ReflectionException $e) { |
|
130 | + } |
|
131 | + catch (\ReflectionException $e) |
|
132 | + { |
|
125 | 133 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
126 | 134 | } |
127 | 135 | |
128 | 136 | $injector = $binding->injector; |
129 | 137 | |
130 | - try { |
|
138 | + try |
|
139 | + { |
|
131 | 140 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
132 | 141 | |
133 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
142 | + if (!$injectorInstance instanceof InjectorInterface) |
|
143 | + { |
|
134 | 144 | throw new InjectionException( |
135 | 145 | \sprintf( |
136 | 146 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -157,7 +167,8 @@ discard block |
||
157 | 167 | default => (string)$context, |
158 | 168 | }); |
159 | 169 | |
160 | - if (!$reflection->isInstance($instance)) { |
|
170 | + if (!$reflection->isInstance($instance)) |
|
171 | + { |
|
161 | 172 | throw new InjectionException( |
162 | 173 | \sprintf( |
163 | 174 | "Invalid injection response for '%s'.", |
@@ -167,7 +178,9 @@ discard block |
||
167 | 178 | } |
168 | 179 | |
169 | 180 | return $instance; |
170 | - } finally { |
|
181 | + } |
|
182 | + finally |
|
183 | + { |
|
171 | 184 | $this->state->bindings[$ctx->class] ??= $binding; |
172 | 185 | } |
173 | 186 | } |
@@ -186,7 +199,8 @@ discard block |
||
186 | 199 | //Binding is pointing to something else |
187 | 200 | : $this->make($binding->alias, $arguments, $context); |
188 | 201 | |
189 | - if ($binding->singleton && $arguments === []) { |
|
202 | + if ($binding->singleton && $arguments === []) |
|
203 | + { |
|
190 | 204 | $this->state->singletons[$alias] = $result; |
191 | 205 | } |
192 | 206 | |
@@ -197,7 +211,8 @@ discard block |
||
197 | 211 | { |
198 | 212 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
199 | 213 | |
200 | - if ($binding->singleton) { |
|
214 | + if ($binding->singleton) |
|
215 | + { |
|
201 | 216 | $this->state->singletons[$alias] = $result; |
202 | 217 | } |
203 | 218 | |
@@ -238,11 +253,14 @@ discard block |
||
238 | 253 | array $arguments, |
239 | 254 | ): mixed { |
240 | 255 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
241 | - try { |
|
256 | + try |
|
257 | + { |
|
242 | 258 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
243 | 259 | ? ($binding->factory)() |
244 | 260 | : $this->invoker->invoke($binding->factory, $arguments); |
245 | - } catch (NotCallableException $e) { |
|
261 | + } |
|
262 | + catch (NotCallableException $e) |
|
263 | + { |
|
246 | 264 | throw new ContainerException( |
247 | 265 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
248 | 266 | $e->getCode(), |
@@ -261,19 +279,24 @@ discard block |
||
261 | 279 | ): ?object { |
262 | 280 | $avoidCache = $arguments !== []; |
263 | 281 | |
264 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
265 | - try { |
|
282 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
283 | + { |
|
284 | + try |
|
285 | + { |
|
266 | 286 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
267 | 287 | |
268 | 288 | $object = $this->createInstance( |
269 | 289 | new Ctx(alias: $alias, class: $alias, context: $context), |
270 | 290 | $arguments, |
271 | 291 | ); |
272 | - if ($avoidCache) { |
|
292 | + if ($avoidCache) |
|
293 | + { |
|
273 | 294 | return $object; |
274 | 295 | } |
275 | 296 | $binding->reference = WeakReference::create($object); |
276 | - } catch (\Throwable) { |
|
297 | + } |
|
298 | + catch (\Throwable) |
|
299 | + { |
|
277 | 300 | throw new ContainerException( |
278 | 301 | $this->tracer->combineTraceMessage( |
279 | 302 | \sprintf( |
@@ -283,7 +306,9 @@ discard block |
||
283 | 306 | ) |
284 | 307 | ) |
285 | 308 | ); |
286 | - } finally { |
|
309 | + } |
|
310 | + finally |
|
311 | + { |
|
287 | 312 | $this->tracer->pop(); |
288 | 313 | } |
289 | 314 | } |
@@ -298,19 +323,26 @@ discard block |
||
298 | 323 | ): mixed { |
299 | 324 | $parent = $this->scope->getParentFactory(); |
300 | 325 | |
301 | - if ($parent !== null) { |
|
302 | - try { |
|
326 | + if ($parent !== null) |
|
327 | + { |
|
328 | + try |
|
329 | + { |
|
303 | 330 | $this->tracer->push(false, ...[ |
304 | 331 | 'current scope' => $this->scope->getScopeName(), |
305 | 332 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
306 | 333 | ]); |
307 | 334 | /** @psalm-suppress TooManyArguments */ |
308 | 335 | return $parent->make($alias, $parameters, $context); |
309 | - } catch (BadScopeException $e) { |
|
310 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
336 | + } |
|
337 | + catch (BadScopeException $e) |
|
338 | + { |
|
339 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
340 | + { |
|
311 | 341 | throw $e; |
312 | 342 | } |
313 | - } catch (ContainerExceptionInterface $e) { |
|
343 | + } |
|
344 | + catch (ContainerExceptionInterface $e) |
|
345 | + { |
|
314 | 346 | $className = match (true) { |
315 | 347 | $e instanceof NotFoundException => NotFoundException::class, |
316 | 348 | default => ContainerException::class, |
@@ -319,19 +351,24 @@ discard block |
||
319 | 351 | 'Can\'t resolve `%s`.', |
320 | 352 | $alias, |
321 | 353 | )), previous: $e); |
322 | - } finally { |
|
354 | + } |
|
355 | + finally |
|
356 | + { |
|
323 | 357 | $this->tracer->pop(false); |
324 | 358 | } |
325 | 359 | } |
326 | 360 | |
327 | 361 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
328 | - try { |
|
362 | + try |
|
363 | + { |
|
329 | 364 | //No direct instructions how to construct class, make is automatically |
330 | 365 | return $this->autowire( |
331 | 366 | new Ctx(alias: $alias, class: $alias, context: $context), |
332 | 367 | $parameters, |
333 | 368 | ); |
334 | - } finally { |
|
369 | + } |
|
370 | + finally |
|
371 | + { |
|
335 | 372 | $this->tracer->pop(false); |
336 | 373 | } |
337 | 374 | } |
@@ -381,9 +418,11 @@ discard block |
||
381 | 418 | // Check scope name |
382 | 419 | $ctx->reflection = new \ReflectionClass($instance); |
383 | 420 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
384 | - if ($scopeName !== null) { |
|
421 | + if ($scopeName !== null) |
|
422 | + { |
|
385 | 423 | $scope = $this->scope; |
386 | - while ($scope->getScopeName() !== $scopeName) { |
|
424 | + while ($scope->getScopeName() !== $scopeName) |
|
425 | + { |
|
387 | 426 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
388 | 427 | } |
389 | 428 | } |
@@ -411,24 +450,30 @@ discard block |
||
411 | 450 | array $arguments, |
412 | 451 | ): object { |
413 | 452 | $class = $ctx->class; |
414 | - try { |
|
453 | + try |
|
454 | + { |
|
415 | 455 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
416 | - } catch (\ReflectionException $e) { |
|
456 | + } |
|
457 | + catch (\ReflectionException $e) |
|
458 | + { |
|
417 | 459 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
418 | 460 | } |
419 | 461 | |
420 | 462 | // Check scope name |
421 | 463 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
422 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
464 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
465 | + { |
|
423 | 466 | throw new BadScopeException($scope, $class); |
424 | 467 | } |
425 | 468 | |
426 | 469 | // We have to construct class using external injector when we know the exact context |
427 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
470 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
471 | + { |
|
428 | 472 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
429 | 473 | } |
430 | 474 | |
431 | - if (!$reflection->isInstantiable()) { |
|
475 | + if (!$reflection->isInstantiable()) |
|
476 | + { |
|
432 | 477 | $itIs = match (true) { |
433 | 478 | $reflection->isEnum() => 'Enum', |
434 | 479 | $reflection->isAbstract() => 'Abstract class', |
@@ -441,12 +486,16 @@ discard block |
||
441 | 486 | |
442 | 487 | $constructor = $reflection->getConstructor(); |
443 | 488 | |
444 | - if ($constructor !== null) { |
|
445 | - try { |
|
489 | + if ($constructor !== null) |
|
490 | + { |
|
491 | + try |
|
492 | + { |
|
446 | 493 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
447 | 494 | $this->tracer->push(true); |
448 | 495 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
449 | - } catch (ValidationException $e) { |
|
496 | + } |
|
497 | + catch (ValidationException $e) |
|
498 | + { |
|
450 | 499 | throw new ContainerException( |
451 | 500 | $this->tracer->combineTraceMessage( |
452 | 501 | \sprintf( |
@@ -456,22 +505,31 @@ discard block |
||
456 | 505 | ) |
457 | 506 | ), |
458 | 507 | ); |
459 | - } finally { |
|
508 | + } |
|
509 | + finally |
|
510 | + { |
|
460 | 511 | $this->tracer->pop(true); |
461 | 512 | $this->tracer->pop(false); |
462 | 513 | } |
463 | - try { |
|
514 | + try |
|
515 | + { |
|
464 | 516 | // Using constructor with resolved arguments |
465 | 517 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
466 | 518 | $this->tracer->push(true); |
467 | 519 | $instance = new $class(...$args); |
468 | - } catch (\TypeError $e) { |
|
520 | + } |
|
521 | + catch (\TypeError $e) |
|
522 | + { |
|
469 | 523 | throw new WrongTypeException($constructor, $e); |
470 | - } finally { |
|
524 | + } |
|
525 | + finally |
|
526 | + { |
|
471 | 527 | $this->tracer->pop(true); |
472 | 528 | $this->tracer->pop(false); |
473 | 529 | } |
474 | - } else { |
|
530 | + } |
|
531 | + else |
|
532 | + { |
|
475 | 533 | // No constructor specified |
476 | 534 | $instance = $reflection->newInstance(); |
477 | 535 | } |
@@ -489,13 +547,15 @@ discard block |
||
489 | 547 | $instance = $this->runInflector($instance); |
490 | 548 | |
491 | 549 | //Declarative singletons |
492 | - if ($this->isSingleton($ctx)) { |
|
550 | + if ($this->isSingleton($ctx)) |
|
551 | + { |
|
493 | 552 | $this->state->singletons[$ctx->alias] = $instance; |
494 | 553 | } |
495 | 554 | |
496 | 555 | // Register finalizer |
497 | 556 | $finalizer = $this->getFinalizer($ctx, $instance); |
498 | - if ($finalizer !== null) { |
|
557 | + if ($finalizer !== null) |
|
558 | + { |
|
499 | 559 | $this->state->finalizers[] = $finalizer; |
500 | 560 | } |
501 | 561 | |
@@ -507,12 +567,14 @@ discard block |
||
507 | 567 | */ |
508 | 568 | private function isSingleton(Ctx $ctx): bool |
509 | 569 | { |
510 | - if ($ctx->singleton === true) { |
|
570 | + if ($ctx->singleton === true) |
|
571 | + { |
|
511 | 572 | return true; |
512 | 573 | } |
513 | 574 | |
514 | 575 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
515 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
576 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
577 | + { |
|
516 | 578 | return true; |
517 | 579 | } |
518 | 580 | |
@@ -526,7 +588,8 @@ discard block |
||
526 | 588 | * @var Attribute\Finalize|null $attribute |
527 | 589 | */ |
528 | 590 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
529 | - if ($attribute === null) { |
|
591 | + if ($attribute === null) |
|
592 | + { |
|
530 | 593 | return null; |
531 | 594 | } |
532 | 595 | |
@@ -540,10 +603,14 @@ discard block |
||
540 | 603 | { |
541 | 604 | $scope = $this->scope; |
542 | 605 | |
543 | - while ($scope !== null) { |
|
544 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
545 | - if ($instance instanceof $class) { |
|
546 | - foreach ($inflectors as $inflector) { |
|
606 | + while ($scope !== null) |
|
607 | + { |
|
608 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
609 | + { |
|
610 | + if ($instance instanceof $class) |
|
611 | + { |
|
612 | + foreach ($inflectors as $inflector) |
|
613 | + { |
|
547 | 614 | $instance = $inflector->getParametersCount() > 1 |
548 | 615 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
549 | 616 | : ($inflector->inflector)($instance); |
@@ -559,9 +626,12 @@ discard block |
||
559 | 626 | |
560 | 627 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
561 | 628 | { |
562 | - try { |
|
629 | + try |
|
630 | + { |
|
563 | 631 | $this->resolver->validateArguments($reflection, $arguments); |
564 | - } catch (\Throwable) { |
|
632 | + } |
|
633 | + catch (\Throwable) |
|
634 | + { |
|
565 | 635 | return false; |
566 | 636 | } |
567 | 637 |