@@ -208,10 +208,13 @@ discard block |
||
208 | 208 | context: Parameter #1 [ <required> Spiral\Tests\Core\Fixtures\InvalidClass $class ] |
209 | 209 | MARKDOWN; |
210 | 210 | |
211 | - try { |
|
211 | + try |
|
212 | + { |
|
212 | 213 | $container->get(InvalidWithContainerInside::class); |
213 | 214 | self::fail('Exception `ContainerException` expected'); |
214 | - } catch (ContainerException $e) { |
|
215 | + } |
|
216 | + catch (ContainerException $e) |
|
217 | + { |
|
215 | 218 | self::assertSame($expectedMessage, $e->getMessage()); |
216 | 219 | } |
217 | 220 | } |
@@ -234,9 +237,12 @@ discard block |
||
234 | 237 | { |
235 | 238 | $container = new Container(); |
236 | 239 | |
237 | - try { |
|
240 | + try |
|
241 | + { |
|
238 | 242 | $container->get('invalid'); |
239 | - } catch (ContainerException $e) { |
|
243 | + } |
|
244 | + catch (ContainerException $e) |
|
245 | + { |
|
240 | 246 | self::assertSame(<<<MARKDOWN |
241 | 247 | Can't autowire `invalid`: class or injector not found. |
242 | 248 | Resolving trace: |
@@ -267,9 +273,12 @@ discard block |
||
267 | 273 | { |
268 | 274 | $this->expectException(ContainerException::class); |
269 | 275 | |
270 | - try { |
|
276 | + try |
|
277 | + { |
|
271 | 278 | $container->get(ClassWithUndefinedDependency::class); |
272 | - } catch (ContainerException $e) { |
|
279 | + } |
|
280 | + catch (ContainerException $e) |
|
281 | + { |
|
273 | 282 | self::assertSame($message, $e->getMessage()); |
274 | 283 | |
275 | 284 | throw $e; |
@@ -166,7 +166,8 @@ discard block |
||
166 | 166 | |
167 | 167 | $root = self::makeContainer(); |
168 | 168 | |
169 | - try { |
|
169 | + try |
|
170 | + { |
|
170 | 171 | $root->runScoped(static function (Container $c1): void { |
171 | 172 | $c1->runScoped(static function (Container $c2): void { |
172 | 173 | $c2->runScoped(static function (Container $c3): void { |
@@ -174,7 +175,9 @@ discard block |
||
174 | 175 | }, name: 'root'); |
175 | 176 | }); |
176 | 177 | }); |
177 | - } catch (NamedScopeDuplicationException $e) { |
|
178 | + } |
|
179 | + catch (NamedScopeDuplicationException $e) |
|
180 | + { |
|
178 | 181 | self::assertSame('root', $e->getScope()); |
179 | 182 | throw $e; |
180 | 183 | } |
@@ -187,7 +190,8 @@ discard block |
||
187 | 190 | #[Group('scrutinizer-ignore')] |
188 | 191 | public function testBadScopeException(): void |
189 | 192 | { |
190 | - try { |
|
193 | + try |
|
194 | + { |
|
191 | 195 | $root = self::makeContainer(); |
192 | 196 | $root->runScoped(static function (Container $c1): void { |
193 | 197 | $c1->runScoped(static function (Container $c2): void { |
@@ -196,7 +200,9 @@ discard block |
||
196 | 200 | }, name: 'bar'); |
197 | 201 | |
198 | 202 | self::fail(BadScopeException::class . ' should be thrown'); |
199 | - } catch (BadScopeException $e) { |
|
203 | + } |
|
204 | + catch (BadScopeException $e) |
|
205 | + { |
|
200 | 206 | self::assertSame('foo', $e->getScope()); |
201 | 207 | } |
202 | 208 | } |
@@ -6,7 +6,9 @@ |
||
6 | 6 | |
7 | 7 | final class PrivateConstructor |
8 | 8 | { |
9 | - private function __construct() {} |
|
9 | + private function __construct() |
|
10 | + { |
|
11 | +} |
|
10 | 12 | |
11 | 13 | private static function privateMethod(int $result): int |
12 | 14 | { |
@@ -40,20 +40,23 @@ discard block |
||
40 | 40 | \Stringable|string|null $context = null, |
41 | 41 | ?Tracer $tracer = null, |
42 | 42 | ): mixed { |
43 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
43 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
44 | + { |
|
44 | 45 | return $this->state->singletons[$alias]; |
45 | 46 | } |
46 | 47 | |
47 | 48 | |
48 | 49 | $this->actor->resolveType($alias, $binding, $singleton, $injector, $actor, false); |
49 | - if ($parameters === [] && $singleton !== null) { |
|
50 | + if ($parameters === [] && $singleton !== null) |
|
51 | + { |
|
50 | 52 | return $singleton; |
51 | 53 | } |
52 | 54 | |
53 | 55 | $tracer ??= new Tracer(); |
54 | 56 | |
55 | 57 | // Resolve without binding |
56 | - if ($binding === null) { |
|
58 | + if ($binding === null) |
|
59 | + { |
|
57 | 60 | $tracer->push( |
58 | 61 | false, |
59 | 62 | action: 'autowire', |
@@ -61,7 +64,8 @@ discard block |
||
61 | 64 | scope: $this->scope->getScopeName(), |
62 | 65 | context: $context, |
63 | 66 | ); |
64 | - try { |
|
67 | + try |
|
68 | + { |
|
65 | 69 | // No direct instructions how to construct class, make is automatically |
66 | 70 | return $this->actor->autowire( |
67 | 71 | new Ctx(alias: $alias, class: $alias, context: $context, singleton: $parameters === [] ? null : false), |
@@ -69,12 +73,15 @@ discard block |
||
69 | 73 | $actor, |
70 | 74 | $tracer, |
71 | 75 | ); |
72 | - } finally { |
|
76 | + } |
|
77 | + finally |
|
78 | + { |
|
73 | 79 | $tracer->pop(false); |
74 | 80 | } |
75 | 81 | } |
76 | 82 | |
77 | - try { |
|
83 | + try |
|
84 | + { |
|
78 | 85 | $tracer->push( |
79 | 86 | false, |
80 | 87 | action: 'resolve from binding', |
@@ -87,7 +94,9 @@ discard block |
||
87 | 94 | |
88 | 95 | // unset($this->state->bindings[$alias]); |
89 | 96 | return $actor->resolveBinding($binding, $alias, $context, $parameters, $tracer); |
90 | - } finally { |
|
97 | + } |
|
98 | + finally |
|
99 | + { |
|
91 | 100 | // $this->state->bindings[$alias] ??= $binding; |
92 | 101 | $tracer->pop(true); |
93 | 102 | $tracer->pop(false); |
@@ -17,7 +17,8 @@ discard block |
||
17 | 17 | |
18 | 18 | public function __construct( |
19 | 19 | private readonly ?string $scopeName = null, |
20 | - ) {} |
|
20 | + ) { |
|
21 | +} |
|
21 | 22 | |
22 | 23 | public function getScopeName(): ?string |
23 | 24 | { |
@@ -39,9 +40,11 @@ discard block |
||
39 | 40 | $this->parentActor = $actor; |
40 | 41 | |
41 | 42 | // Check a scope with the same name is not already registered |
42 | - if ($this->scopeName !== null) { |
|
43 | + if ($this->scopeName !== null) |
|
44 | + { |
|
43 | 45 | $tmp = $this; |
44 | - while ($tmp->parentScope !== null) { |
|
46 | + while ($tmp->parentScope !== null) |
|
47 | + { |
|
45 | 48 | $tmp = $tmp->parentScope; |
46 | 49 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
47 | 50 | } |
@@ -64,7 +67,8 @@ discard block |
||
64 | 67 | $result = [$this->scopeName]; |
65 | 68 | |
66 | 69 | $parent = $this; |
67 | - while ($parent->parentScope !== null) { |
|
70 | + while ($parent->parentScope !== null) |
|
71 | + { |
|
68 | 72 | $parent = $parent->parentScope; |
69 | 73 | $result[] = $parent->scopeName; |
70 | 74 | } |
@@ -42,26 +42,34 @@ discard block |
||
42 | 42 | */ |
43 | 43 | public function invoke(mixed $target, array $parameters = []): mixed |
44 | 44 | { |
45 | - if (\is_array($target) && isset($target[1])) { |
|
45 | + if (\is_array($target) && isset($target[1])) |
|
46 | + { |
|
46 | 47 | // In a form of resolver and method |
47 | 48 | [$resolver, $method] = $target; |
48 | 49 | |
49 | 50 | // Resolver instance or class name if the method is static (i.e. [ClassName::class, 'method']) |
50 | - if (\is_string($resolver)) { |
|
51 | + if (\is_string($resolver)) |
|
52 | + { |
|
51 | 53 | // Detect return type |
52 | 54 | $type = $this->actor->resolveType($resolver, $binding, $singleton, $injector); |
53 | 55 | |
54 | - if ($singleton === null) { |
|
56 | + if ($singleton === null) |
|
57 | + { |
|
55 | 58 | $type ??= $injector === null && $binding === null ? $resolver : null; |
56 | 59 | $resolver = \is_callable([$type, $method]) ? $type : $this->container->get($resolver); |
57 | - } else { |
|
60 | + } |
|
61 | + else |
|
62 | + { |
|
58 | 63 | $resolver = $singleton; |
59 | 64 | } |
60 | 65 | } |
61 | 66 | |
62 | - try { |
|
67 | + try |
|
68 | + { |
|
63 | 69 | $method = new \ReflectionMethod($resolver, $method); |
64 | - } catch (\ReflectionException $e) { |
|
70 | + } |
|
71 | + catch (\ReflectionException $e) |
|
72 | + { |
|
65 | 73 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
66 | 74 | } |
67 | 75 | |
@@ -72,14 +80,19 @@ discard block |
||
72 | 80 | ); |
73 | 81 | } |
74 | 82 | |
75 | - if (\is_string($target) && \is_callable($target)) { |
|
83 | + if (\is_string($target) && \is_callable($target)) |
|
84 | + { |
|
76 | 85 | $target = $target(...); |
77 | 86 | } |
78 | 87 | |
79 | - if ($target instanceof \Closure) { |
|
80 | - try { |
|
88 | + if ($target instanceof \Closure) |
|
89 | + { |
|
90 | + try |
|
91 | + { |
|
81 | 92 | $reflection = new \ReflectionFunction($target); |
82 | - } catch (\ReflectionException $e) { |
|
93 | + } |
|
94 | + catch (\ReflectionException $e) |
|
95 | + { |
|
83 | 96 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
84 | 97 | } |
85 | 98 |
@@ -85,21 +85,26 @@ discard block |
||
85 | 85 | // Aliases to prevent circular dependencies |
86 | 86 | $as = []; |
87 | 87 | $actor = $this; |
88 | - do { |
|
88 | + do |
|
89 | + { |
|
89 | 90 | $bindings = &$actor->state->bindings; |
90 | 91 | $singletons = &$actor->state->singletons; |
91 | 92 | $injectors = &$actor->state->injectors; |
92 | - if (\array_key_exists($alias, $singletons)) { |
|
93 | + if (\array_key_exists($alias, $singletons)) |
|
94 | + { |
|
93 | 95 | $singleton = $singletons[$alias]; |
94 | 96 | $binding = $bindings[$alias] ?? null; |
95 | 97 | $injector = $injectors[$alias] ?? null; |
96 | 98 | return \is_object($singleton::class) ? $singleton::class : null; |
97 | 99 | } |
98 | 100 | |
99 | - if (\array_key_exists($alias, $bindings)) { |
|
101 | + if (\array_key_exists($alias, $bindings)) |
|
102 | + { |
|
100 | 103 | $binding = $bindings[$alias]; |
101 | - if ($followAlias && $binding::class === Alias::class) { |
|
102 | - if ($binding->alias === $alias) { |
|
104 | + if ($followAlias && $binding::class === Alias::class) |
|
105 | + { |
|
106 | + if ($binding->alias === $alias) |
|
107 | + { |
|
103 | 108 | break; |
104 | 109 | } |
105 | 110 | |
@@ -114,7 +119,8 @@ discard block |
||
114 | 119 | return $binding->getReturnClass(); |
115 | 120 | } |
116 | 121 | |
117 | - if (\array_key_exists($alias, $injectors)) { |
|
122 | + if (\array_key_exists($alias, $injectors)) |
|
123 | + { |
|
118 | 124 | $injector = $injectors[$alias]; |
119 | 125 | $binding = $bindings[$alias] ?? null; |
120 | 126 | return $alias; |
@@ -122,7 +128,8 @@ discard block |
||
122 | 128 | |
123 | 129 | // Go to parent scope |
124 | 130 | $parent = $actor->scope->getParentActor(); |
125 | - if ($parent === null) { |
|
131 | + if ($parent === null) |
|
132 | + { |
|
126 | 133 | break; |
127 | 134 | } |
128 | 135 | |
@@ -192,18 +199,23 @@ discard block |
||
192 | 199 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments, Tracer $tracer) |
193 | 200 | { |
194 | 201 | $context = $ctx->context; |
195 | - try { |
|
202 | + try |
|
203 | + { |
|
196 | 204 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
197 | - } catch (\ReflectionException $e) { |
|
205 | + } |
|
206 | + catch (\ReflectionException $e) |
|
207 | + { |
|
198 | 208 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
199 | 209 | } |
200 | 210 | |
201 | 211 | $injector = $binding->injector; |
202 | 212 | |
203 | - try { |
|
213 | + try |
|
214 | + { |
|
204 | 215 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
205 | 216 | |
206 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
217 | + if (!$injectorInstance instanceof InjectorInterface) |
|
218 | + { |
|
207 | 219 | throw new InjectionException( |
208 | 220 | \sprintf( |
209 | 221 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -230,7 +242,8 @@ discard block |
||
230 | 242 | default => (string) $context, |
231 | 243 | }); |
232 | 244 | |
233 | - if (!$reflection->isInstance($instance)) { |
|
245 | + if (!$reflection->isInstance($instance)) |
|
246 | + { |
|
234 | 247 | throw new InjectionException( |
235 | 248 | \sprintf( |
236 | 249 | "Invalid injection response for '%s'.", |
@@ -240,12 +253,16 @@ discard block |
||
240 | 253 | } |
241 | 254 | |
242 | 255 | return $instance; |
243 | - } catch (TracedContainerException $e) { |
|
256 | + } |
|
257 | + catch (TracedContainerException $e) |
|
258 | + { |
|
244 | 259 | throw isset($injectorInstance) ? $e : $e::extendTracedException(\sprintf( |
245 | 260 | 'Can\'t resolve `%s`.', |
246 | 261 | $tracer->getRootAlias(), |
247 | 262 | ), $tracer->getTraces(), $e); |
248 | - } finally { |
|
263 | + } |
|
264 | + finally |
|
265 | + { |
|
249 | 266 | $this->state->bindings[$ctx->class] ??= $binding; |
250 | 267 | } |
251 | 268 | } |
@@ -257,18 +274,24 @@ discard block |
||
257 | 274 | array $arguments, |
258 | 275 | Tracer $tracer, |
259 | 276 | ): mixed { |
260 | - if ($binding->alias === $alias) { |
|
277 | + if ($binding->alias === $alias) |
|
278 | + { |
|
261 | 279 | $instance = $this->autowire( |
262 | 280 | new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton && $arguments === []), |
263 | 281 | $arguments, |
264 | 282 | $this, |
265 | 283 | $tracer, |
266 | 284 | ); |
267 | - } else { |
|
268 | - try { |
|
285 | + } |
|
286 | + else |
|
287 | + { |
|
288 | + try |
|
289 | + { |
|
269 | 290 | //Binding is pointing to something else |
270 | 291 | $instance = $this->factory->make($binding->alias, $arguments, $context); |
271 | - } catch (TracedContainerException $e) { |
|
292 | + } |
|
293 | + catch (TracedContainerException $e) |
|
294 | + { |
|
272 | 295 | throw $e::extendTracedException( |
273 | 296 | $alias === $tracer->getRootAlias() |
274 | 297 | ? "Can't resolve `{$alias}`." |
@@ -287,7 +310,8 @@ discard block |
||
287 | 310 | |
288 | 311 | private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
289 | 312 | { |
290 | - if ($context instanceof RetryContext) { |
|
313 | + if ($context instanceof RetryContext) |
|
314 | + { |
|
291 | 315 | return $binding->fallbackFactory === null |
292 | 316 | ? throw new RecursiveProxyException( |
293 | 317 | $alias, |
@@ -298,7 +322,8 @@ discard block |
||
298 | 322 | |
299 | 323 | $result = Proxy::create(new \ReflectionClass($binding->getReturnClass()), $context, new Attribute\Proxy()); |
300 | 324 | |
301 | - if ($binding->singleton) { |
|
325 | + if ($binding->singleton) |
|
326 | + { |
|
302 | 327 | $this->state->singletons[$alias] = $result; |
303 | 328 | } |
304 | 329 | |
@@ -312,7 +337,8 @@ discard block |
||
312 | 337 | array $arguments, |
313 | 338 | Tracer $tracer, |
314 | 339 | ): object { |
315 | - if ($arguments !== []) { |
|
340 | + if ($arguments !== []) |
|
341 | + { |
|
316 | 342 | // Avoid singleton cache |
317 | 343 | return $this->createInstance( |
318 | 344 | new Ctx(alias: $alias, class: $binding->value::class, context: $context, singleton: false), |
@@ -322,7 +348,8 @@ discard block |
||
322 | 348 | ); |
323 | 349 | } |
324 | 350 | |
325 | - if ($binding->singleton) { |
|
351 | + if ($binding->singleton) |
|
352 | + { |
|
326 | 353 | $this->state->singletons[$alias] = $binding->value; |
327 | 354 | } |
328 | 355 | |
@@ -339,9 +366,12 @@ discard block |
||
339 | 366 | $target = $binding->autowire->alias; |
340 | 367 | $ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
341 | 368 | |
342 | - if ($alias === $target) { |
|
369 | + if ($alias === $target) |
|
370 | + { |
|
343 | 371 | $instance = $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments), $this, $tracer); |
344 | - } else { |
|
372 | + } |
|
373 | + else |
|
374 | + { |
|
345 | 375 | $instance = $binding->autowire->resolve($this->factory, $arguments); |
346 | 376 | $this->validateConstraint($instance, $ctx); |
347 | 377 | } |
@@ -357,17 +387,22 @@ discard block |
||
357 | 387 | Tracer $tracer, |
358 | 388 | ): mixed { |
359 | 389 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
360 | - try { |
|
390 | + try |
|
391 | + { |
|
361 | 392 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
362 | 393 | ? ($binding->factory)() |
363 | 394 | : $this->invoker->invoke($binding->factory, $arguments); |
364 | - } catch (NotCallableException $e) { |
|
395 | + } |
|
396 | + catch (NotCallableException $e) |
|
397 | + { |
|
365 | 398 | throw TracedContainerException::createWithTrace( |
366 | 399 | \sprintf('Invalid callable binding for `%s`.', $ctx->alias), |
367 | 400 | $tracer->getTraces(), |
368 | 401 | $e, |
369 | 402 | ); |
370 | - } catch (TracedContainerException $e) { |
|
403 | + } |
|
404 | + catch (TracedContainerException $e) |
|
405 | + { |
|
371 | 406 | throw $e::extendTracedException( |
372 | 407 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
373 | 408 | $tracer->getTraces(), |
@@ -375,7 +410,8 @@ discard block |
||
375 | 410 | ); |
376 | 411 | } |
377 | 412 | |
378 | - if (\is_object($instance)) { |
|
413 | + if (\is_object($instance)) |
|
414 | + { |
|
379 | 415 | $this->validateConstraint($instance, $ctx); |
380 | 416 | return $this->registerInstance($ctx, $instance); |
381 | 417 | } |
@@ -392,8 +428,10 @@ discard block |
||
392 | 428 | ): ?object { |
393 | 429 | $avoidCache = $arguments !== []; |
394 | 430 | |
395 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
396 | - try { |
|
431 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
432 | + { |
|
433 | + try |
|
434 | + { |
|
397 | 435 | $tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
398 | 436 | |
399 | 437 | $object = $this->createInstance( |
@@ -402,17 +440,22 @@ discard block |
||
402 | 440 | $this, |
403 | 441 | $tracer, |
404 | 442 | ); |
405 | - if ($avoidCache) { |
|
443 | + if ($avoidCache) |
|
444 | + { |
|
406 | 445 | return $object; |
407 | 446 | } |
408 | 447 | $binding->reference = \WeakReference::create($object); |
409 | - } catch (\Throwable) { |
|
448 | + } |
|
449 | + catch (\Throwable) |
|
450 | + { |
|
410 | 451 | throw ContainerException::createWithTrace(\sprintf( |
411 | 452 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
412 | 453 | $tracer->getRootAlias(), |
413 | 454 | $alias, |
414 | 455 | ), $tracer->getTraces()); |
415 | - } finally { |
|
456 | + } |
|
457 | + finally |
|
458 | + { |
|
416 | 459 | $tracer->pop(); |
417 | 460 | } |
418 | 461 | } |
@@ -428,13 +471,16 @@ discard block |
||
428 | 471 | object $instance, |
429 | 472 | Ctx $ctx, |
430 | 473 | ): void { |
431 | - if ($this->options->checkScope) { |
|
474 | + if ($this->options->checkScope) |
|
475 | + { |
|
432 | 476 | // Check scope name |
433 | 477 | $ctx->reflection ??= new \ReflectionClass($instance); |
434 | 478 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
435 | - if ($scopeName !== null) { |
|
479 | + if ($scopeName !== null) |
|
480 | + { |
|
436 | 481 | $scope = $this->scope; |
437 | - while ($scope->getScopeName() !== $scopeName) { |
|
482 | + while ($scope->getScopeName() !== $scopeName) |
|
483 | + { |
|
438 | 484 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
439 | 485 | } |
440 | 486 | } |
@@ -461,26 +507,34 @@ discard block |
||
461 | 507 | Tracer $tracer, |
462 | 508 | ): object { |
463 | 509 | $class = $ctx->class; |
464 | - try { |
|
510 | + try |
|
511 | + { |
|
465 | 512 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
466 | - } catch (\ReflectionException $e) { |
|
513 | + } |
|
514 | + catch (\ReflectionException $e) |
|
515 | + { |
|
467 | 516 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
468 | 517 | } |
469 | 518 | |
470 | 519 | // Check Scope attribute |
471 | 520 | $actor = $fallbackActor ?? $this; |
472 | - if ($this->options->checkScope) { # todo |
|
521 | + if ($this->options->checkScope) |
|
522 | + { |
|
523 | +# todo |
|
473 | 524 | $ar = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null); |
474 | - if ($ar !== null) { |
|
525 | + if ($ar !== null) |
|
526 | + { |
|
475 | 527 | /** @var Attribute\Scope $attr */ |
476 | 528 | $attr = $ar->newInstance(); |
477 | 529 | $scope = $this->scope; |
478 | 530 | $actor = $this; |
479 | 531 | // Go through all parent scopes |
480 | 532 | $needed = $actor; |
481 | - while ($attr->name !== $scope->getScopeName()) { |
|
533 | + while ($attr->name !== $scope->getScopeName()) |
|
534 | + { |
|
482 | 535 | $needed = $scope->getParentActor(); |
483 | - if ($needed === null) { |
|
536 | + if ($needed === null) |
|
537 | + { |
|
484 | 538 | throw new BadScopeException($attr->name, $class); |
485 | 539 | } |
486 | 540 | |
@@ -493,11 +547,13 @@ discard block |
||
493 | 547 | } # todo |
494 | 548 | |
495 | 549 | // We have to construct class using external injector when we know the exact context |
496 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
550 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
551 | + { |
|
497 | 552 | return $actor->resolveInjector($actor->state->bindings[$ctx->class], $ctx, $arguments, $tracer); |
498 | 553 | } |
499 | 554 | |
500 | - if (!$reflection->isInstantiable()) { |
|
555 | + if (!$reflection->isInstantiable()) |
|
556 | + { |
|
501 | 557 | $itIs = match (true) { |
502 | 558 | $reflection->isEnum() => 'Enum', |
503 | 559 | $reflection->isAbstract() => 'Abstract class', |
@@ -511,8 +567,10 @@ discard block |
||
511 | 567 | |
512 | 568 | $constructor = $reflection->getConstructor(); |
513 | 569 | |
514 | - if ($constructor !== null) { |
|
515 | - try { |
|
570 | + if ($constructor !== null) |
|
571 | + { |
|
572 | + try |
|
573 | + { |
|
516 | 574 | $newScope = $this !== $actor; |
517 | 575 | $debug = [ |
518 | 576 | 'action' => 'resolve arguments', |
@@ -526,39 +584,54 @@ discard block |
||
526 | 584 | $tracer->push($newScope, ...$debug); |
527 | 585 | $tracer->push(true); |
528 | 586 | $args = $actor->resolver->resolveArguments($constructor, $arguments, $actor->options->validateArguments); |
529 | - } catch (ValidationException $e) { |
|
587 | + } |
|
588 | + catch (ValidationException $e) |
|
589 | + { |
|
530 | 590 | throw TracedContainerException::createWithTrace(\sprintf( |
531 | 591 | 'Can\'t resolve `%s`. %s', |
532 | 592 | $tracer->getRootAlias(), |
533 | 593 | $e->getMessage(), |
534 | 594 | ), $tracer->getTraces()); |
535 | - } catch (TracedContainerException $e) { |
|
595 | + } |
|
596 | + catch (TracedContainerException $e) |
|
597 | + { |
|
536 | 598 | throw $e::extendTracedException(\sprintf( |
537 | 599 | 'Can\'t resolve `%s`.', |
538 | 600 | $tracer->getRootAlias(), |
539 | 601 | ), $tracer->getTraces(), $e); |
540 | - } finally { |
|
602 | + } |
|
603 | + finally |
|
604 | + { |
|
541 | 605 | $tracer->pop($newScope); |
542 | 606 | $tracer->pop(false); |
543 | 607 | } |
544 | - try { |
|
608 | + try |
|
609 | + { |
|
545 | 610 | // Using constructor with resolved arguments |
546 | 611 | $tracer->push(false, call: "$class::__construct", arguments: $args); |
547 | 612 | $tracer->push(true); |
548 | 613 | $instance = new $class(...$args); |
549 | - } catch (\TypeError $e) { |
|
614 | + } |
|
615 | + catch (\TypeError $e) |
|
616 | + { |
|
550 | 617 | throw new WrongTypeException($constructor, $e); |
551 | - } catch (TracedContainerException $e) { |
|
618 | + } |
|
619 | + catch (TracedContainerException $e) |
|
620 | + { |
|
552 | 621 | throw $e::extendTracedException(\sprintf( |
553 | 622 | 'Can\'t resolve `%s`: failed constructing `%s`.', |
554 | 623 | $tracer->getRootAlias(), |
555 | 624 | $class, |
556 | 625 | ), $tracer->getTraces(), $e); |
557 | - } finally { |
|
626 | + } |
|
627 | + finally |
|
628 | + { |
|
558 | 629 | $tracer->pop(true); |
559 | 630 | $tracer->pop(false); |
560 | 631 | } |
561 | - } else { |
|
632 | + } |
|
633 | + else |
|
634 | + { |
|
562 | 635 | // No constructor specified |
563 | 636 | $instance = $reflection->newInstance(); |
564 | 637 | } |
@@ -590,12 +663,14 @@ discard block |
||
590 | 663 | */ |
591 | 664 | private function isSingleton(Ctx $ctx): bool |
592 | 665 | { |
593 | - if (is_bool($ctx->singleton)) { |
|
666 | + if (is_bool($ctx->singleton)) |
|
667 | + { |
|
594 | 668 | return $ctx->singleton; |
595 | 669 | } |
596 | 670 | |
597 | 671 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
598 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
672 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
673 | + { |
|
599 | 674 | return true; |
600 | 675 | } |
601 | 676 | |
@@ -609,7 +684,8 @@ discard block |
||
609 | 684 | * @var Attribute\Finalize|null $attribute |
610 | 685 | */ |
611 | 686 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
612 | - if ($attribute === null) { |
|
687 | + if ($attribute === null) |
|
688 | + { |
|
613 | 689 | return null; |
614 | 690 | } |
615 | 691 | |
@@ -623,10 +699,14 @@ discard block |
||
623 | 699 | { |
624 | 700 | $scope = $this->scope; |
625 | 701 | |
626 | - while ($scope !== null) { |
|
627 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
628 | - if ($instance instanceof $class) { |
|
629 | - foreach ($inflectors as $inflector) { |
|
702 | + while ($scope !== null) |
|
703 | + { |
|
704 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
705 | + { |
|
706 | + if ($instance instanceof $class) |
|
707 | + { |
|
708 | + foreach ($inflectors as $inflector) |
|
709 | + { |
|
630 | 710 | $instance = $inflector->getParametersCount() > 1 |
631 | 711 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
632 | 712 | : ($inflector->inflector)($instance); |
@@ -642,9 +722,12 @@ discard block |
||
642 | 722 | |
643 | 723 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
644 | 724 | { |
645 | - try { |
|
725 | + try |
|
726 | + { |
|
646 | 727 | $this->resolver->validateArguments($reflection, $arguments); |
647 | - } catch (\Throwable) { |
|
728 | + } |
|
729 | + catch (\Throwable) |
|
730 | + { |
|
648 | 731 | return false; |
649 | 732 | } |
650 | 733 |
@@ -34,19 +34,24 @@ discard block |
||
34 | 34 | public function push(bool $nextLevel, mixed ...$details): void |
35 | 35 | { |
36 | 36 | $trace = $details === [] ? null : new Trace($details); |
37 | - if ($nextLevel || $this->traces === []) { |
|
37 | + if ($nextLevel || $this->traces === []) |
|
38 | + { |
|
38 | 39 | $this->traces[] = $trace === null ? [] : [$trace]; |
39 | - } elseif ($trace !== null) { |
|
40 | + } |
|
41 | + elseif ($trace !== null) |
|
42 | + { |
|
40 | 43 | $this->traces[\array_key_last($this->traces)][] = $trace; |
41 | 44 | } |
42 | 45 | } |
43 | 46 | |
44 | 47 | public function pop(bool $previousLevel = false): void |
45 | 48 | { |
46 | - if ($this->traces === []) { |
|
49 | + if ($this->traces === []) |
|
50 | + { |
|
47 | 51 | return; |
48 | 52 | } |
49 | - if ($previousLevel) { |
|
53 | + if ($previousLevel) |
|
54 | + { |
|
50 | 55 | \array_pop($this->traces); |
51 | 56 | return; |
52 | 57 | } |
@@ -72,7 +77,8 @@ discard block |
||
72 | 77 | { |
73 | 78 | $result = []; |
74 | 79 | $i = 0; |
75 | - foreach ($blocks as $block) { |
|
80 | + foreach ($blocks as $block) |
|
81 | + { |
|
76 | 82 | \array_push($result, ...self::blockToStringList($block, $i++)); |
77 | 83 | } |
78 | 84 | return \implode("\n", $result); |
@@ -92,7 +98,8 @@ discard block |
||
92 | 98 | // Separator |
93 | 99 | $s = "\n"; |
94 | 100 | $nexPrefix = "$s$padding "; |
95 | - foreach ($items as $item) { |
|
101 | + foreach ($items as $item) |
|
102 | + { |
|
96 | 103 | $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string) $item); |
97 | 104 | } |
98 | 105 | return $result; |
@@ -64,7 +64,8 @@ discard block |
||
64 | 64 | string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
65 | 65 | private Options $options = new Options(), |
66 | 66 | ) { |
67 | - if (\is_object($scopeName)) { |
|
67 | + if (\is_object($scopeName)) |
|
68 | + { |
|
68 | 69 | $scopeName = (string) $scopeName->value; |
69 | 70 | } |
70 | 71 | |
@@ -166,23 +167,29 @@ discard block |
||
166 | 167 | */ |
167 | 168 | public function runScope(Scope|array $bindings, callable $scope): mixed |
168 | 169 | { |
169 | - if (!\is_array($bindings)) { |
|
170 | + if (!\is_array($bindings)) |
|
171 | + { |
|
170 | 172 | return $this->runIsolatedScope($bindings, $scope); |
171 | 173 | } |
172 | 174 | |
173 | 175 | $binds = &$this->state->bindings; |
174 | 176 | $singletons = &$this->state->singletons; |
175 | 177 | $cleanup = $previous = $prevSin = []; |
176 | - foreach ($bindings as $alias => $resolver) { |
|
178 | + foreach ($bindings as $alias => $resolver) |
|
179 | + { |
|
177 | 180 | // Store previous bindings |
178 | - if (isset($binds[$alias])) { |
|
181 | + if (isset($binds[$alias])) |
|
182 | + { |
|
179 | 183 | $previous[$alias] = $binds[$alias]; |
180 | - } else { |
|
184 | + } |
|
185 | + else |
|
186 | + { |
|
181 | 187 | // Store bindings to be removed |
182 | 188 | $cleanup[] = $alias; |
183 | 189 | } |
184 | 190 | // Store previous singletons |
185 | - if (isset($singletons[$alias])) { |
|
191 | + if (isset($singletons[$alias])) |
|
192 | + { |
|
186 | 193 | $prevSin[$alias] = $singletons[$alias]; |
187 | 194 | unset($singletons[$alias]); |
188 | 195 | } |
@@ -190,21 +197,27 @@ discard block |
||
190 | 197 | $this->binder->bind($alias, $resolver); |
191 | 198 | } |
192 | 199 | |
193 | - try { |
|
200 | + try |
|
201 | + { |
|
194 | 202 | return ContainerScope::getContainer() !== $this |
195 | 203 | ? ContainerScope::runScope($this, $scope) |
196 | 204 | : $scope($this); |
197 | - } finally { |
|
205 | + } |
|
206 | + finally |
|
207 | + { |
|
198 | 208 | // Remove new bindings |
199 | - foreach ($cleanup as $alias) { |
|
209 | + foreach ($cleanup as $alias) |
|
210 | + { |
|
200 | 211 | unset($binds[$alias], $singletons[$alias]); |
201 | 212 | } |
202 | 213 | // Restore previous bindings |
203 | - foreach ($previous as $alias => $resolver) { |
|
214 | + foreach ($previous as $alias => $resolver) |
|
215 | + { |
|
204 | 216 | $binds[$alias] = $resolver; |
205 | 217 | } |
206 | 218 | // Restore singletons |
207 | - foreach ($prevSin as $alias => $instance) { |
|
219 | + foreach ($prevSin as $alias => $instance) |
|
220 | + { |
|
208 | 221 | $singletons[$alias] = $instance; |
209 | 222 | } |
210 | 223 | } |
@@ -254,7 +267,8 @@ discard block |
||
254 | 267 | */ |
255 | 268 | public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
256 | 269 | { |
257 | - if ($force ?? $this->options->allowSingletonsRebinding) { |
|
270 | + if ($force ?? $this->options->allowSingletonsRebinding) |
|
271 | + { |
|
258 | 272 | $this->binder->removeBinding($alias); |
259 | 273 | } |
260 | 274 | |
@@ -338,8 +352,10 @@ discard block |
||
338 | 352 | ], $this->options); |
339 | 353 | |
340 | 354 | // Create container services |
341 | - foreach ($container->config as $property => $class) { |
|
342 | - if (\property_exists($container, $property)) { |
|
355 | + foreach ($container->config as $property => $class) |
|
356 | + { |
|
357 | + if (\property_exists($container, $property)) |
|
358 | + { |
|
343 | 359 | $container->$property = $constructor->get($property, $class); |
344 | 360 | } |
345 | 361 | } |
@@ -353,7 +369,8 @@ discard block |
||
353 | 369 | private function closeScope(): void |
354 | 370 | { |
355 | 371 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
356 | - if (!isset($this->scope)) { |
|
372 | + if (!isset($this->scope)) |
|
373 | + { |
|
357 | 374 | $this->destruct(); |
358 | 375 | return; |
359 | 376 | } |
@@ -362,10 +379,14 @@ discard block |
||
362 | 379 | |
363 | 380 | // Run finalizers |
364 | 381 | $errors = []; |
365 | - foreach ($this->state->finalizers as $finalizer) { |
|
366 | - try { |
|
382 | + foreach ($this->state->finalizers as $finalizer) |
|
383 | + { |
|
384 | + try |
|
385 | + { |
|
367 | 386 | $this->invoker->invoke($finalizer); |
368 | - } catch (\Throwable $e) { |
|
387 | + } |
|
388 | + catch (\Throwable $e) |
|
389 | + { |
|
369 | 390 | $errors[] = $e; |
370 | 391 | } |
371 | 392 | } |
@@ -374,7 +395,8 @@ discard block |
||
374 | 395 | $this->destruct(); |
375 | 396 | |
376 | 397 | // Throw collected errors |
377 | - if ($errors !== []) { |
|
398 | + if ($errors !== []) |
|
399 | + { |
|
378 | 400 | throw new FinalizersException($scopeName, $errors); |
379 | 401 | } |
380 | 402 | } |
@@ -396,18 +418,22 @@ discard block |
||
396 | 418 | $container->scope->setParent($this, $this->scope, $this->actor); |
397 | 419 | |
398 | 420 | // Add specific bindings |
399 | - foreach ($config->bindings as $alias => $resolver) { |
|
421 | + foreach ($config->bindings as $alias => $resolver) |
|
422 | + { |
|
400 | 423 | $container->binder->bind($alias, $resolver); |
401 | 424 | } |
402 | 425 | |
403 | 426 | return ContainerScope::runScope( |
404 | 427 | $container, |
405 | 428 | static function (self $container) use ($config, $closure): mixed { |
406 | - try { |
|
429 | + try |
|
430 | + { |
|
407 | 431 | return $config->autowire |
408 | 432 | ? $container->invoke($closure) |
409 | 433 | : $closure($container); |
410 | - } finally { |
|
434 | + } |
|
435 | + finally |
|
436 | + { |
|
411 | 437 | $container->closeScope(); |
412 | 438 | } |
413 | 439 | }, |