@@ -49,7 +49,8 @@ |
||
| 49 | 49 | */ |
| 50 | 50 | public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
| 51 | 51 | { |
| 52 | - if ($id instanceof Autowire) { |
|
| 52 | + if ($id instanceof Autowire) |
|
| 53 | + { |
|
| 53 | 54 | return $id->resolve($this->factory); |
| 54 | 55 | } |
| 55 | 56 | |
@@ -68,17 +68,20 @@ discard block |
||
| 68 | 68 | */ |
| 69 | 69 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
| 70 | 70 | { |
| 71 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 71 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
| 72 | + { |
|
| 72 | 73 | return $this->state->singletons[$alias]; |
| 73 | 74 | } |
| 74 | 75 | |
| 75 | 76 | $binding = $this->state->bindings[$alias] ?? null; |
| 76 | 77 | |
| 77 | - if ($binding === null) { |
|
| 78 | + if ($binding === null) |
|
| 79 | + { |
|
| 78 | 80 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 79 | 81 | } |
| 80 | 82 | |
| 81 | - try { |
|
| 83 | + try |
|
| 84 | + { |
|
| 82 | 85 | $this->tracer->push( |
| 83 | 86 | false, |
| 84 | 87 | action: 'resolve from binding', |
@@ -106,7 +109,9 @@ discard block |
||
| 106 | 109 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 107 | 110 | default => $binding, |
| 108 | 111 | }; |
| 109 | - } finally { |
|
| 112 | + } |
|
| 113 | + finally |
|
| 114 | + { |
|
| 110 | 115 | $this->state->bindings[$alias] ??= $binding; |
| 111 | 116 | $this->tracer->pop(true); |
| 112 | 117 | $this->tracer->pop(false); |
@@ -116,18 +121,23 @@ discard block |
||
| 116 | 121 | private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments) |
| 117 | 122 | { |
| 118 | 123 | $context = $ctx->context; |
| 119 | - try { |
|
| 124 | + try |
|
| 125 | + { |
|
| 120 | 126 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 121 | - } catch (\ReflectionException $e) { |
|
| 127 | + } |
|
| 128 | + catch (\ReflectionException $e) |
|
| 129 | + { |
|
| 122 | 130 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 123 | 131 | } |
| 124 | 132 | |
| 125 | 133 | $injector = $binding->injector; |
| 126 | 134 | |
| 127 | - try { |
|
| 135 | + try |
|
| 136 | + { |
|
| 128 | 137 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 129 | 138 | |
| 130 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 139 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 140 | + { |
|
| 131 | 141 | throw new InjectionException( |
| 132 | 142 | \sprintf( |
| 133 | 143 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -155,7 +165,8 @@ discard block |
||
| 155 | 165 | default => (string)$context, |
| 156 | 166 | }); |
| 157 | 167 | |
| 158 | - if (!$reflection->isInstance($instance)) { |
|
| 168 | + if (!$reflection->isInstance($instance)) |
|
| 169 | + { |
|
| 159 | 170 | throw new InjectionException( |
| 160 | 171 | \sprintf( |
| 161 | 172 | "Invalid injection response for '%s'.", |
@@ -165,7 +176,9 @@ discard block |
||
| 165 | 176 | } |
| 166 | 177 | |
| 167 | 178 | return $instance; |
| 168 | - } finally { |
|
| 179 | + } |
|
| 180 | + finally |
|
| 181 | + { |
|
| 169 | 182 | $this->state->bindings[$reflection->getName()] ??= $binding; |
| 170 | 183 | } |
| 171 | 184 | } |
@@ -184,7 +197,8 @@ discard block |
||
| 184 | 197 | //Binding is pointing to something else |
| 185 | 198 | : $this->make($binding->alias, $arguments, $context); |
| 186 | 199 | |
| 187 | - if ($binding->singleton && $arguments === []) { |
|
| 200 | + if ($binding->singleton && $arguments === []) |
|
| 201 | + { |
|
| 188 | 202 | $this->state->singletons[$alias] = $result; |
| 189 | 203 | } |
| 190 | 204 | |
@@ -225,11 +239,14 @@ discard block |
||
| 225 | 239 | array $arguments, |
| 226 | 240 | ): mixed { |
| 227 | 241 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
| 228 | - try { |
|
| 242 | + try |
|
| 243 | + { |
|
| 229 | 244 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
| 230 | 245 | ? ($binding->factory)() |
| 231 | 246 | : $this->invoker->invoke($binding->factory, $arguments); |
| 232 | - } catch (NotCallableException $e) { |
|
| 247 | + } |
|
| 248 | + catch (NotCallableException $e) |
|
| 249 | + { |
|
| 233 | 250 | throw new ContainerException( |
| 234 | 251 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 235 | 252 | $e->getCode(), |
@@ -248,19 +265,24 @@ discard block |
||
| 248 | 265 | ): ?object { |
| 249 | 266 | $avoidCache = $arguments !== []; |
| 250 | 267 | |
| 251 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 252 | - try { |
|
| 268 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 269 | + { |
|
| 270 | + try |
|
| 271 | + { |
|
| 253 | 272 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 254 | 273 | |
| 255 | 274 | $object = $this->createInstance( |
| 256 | 275 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 257 | 276 | $arguments, |
| 258 | 277 | ); |
| 259 | - if ($avoidCache) { |
|
| 278 | + if ($avoidCache) |
|
| 279 | + { |
|
| 260 | 280 | return $object; |
| 261 | 281 | } |
| 262 | 282 | $binding->reference = WeakReference::create($object); |
| 263 | - } catch (\Throwable) { |
|
| 283 | + } |
|
| 284 | + catch (\Throwable) |
|
| 285 | + { |
|
| 264 | 286 | throw new ContainerException( |
| 265 | 287 | $this->tracer->combineTraceMessage( |
| 266 | 288 | \sprintf( |
@@ -270,7 +292,9 @@ discard block |
||
| 270 | 292 | ) |
| 271 | 293 | ) |
| 272 | 294 | ); |
| 273 | - } finally { |
|
| 295 | + } |
|
| 296 | + finally |
|
| 297 | + { |
|
| 274 | 298 | $this->tracer->pop(); |
| 275 | 299 | } |
| 276 | 300 | } |
@@ -285,18 +309,25 @@ discard block |
||
| 285 | 309 | ): mixed { |
| 286 | 310 | $parent = $this->scope->getParent(); |
| 287 | 311 | |
| 288 | - if ($parent !== null) { |
|
| 289 | - try { |
|
| 312 | + if ($parent !== null) |
|
| 313 | + { |
|
| 314 | + try |
|
| 315 | + { |
|
| 290 | 316 | $this->tracer->push(false, ...[ |
| 291 | 317 | 'current scope' => $this->scope->getScopeName(), |
| 292 | 318 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 293 | 319 | ]); |
| 294 | 320 | return $parent->make($alias, $parameters, $context); |
| 295 | - } catch (BadScopeException $e) { |
|
| 296 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 321 | + } |
|
| 322 | + catch (BadScopeException $e) |
|
| 323 | + { |
|
| 324 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 325 | + { |
|
| 297 | 326 | throw $e; |
| 298 | 327 | } |
| 299 | - } catch (ContainerExceptionInterface $e) { |
|
| 328 | + } |
|
| 329 | + catch (ContainerExceptionInterface $e) |
|
| 330 | + { |
|
| 300 | 331 | $className = match (true) { |
| 301 | 332 | $e instanceof NotFoundException => NotFoundException::class, |
| 302 | 333 | default => ContainerException::class, |
@@ -305,19 +336,24 @@ discard block |
||
| 305 | 336 | 'Can\'t resolve `%s`.', |
| 306 | 337 | $alias, |
| 307 | 338 | )), previous: $e); |
| 308 | - } finally { |
|
| 339 | + } |
|
| 340 | + finally |
|
| 341 | + { |
|
| 309 | 342 | $this->tracer->pop(false); |
| 310 | 343 | } |
| 311 | 344 | } |
| 312 | 345 | |
| 313 | 346 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 314 | - try { |
|
| 347 | + try |
|
| 348 | + { |
|
| 315 | 349 | //No direct instructions how to construct class, make is automatically |
| 316 | 350 | return $this->autowire( |
| 317 | 351 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 318 | 352 | $parameters, |
| 319 | 353 | ); |
| 320 | - } finally { |
|
| 354 | + } |
|
| 355 | + finally |
|
| 356 | + { |
|
| 321 | 357 | $this->tracer->pop(false); |
| 322 | 358 | } |
| 323 | 359 | } |
@@ -367,7 +403,8 @@ discard block |
||
| 367 | 403 | // Check scope name |
| 368 | 404 | $ctx->reflection = new \ReflectionClass($instance); |
| 369 | 405 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 370 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 406 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
| 407 | + { |
|
| 371 | 408 | throw new BadScopeException($scopeName, $instance::class); |
| 372 | 409 | } |
| 373 | 410 | |
@@ -394,24 +431,30 @@ discard block |
||
| 394 | 431 | array $parameters, |
| 395 | 432 | ): object { |
| 396 | 433 | $class = $ctx->class; |
| 397 | - try { |
|
| 434 | + try |
|
| 435 | + { |
|
| 398 | 436 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 399 | - } catch (\ReflectionException $e) { |
|
| 437 | + } |
|
| 438 | + catch (\ReflectionException $e) |
|
| 439 | + { |
|
| 400 | 440 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 401 | 441 | } |
| 402 | 442 | |
| 403 | 443 | // Check scope name |
| 404 | 444 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 405 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 445 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 446 | + { |
|
| 406 | 447 | throw new BadScopeException($scope, $class); |
| 407 | 448 | } |
| 408 | 449 | |
| 409 | 450 | //We have to construct class using external injector when we know exact context |
| 410 | - if ($this->binder->hasInjector($class)) { |
|
| 451 | + if ($this->binder->hasInjector($class)) |
|
| 452 | + { |
|
| 411 | 453 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $parameters); |
| 412 | 454 | } |
| 413 | 455 | |
| 414 | - if (!$reflection->isInstantiable()) { |
|
| 456 | + if (!$reflection->isInstantiable()) |
|
| 457 | + { |
|
| 415 | 458 | $itIs = match (true) { |
| 416 | 459 | $reflection->isEnum() => 'Enum', |
| 417 | 460 | $reflection->isAbstract() => 'Abstract class', |
@@ -424,12 +467,16 @@ discard block |
||
| 424 | 467 | |
| 425 | 468 | $constructor = $reflection->getConstructor(); |
| 426 | 469 | |
| 427 | - if ($constructor !== null) { |
|
| 428 | - try { |
|
| 470 | + if ($constructor !== null) |
|
| 471 | + { |
|
| 472 | + try |
|
| 473 | + { |
|
| 429 | 474 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 430 | 475 | $this->tracer->push(true); |
| 431 | 476 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 432 | - } catch (ValidationException $e) { |
|
| 477 | + } |
|
| 478 | + catch (ValidationException $e) |
|
| 479 | + { |
|
| 433 | 480 | throw new ContainerException( |
| 434 | 481 | $this->tracer->combineTraceMessage( |
| 435 | 482 | \sprintf( |
@@ -439,22 +486,31 @@ discard block |
||
| 439 | 486 | ) |
| 440 | 487 | ), |
| 441 | 488 | ); |
| 442 | - } finally { |
|
| 489 | + } |
|
| 490 | + finally |
|
| 491 | + { |
|
| 443 | 492 | $this->tracer->pop(true); |
| 444 | 493 | $this->tracer->pop(false); |
| 445 | 494 | } |
| 446 | - try { |
|
| 495 | + try |
|
| 496 | + { |
|
| 447 | 497 | // Using constructor with resolved arguments |
| 448 | 498 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 449 | 499 | $this->tracer->push(true); |
| 450 | 500 | $instance = new $class(...$arguments); |
| 451 | - } catch (\TypeError $e) { |
|
| 501 | + } |
|
| 502 | + catch (\TypeError $e) |
|
| 503 | + { |
|
| 452 | 504 | throw new WrongTypeException($constructor, $e); |
| 453 | - } finally { |
|
| 505 | + } |
|
| 506 | + finally |
|
| 507 | + { |
|
| 454 | 508 | $this->tracer->pop(true); |
| 455 | 509 | $this->tracer->pop(false); |
| 456 | 510 | } |
| 457 | - } else { |
|
| 511 | + } |
|
| 512 | + else |
|
| 513 | + { |
|
| 458 | 514 | // No constructor specified |
| 459 | 515 | $instance = $reflection->newInstance(); |
| 460 | 516 | } |
@@ -472,13 +528,15 @@ discard block |
||
| 472 | 528 | $instance = $this->runInflector($instance); |
| 473 | 529 | |
| 474 | 530 | //Declarative singletons |
| 475 | - if ($this->isSingleton($ctx)) { |
|
| 531 | + if ($this->isSingleton($ctx)) |
|
| 532 | + { |
|
| 476 | 533 | $this->state->singletons[$ctx->alias] = $instance; |
| 477 | 534 | } |
| 478 | 535 | |
| 479 | 536 | // Register finalizer |
| 480 | 537 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 481 | - if ($finalizer !== null) { |
|
| 538 | + if ($finalizer !== null) |
|
| 539 | + { |
|
| 482 | 540 | $this->state->finalizers[] = $finalizer; |
| 483 | 541 | } |
| 484 | 542 | |
@@ -490,12 +548,14 @@ discard block |
||
| 490 | 548 | */ |
| 491 | 549 | private function isSingleton(Ctx $ctx): bool |
| 492 | 550 | { |
| 493 | - if ($ctx->singleton === true) { |
|
| 551 | + if ($ctx->singleton === true) |
|
| 552 | + { |
|
| 494 | 553 | return true; |
| 495 | 554 | } |
| 496 | 555 | |
| 497 | 556 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 498 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 557 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 558 | + { |
|
| 499 | 559 | return true; |
| 500 | 560 | } |
| 501 | 561 | |
@@ -509,7 +569,8 @@ discard block |
||
| 509 | 569 | * @var Finalize|null $attribute |
| 510 | 570 | */ |
| 511 | 571 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 512 | - if ($attribute === null) { |
|
| 572 | + if ($attribute === null) |
|
| 573 | + { |
|
| 513 | 574 | return null; |
| 514 | 575 | } |
| 515 | 576 | |
@@ -523,10 +584,14 @@ discard block |
||
| 523 | 584 | { |
| 524 | 585 | $scope = $this->scope; |
| 525 | 586 | |
| 526 | - while ($scope !== null) { |
|
| 527 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 528 | - if ($instance instanceof $class) { |
|
| 529 | - foreach ($inflectors as $inflector) { |
|
| 587 | + while ($scope !== null) |
|
| 588 | + { |
|
| 589 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 590 | + { |
|
| 591 | + if ($instance instanceof $class) |
|
| 592 | + { |
|
| 593 | + foreach ($inflectors as $inflector) |
|
| 594 | + { |
|
| 530 | 595 | $instance = $inflector->getParametersCount() > 1 |
| 531 | 596 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 532 | 597 | : ($inflector->inflector)($instance); |
@@ -542,9 +607,12 @@ discard block |
||
| 542 | 607 | |
| 543 | 608 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 544 | 609 | { |
| 545 | - try { |
|
| 610 | + try |
|
| 611 | + { |
|
| 546 | 612 | $this->resolver->validateArguments($reflection, $arguments); |
| 547 | - } catch (\Throwable) { |
|
| 613 | + } |
|
| 614 | + catch (\Throwable) |
|
| 615 | + { |
|
| 548 | 616 | return false; |
| 549 | 617 | } |
| 550 | 618 | |
@@ -72,22 +72,28 @@ |
||
| 72 | 72 | |
| 73 | 73 | public static function provideRenderMethod(): iterable |
| 74 | 74 | { |
| 75 | - $class = new class { |
|
| 75 | + $class = new class |
|
| 76 | + { |
|
| 76 | 77 | public const INT_CONST = 42; |
| 77 | 78 | |
| 78 | 79 | #[ExpectedAttribute('public function test1(...$variadic)')] |
| 79 | - public function test1(...$variadic) {} |
|
| 80 | + public function test1(...$variadic) |
|
| 81 | + { |
|
| 82 | +} |
|
| 80 | 83 | #[ExpectedAttribute('public function test2(string|int $string = self::INT_CONST): string|int')] |
| 81 | 84 | public function test2(string|int $string = self::INT_CONST): string|int {} |
| 82 | 85 | #[ExpectedAttribute('public function test3(object $obj = new \stdClass(new \stdClass(), new \stdClass()))')] |
| 83 | - public function test3(object $obj = new stdClass(new stdClass(), new stdClass())) {} |
|
| 86 | + public function test3(object $obj = new stdClass(new stdClass(), new stdClass())) |
|
| 87 | + { |
|
| 88 | +} |
|
| 84 | 89 | #[ExpectedAttribute('public function test4(): \\' . ProxyClassRendererTest::class)] |
| 85 | 90 | public function test4(): ProxyClassRendererTest {} |
| 86 | 91 | #[ExpectedAttribute('public function &test5(): string')] |
| 87 | 92 | public function &test5(): string {} |
| 88 | 93 | }; |
| 89 | 94 | |
| 90 | - foreach ((new \ReflectionClass($class))->getMethods() as $method) { |
|
| 95 | + foreach ((new \ReflectionClass($class))->getMethods() as $method) |
|
| 96 | + { |
|
| 91 | 97 | $expected = $method->getAttributes(ExpectedAttribute::class)[0]->newInstance(); |
| 92 | 98 | |
| 93 | 99 | yield [$method, $expected->value]; |
@@ -28,7 +28,8 @@ discard block |
||
| 28 | 28 | $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class); |
| 29 | 29 | $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class); |
| 30 | 30 | |
| 31 | - $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) { |
|
| 31 | + $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) |
|
| 32 | + { |
|
| 32 | 33 | /** @var MockInterfaceImpl $proxy */ |
| 33 | 34 | $proxy = $$var; |
| 34 | 35 | $proxy->bar(name: 'foo'); // Possible to run |
@@ -47,7 +48,8 @@ discard block |
||
| 47 | 48 | $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class); |
| 48 | 49 | $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class); |
| 49 | 50 | |
| 50 | - $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) { |
|
| 51 | + $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) |
|
| 52 | + { |
|
| 51 | 53 | /** @var MockInterfaceImpl $proxy */ |
| 52 | 54 | $proxy = $$var; |
| 53 | 55 | self::assertSame(['foo', 'bar', 'baz', 69], $proxy->extra('foo', 'bar', 'baz', 69)); |
@@ -66,7 +68,8 @@ discard block |
||
| 66 | 68 | $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class); |
| 67 | 69 | $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class); |
| 68 | 70 | |
| 69 | - $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) { |
|
| 71 | + $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) |
|
| 72 | + { |
|
| 70 | 73 | /** @var MockInterfaceImpl $proxy */ |
| 71 | 74 | $proxy = $$var; |
| 72 | 75 | self::assertSame(['foo', 'bar', 'baz', 69], $proxy->extraVariadic('foo', 'bar', 'baz', 69)); |
@@ -90,7 +93,8 @@ discard block |
||
| 90 | 93 | $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class); |
| 91 | 94 | $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class); |
| 92 | 95 | |
| 93 | - $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) { |
|
| 96 | + $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) |
|
| 97 | + { |
|
| 94 | 98 | /** @var MockInterfaceImpl $proxy */ |
| 95 | 99 | $proxy = $$var; |
| 96 | 100 | $str = 'bar'; |
@@ -112,7 +116,8 @@ discard block |
||
| 112 | 116 | $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class); |
| 113 | 117 | $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class); |
| 114 | 118 | |
| 115 | - $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) { |
|
| 119 | + $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) |
|
| 120 | + { |
|
| 116 | 121 | /** @var MockInterfaceImpl $proxy */ |
| 117 | 122 | $proxy = $$var; |
| 118 | 123 | |
@@ -137,7 +142,8 @@ discard block |
||
| 137 | 142 | $root->bindSingleton(MockInterface::class, Stub\MockInterfaceImpl::class); |
| 138 | 143 | $root->bindSingleton(EmptyInterface::class, Stub\MockInterfaceImpl::class); |
| 139 | 144 | |
| 140 | - $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) { |
|
| 145 | + $root->invoke(static function (#[Proxy] MockInterface $mock, #[Proxy] EmptyInterface $empty) use ($var) |
|
| 146 | + { |
|
| 141 | 147 | /** @var MockInterfaceImpl $proxy */ |
| 142 | 148 | $proxy = $$var; |
| 143 | 149 | $str1 = 'bar'; |
@@ -48,7 +48,8 @@ |
||
| 48 | 48 | |
| 49 | 49 | public function concatMultiple(string $prefix, string &...$byLink): array |
| 50 | 50 | { |
| 51 | - foreach ($byLink as $k => $link) { |
|
| 51 | + foreach ($byLink as $k => $link) |
|
| 52 | + { |
|
| 52 | 53 | $byLink[$k] = $prefix . $link; |
| 53 | 54 | unset($link); |
| 54 | 55 | } |
@@ -30,7 +30,8 @@ discard block |
||
| 30 | 30 | |
| 31 | 31 | FiberHelper::runInFiber( |
| 32 | 32 | self::functionScopedTestDataIterator(), |
| 33 | - static function (mixed $suspendValue) { |
|
| 33 | + static function (mixed $suspendValue) |
|
| 34 | + { |
|
| 34 | 35 | self::assertNull(ContainerScope::getContainer()); |
| 35 | 36 | self::assertTrue(\in_array($suspendValue, self::TEST_DATA, true)); |
| 36 | 37 | }, |
@@ -63,7 +64,8 @@ discard block |
||
| 63 | 64 | ); |
| 64 | 65 | |
| 65 | 66 | self::assertCount(5, $result); |
| 66 | - foreach ($result as $suspendValue) { |
|
| 67 | + foreach ($result as $suspendValue) |
|
| 68 | + { |
|
| 67 | 69 | self::assertSame(self::TEST_DATA, $suspendValue); |
| 68 | 70 | } |
| 69 | 71 | } |
@@ -81,7 +83,8 @@ discard block |
||
| 81 | 83 | ); |
| 82 | 84 | |
| 83 | 85 | self::assertCount(2, $result); |
| 84 | - foreach ($result as $suspendValue) { |
|
| 86 | + foreach ($result as $suspendValue) |
|
| 87 | + { |
|
| 85 | 88 | self::assertSame(self::TEST_DATA, $suspendValue); |
| 86 | 89 | } |
| 87 | 90 | } |
@@ -92,7 +95,8 @@ discard block |
||
| 92 | 95 | $this->expectExceptionMessage('test'); |
| 93 | 96 | |
| 94 | 97 | FiberHelper::runInFiber( |
| 95 | - static function () { |
|
| 98 | + static function () |
|
| 99 | + { |
|
| 96 | 100 | return (new Container())->runScoped( |
| 97 | 101 | function (): string { |
| 98 | 102 | $result = ''; |
@@ -114,15 +118,19 @@ discard block |
||
| 114 | 118 | public function testCatchThrownException(): void |
| 115 | 119 | { |
| 116 | 120 | $result = FiberHelper::runInFiber( |
| 117 | - static function () { |
|
| 121 | + static function () |
|
| 122 | + { |
|
| 118 | 123 | return (new Container())->runScoped( |
| 119 | 124 | function (): string { |
| 120 | 125 | $result = ''; |
| 121 | 126 | $result .= Fiber::suspend('foo'); |
| 122 | 127 | $result .= Fiber::suspend('bar'); |
| 123 | - try { |
|
| 128 | + try |
|
| 129 | + { |
|
| 124 | 130 | $result .= Fiber::suspend('error'); |
| 125 | - } catch (\Throwable $e) { |
|
| 131 | + } |
|
| 132 | + catch (\Throwable $e) |
|
| 133 | + { |
|
| 126 | 134 | $result .= $e->getMessage(); |
| 127 | 135 | } |
| 128 | 136 | $result .= Fiber::suspend('baz'); |
@@ -160,20 +168,23 @@ discard block |
||
| 160 | 168 | $c1 = $container ?? new Container(); |
| 161 | 169 | $c1->bindSingleton('resource', new stdClass()); |
| 162 | 170 | |
| 163 | - $result = $c1->runScoped(static function (Container $c2) use ($load) { |
|
| 171 | + $result = $c1->runScoped(static function (Container $c2) use ($load) |
|
| 172 | + { |
|
| 164 | 173 | // check local binding |
| 165 | 174 | self::assertTrue($c2->has('foo')); |
| 166 | 175 | self::assertInstanceOf(DateTime::class, $c2->get('foo')); |
| 167 | 176 | |
| 168 | 177 | return $c2->runScoped( |
| 169 | - static function (ContainerInterface $c3) use ($load) { |
|
| 178 | + static function (ContainerInterface $c3) use ($load) |
|
| 179 | + { |
|
| 170 | 180 | // check local binding |
| 171 | 181 | self::assertTrue($c3->has('bar')); |
| 172 | 182 | |
| 173 | 183 | $resource = $c3->get('resource'); |
| 174 | 184 | self::assertInstanceOf(DateTimeImmutable::class, $c3->get('bar')); |
| 175 | 185 | self::assertInstanceOf(stdClass::class, $resource); |
| 176 | - foreach (self::TEST_DATA as $key => $value) { |
|
| 186 | + foreach (self::TEST_DATA as $key => $value) |
|
| 187 | + { |
|
| 177 | 188 | $resource->$key = $value; |
| 178 | 189 | $load === null or $load(); |
| 179 | 190 | Fiber::suspend($value); |
@@ -22,11 +22,16 @@ discard block |
||
| 22 | 22 | { |
| 23 | 23 | $fiber = new Fiber($callable); |
| 24 | 24 | $value = $fiber->start(); |
| 25 | - while (!$fiber->isTerminated()) { |
|
| 26 | - if ($check !== null) { |
|
| 27 | - try { |
|
| 25 | + while (!$fiber->isTerminated()) |
|
| 26 | + { |
|
| 27 | + if ($check !== null) |
|
| 28 | + { |
|
| 29 | + try |
|
| 30 | + { |
|
| 28 | 31 | $value = $check($value); |
| 29 | - } catch (\Throwable $e) { |
|
| 32 | + } |
|
| 33 | + catch (\Throwable $e) |
|
| 34 | + { |
|
| 30 | 35 | $value = $fiber->throw($e); |
| 31 | 36 | continue; |
| 32 | 37 | } |
@@ -50,14 +55,17 @@ discard block |
||
| 50 | 55 | /** Values that were suspended by the fiber. */ |
| 51 | 56 | $suspends = []; |
| 52 | 57 | $results = []; |
| 53 | - foreach ($callables as $key => $callable) { |
|
| 54 | - $fiberGenerators[$key] = (static function () use ($callable) { |
|
| 58 | + foreach ($callables as $key => $callable) |
|
| 59 | + { |
|
| 60 | + $fiberGenerators[$key] = (static function () use ($callable) |
|
| 61 | + { |
|
| 55 | 62 | $fiber = new Fiber($callable); |
| 56 | 63 | // Get ready |
| 57 | 64 | yield null; |
| 58 | 65 | |
| 59 | 66 | $value = yield $fiber->start(); |
| 60 | - while (!$fiber->isTerminated()) { |
|
| 67 | + while (!$fiber->isTerminated()) |
|
| 68 | + { |
|
| 61 | 69 | $value = yield $fiber->resume($value); |
| 62 | 70 | } |
| 63 | 71 | return $fiber->getReturn(); |
@@ -67,15 +75,21 @@ discard block |
||
| 67 | 75 | } |
| 68 | 76 | |
| 69 | 77 | |
| 70 | - while ($fiberGenerators !== []) { |
|
| 71 | - foreach ($fiberGenerators as $key => $generator) { |
|
| 72 | - try { |
|
| 78 | + while ($fiberGenerators !== []) |
|
| 79 | + { |
|
| 80 | + foreach ($fiberGenerators as $key => $generator) |
|
| 81 | + { |
|
| 82 | + try |
|
| 83 | + { |
|
| 73 | 84 | $suspends[$key] = $generator->send($suspends[$key]); |
| 74 | - if (!$generator->valid()) { |
|
| 85 | + if (!$generator->valid()) |
|
| 86 | + { |
|
| 75 | 87 | $results[$key] = $generator->getReturn(); |
| 76 | 88 | unset($fiberGenerators[$key]); |
| 77 | 89 | } |
| 78 | - } catch (\Throwable $e) { |
|
| 90 | + } |
|
| 91 | + catch (\Throwable $e) |
|
| 92 | + { |
|
| 79 | 93 | unset($fiberGenerators[$key]); |
| 80 | 94 | $results[$key] = $e; |
| 81 | 95 | } |
@@ -28,7 +28,8 @@ discard block |
||
| 28 | 28 | $root = new Container(); |
| 29 | 29 | $root->bind('foo', SampleClass::class); |
| 30 | 30 | |
| 31 | - $root->runScoped(function (ContainerInterface $c1) { |
|
| 31 | + $root->runScoped(function (ContainerInterface $c1) |
|
| 32 | + { |
|
| 32 | 33 | $c1->get('foo'); |
| 33 | 34 | }, bindings: ['foo' => SampleClass::class]); |
| 34 | 35 | |
@@ -61,7 +62,8 @@ discard block |
||
| 61 | 62 | { |
| 62 | 63 | $root = new Container(); |
| 63 | 64 | |
| 64 | - $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) { |
|
| 65 | + $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) |
|
| 66 | + { |
|
| 65 | 67 | $obj1 = $c1->get($alias); |
| 66 | 68 | $obj2 = $c1->get($alias); |
| 67 | 69 | |
@@ -86,14 +88,16 @@ discard block |
||
| 86 | 88 | { |
| 87 | 89 | $root = new Container(); |
| 88 | 90 | |
| 89 | - $root->runScoped(function (ContainerInterface $c1) use ($root) { |
|
| 91 | + $root->runScoped(function (ContainerInterface $c1) use ($root) |
|
| 92 | + { |
|
| 90 | 93 | $obj1 = $c1->get('foo'); |
| 91 | 94 | $this->weakMap->offsetSet($obj1, true); |
| 92 | 95 | |
| 93 | 96 | self::assertNotSame($root, $c1); |
| 94 | 97 | self::assertInstanceOf(stdClass::class, $obj1); |
| 95 | 98 | |
| 96 | - $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) { |
|
| 99 | + $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) |
|
| 100 | + { |
|
| 97 | 101 | $obj2 = $c2->get('foo'); |
| 98 | 102 | $this->weakMap->offsetSet($obj2, true); |
| 99 | 103 | |
@@ -122,14 +126,16 @@ discard block |
||
| 122 | 126 | $root->bindSingleton('bar', [Factory::class, 'makeStdClass']); |
| 123 | 127 | $root->bind(stdClass::class, new stdClass()); |
| 124 | 128 | |
| 125 | - $root->runScoped(function (ContainerInterface $c1) use ($root) { |
|
| 129 | + $root->runScoped(function (ContainerInterface $c1) use ($root) |
|
| 130 | + { |
|
| 126 | 131 | $obj1 = $c1->get('foo'); |
| 127 | 132 | $this->weakMap->offsetSet($obj1, true); |
| 128 | 133 | |
| 129 | 134 | self::assertInstanceOf(stdClass::class, $obj1); |
| 130 | 135 | // Singleton must be the same |
| 131 | 136 | self::assertSame($c1->get('bar'), $root->get('bar')); |
| 132 | - $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) { |
|
| 137 | + $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) |
|
| 138 | + { |
|
| 133 | 139 | $obj2 = $c2->get('foo'); |
| 134 | 140 | |
| 135 | 141 | self::assertInstanceOf(stdClass::class, $obj2); |
@@ -262,7 +268,8 @@ discard block |
||
| 262 | 268 | { |
| 263 | 269 | $root = new Container(); |
| 264 | 270 | |
| 265 | - $root->invoke(static function () use ($root) { |
|
| 271 | + $root->invoke(static function () use ($root) |
|
| 272 | + { |
|
| 266 | 273 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
| 267 | 274 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
| 268 | 275 | }); |
@@ -271,7 +278,8 @@ discard block |
||
| 271 | 278 | public function testRegisterContainerOnGet(): void |
| 272 | 279 | { |
| 273 | 280 | $root = new Container(); |
| 274 | - $root->bind('foo', function () use ($root) { |
|
| 281 | + $root->bind('foo', function () use ($root) |
|
| 282 | + { |
|
| 275 | 283 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
| 276 | 284 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
| 277 | 285 | }); |
@@ -282,7 +290,8 @@ discard block |
||
| 282 | 290 | public function testRegisterContainerOnMake(): void |
| 283 | 291 | { |
| 284 | 292 | $root = new Container(); |
| 285 | - $root->bind('foo', function () use ($root) { |
|
| 293 | + $root->bind('foo', function () use ($root) |
|
| 294 | + { |
|
| 286 | 295 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
| 287 | 296 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
| 288 | 297 | }); |
@@ -170,23 +170,29 @@ discard block |
||
| 170 | 170 | */ |
| 171 | 171 | public function runScope(Scope|array $bindings, callable $scope): mixed |
| 172 | 172 | { |
| 173 | - if (!\is_array($bindings)) { |
|
| 173 | + if (!\is_array($bindings)) |
|
| 174 | + { |
|
| 174 | 175 | return $this->runIsolatedScope($bindings, $scope); |
| 175 | 176 | } |
| 176 | 177 | |
| 177 | 178 | $binds = &$this->state->bindings; |
| 178 | 179 | $singletons = &$this->state->singletons; |
| 179 | 180 | $cleanup = $previous = $prevSin = []; |
| 180 | - foreach ($bindings as $alias => $resolver) { |
|
| 181 | + foreach ($bindings as $alias => $resolver) |
|
| 182 | + { |
|
| 181 | 183 | // Store previous bindings |
| 182 | - if (isset($binds[$alias])) { |
|
| 184 | + if (isset($binds[$alias])) |
|
| 185 | + { |
|
| 183 | 186 | $previous[$alias] = $binds[$alias]; |
| 184 | - } else { |
|
| 187 | + } |
|
| 188 | + else |
|
| 189 | + { |
|
| 185 | 190 | // Store bindings to be removed |
| 186 | 191 | $cleanup[] = $alias; |
| 187 | 192 | } |
| 188 | 193 | // Store previous singletons |
| 189 | - if (isset($singletons[$alias])) { |
|
| 194 | + if (isset($singletons[$alias])) |
|
| 195 | + { |
|
| 190 | 196 | $prevSin[$alias] = $singletons[$alias]; |
| 191 | 197 | unset($singletons[$alias]); |
| 192 | 198 | } |
@@ -194,21 +200,27 @@ discard block |
||
| 194 | 200 | $this->binder->bind($alias, $resolver); |
| 195 | 201 | } |
| 196 | 202 | |
| 197 | - try { |
|
| 203 | + try |
|
| 204 | + { |
|
| 198 | 205 | return ContainerScope::getContainer() !== $this |
| 199 | 206 | ? ContainerScope::runScope($this, $scope) |
| 200 | 207 | : $scope($this); |
| 201 | - } finally { |
|
| 208 | + } |
|
| 209 | + finally |
|
| 210 | + { |
|
| 202 | 211 | // Remove new bindings |
| 203 | - foreach ($cleanup as $alias) { |
|
| 212 | + foreach ($cleanup as $alias) |
|
| 213 | + { |
|
| 204 | 214 | unset($binds[$alias], $singletons[$alias]); |
| 205 | 215 | } |
| 206 | 216 | // Restore previous bindings |
| 207 | - foreach ($previous as $alias => $resolver) { |
|
| 217 | + foreach ($previous as $alias => $resolver) |
|
| 218 | + { |
|
| 208 | 219 | $binds[$alias] = $resolver; |
| 209 | 220 | } |
| 210 | 221 | // Restore singletons |
| 211 | - foreach ($prevSin as $alias => $instance) { |
|
| 222 | + foreach ($prevSin as $alias => $instance) |
|
| 223 | + { |
|
| 212 | 224 | $singletons[$alias] = $instance; |
| 213 | 225 | } |
| 214 | 226 | } |
@@ -265,7 +277,8 @@ discard block |
||
| 265 | 277 | */ |
| 266 | 278 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
| 267 | 279 | { |
| 268 | - if ($force) { |
|
| 280 | + if ($force) |
|
| 281 | + { |
|
| 269 | 282 | $this->binder->removeBinding($alias); |
| 270 | 283 | } |
| 271 | 284 | |
@@ -336,8 +349,10 @@ discard block |
||
| 336 | 349 | ]); |
| 337 | 350 | |
| 338 | 351 | // Create container services |
| 339 | - foreach ($container->config as $property => $class) { |
|
| 340 | - if (\property_exists($container, $property)) { |
|
| 352 | + foreach ($container->config as $property => $class) |
|
| 353 | + { |
|
| 354 | + if (\property_exists($container, $property)) |
|
| 355 | + { |
|
| 341 | 356 | $container->$property = $constructor->get($property, $class); |
| 342 | 357 | } |
| 343 | 358 | } |
@@ -351,7 +366,8 @@ discard block |
||
| 351 | 366 | private function closeScope(): void |
| 352 | 367 | { |
| 353 | 368 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 354 | - if (!isset($this->scope)) { |
|
| 369 | + if (!isset($this->scope)) |
|
| 370 | + { |
|
| 355 | 371 | $this->destruct(); |
| 356 | 372 | return; |
| 357 | 373 | } |
@@ -360,10 +376,14 @@ discard block |
||
| 360 | 376 | |
| 361 | 377 | // Run finalizers |
| 362 | 378 | $errors = []; |
| 363 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 364 | - try { |
|
| 379 | + foreach ($this->state->finalizers as $finalizer) |
|
| 380 | + { |
|
| 381 | + try |
|
| 382 | + { |
|
| 365 | 383 | $this->invoker->invoke($finalizer); |
| 366 | - } catch (\Throwable $e) { |
|
| 384 | + } |
|
| 385 | + catch (\Throwable $e) |
|
| 386 | + { |
|
| 367 | 387 | $errors[] = $e; |
| 368 | 388 | } |
| 369 | 389 | } |
@@ -372,7 +392,8 @@ discard block |
||
| 372 | 392 | $this->destruct(); |
| 373 | 393 | |
| 374 | 394 | // Throw collected errors |
| 375 | - if ($errors !== []) { |
|
| 395 | + if ($errors !== []) |
|
| 396 | + { |
|
| 376 | 397 | throw new FinalizersException($scopeName, $errors); |
| 377 | 398 | } |
| 378 | 399 | } |
@@ -394,18 +415,22 @@ discard block |
||
| 394 | 415 | $container->scope->setParent($this, $this->scope); |
| 395 | 416 | |
| 396 | 417 | // Add specific bindings |
| 397 | - foreach ($config->bindings as $alias => $resolver) { |
|
| 418 | + foreach ($config->bindings as $alias => $resolver) |
|
| 419 | + { |
|
| 398 | 420 | $container->binder->bind($alias, $resolver); |
| 399 | 421 | } |
| 400 | 422 | |
| 401 | 423 | return ContainerScope::runScope( |
| 402 | 424 | $container, |
| 403 | 425 | static function (self $container) use ($config, $closure): mixed { |
| 404 | - try { |
|
| 426 | + try |
|
| 427 | + { |
|
| 405 | 428 | return $config->autowire |
| 406 | 429 | ? $container->invoke($closure) |
| 407 | 430 | : $closure($container); |
| 408 | - } finally { |
|
| 431 | + } |
|
| 432 | + finally |
|
| 433 | + { |
|
| 409 | 434 | $container->closeScope(); |
| 410 | 435 | } |
| 411 | 436 | } |