@@ -13,11 +13,11 @@ |
||
13 | 13 | public function __construct( |
14 | 14 | public readonly string $alias, |
15 | 15 | public readonly ?string $bindingScope = null, |
16 | - public readonly ?array $callingScope = null, |
|
17 | - ) { |
|
16 | + public readonly ? array $callingScope = null, |
|
17 | + ){ |
|
18 | 18 | $message = "Recursive proxy detected for `$alias`."; |
19 | 19 | $bindingScope === null or $message .= "\nBinding scope: `$bindingScope`."; |
20 | - $callingScope === null or $message .= "\nCalling scope: `" . \implode('.', $callingScope) . '`.'; |
|
20 | + $callingScope === null or $message .= "\nCalling scope: `".\implode('.', $callingScope).'`.'; |
|
21 | 21 | parent::__construct($message); |
22 | 22 | } |
23 | 23 | } |
@@ -17,8 +17,8 @@ |
||
17 | 17 | public function __construct( |
18 | 18 | protected readonly string $interface, |
19 | 19 | public readonly bool $singleton = false, |
20 | - public readonly ?\Closure $fallbackFactory = null, |
|
21 | - ) { |
|
20 | + public readonly ? \Closure $fallbackFactory = null, |
|
21 | + ){ |
|
22 | 22 | \interface_exists($interface) or throw new \InvalidArgumentException( |
23 | 23 | "Interface `{$interface}` does not exist.", |
24 | 24 | ); |
@@ -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 | |
@@ -252,11 +268,14 @@ discard block |
||
252 | 268 | array $arguments, |
253 | 269 | ): mixed { |
254 | 270 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
255 | - try { |
|
271 | + try |
|
272 | + { |
|
256 | 273 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
257 | 274 | ? ($binding->factory)() |
258 | 275 | : $this->invoker->invoke($binding->factory, $arguments); |
259 | - } catch (NotCallableException $e) { |
|
276 | + } |
|
277 | + catch (NotCallableException $e) |
|
278 | + { |
|
260 | 279 | throw new ContainerException( |
261 | 280 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
262 | 281 | $e->getCode(), |
@@ -275,19 +294,24 @@ discard block |
||
275 | 294 | ): ?object { |
276 | 295 | $avoidCache = $arguments !== []; |
277 | 296 | |
278 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
279 | - try { |
|
297 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
298 | + { |
|
299 | + try |
|
300 | + { |
|
280 | 301 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
281 | 302 | |
282 | 303 | $object = $this->createInstance( |
283 | 304 | new Ctx(alias: $alias, class: $alias, context: $context), |
284 | 305 | $arguments, |
285 | 306 | ); |
286 | - if ($avoidCache) { |
|
307 | + if ($avoidCache) |
|
308 | + { |
|
287 | 309 | return $object; |
288 | 310 | } |
289 | 311 | $binding->reference = WeakReference::create($object); |
290 | - } catch (\Throwable) { |
|
312 | + } |
|
313 | + catch (\Throwable) |
|
314 | + { |
|
291 | 315 | throw new ContainerException( |
292 | 316 | $this->tracer->combineTraceMessage( |
293 | 317 | \sprintf( |
@@ -297,7 +321,9 @@ discard block |
||
297 | 321 | ) |
298 | 322 | ) |
299 | 323 | ); |
300 | - } finally { |
|
324 | + } |
|
325 | + finally |
|
326 | + { |
|
301 | 327 | $this->tracer->pop(); |
302 | 328 | } |
303 | 329 | } |
@@ -312,19 +338,26 @@ discard block |
||
312 | 338 | ): mixed { |
313 | 339 | $parent = $this->scope->getParentFactory(); |
314 | 340 | |
315 | - if ($parent !== null) { |
|
316 | - try { |
|
341 | + if ($parent !== null) |
|
342 | + { |
|
343 | + try |
|
344 | + { |
|
317 | 345 | $this->tracer->push(false, ...[ |
318 | 346 | 'current scope' => $this->scope->getScopeName(), |
319 | 347 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
320 | 348 | ]); |
321 | 349 | /** @psalm-suppress TooManyArguments */ |
322 | 350 | return $parent->make($alias, $parameters, $context); |
323 | - } catch (BadScopeException $e) { |
|
324 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
351 | + } |
|
352 | + catch (BadScopeException $e) |
|
353 | + { |
|
354 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
355 | + { |
|
325 | 356 | throw $e; |
326 | 357 | } |
327 | - } catch (ContainerExceptionInterface $e) { |
|
358 | + } |
|
359 | + catch (ContainerExceptionInterface $e) |
|
360 | + { |
|
328 | 361 | $className = match (true) { |
329 | 362 | $e instanceof NotFoundException => NotFoundException::class, |
330 | 363 | $e instanceof RecursiveProxyException => throw $e, |
@@ -334,19 +367,24 @@ discard block |
||
334 | 367 | 'Can\'t resolve `%s`.', |
335 | 368 | $alias, |
336 | 369 | )), previous: $e); |
337 | - } finally { |
|
370 | + } |
|
371 | + finally |
|
372 | + { |
|
338 | 373 | $this->tracer->pop(false); |
339 | 374 | } |
340 | 375 | } |
341 | 376 | |
342 | 377 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
343 | - try { |
|
378 | + try |
|
379 | + { |
|
344 | 380 | //No direct instructions how to construct class, make is automatically |
345 | 381 | return $this->autowire( |
346 | 382 | new Ctx(alias: $alias, class: $alias, context: $context), |
347 | 383 | $parameters, |
348 | 384 | ); |
349 | - } finally { |
|
385 | + } |
|
386 | + finally |
|
387 | + { |
|
350 | 388 | $this->tracer->pop(false); |
351 | 389 | } |
352 | 390 | } |
@@ -393,13 +431,16 @@ discard block |
||
393 | 431 | Ctx $ctx, |
394 | 432 | array $arguments, |
395 | 433 | ): object { |
396 | - if ($this->options->checkScope) { |
|
434 | + if ($this->options->checkScope) |
|
435 | + { |
|
397 | 436 | // Check scope name |
398 | 437 | $ctx->reflection = new \ReflectionClass($instance); |
399 | 438 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
400 | - if ($scopeName !== null) { |
|
439 | + if ($scopeName !== null) |
|
440 | + { |
|
401 | 441 | $scope = $this->scope; |
402 | - while ($scope->getScopeName() !== $scopeName) { |
|
442 | + while ($scope->getScopeName() !== $scopeName) |
|
443 | + { |
|
403 | 444 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
404 | 445 | } |
405 | 446 | } |
@@ -428,26 +469,33 @@ discard block |
||
428 | 469 | array $arguments, |
429 | 470 | ): object { |
430 | 471 | $class = $ctx->class; |
431 | - try { |
|
472 | + try |
|
473 | + { |
|
432 | 474 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
433 | - } catch (\ReflectionException $e) { |
|
475 | + } |
|
476 | + catch (\ReflectionException $e) |
|
477 | + { |
|
434 | 478 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
435 | 479 | } |
436 | 480 | |
437 | 481 | // Check scope name |
438 | - if ($this->options->checkScope) { |
|
482 | + if ($this->options->checkScope) |
|
483 | + { |
|
439 | 484 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
440 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
485 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
486 | + { |
|
441 | 487 | throw new BadScopeException($scope, $class); |
442 | 488 | } |
443 | 489 | } |
444 | 490 | |
445 | 491 | // We have to construct class using external injector when we know the exact context |
446 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
492 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
493 | + { |
|
447 | 494 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
448 | 495 | } |
449 | 496 | |
450 | - if (!$reflection->isInstantiable()) { |
|
497 | + if (!$reflection->isInstantiable()) |
|
498 | + { |
|
451 | 499 | $itIs = match (true) { |
452 | 500 | $reflection->isEnum() => 'Enum', |
453 | 501 | $reflection->isAbstract() => 'Abstract class', |
@@ -460,12 +508,16 @@ discard block |
||
460 | 508 | |
461 | 509 | $constructor = $reflection->getConstructor(); |
462 | 510 | |
463 | - if ($constructor !== null) { |
|
464 | - try { |
|
511 | + if ($constructor !== null) |
|
512 | + { |
|
513 | + try |
|
514 | + { |
|
465 | 515 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
466 | 516 | $this->tracer->push(true); |
467 | 517 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
468 | - } catch (ValidationException $e) { |
|
518 | + } |
|
519 | + catch (ValidationException $e) |
|
520 | + { |
|
469 | 521 | throw new ContainerException( |
470 | 522 | $this->tracer->combineTraceMessage( |
471 | 523 | \sprintf( |
@@ -475,22 +527,31 @@ discard block |
||
475 | 527 | ) |
476 | 528 | ), |
477 | 529 | ); |
478 | - } finally { |
|
530 | + } |
|
531 | + finally |
|
532 | + { |
|
479 | 533 | $this->tracer->pop(true); |
480 | 534 | $this->tracer->pop(false); |
481 | 535 | } |
482 | - try { |
|
536 | + try |
|
537 | + { |
|
483 | 538 | // Using constructor with resolved arguments |
484 | 539 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
485 | 540 | $this->tracer->push(true); |
486 | 541 | $instance = new $class(...$args); |
487 | - } catch (\TypeError $e) { |
|
542 | + } |
|
543 | + catch (\TypeError $e) |
|
544 | + { |
|
488 | 545 | throw new WrongTypeException($constructor, $e); |
489 | - } finally { |
|
546 | + } |
|
547 | + finally |
|
548 | + { |
|
490 | 549 | $this->tracer->pop(true); |
491 | 550 | $this->tracer->pop(false); |
492 | 551 | } |
493 | - } else { |
|
552 | + } |
|
553 | + else |
|
554 | + { |
|
494 | 555 | // No constructor specified |
495 | 556 | $instance = $reflection->newInstance(); |
496 | 557 | } |
@@ -508,13 +569,15 @@ discard block |
||
508 | 569 | $instance = $this->runInflector($instance); |
509 | 570 | |
510 | 571 | //Declarative singletons |
511 | - if ($this->isSingleton($ctx)) { |
|
572 | + if ($this->isSingleton($ctx)) |
|
573 | + { |
|
512 | 574 | $this->state->singletons[$ctx->alias] = $instance; |
513 | 575 | } |
514 | 576 | |
515 | 577 | // Register finalizer |
516 | 578 | $finalizer = $this->getFinalizer($ctx, $instance); |
517 | - if ($finalizer !== null) { |
|
579 | + if ($finalizer !== null) |
|
580 | + { |
|
518 | 581 | $this->state->finalizers[] = $finalizer; |
519 | 582 | } |
520 | 583 | |
@@ -526,12 +589,14 @@ discard block |
||
526 | 589 | */ |
527 | 590 | private function isSingleton(Ctx $ctx): bool |
528 | 591 | { |
529 | - if ($ctx->singleton === true) { |
|
592 | + if ($ctx->singleton === true) |
|
593 | + { |
|
530 | 594 | return true; |
531 | 595 | } |
532 | 596 | |
533 | 597 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
534 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
598 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
599 | + { |
|
535 | 600 | return true; |
536 | 601 | } |
537 | 602 | |
@@ -545,7 +610,8 @@ discard block |
||
545 | 610 | * @var Attribute\Finalize|null $attribute |
546 | 611 | */ |
547 | 612 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
548 | - if ($attribute === null) { |
|
613 | + if ($attribute === null) |
|
614 | + { |
|
549 | 615 | return null; |
550 | 616 | } |
551 | 617 | |
@@ -559,10 +625,14 @@ discard block |
||
559 | 625 | { |
560 | 626 | $scope = $this->scope; |
561 | 627 | |
562 | - while ($scope !== null) { |
|
563 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
564 | - if ($instance instanceof $class) { |
|
565 | - foreach ($inflectors as $inflector) { |
|
628 | + while ($scope !== null) |
|
629 | + { |
|
630 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
631 | + { |
|
632 | + if ($instance instanceof $class) |
|
633 | + { |
|
634 | + foreach ($inflectors as $inflector) |
|
635 | + { |
|
566 | 636 | $instance = $inflector->getParametersCount() > 1 |
567 | 637 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
568 | 638 | : ($inflector->inflector)($instance); |
@@ -578,9 +648,12 @@ discard block |
||
578 | 648 | |
579 | 649 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
580 | 650 | { |
581 | - try { |
|
651 | + try |
|
652 | + { |
|
582 | 653 | $this->resolver->validateArguments($reflection, $arguments); |
583 | - } catch (\Throwable) { |
|
654 | + } |
|
655 | + catch (\Throwable) |
|
656 | + { |
|
584 | 657 | return false; |
585 | 658 | } |
586 | 659 |
@@ -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,13 +221,13 @@ 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 | return $avoidCache |
229 | 229 | ? $this->createInstance( |
230 | - new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
|
230 | + new Ctx(alias : $alias, class : $binding->value::class, context : $context), |
|
231 | 231 | $arguments, |
232 | 232 | ) |
233 | 233 | : $binding->value; |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | private function resolveAutowire( |
237 | 237 | Config\Autowire $binding, |
238 | 238 | string $alias, |
239 | - Stringable|string|null $context, |
|
239 | + Stringable | string | null $context, |
|
240 | 240 | array $arguments, |
241 | 241 | ): mixed { |
242 | 242 | $target = $binding->autowire->alias; |
@@ -250,17 +250,17 @@ discard block |
||
250 | 250 | } |
251 | 251 | |
252 | 252 | private function resolveFactory( |
253 | - Config\Factory|Config\DeferredFactory $binding, |
|
253 | + Config\Factory | Config\DeferredFactory $binding, |
|
254 | 254 | string $alias, |
255 | - Stringable|string|null $context, |
|
255 | + Stringable | string | null $context, |
|
256 | 256 | array $arguments, |
257 | 257 | ): mixed { |
258 | 258 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
259 | - try { |
|
259 | + try{ |
|
260 | 260 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
261 | 261 | ? ($binding->factory)() |
262 | 262 | : $this->invoker->invoke($binding->factory, $arguments); |
263 | - } catch (NotCallableException $e) { |
|
263 | + }catch (NotCallableException $e){ |
|
264 | 264 | throw new ContainerException( |
265 | 265 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
266 | 266 | $e->getCode(), |
@@ -274,24 +274,24 @@ discard block |
||
274 | 274 | private function resolveWeakReference( |
275 | 275 | Config\WeakReference $binding, |
276 | 276 | string $alias, |
277 | - Stringable|string|null $context, |
|
277 | + Stringable | string | null $context, |
|
278 | 278 | array $arguments, |
279 | 279 | ): ?object { |
280 | 280 | $avoidCache = $arguments !== []; |
281 | 281 | |
282 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
283 | - try { |
|
282 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
283 | + try{ |
|
284 | 284 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
285 | 285 | |
286 | 286 | $object = $this->createInstance( |
287 | 287 | new Ctx(alias: $alias, class: $alias, context: $context), |
288 | 288 | $arguments, |
289 | 289 | ); |
290 | - if ($avoidCache) { |
|
290 | + if ($avoidCache){ |
|
291 | 291 | return $object; |
292 | 292 | } |
293 | 293 | $binding->reference = WeakReference::create($object); |
294 | - } catch (\Throwable) { |
|
294 | + }catch (\Throwable){ |
|
295 | 295 | throw new ContainerException( |
296 | 296 | $this->tracer->combineTraceMessage( |
297 | 297 | \sprintf( |
@@ -301,7 +301,7 @@ discard block |
||
301 | 301 | ) |
302 | 302 | ) |
303 | 303 | ); |
304 | - } finally { |
|
304 | + }finally{ |
|
305 | 305 | $this->tracer->pop(); |
306 | 306 | } |
307 | 307 | } |
@@ -312,23 +312,23 @@ discard block |
||
312 | 312 | private function resolveWithoutBinding( |
313 | 313 | string $alias, |
314 | 314 | array $parameters = [], |
315 | - Stringable|string|null $context = null |
|
315 | + Stringable | string | null $context = null |
|
316 | 316 | ): mixed { |
317 | 317 | $parent = $this->scope->getParentFactory(); |
318 | 318 | |
319 | - if ($parent !== null) { |
|
320 | - try { |
|
319 | + if ($parent !== null){ |
|
320 | + try{ |
|
321 | 321 | $this->tracer->push(false, ...[ |
322 | 322 | 'current scope' => $this->scope->getScopeName(), |
323 | 323 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
324 | 324 | ]); |
325 | 325 | /** @psalm-suppress TooManyArguments */ |
326 | 326 | return $parent->make($alias, $parameters, $context); |
327 | - } catch (BadScopeException $e) { |
|
328 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
327 | + }catch (BadScopeException $e){ |
|
328 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
329 | 329 | throw $e; |
330 | 330 | } |
331 | - } catch (ContainerExceptionInterface $e) { |
|
331 | + }catch (ContainerExceptionInterface $e){ |
|
332 | 332 | $className = match (true) { |
333 | 333 | $e instanceof NotFoundException => NotFoundException::class, |
334 | 334 | $e instanceof RecursiveProxyException => throw $e, |
@@ -338,19 +338,19 @@ discard block |
||
338 | 338 | 'Can\'t resolve `%s`.', |
339 | 339 | $alias, |
340 | 340 | )), previous: $e); |
341 | - } finally { |
|
341 | + }finally{ |
|
342 | 342 | $this->tracer->pop(false); |
343 | 343 | } |
344 | 344 | } |
345 | 345 | |
346 | 346 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
347 | - try { |
|
347 | + try{ |
|
348 | 348 | //No direct instructions how to construct class, make is automatically |
349 | 349 | return $this->autowire( |
350 | 350 | new Ctx(alias: $alias, class: $alias, context: $context), |
351 | 351 | $parameters, |
352 | 352 | ); |
353 | - } finally { |
|
353 | + }finally{ |
|
354 | 354 | $this->tracer->pop(false); |
355 | 355 | } |
356 | 356 | } |
@@ -371,7 +371,7 @@ discard block |
||
371 | 371 | && |
372 | 372 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
373 | 373 | )) |
374 | - ) { |
|
374 | + ){ |
|
375 | 375 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
376 | 376 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
377 | 377 | $this->tracer->getRootAlias(), |
@@ -397,13 +397,13 @@ discard block |
||
397 | 397 | Ctx $ctx, |
398 | 398 | array $arguments, |
399 | 399 | ): object { |
400 | - if ($this->options->checkScope) { |
|
400 | + if ($this->options->checkScope){ |
|
401 | 401 | // Check scope name |
402 | 402 | $ctx->reflection = new \ReflectionClass($instance); |
403 | 403 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
404 | - if ($scopeName !== null) { |
|
404 | + if ($scopeName !== null){ |
|
405 | 405 | $scope = $this->scope; |
406 | - while ($scope->getScopeName() !== $scopeName) { |
|
406 | + while ($scope->getScopeName() !== $scopeName){ |
|
407 | 407 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
408 | 408 | } |
409 | 409 | } |
@@ -432,26 +432,26 @@ discard block |
||
432 | 432 | array $arguments, |
433 | 433 | ): object { |
434 | 434 | $class = $ctx->class; |
435 | - try { |
|
435 | + try{ |
|
436 | 436 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
437 | - } catch (\ReflectionException $e) { |
|
437 | + }catch (\ReflectionException $e){ |
|
438 | 438 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
439 | 439 | } |
440 | 440 | |
441 | 441 | // Check scope name |
442 | - if ($this->options->checkScope) { |
|
442 | + if ($this->options->checkScope){ |
|
443 | 443 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
444 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
444 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
445 | 445 | throw new BadScopeException($scope, $class); |
446 | 446 | } |
447 | 447 | } |
448 | 448 | |
449 | 449 | // We have to construct class using external injector when we know the exact context |
450 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
450 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
451 | 451 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
452 | 452 | } |
453 | 453 | |
454 | - if (!$reflection->isInstantiable()) { |
|
454 | + if (!$reflection->isInstantiable()){ |
|
455 | 455 | $itIs = match (true) { |
456 | 456 | $reflection->isEnum() => 'Enum', |
457 | 457 | $reflection->isAbstract() => 'Abstract class', |
@@ -464,12 +464,12 @@ discard block |
||
464 | 464 | |
465 | 465 | $constructor = $reflection->getConstructor(); |
466 | 466 | |
467 | - if ($constructor !== null) { |
|
468 | - try { |
|
467 | + if ($constructor !== null){ |
|
468 | + try{ |
|
469 | 469 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
470 | 470 | $this->tracer->push(true); |
471 | 471 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
472 | - } catch (ValidationException $e) { |
|
472 | + }catch (ValidationException $e){ |
|
473 | 473 | throw new ContainerException( |
474 | 474 | $this->tracer->combineTraceMessage( |
475 | 475 | \sprintf( |
@@ -479,22 +479,22 @@ discard block |
||
479 | 479 | ) |
480 | 480 | ), |
481 | 481 | ); |
482 | - } finally { |
|
482 | + }finally{ |
|
483 | 483 | $this->tracer->pop(true); |
484 | 484 | $this->tracer->pop(false); |
485 | 485 | } |
486 | - try { |
|
486 | + try{ |
|
487 | 487 | // Using constructor with resolved arguments |
488 | 488 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
489 | 489 | $this->tracer->push(true); |
490 | 490 | $instance = new $class(...$args); |
491 | - } catch (\TypeError $e) { |
|
491 | + }catch (\TypeError $e){ |
|
492 | 492 | throw new WrongTypeException($constructor, $e); |
493 | - } finally { |
|
493 | + }finally{ |
|
494 | 494 | $this->tracer->pop(true); |
495 | 495 | $this->tracer->pop(false); |
496 | 496 | } |
497 | - } else { |
|
497 | + }else{ |
|
498 | 498 | // No constructor specified |
499 | 499 | $instance = $reflection->newInstance(); |
500 | 500 | } |
@@ -512,13 +512,13 @@ discard block |
||
512 | 512 | $instance = $this->runInflector($instance); |
513 | 513 | |
514 | 514 | //Declarative singletons |
515 | - if ($this->isSingleton($ctx)) { |
|
515 | + if ($this->isSingleton($ctx)){ |
|
516 | 516 | $this->state->singletons[$ctx->alias] = $instance; |
517 | 517 | } |
518 | 518 | |
519 | 519 | // Register finalizer |
520 | 520 | $finalizer = $this->getFinalizer($ctx, $instance); |
521 | - if ($finalizer !== null) { |
|
521 | + if ($finalizer !== null){ |
|
522 | 522 | $this->state->finalizers[] = $finalizer; |
523 | 523 | } |
524 | 524 | |
@@ -530,12 +530,12 @@ discard block |
||
530 | 530 | */ |
531 | 531 | private function isSingleton(Ctx $ctx): bool |
532 | 532 | { |
533 | - if ($ctx->singleton === true) { |
|
533 | + if ($ctx->singleton === true){ |
|
534 | 534 | return true; |
535 | 535 | } |
536 | 536 | |
537 | 537 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
538 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
538 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
539 | 539 | return true; |
540 | 540 | } |
541 | 541 | |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | * @var Attribute\Finalize|null $attribute |
550 | 550 | */ |
551 | 551 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
552 | - if ($attribute === null) { |
|
552 | + if ($attribute === null){ |
|
553 | 553 | return null; |
554 | 554 | } |
555 | 555 | |
@@ -563,10 +563,10 @@ discard block |
||
563 | 563 | { |
564 | 564 | $scope = $this->scope; |
565 | 565 | |
566 | - while ($scope !== null) { |
|
567 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
568 | - if ($instance instanceof $class) { |
|
569 | - foreach ($inflectors as $inflector) { |
|
566 | + while ($scope !== null){ |
|
567 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
568 | + if ($instance instanceof $class){ |
|
569 | + foreach ($inflectors as $inflector){ |
|
570 | 570 | $instance = $inflector->getParametersCount() > 1 |
571 | 571 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
572 | 572 | : ($inflector->inflector)($instance); |
@@ -582,9 +582,9 @@ discard block |
||
582 | 582 | |
583 | 583 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
584 | 584 | { |
585 | - try { |
|
585 | + try{ |
|
586 | 586 | $this->resolver->validateArguments($reflection, $arguments); |
587 | - } catch (\Throwable) { |
|
587 | + }catch (\Throwable){ |
|
588 | 588 | return false; |
589 | 589 | } |
590 | 590 |
@@ -19,27 +19,27 @@ discard block |
||
19 | 19 | { |
20 | 20 | public static function resolve( |
21 | 21 | string $alias, |
22 | - \Stringable|string|null $context = null, |
|
22 | + \Stringable | string | null $context = null, |
|
23 | 23 | ?ContainerInterface $c = null, |
24 | 24 | ): object { |
25 | 25 | $c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
26 | 26 | |
27 | - try { |
|
27 | + try{ |
|
28 | 28 | /** @psalm-suppress TooManyArguments */ |
29 | 29 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
30 | 30 | 'Resolved `null` from the container.', |
31 | 31 | ); |
32 | - } catch (\Throwable $e) { |
|
32 | + }catch (\Throwable $e){ |
|
33 | 33 | $scope = self::getScope($c); |
34 | 34 | throw new ContainerException( |
35 | 35 | $scope === null |
36 | 36 | ? "Unable to resolve `{$alias}` in a Proxy." |
37 | 37 | : \sprintf('Unable to resolve `%s` in a Proxy in `%s` scope.', $alias, \implode('.', $scope)), |
38 | - previous: $e, |
|
38 | + previous : $e, |
|
39 | 39 | ); |
40 | 40 | } |
41 | 41 | |
42 | - if (!Proxy::isProxy($result)) { |
|
42 | + if (!Proxy::isProxy($result)){ |
|
43 | 43 | return $result; |
44 | 44 | } |
45 | 45 | |
@@ -48,10 +48,10 @@ discard block |
||
48 | 48 | * to try to get the instance from the Proxy Fallback Factory. |
49 | 49 | * If there is no the Proxy Fallback Factory, {@see RecursiveProxyException} will be thrown. |
50 | 50 | */ |
51 | - try { |
|
51 | + try{ |
|
52 | 52 | /** @psalm-suppress TooManyArguments */ |
53 | 53 | $result = $c->get($alias, new RetryContext($context)); |
54 | - } catch (RecursiveProxyException $e) { |
|
54 | + }catch (RecursiveProxyException $e){ |
|
55 | 55 | throw new RecursiveProxyException($e->alias, $e->bindingScope, self::getScope($c)); |
56 | 56 | } |
57 | 57 | |
@@ -66,8 +66,8 @@ discard block |
||
66 | 66 | */ |
67 | 67 | private static function getScope(ContainerInterface $c): ?array |
68 | 68 | { |
69 | - if (!$c instanceof Container) { |
|
70 | - if (!Proxy::isProxy($c)) { |
|
69 | + if (!$c instanceof Container){ |
|
70 | + if (!Proxy::isProxy($c)){ |
|
71 | 71 | return null; |
72 | 72 | } |
73 | 73 | |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | } |
76 | 76 | |
77 | 77 | return \array_reverse(\array_map( |
78 | - static fn (?string $name): string => $name ?? 'null', |
|
78 | + static fn (?string $name) : string => $name ?? 'null', |
|
79 | 79 | Introspector::scopeNames($c), |
80 | 80 | )); |
81 | 81 | } |
@@ -24,12 +24,15 @@ discard block |
||
24 | 24 | ): object { |
25 | 25 | $c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
26 | 26 | |
27 | - try { |
|
27 | + try |
|
28 | + { |
|
28 | 29 | /** @psalm-suppress TooManyArguments */ |
29 | 30 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
30 | 31 | 'Resolved `null` from the container.', |
31 | 32 | ); |
32 | - } catch (\Throwable $e) { |
|
33 | + } |
|
34 | + catch (\Throwable $e) |
|
35 | + { |
|
33 | 36 | $scope = self::getScope($c); |
34 | 37 | throw new ContainerException( |
35 | 38 | $scope === null |
@@ -39,7 +42,8 @@ discard block |
||
39 | 42 | ); |
40 | 43 | } |
41 | 44 | |
42 | - if (!Proxy::isProxy($result)) { |
|
45 | + if (!Proxy::isProxy($result)) |
|
46 | + { |
|
43 | 47 | return $result; |
44 | 48 | } |
45 | 49 | |
@@ -48,10 +52,13 @@ discard block |
||
48 | 52 | * to try to get the instance from the Proxy Fallback Factory. |
49 | 53 | * If there is no the Proxy Fallback Factory, {@see RecursiveProxyException} will be thrown. |
50 | 54 | */ |
51 | - try { |
|
55 | + try |
|
56 | + { |
|
52 | 57 | /** @psalm-suppress TooManyArguments */ |
53 | 58 | $result = $c->get($alias, new RetryContext($context)); |
54 | - } catch (RecursiveProxyException $e) { |
|
59 | + } |
|
60 | + catch (RecursiveProxyException $e) |
|
61 | + { |
|
55 | 62 | throw new RecursiveProxyException($e->alias, $e->bindingScope, self::getScope($c)); |
56 | 63 | } |
57 | 64 | |
@@ -66,8 +73,10 @@ discard block |
||
66 | 73 | */ |
67 | 74 | private static function getScope(ContainerInterface $c): ?array |
68 | 75 | { |
69 | - if (!$c instanceof Container) { |
|
70 | - if (!Proxy::isProxy($c)) { |
|
76 | + if (!$c instanceof Container) |
|
77 | + { |
|
78 | + if (!Proxy::isProxy($c)) |
|
79 | + { |
|
71 | 80 | return null; |
72 | 81 | } |
73 | 82 |
@@ -15,12 +15,12 @@ |
||
15 | 15 | * @param \Stringable|string|null $context Original context. |
16 | 16 | */ |
17 | 17 | public function __construct( |
18 | - public \Stringable|string|null $context = null, |
|
19 | - ) { |
|
18 | + public \Stringable | string | null $context = null, |
|
19 | + ){ |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public function __toString(): string |
23 | 23 | { |
24 | - return (string) $this->context; |
|
24 | + return (string)$this->context; |
|
25 | 25 | } |
26 | 26 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | { |
20 | 20 | public function init(HttpBootloader $http): void |
21 | 21 | { |
22 | - foreach ($this->globalMiddleware() as $middleware) { |
|
22 | + foreach ($this->globalMiddleware() as $middleware){ |
|
23 | 23 | $http->addMiddleware($middleware); |
24 | 24 | } |
25 | 25 | } |
@@ -61,11 +61,11 @@ discard block |
||
61 | 61 | |
62 | 62 | private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void |
63 | 63 | { |
64 | - foreach ($groups as $group => $middleware) { |
|
64 | + foreach ($groups as $group => $middleware){ |
|
65 | 65 | $binder |
66 | 66 | ->getBinder('http') |
67 | 67 | ->bind( |
68 | - 'middleware:' . $group, |
|
68 | + 'middleware:'.$group, |
|
69 | 69 | static function (FactoryInterface $factory) use ($middleware): LazyPipeline { |
70 | 70 | return $factory->make(LazyPipeline::class)->withAddedMiddleware(...$middleware); |
71 | 71 | } |
@@ -75,8 +75,8 @@ discard block |
||
75 | 75 | |
76 | 76 | private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void |
77 | 77 | { |
78 | - foreach ($groups as $group) { |
|
79 | - $registry->getGroup($group)->addMiddleware('middleware:' . $group); |
|
78 | + foreach ($groups as $group){ |
|
79 | + $registry->getGroup($group)->addMiddleware('middleware:'.$group); |
|
80 | 80 | } |
81 | 81 | } |
82 | 82 | } |
@@ -19,7 +19,8 @@ discard block |
||
19 | 19 | { |
20 | 20 | public function init(HttpBootloader $http): void |
21 | 21 | { |
22 | - foreach ($this->globalMiddleware() as $middleware) { |
|
22 | + foreach ($this->globalMiddleware() as $middleware) |
|
23 | + { |
|
23 | 24 | $http->addMiddleware($middleware); |
24 | 25 | } |
25 | 26 | } |
@@ -61,7 +62,8 @@ discard block |
||
61 | 62 | |
62 | 63 | private function registerMiddlewareGroups(BinderInterface $binder, array $groups): void |
63 | 64 | { |
64 | - foreach ($groups as $group => $middleware) { |
|
65 | + foreach ($groups as $group => $middleware) |
|
66 | + { |
|
65 | 67 | $binder |
66 | 68 | ->getBinder('http') |
67 | 69 | ->bind( |
@@ -75,7 +77,8 @@ discard block |
||
75 | 77 | |
76 | 78 | private function registerMiddlewareForRouteGroups(GroupRegistry $registry, array $groups): void |
77 | 79 | { |
78 | - foreach ($groups as $group) { |
|
80 | + foreach ($groups as $group) |
|
81 | + { |
|
79 | 82 | $registry->getGroup($group)->addMiddleware('middleware:' . $group); |
80 | 83 | } |
81 | 84 | } |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | public function __construct( |
40 | 40 | #[Proxy] private readonly ContainerInterface $container, |
41 | 41 | private readonly ?EventDispatcherInterface $dispatcher = null, |
42 | - ) { |
|
42 | + ){ |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | /** |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | * |
48 | 48 | * @param MiddlewareInterface ...$middleware List of middleware or its definition. |
49 | 49 | */ |
50 | - public function withAddedMiddleware(MiddlewareInterface|Autowire|string ...$middleware): self |
|
50 | + public function withAddedMiddleware(MiddlewareInterface | Autowire | string ...$middleware): self |
|
51 | 51 | { |
52 | 52 | $pipeline = clone $this; |
53 | 53 | $pipeline->middleware = \array_merge($pipeline->middleware, $middleware); |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @param MiddlewareInterface ...$middleware List of middleware or its definition. |
61 | 61 | */ |
62 | - public function withMiddleware(MiddlewareInterface|Autowire|string ...$middleware): self |
|
62 | + public function withMiddleware(MiddlewareInterface | Autowire | string ...$middleware): self |
|
63 | 63 | { |
64 | 64 | $pipeline = clone $this; |
65 | 65 | $pipeline->middleware = $middleware; |
@@ -92,9 +92,9 @@ discard block |
||
92 | 92 | |
93 | 93 | $previousRequest = $currentRequest->get(); |
94 | 94 | $currentRequest->set($request); |
95 | - try { |
|
95 | + try{ |
|
96 | 96 | // There is no middleware to process, let's pass the request to the handler |
97 | - if (!\array_key_exists($this->position, $this->middleware)) { |
|
97 | + if (!\array_key_exists($this->position, $this->middleware)){ |
|
98 | 98 | return $this->handler->handle($request); |
99 | 99 | } |
100 | 100 | |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | ? \sprintf('%s=%s', $this->middleware[$this->position], $middleware::class) |
109 | 109 | : $middleware::class; |
110 | 110 | // Init a tracing span when the pipeline starts |
111 | - if ($span === null) { |
|
111 | + if ($span === null){ |
|
112 | 112 | /** @var TracerInterface $tracer */ |
113 | 113 | $tracer = $this->container->get(TracerInterface::class); |
114 | 114 | return $tracer->trace( |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $span->setAttribute('http.middleware', $middlewares); |
127 | 127 | |
128 | 128 | return $middleware->process($request, $this->next($span)); |
129 | - } finally { |
|
129 | + }finally{ |
|
130 | 130 | $currentRequest->set($previousRequest); |
131 | 131 | } |
132 | 132 | } |
@@ -92,9 +92,11 @@ discard block |
||
92 | 92 | |
93 | 93 | $previousRequest = $currentRequest->get(); |
94 | 94 | $currentRequest->set($request); |
95 | - try { |
|
95 | + try |
|
96 | + { |
|
96 | 97 | // There is no middleware to process, let's pass the request to the handler |
97 | - if (!\array_key_exists($this->position, $this->middleware)) { |
|
98 | + if (!\array_key_exists($this->position, $this->middleware)) |
|
99 | + { |
|
98 | 100 | return $this->handler->handle($request); |
99 | 101 | } |
100 | 102 | |
@@ -108,7 +110,8 @@ discard block |
||
108 | 110 | ? \sprintf('%s=%s', $this->middleware[$this->position], $middleware::class) |
109 | 111 | : $middleware::class; |
110 | 112 | // Init a tracing span when the pipeline starts |
111 | - if ($span === null) { |
|
113 | + if ($span === null) |
|
114 | + { |
|
112 | 115 | /** @var TracerInterface $tracer */ |
113 | 116 | $tracer = $this->container->get(TracerInterface::class); |
114 | 117 | return $tracer->trace( |
@@ -126,7 +129,9 @@ discard block |
||
126 | 129 | $span->setAttribute('http.middleware', $middlewares); |
127 | 130 | |
128 | 131 | return $middleware->process($request, $this->next($span)); |
129 | - } finally { |
|
132 | + } |
|
133 | + finally |
|
134 | + { |
|
130 | 135 | $currentRequest->set($previousRequest); |
131 | 136 | } |
132 | 137 | } |
@@ -25,21 +25,21 @@ discard block |
||
25 | 25 | { |
26 | 26 | private ?RequestHandlerInterface $handler = null; |
27 | 27 | private readonly TracerFactoryInterface $tracerFactory; |
28 | - private readonly Pipeline|LazyPipeline $pipeline; |
|
28 | + private readonly Pipeline | LazyPipeline $pipeline; |
|
29 | 29 | |
30 | 30 | public function __construct( |
31 | 31 | private readonly HttpConfig $config, |
32 | - Pipeline|LazyPipeline $pipeline, |
|
32 | + Pipeline | LazyPipeline $pipeline, |
|
33 | 33 | private readonly ResponseFactoryInterface $responseFactory, |
34 | 34 | private readonly ContainerInterface $container, |
35 | 35 | ?TracerFactoryInterface $tracerFactory = null, |
36 | 36 | private readonly ?EventDispatcherInterface $dispatcher = null, |
37 | - ) { |
|
38 | - if ($pipeline instanceof Pipeline) { |
|
39 | - foreach ($this->config->getMiddleware() as $middleware) { |
|
37 | + ){ |
|
38 | + if ($pipeline instanceof Pipeline){ |
|
39 | + foreach ($this->config->getMiddleware() as $middleware){ |
|
40 | 40 | $pipeline->pushMiddleware($this->container->get($middleware)); |
41 | 41 | } |
42 | - } else { |
|
42 | + }else{ |
|
43 | 43 | $pipeline = $pipeline->withAddedMiddleware( |
44 | 44 | ...$this->config->getMiddleware() |
45 | 45 | ); |
@@ -53,12 +53,12 @@ discard block |
||
53 | 53 | /** |
54 | 54 | * @internal |
55 | 55 | */ |
56 | - public function getPipeline(): Pipeline|LazyPipeline |
|
56 | + public function getPipeline(): Pipeline | LazyPipeline |
|
57 | 57 | { |
58 | 58 | return $this->pipeline; |
59 | 59 | } |
60 | 60 | |
61 | - public function setHandler(callable|RequestHandlerInterface $handler): self |
|
61 | + public function setHandler(callable | RequestHandlerInterface $handler): self |
|
62 | 62 | { |
63 | 63 | $this->handler = $handler instanceof RequestHandlerInterface |
64 | 64 | ? $handler |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | |
78 | 78 | $this->dispatcher?->dispatch(new RequestReceived($request)); |
79 | 79 | |
80 | - if ($this->handler === null) { |
|
80 | + if ($this->handler === null){ |
|
81 | 81 | throw new HttpException('Unable to run HttpCore, no handler is set.'); |
82 | 82 | } |
83 | 83 | |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | callback: $callback, |
108 | 108 | attributes: [ |
109 | 109 | 'http.method' => $request->getMethod(), |
110 | - 'http.url' => (string) $request->getUri(), |
|
110 | + 'http.url' => (string)$request->getUri(), |
|
111 | 111 | 'http.headers' => \array_map( |
112 | 112 | static fn (array $values): string => \implode(',', $values), |
113 | 113 | $request->getHeaders(), |
@@ -117,7 +117,7 @@ discard block |
||
117 | 117 | traceKind: TraceKind::SERVER, |
118 | 118 | ); |
119 | 119 | |
120 | - foreach ($tracer->getContext() as $key => $value) { |
|
120 | + foreach ($tracer->getContext() as $key => $value){ |
|
121 | 121 | $response = $response->withHeader($key, $value); |
122 | 122 | } |
123 | 123 |
@@ -35,11 +35,15 @@ discard block |
||
35 | 35 | ?TracerFactoryInterface $tracerFactory = null, |
36 | 36 | private readonly ?EventDispatcherInterface $dispatcher = null, |
37 | 37 | ) { |
38 | - if ($pipeline instanceof Pipeline) { |
|
39 | - foreach ($this->config->getMiddleware() as $middleware) { |
|
38 | + if ($pipeline instanceof Pipeline) |
|
39 | + { |
|
40 | + foreach ($this->config->getMiddleware() as $middleware) |
|
41 | + { |
|
40 | 42 | $pipeline->pushMiddleware($this->container->get($middleware)); |
41 | 43 | } |
42 | - } else { |
|
44 | + } |
|
45 | + else |
|
46 | + { |
|
43 | 47 | $pipeline = $pipeline->withAddedMiddleware( |
44 | 48 | ...$this->config->getMiddleware() |
45 | 49 | ); |
@@ -77,7 +81,8 @@ discard block |
||
77 | 81 | |
78 | 82 | $this->dispatcher?->dispatch(new RequestReceived($request)); |
79 | 83 | |
80 | - if ($this->handler === null) { |
|
84 | + if ($this->handler === null) |
|
85 | + { |
|
81 | 86 | throw new HttpException('Unable to run HttpCore, no handler is set.'); |
82 | 87 | } |
83 | 88 | |
@@ -117,7 +122,8 @@ discard block |
||
117 | 122 | traceKind: TraceKind::SERVER, |
118 | 123 | ); |
119 | 124 | |
120 | - foreach ($tracer->getContext() as $key => $value) { |
|
125 | + foreach ($tracer->getContext() as $key => $value) |
|
126 | + { |
|
121 | 127 | $response = $response->withHeader($key, $value); |
122 | 128 | } |
123 | 129 |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | { |
21 | 21 | use ContainerTrait; |
22 | 22 | |
23 | - protected Pipeline|LazyPipeline|null $pipeline = null; |
|
23 | + protected Pipeline | LazyPipeline | null $pipeline = null; |
|
24 | 24 | |
25 | 25 | /** @psalm-var array<array-key, MiddlewareType> */ |
26 | 26 | protected array $middleware = []; |
@@ -44,16 +44,16 @@ discard block |
||
44 | 44 | $route = clone $this; |
45 | 45 | |
46 | 46 | // array fallback |
47 | - if (\count($middleware) === 1 && \is_array($middleware[0])) { |
|
47 | + if (\count($middleware) === 1 && \is_array($middleware[0])){ |
|
48 | 48 | $middleware = $middleware[0]; |
49 | 49 | } |
50 | 50 | |
51 | 51 | /** @var MiddlewareType[] $middleware */ |
52 | - foreach ($middleware as $item) { |
|
52 | + foreach ($middleware as $item){ |
|
53 | 53 | $route->middleware[] = $item; |
54 | 54 | } |
55 | 55 | |
56 | - if ($route->pipeline !== null) { |
|
56 | + if ($route->pipeline !== null){ |
|
57 | 57 | $route->pipeline = $route->makeLazyPipeline(); |
58 | 58 | } |
59 | 59 | |
@@ -80,11 +80,11 @@ discard block |
||
80 | 80 | protected function makePipeline(): Pipeline |
81 | 81 | { |
82 | 82 | \assert($this->container !== null); |
83 | - try { |
|
83 | + try{ |
|
84 | 84 | return $this->container |
85 | 85 | ->get(PipelineFactory::class) |
86 | 86 | ->createWithMiddleware($this->middleware); |
87 | - } catch (ContainerExceptionInterface $e) { |
|
87 | + }catch (ContainerExceptionInterface $e){ |
|
88 | 88 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
89 | 89 | } |
90 | 90 | } |
@@ -97,11 +97,11 @@ discard block |
||
97 | 97 | protected function makeLazyPipeline(): LazyPipeline |
98 | 98 | { |
99 | 99 | \assert($this->container !== null); |
100 | - try { |
|
100 | + try{ |
|
101 | 101 | /** @var LazyPipeline $pipeline */ |
102 | 102 | $pipeline = $this->container->get(LazyPipeline::class); |
103 | 103 | return $pipeline->withMiddleware(...$this->middleware); |
104 | - } catch (ContainerExceptionInterface $e) { |
|
104 | + }catch (ContainerExceptionInterface $e){ |
|
105 | 105 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
106 | 106 | } |
107 | 107 | } |
@@ -44,16 +44,19 @@ discard block |
||
44 | 44 | $route = clone $this; |
45 | 45 | |
46 | 46 | // array fallback |
47 | - if (\count($middleware) === 1 && \is_array($middleware[0])) { |
|
47 | + if (\count($middleware) === 1 && \is_array($middleware[0])) |
|
48 | + { |
|
48 | 49 | $middleware = $middleware[0]; |
49 | 50 | } |
50 | 51 | |
51 | 52 | /** @var MiddlewareType[] $middleware */ |
52 | - foreach ($middleware as $item) { |
|
53 | + foreach ($middleware as $item) |
|
54 | + { |
|
53 | 55 | $route->middleware[] = $item; |
54 | 56 | } |
55 | 57 | |
56 | - if ($route->pipeline !== null) { |
|
58 | + if ($route->pipeline !== null) |
|
59 | + { |
|
57 | 60 | $route->pipeline = $route->makeLazyPipeline(); |
58 | 61 | } |
59 | 62 | |
@@ -80,11 +83,14 @@ discard block |
||
80 | 83 | protected function makePipeline(): Pipeline |
81 | 84 | { |
82 | 85 | \assert($this->container !== null); |
83 | - try { |
|
86 | + try |
|
87 | + { |
|
84 | 88 | return $this->container |
85 | 89 | ->get(PipelineFactory::class) |
86 | 90 | ->createWithMiddleware($this->middleware); |
87 | - } catch (ContainerExceptionInterface $e) { |
|
91 | + } |
|
92 | + catch (ContainerExceptionInterface $e) |
|
93 | + { |
|
88 | 94 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
89 | 95 | } |
90 | 96 | } |
@@ -97,11 +103,14 @@ discard block |
||
97 | 103 | protected function makeLazyPipeline(): LazyPipeline |
98 | 104 | { |
99 | 105 | \assert($this->container !== null); |
100 | - try { |
|
106 | + try |
|
107 | + { |
|
101 | 108 | /** @var LazyPipeline $pipeline */ |
102 | 109 | $pipeline = $this->container->get(LazyPipeline::class); |
103 | 110 | return $pipeline->withMiddleware(...$this->middleware); |
104 | - } catch (ContainerExceptionInterface $e) { |
|
111 | + } |
|
112 | + catch (ContainerExceptionInterface $e) |
|
113 | + { |
|
105 | 114 | throw new RouteException($e->getMessage(), $e->getCode(), $e); |
106 | 115 | } |
107 | 116 | } |