@@ -22,11 +22,11 @@ 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 | + if ($check !== null){ |
|
| 27 | + try{ |
|
| 28 | 28 | $value = $check($value); |
| 29 | - } catch (\Throwable $e) { |
|
| 29 | + }catch (\Throwable $e){ |
|
| 30 | 30 | $value = $fiber->throw($e); |
| 31 | 31 | continue; |
| 32 | 32 | } |
@@ -50,14 +50,14 @@ discard block |
||
| 50 | 50 | /** Values that were suspended by the fiber. */ |
| 51 | 51 | $suspends = []; |
| 52 | 52 | $results = []; |
| 53 | - foreach ($callables as $key => $callable) { |
|
| 53 | + foreach ($callables as $key => $callable){ |
|
| 54 | 54 | $fiberGenerators[$key] = (static function () use ($callable) { |
| 55 | 55 | $fiber = new Fiber($callable); |
| 56 | 56 | // Get ready |
| 57 | 57 | yield null; |
| 58 | 58 | |
| 59 | 59 | $value = yield $fiber->start(); |
| 60 | - while (!$fiber->isTerminated()) { |
|
| 60 | + while (!$fiber->isTerminated()){ |
|
| 61 | 61 | $value = yield $fiber->resume($value); |
| 62 | 62 | } |
| 63 | 63 | return $fiber->getReturn(); |
@@ -67,15 +67,15 @@ discard block |
||
| 67 | 67 | } |
| 68 | 68 | |
| 69 | 69 | |
| 70 | - while ($fiberGenerators !== []) { |
|
| 71 | - foreach ($fiberGenerators as $key => $generator) { |
|
| 72 | - try { |
|
| 70 | + while ($fiberGenerators !== []){ |
|
| 71 | + foreach ($fiberGenerators as $key => $generator){ |
|
| 72 | + try{ |
|
| 73 | 73 | $suspends[$key] = $generator->send($suspends[$key]); |
| 74 | - if (!$generator->valid()) { |
|
| 74 | + if (!$generator->valid()){ |
|
| 75 | 75 | $results[$key] = $generator->getReturn(); |
| 76 | 76 | unset($fiberGenerators[$key]); |
| 77 | 77 | } |
| 78 | - } catch (\Throwable $e) { |
|
| 78 | + }catch (\Throwable $e){ |
|
| 79 | 79 | unset($fiberGenerators[$key]); |
| 80 | 80 | $results[$key] = $e; |
| 81 | 81 | } |
@@ -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 | }); |
@@ -23,6 +23,6 @@ |
||
| 23 | 23 | public readonly ?string $name = null, |
| 24 | 24 | public readonly array $bindings = [], |
| 25 | 25 | public readonly bool $autowire = true, |
| 26 | - ) { |
|
| 26 | + ){ |
|
| 27 | 27 | } |
| 28 | 28 | } |
@@ -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 | } |
@@ -47,11 +47,11 @@ discard block |
||
| 47 | 47 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
| 48 | 48 | |
| 49 | 49 | private Internal\State $state; |
| 50 | - private ResolverInterface|Internal\Resolver $resolver; |
|
| 51 | - private FactoryInterface|Internal\Factory $factory; |
|
| 52 | - private ContainerInterface|Internal\Container $container; |
|
| 53 | - private BinderInterface|Internal\Binder $binder; |
|
| 54 | - private InvokerInterface|Internal\Invoker $invoker; |
|
| 50 | + private ResolverInterface | Internal\Resolver $resolver; |
|
| 51 | + private FactoryInterface | Internal\Factory $factory; |
|
| 52 | + private ContainerInterface | Internal\Container $container; |
|
| 53 | + private BinderInterface | Internal\Binder $binder; |
|
| 54 | + private InvokerInterface | Internal\Invoker $invoker; |
|
| 55 | 55 | private Internal\Scope $scope; |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | public function __construct( |
| 61 | 61 | private Config $config = new Config(), |
| 62 | 62 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
| 63 | - ) { |
|
| 63 | + ){ |
|
| 64 | 64 | $this->initServices($this, $scopeName); |
| 65 | 65 | |
| 66 | 66 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | * @throws \Throwable |
| 113 | 113 | * @psalm-suppress TooManyArguments |
| 114 | 114 | */ |
| 115 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
| 115 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
| 116 | 116 | { |
| 117 | 117 | return ContainerScope::getContainer() === $this |
| 118 | 118 | ? $this->factory->make($alias, $parameters, $context) |
@@ -138,7 +138,7 @@ discard block |
||
| 138 | 138 | * @throws \Throwable |
| 139 | 139 | * @psalm-suppress TooManyArguments |
| 140 | 140 | */ |
| 141 | - public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
| 141 | + public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed |
|
| 142 | 142 | { |
| 143 | 143 | return ContainerScope::getContainer() === $this |
| 144 | 144 | ? $this->container->get($id, $context) |
@@ -168,25 +168,25 @@ discard block |
||
| 168 | 168 | /** |
| 169 | 169 | * @throws \Throwable |
| 170 | 170 | */ |
| 171 | - public function runScope(Scope|array $bindings, callable $scope): mixed |
|
| 171 | + public function runScope(Scope | array $bindings, callable $scope): mixed |
|
| 172 | 172 | { |
| 173 | - if (!\is_array($bindings)) { |
|
| 173 | + if (!\is_array($bindings)){ |
|
| 174 | 174 | return $this->runIsolatedScope($bindings, $scope); |
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | $binds = &$this->state->bindings; |
| 178 | 178 | $singletons = &$this->state->singletons; |
| 179 | 179 | $cleanup = $previous = $prevSin = []; |
| 180 | - foreach ($bindings as $alias => $resolver) { |
|
| 180 | + foreach ($bindings as $alias => $resolver){ |
|
| 181 | 181 | // Store previous bindings |
| 182 | - if (isset($binds[$alias])) { |
|
| 182 | + if (isset($binds[$alias])){ |
|
| 183 | 183 | $previous[$alias] = $binds[$alias]; |
| 184 | - } else { |
|
| 184 | + }else{ |
|
| 185 | 185 | // Store bindings to be removed |
| 186 | 186 | $cleanup[] = $alias; |
| 187 | 187 | } |
| 188 | 188 | // Store previous singletons |
| 189 | - if (isset($singletons[$alias])) { |
|
| 189 | + if (isset($singletons[$alias])){ |
|
| 190 | 190 | $prevSin[$alias] = $singletons[$alias]; |
| 191 | 191 | unset($singletons[$alias]); |
| 192 | 192 | } |
@@ -194,21 +194,21 @@ discard block |
||
| 194 | 194 | $this->binder->bind($alias, $resolver); |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - try { |
|
| 197 | + try{ |
|
| 198 | 198 | return ContainerScope::getContainer() !== $this |
| 199 | 199 | ? ContainerScope::runScope($this, $scope) |
| 200 | 200 | : $scope($this); |
| 201 | - } finally { |
|
| 201 | + }finally{ |
|
| 202 | 202 | // Remove new bindings |
| 203 | - foreach ($cleanup as $alias) { |
|
| 203 | + foreach ($cleanup as $alias){ |
|
| 204 | 204 | unset($binds[$alias], $singletons[$alias]); |
| 205 | 205 | } |
| 206 | 206 | // Restore previous bindings |
| 207 | - foreach ($previous as $alias => $resolver) { |
|
| 207 | + foreach ($previous as $alias => $resolver){ |
|
| 208 | 208 | $binds[$alias] = $resolver; |
| 209 | 209 | } |
| 210 | 210 | // Restore singletons |
| 211 | - foreach ($prevSin as $alias => $instance) { |
|
| 211 | + foreach ($prevSin as $alias => $instance){ |
|
| 212 | 212 | $singletons[$alias] = $instance; |
| 213 | 213 | } |
| 214 | 214 | } |
@@ -254,9 +254,9 @@ discard block |
||
| 254 | 254 | * @param bool $force If the value is false, an exception will be thrown when attempting |
| 255 | 255 | * to bind an already constructed singleton. |
| 256 | 256 | */ |
| 257 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
|
| 257 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, bool $force = true): void |
|
| 258 | 258 | { |
| 259 | - if ($force) { |
|
| 259 | + if ($force){ |
|
| 260 | 260 | $this->binder->removeBinding($alias); |
| 261 | 261 | } |
| 262 | 262 | |
@@ -327,8 +327,8 @@ discard block |
||
| 327 | 327 | ]); |
| 328 | 328 | |
| 329 | 329 | // Create container services |
| 330 | - foreach ($container->config as $property => $class) { |
|
| 331 | - if (\property_exists($container, $property)) { |
|
| 330 | + foreach ($container->config as $property => $class){ |
|
| 331 | + if (\property_exists($container, $property)){ |
|
| 332 | 332 | $container->$property = $constructor->get($property, $class); |
| 333 | 333 | } |
| 334 | 334 | } |
@@ -342,7 +342,7 @@ discard block |
||
| 342 | 342 | private function closeScope(): void |
| 343 | 343 | { |
| 344 | 344 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 345 | - if (!isset($this->scope)) { |
|
| 345 | + if (!isset($this->scope)){ |
|
| 346 | 346 | $this->destruct(); |
| 347 | 347 | return; |
| 348 | 348 | } |
@@ -351,10 +351,10 @@ discard block |
||
| 351 | 351 | |
| 352 | 352 | // Run finalizers |
| 353 | 353 | $errors = []; |
| 354 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 355 | - try { |
|
| 354 | + foreach ($this->state->finalizers as $finalizer){ |
|
| 355 | + try{ |
|
| 356 | 356 | $this->invoker->invoke($finalizer); |
| 357 | - } catch (\Throwable $e) { |
|
| 357 | + }catch (\Throwable $e){ |
|
| 358 | 358 | $errors[] = $e; |
| 359 | 359 | } |
| 360 | 360 | } |
@@ -363,7 +363,7 @@ discard block |
||
| 363 | 363 | $this->destruct(); |
| 364 | 364 | |
| 365 | 365 | // Throw collected errors |
| 366 | - if ($errors !== []) { |
|
| 366 | + if ($errors !== []){ |
|
| 367 | 367 | throw new FinalizersException($scopeName, $errors); |
| 368 | 368 | } |
| 369 | 369 | } |
@@ -385,18 +385,18 @@ discard block |
||
| 385 | 385 | $container->scope->setParent($this, $this->scope); |
| 386 | 386 | |
| 387 | 387 | // Add specific bindings |
| 388 | - foreach ($config->bindings as $alias => $resolver) { |
|
| 388 | + foreach ($config->bindings as $alias => $resolver){ |
|
| 389 | 389 | $container->binder->bind($alias, $resolver); |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | return ContainerScope::runScope( |
| 393 | 393 | $container, |
| 394 | 394 | static function (self $container) use ($config, $closure): mixed { |
| 395 | - try { |
|
| 395 | + try{ |
|
| 396 | 396 | return $config->autowire |
| 397 | 397 | ? $container->invoke($closure) |
| 398 | 398 | : $closure($container); |
| 399 | - } finally { |
|
| 399 | + }finally{ |
|
| 400 | 400 | $container->closeScope(); |
| 401 | 401 | } |
| 402 | 402 | } |
@@ -10,7 +10,7 @@ |
||
| 10 | 10 | { |
| 11 | 11 | public function __construct( |
| 12 | 12 | #[Proxy] public ContextInterface $context, |
| 13 | - ) { |
|
| 13 | + ){ |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | public function getContext(): ContextInterface |
@@ -7,11 +7,11 @@ |
||
| 7 | 7 | final class Context implements ContextInterface |
| 8 | 8 | { |
| 9 | 9 | public function __construct( |
| 10 | - public \Stringable|string|null $value, |
|
| 11 | - ) { |
|
| 10 | + public \Stringable | string | null $value, |
|
| 11 | + ){ |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | - public function getValue(): \Stringable|string|null |
|
| 14 | + public function getValue(): \Stringable | string | null |
|
| 15 | 15 | { |
| 16 | 16 | return $this->value; |
| 17 | 17 | } |
@@ -6,5 +6,5 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface ContextInterface |
| 8 | 8 | { |
| 9 | - public function getValue(): \Stringable|string|null; |
|
| 9 | + public function getValue(): \Stringable | string | null; |
|
| 10 | 10 | } |
@@ -19,19 +19,19 @@ discard block |
||
| 19 | 19 | * @param \ReflectionClass<TClass> $type |
| 20 | 20 | * @return TClass |
| 21 | 21 | */ |
| 22 | - public static function create(\ReflectionClass $type, \Stringable|string|null $context): object |
|
| 22 | + public static function create(\ReflectionClass $type, \Stringable | string | null $context): object |
|
| 23 | 23 | { |
| 24 | 24 | $interface = $type->getName(); |
| 25 | 25 | |
| 26 | - if (!\array_key_exists($interface, self::$cache)) { |
|
| 26 | + if (!\array_key_exists($interface, self::$cache)){ |
|
| 27 | 27 | /** @var class-string<TClass> $className */ |
| 28 | 28 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
| 29 | 29 | |
| 30 | - try { |
|
| 30 | + try{ |
|
| 31 | 31 | $classString = ProxyClassRenderer::renderClass($type, $className); |
| 32 | 32 | |
| 33 | 33 | eval($classString); |
| 34 | - } catch (\Throwable $e) { |
|
| 34 | + }catch (\Throwable $e){ |
|
| 35 | 35 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
| 36 | 36 | } |
| 37 | 37 | |
@@ -40,12 +40,12 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | // Store in cache without context |
| 42 | 42 | self::$cache[$interface] = $instance; |
| 43 | - } else { |
|
| 43 | + }else{ |
|
| 44 | 44 | /** @var TClass $instance */ |
| 45 | 45 | $instance = self::$cache[$interface]; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - if ($context !== null) { |
|
| 48 | + if ($context !== null){ |
|
| 49 | 49 | $instance = clone $instance; |
| 50 | 50 | (static fn() => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
| 51 | 51 | } |
@@ -23,15 +23,19 @@ discard block |
||
| 23 | 23 | { |
| 24 | 24 | $interface = $type->getName(); |
| 25 | 25 | |
| 26 | - if (!\array_key_exists($interface, self::$cache)) { |
|
| 26 | + if (!\array_key_exists($interface, self::$cache)) |
|
| 27 | + { |
|
| 27 | 28 | /** @var class-string<TClass> $className */ |
| 28 | 29 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
| 29 | 30 | |
| 30 | - try { |
|
| 31 | + try |
|
| 32 | + { |
|
| 31 | 33 | $classString = ProxyClassRenderer::renderClass($type, $className); |
| 32 | 34 | |
| 33 | 35 | eval($classString); |
| 34 | - } catch (\Throwable $e) { |
|
| 36 | + } |
|
| 37 | + catch (\Throwable $e) |
|
| 38 | + { |
|
| 35 | 39 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
| 36 | 40 | } |
| 37 | 41 | |
@@ -40,12 +44,15 @@ discard block |
||
| 40 | 44 | |
| 41 | 45 | // Store in cache without context |
| 42 | 46 | self::$cache[$interface] = $instance; |
| 43 | - } else { |
|
| 47 | + } |
|
| 48 | + else |
|
| 49 | + { |
|
| 44 | 50 | /** @var TClass $instance */ |
| 45 | 51 | $instance = self::$cache[$interface]; |
| 46 | 52 | } |
| 47 | 53 | |
| 48 | - if ($context !== null) { |
|
| 54 | + if ($context !== null) |
|
| 55 | + { |
|
| 49 | 56 | $instance = clone $instance; |
| 50 | 57 | (static fn() => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
| 51 | 58 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | ): array { |
| 54 | 54 | $state = new ResolvingState($reflection, $parameters); |
| 55 | 55 | |
| 56 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 56 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 57 | 57 | $this->resolveParameter($parameter, $state, $validate) |
| 58 | 58 | or |
| 59 | 59 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -67,47 +67,47 @@ discard block |
||
| 67 | 67 | $positional = true; |
| 68 | 68 | $variadic = false; |
| 69 | 69 | $parameters = $reflection->getParameters(); |
| 70 | - if (\count($parameters) === 0) { |
|
| 70 | + if (\count($parameters) === 0){ |
|
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | $parameter = null; |
| 75 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 75 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
| 76 | 76 | // get related argument value |
| 77 | 77 | $key = \key($arguments); |
| 78 | 78 | |
| 79 | 79 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 80 | 80 | // But you can't send positional argument after named in any case |
| 81 | - if (\is_int($key) && !$positional) { |
|
| 81 | + if (\is_int($key) && !$positional){ |
|
| 82 | 82 | throw new PositionalArgumentException($reflection, $key); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $positional = $positional && \is_int($key); |
| 86 | 86 | |
| 87 | - if (!$variadic) { |
|
| 87 | + if (!$variadic){ |
|
| 88 | 88 | $parameter = \array_shift($parameters); |
| 89 | 89 | $variadic = $parameter?->isVariadic() ?? false; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - if ($parameter === null) { |
|
| 92 | + if ($parameter === null){ |
|
| 93 | 93 | throw new UnknownParameterException($reflection, $key); |
| 94 | 94 | } |
| 95 | 95 | $name = $parameter->getName(); |
| 96 | 96 | |
| 97 | - if (($positional || $variadic) && $key !== null) { |
|
| 97 | + if (($positional || $variadic) && $key !== null){ |
|
| 98 | 98 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 99 | 99 | $value = \array_shift($arguments); |
| 100 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 101 | - if ($parameter->isOptional()) { |
|
| 100 | + } elseif ($key === null || !\array_key_exists($name, $arguments)){ |
|
| 101 | + if ($parameter->isOptional()){ |
|
| 102 | 102 | continue; |
| 103 | 103 | } |
| 104 | 104 | throw new MissingRequiredArgumentException($reflection, $name); |
| 105 | - } else { |
|
| 105 | + }else{ |
|
| 106 | 106 | $value = &$arguments[$name]; |
| 107 | 107 | unset($arguments[$name]); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 110 | + if (!$this->validateValueToParameter($parameter, $value)){ |
|
| 111 | 111 | throw new InvalidArgumentException($reflection, $name); |
| 112 | 112 | } |
| 113 | 113 | } |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | |
| 116 | 116 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 117 | 117 | { |
| 118 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 118 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)){ |
|
| 119 | 119 | return true; |
| 120 | 120 | } |
| 121 | 121 | $type = $parameter->getType(); |
@@ -126,17 +126,17 @@ discard block |
||
| 126 | 126 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 127 | 127 | }; |
| 128 | 128 | |
| 129 | - foreach ($types as $t) { |
|
| 129 | + foreach ($types as $t){ |
|
| 130 | 130 | \assert($t instanceof ReflectionNamedType); |
| 131 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 131 | + if (!$this->validateValueNamedType($t, $value)){ |
|
| 132 | 132 | // If it is TypeIntersection |
| 133 | - if ($or) { |
|
| 133 | + if ($or){ |
|
| 134 | 134 | continue; |
| 135 | 135 | } |
| 136 | 136 | return false; |
| 137 | 137 | } |
| 138 | 138 | // If it is not type intersection then we can skip that value after first successful check |
| 139 | - if ($or) { |
|
| 139 | + if ($or){ |
|
| 140 | 140 | return true; |
| 141 | 141 | } |
| 142 | 142 | } |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | { |
| 152 | 152 | $name = $type->getName(); |
| 153 | 153 | |
| 154 | - if ($type->isBuiltin()) { |
|
| 154 | + if ($type->isBuiltin()){ |
|
| 155 | 155 | return match ($name) { |
| 156 | 156 | 'mixed' => true, |
| 157 | 157 | 'string' => \is_string($value), |
@@ -184,13 +184,13 @@ discard block |
||
| 184 | 184 | |
| 185 | 185 | // Try to resolve parameter by name |
| 186 | 186 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
| 187 | - if ($res !== [] || $isVariadic) { |
|
| 187 | + if ($res !== [] || $isVariadic){ |
|
| 188 | 188 | // validate |
| 189 | - if ($isVariadic) { |
|
| 190 | - foreach ($res as $k => &$v) { |
|
| 191 | - $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
|
| 189 | + if ($isVariadic){ |
|
| 190 | + foreach ($res as $k => &$v){ |
|
| 191 | + $this->processArgument($state, $v, validateWith: $validate ? $param : null, key : $k); |
|
| 192 | 192 | } |
| 193 | - } else { |
|
| 193 | + }else{ |
|
| 194 | 194 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
| 195 | 195 | } |
| 196 | 196 | |
@@ -198,38 +198,38 @@ discard block |
||
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | $error = null; |
| 201 | - while ($hasType) { |
|
| 201 | + while ($hasType){ |
|
| 202 | 202 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $refType */ |
| 203 | 203 | $refType = $param->getType(); |
| 204 | 204 | |
| 205 | - if ($refType::class === ReflectionNamedType::class) { |
|
| 206 | - if ($refType->isBuiltin()) { |
|
| 205 | + if ($refType::class === ReflectionNamedType::class){ |
|
| 206 | + if ($refType->isBuiltin()){ |
|
| 207 | 207 | break; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | - if (\interface_exists($refType->getName()) && !empty($param->getAttributes(ProxyAttribute::class))) { |
|
| 210 | + if (\interface_exists($refType->getName()) && !empty($param->getAttributes(ProxyAttribute::class))){ |
|
| 211 | 211 | $proxy = Proxy::create(new \ReflectionClass($refType->getName()), $param); |
| 212 | 212 | $this->processArgument($state, $proxy); |
| 213 | 213 | return true; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - try { |
|
| 217 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
| 216 | + try{ |
|
| 217 | + if ($this->resolveObject($state, $refType, $param, $validate)){ |
|
| 218 | 218 | return true; |
| 219 | 219 | } |
| 220 | - } catch (Throwable $e) { |
|
| 220 | + }catch (Throwable $e){ |
|
| 221 | 221 | $error = $e; |
| 222 | 222 | } |
| 223 | 223 | break; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - if ($refType::class === ReflectionUnionType::class) { |
|
| 227 | - foreach ($refType->getTypes() as $namedType) { |
|
| 228 | - try { |
|
| 229 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
| 226 | + if ($refType::class === ReflectionUnionType::class){ |
|
| 227 | + foreach ($refType->getTypes() as $namedType){ |
|
| 228 | + try{ |
|
| 229 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)){ |
|
| 230 | 230 | return true; |
| 231 | 231 | } |
| 232 | - } catch (Throwable $e) { |
|
| 232 | + }catch (Throwable $e){ |
|
| 233 | 233 | $error = $e; |
| 234 | 234 | } |
| 235 | 235 | } |
@@ -239,19 +239,19 @@ discard block |
||
| 239 | 239 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - if ($param->isDefaultValueAvailable()) { |
|
| 242 | + if ($param->isDefaultValueAvailable()){ |
|
| 243 | 243 | $argument = $param->getDefaultValue(); |
| 244 | 244 | $this->processArgument($state, $argument); |
| 245 | 245 | return true; |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | - if ($hasType && $param->allowsNull()) { |
|
| 248 | + if ($hasType && $param->allowsNull()){ |
|
| 249 | 249 | $argument = null; |
| 250 | 250 | $this->processArgument($state, $argument); |
| 251 | 251 | return true; |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | - if ($error === null) { |
|
| 254 | + if ($error === null){ |
|
| 255 | 255 | return false; |
| 256 | 256 | } |
| 257 | 257 | |
@@ -289,15 +289,15 @@ discard block |
||
| 289 | 289 | ResolvingState $state, |
| 290 | 290 | mixed &$value, |
| 291 | 291 | ReflectionParameter $validateWith = null, |
| 292 | - int|string $key = null |
|
| 292 | + int | string $key = null |
|
| 293 | 293 | ): void { |
| 294 | 294 | // Resolve Autowire objects |
| 295 | - if ($value instanceof Autowire) { |
|
| 295 | + if ($value instanceof Autowire){ |
|
| 296 | 296 | $value = $value->resolve($this->factory); |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | // Validation |
| 300 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 300 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
| 301 | 301 | throw new InvalidArgumentException( |
| 302 | 302 | $validateWith->getDeclaringFunction(), |
| 303 | 303 | $validateWith->getName() |
@@ -53,7 +53,8 @@ discard block |
||
| 53 | 53 | ): array { |
| 54 | 54 | $state = new ResolvingState($reflection, $parameters); |
| 55 | 55 | |
| 56 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 56 | + foreach ($reflection->getParameters() as $parameter) |
|
| 57 | + { |
|
| 57 | 58 | $this->resolveParameter($parameter, $state, $validate) |
| 58 | 59 | or |
| 59 | 60 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -67,47 +68,59 @@ discard block |
||
| 67 | 68 | $positional = true; |
| 68 | 69 | $variadic = false; |
| 69 | 70 | $parameters = $reflection->getParameters(); |
| 70 | - if (\count($parameters) === 0) { |
|
| 71 | + if (\count($parameters) === 0) |
|
| 72 | + { |
|
| 71 | 73 | return; |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | $parameter = null; |
| 75 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 77 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
| 78 | + { |
|
| 76 | 79 | // get related argument value |
| 77 | 80 | $key = \key($arguments); |
| 78 | 81 | |
| 79 | 82 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 80 | 83 | // But you can't send positional argument after named in any case |
| 81 | - if (\is_int($key) && !$positional) { |
|
| 84 | + if (\is_int($key) && !$positional) |
|
| 85 | + { |
|
| 82 | 86 | throw new PositionalArgumentException($reflection, $key); |
| 83 | 87 | } |
| 84 | 88 | |
| 85 | 89 | $positional = $positional && \is_int($key); |
| 86 | 90 | |
| 87 | - if (!$variadic) { |
|
| 91 | + if (!$variadic) |
|
| 92 | + { |
|
| 88 | 93 | $parameter = \array_shift($parameters); |
| 89 | 94 | $variadic = $parameter?->isVariadic() ?? false; |
| 90 | 95 | } |
| 91 | 96 | |
| 92 | - if ($parameter === null) { |
|
| 97 | + if ($parameter === null) |
|
| 98 | + { |
|
| 93 | 99 | throw new UnknownParameterException($reflection, $key); |
| 94 | 100 | } |
| 95 | 101 | $name = $parameter->getName(); |
| 96 | 102 | |
| 97 | - if (($positional || $variadic) && $key !== null) { |
|
| 103 | + if (($positional || $variadic) && $key !== null) |
|
| 104 | + { |
|
| 98 | 105 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 99 | 106 | $value = \array_shift($arguments); |
| 100 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 101 | - if ($parameter->isOptional()) { |
|
| 107 | + } |
|
| 108 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
| 109 | + { |
|
| 110 | + if ($parameter->isOptional()) |
|
| 111 | + { |
|
| 102 | 112 | continue; |
| 103 | 113 | } |
| 104 | 114 | throw new MissingRequiredArgumentException($reflection, $name); |
| 105 | - } else { |
|
| 115 | + } |
|
| 116 | + else |
|
| 117 | + { |
|
| 106 | 118 | $value = &$arguments[$name]; |
| 107 | 119 | unset($arguments[$name]); |
| 108 | 120 | } |
| 109 | 121 | |
| 110 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 122 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
| 123 | + { |
|
| 111 | 124 | throw new InvalidArgumentException($reflection, $name); |
| 112 | 125 | } |
| 113 | 126 | } |
@@ -115,7 +128,8 @@ discard block |
||
| 115 | 128 | |
| 116 | 129 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 117 | 130 | { |
| 118 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 131 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
| 132 | + { |
|
| 119 | 133 | return true; |
| 120 | 134 | } |
| 121 | 135 | $type = $parameter->getType(); |
@@ -126,17 +140,21 @@ discard block |
||
| 126 | 140 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 127 | 141 | }; |
| 128 | 142 | |
| 129 | - foreach ($types as $t) { |
|
| 143 | + foreach ($types as $t) |
|
| 144 | + { |
|
| 130 | 145 | \assert($t instanceof ReflectionNamedType); |
| 131 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 146 | + if (!$this->validateValueNamedType($t, $value)) |
|
| 147 | + { |
|
| 132 | 148 | // If it is TypeIntersection |
| 133 | - if ($or) { |
|
| 149 | + if ($or) |
|
| 150 | + { |
|
| 134 | 151 | continue; |
| 135 | 152 | } |
| 136 | 153 | return false; |
| 137 | 154 | } |
| 138 | 155 | // If it is not type intersection then we can skip that value after first successful check |
| 139 | - if ($or) { |
|
| 156 | + if ($or) |
|
| 157 | + { |
|
| 140 | 158 | return true; |
| 141 | 159 | } |
| 142 | 160 | } |
@@ -151,7 +169,8 @@ discard block |
||
| 151 | 169 | { |
| 152 | 170 | $name = $type->getName(); |
| 153 | 171 | |
| 154 | - if ($type->isBuiltin()) { |
|
| 172 | + if ($type->isBuiltin()) |
|
| 173 | + { |
|
| 155 | 174 | return match ($name) { |
| 156 | 175 | 'mixed' => true, |
| 157 | 176 | 'string' => \is_string($value), |
@@ -184,13 +203,18 @@ discard block |
||
| 184 | 203 | |
| 185 | 204 | // Try to resolve parameter by name |
| 186 | 205 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
| 187 | - if ($res !== [] || $isVariadic) { |
|
| 206 | + if ($res !== [] || $isVariadic) |
|
| 207 | + { |
|
| 188 | 208 | // validate |
| 189 | - if ($isVariadic) { |
|
| 190 | - foreach ($res as $k => &$v) { |
|
| 209 | + if ($isVariadic) |
|
| 210 | + { |
|
| 211 | + foreach ($res as $k => &$v) |
|
| 212 | + { |
|
| 191 | 213 | $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
| 192 | 214 | } |
| 193 | - } else { |
|
| 215 | + } |
|
| 216 | + else |
|
| 217 | + { |
|
| 194 | 218 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
| 195 | 219 | } |
| 196 | 220 | |
@@ -198,38 +222,52 @@ discard block |
||
| 198 | 222 | } |
| 199 | 223 | |
| 200 | 224 | $error = null; |
| 201 | - while ($hasType) { |
|
| 225 | + while ($hasType) |
|
| 226 | + { |
|
| 202 | 227 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $refType */ |
| 203 | 228 | $refType = $param->getType(); |
| 204 | 229 | |
| 205 | - if ($refType::class === ReflectionNamedType::class) { |
|
| 206 | - if ($refType->isBuiltin()) { |
|
| 230 | + if ($refType::class === ReflectionNamedType::class) |
|
| 231 | + { |
|
| 232 | + if ($refType->isBuiltin()) |
|
| 233 | + { |
|
| 207 | 234 | break; |
| 208 | 235 | } |
| 209 | 236 | |
| 210 | - if (\interface_exists($refType->getName()) && !empty($param->getAttributes(ProxyAttribute::class))) { |
|
| 237 | + if (\interface_exists($refType->getName()) && !empty($param->getAttributes(ProxyAttribute::class))) |
|
| 238 | + { |
|
| 211 | 239 | $proxy = Proxy::create(new \ReflectionClass($refType->getName()), $param); |
| 212 | 240 | $this->processArgument($state, $proxy); |
| 213 | 241 | return true; |
| 214 | 242 | } |
| 215 | 243 | |
| 216 | - try { |
|
| 217 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
| 244 | + try |
|
| 245 | + { |
|
| 246 | + if ($this->resolveObject($state, $refType, $param, $validate)) |
|
| 247 | + { |
|
| 218 | 248 | return true; |
| 219 | 249 | } |
| 220 | - } catch (Throwable $e) { |
|
| 250 | + } |
|
| 251 | + catch (Throwable $e) |
|
| 252 | + { |
|
| 221 | 253 | $error = $e; |
| 222 | 254 | } |
| 223 | 255 | break; |
| 224 | 256 | } |
| 225 | 257 | |
| 226 | - if ($refType::class === ReflectionUnionType::class) { |
|
| 227 | - foreach ($refType->getTypes() as $namedType) { |
|
| 228 | - try { |
|
| 229 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
| 258 | + if ($refType::class === ReflectionUnionType::class) |
|
| 259 | + { |
|
| 260 | + foreach ($refType->getTypes() as $namedType) |
|
| 261 | + { |
|
| 262 | + try |
|
| 263 | + { |
|
| 264 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) |
|
| 265 | + { |
|
| 230 | 266 | return true; |
| 231 | 267 | } |
| 232 | - } catch (Throwable $e) { |
|
| 268 | + } |
|
| 269 | + catch (Throwable $e) |
|
| 270 | + { |
|
| 233 | 271 | $error = $e; |
| 234 | 272 | } |
| 235 | 273 | } |
@@ -239,19 +277,22 @@ discard block |
||
| 239 | 277 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
| 240 | 278 | } |
| 241 | 279 | |
| 242 | - if ($param->isDefaultValueAvailable()) { |
|
| 280 | + if ($param->isDefaultValueAvailable()) |
|
| 281 | + { |
|
| 243 | 282 | $argument = $param->getDefaultValue(); |
| 244 | 283 | $this->processArgument($state, $argument); |
| 245 | 284 | return true; |
| 246 | 285 | } |
| 247 | 286 | |
| 248 | - if ($hasType && $param->allowsNull()) { |
|
| 287 | + if ($hasType && $param->allowsNull()) |
|
| 288 | + { |
|
| 249 | 289 | $argument = null; |
| 250 | 290 | $this->processArgument($state, $argument); |
| 251 | 291 | return true; |
| 252 | 292 | } |
| 253 | 293 | |
| 254 | - if ($error === null) { |
|
| 294 | + if ($error === null) |
|
| 295 | + { |
|
| 255 | 296 | return false; |
| 256 | 297 | } |
| 257 | 298 | |
@@ -292,12 +333,14 @@ discard block |
||
| 292 | 333 | int|string $key = null |
| 293 | 334 | ): void { |
| 294 | 335 | // Resolve Autowire objects |
| 295 | - if ($value instanceof Autowire) { |
|
| 336 | + if ($value instanceof Autowire) |
|
| 337 | + { |
|
| 296 | 338 | $value = $value->resolve($this->factory); |
| 297 | 339 | } |
| 298 | 340 | |
| 299 | 341 | // Validation |
| 300 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 342 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
| 343 | + { |
|
| 301 | 344 | throw new InvalidArgumentException( |
| 302 | 345 | $validateWith->getDeclaringFunction(), |
| 303 | 346 | $validateWith->getName() |