@@ -31,7 +31,7 @@ |
||
| 31 | 31 | $option->checkScope = true; |
| 32 | 32 | yield [$option]; |
| 33 | 33 | |
| 34 | - $option = new class extends Options { |
|
| 34 | + $option = new class extends Options{ |
|
| 35 | 35 | public int $customOption = 3; |
| 36 | 36 | }; |
| 37 | 37 | $option->customOption = 5; |
@@ -31,7 +31,8 @@ |
||
| 31 | 31 | $option->checkScope = true; |
| 32 | 32 | yield [$option]; |
| 33 | 33 | |
| 34 | - $option = new class extends Options { |
|
| 34 | + $option = new class extends Options |
|
| 35 | + { |
|
| 35 | 36 | public int $customOption = 3; |
| 36 | 37 | }; |
| 37 | 38 | $option->customOption = 5; |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | private Config $config, |
| 20 | 20 | private array $objects = [], |
| 21 | 21 | private Options $options = new Options(), |
| 22 | - ) { |
|
| 22 | + ){ |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | 25 | public function set(string $name, object $value): void |
@@ -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 | /** |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | private Config $config = new Config(), |
| 62 | 62 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
| 63 | 63 | private Options $options = new Options(), |
| 64 | - ) { |
|
| 64 | + ){ |
|
| 65 | 65 | $this->initServices($this, $scopeName); |
| 66 | 66 | |
| 67 | 67 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | * @throws \Throwable |
| 114 | 114 | * @psalm-suppress TooManyArguments |
| 115 | 115 | */ |
| 116 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
| 116 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
| 117 | 117 | { |
| 118 | 118 | return ContainerScope::getContainer() === $this |
| 119 | 119 | ? $this->factory->make($alias, $parameters, $context) |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * @throws \Throwable |
| 140 | 140 | * @psalm-suppress TooManyArguments |
| 141 | 141 | */ |
| 142 | - public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
| 142 | + public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed |
|
| 143 | 143 | { |
| 144 | 144 | return ContainerScope::getContainer() === $this |
| 145 | 145 | ? $this->container->get($id, $context) |
@@ -169,25 +169,25 @@ discard block |
||
| 169 | 169 | /** |
| 170 | 170 | * @throws \Throwable |
| 171 | 171 | */ |
| 172 | - public function runScope(Scope|array $bindings, callable $scope): mixed |
|
| 172 | + public function runScope(Scope | array $bindings, callable $scope): mixed |
|
| 173 | 173 | { |
| 174 | - if (!\is_array($bindings)) { |
|
| 174 | + if (!\is_array($bindings)){ |
|
| 175 | 175 | return $this->runIsolatedScope($bindings, $scope); |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | 178 | $binds = &$this->state->bindings; |
| 179 | 179 | $singletons = &$this->state->singletons; |
| 180 | 180 | $cleanup = $previous = $prevSin = []; |
| 181 | - foreach ($bindings as $alias => $resolver) { |
|
| 181 | + foreach ($bindings as $alias => $resolver){ |
|
| 182 | 182 | // Store previous bindings |
| 183 | - if (isset($binds[$alias])) { |
|
| 183 | + if (isset($binds[$alias])){ |
|
| 184 | 184 | $previous[$alias] = $binds[$alias]; |
| 185 | - } else { |
|
| 185 | + }else{ |
|
| 186 | 186 | // Store bindings to be removed |
| 187 | 187 | $cleanup[] = $alias; |
| 188 | 188 | } |
| 189 | 189 | // Store previous singletons |
| 190 | - if (isset($singletons[$alias])) { |
|
| 190 | + if (isset($singletons[$alias])){ |
|
| 191 | 191 | $prevSin[$alias] = $singletons[$alias]; |
| 192 | 192 | unset($singletons[$alias]); |
| 193 | 193 | } |
@@ -195,21 +195,21 @@ discard block |
||
| 195 | 195 | $this->binder->bind($alias, $resolver); |
| 196 | 196 | } |
| 197 | 197 | |
| 198 | - try { |
|
| 198 | + try{ |
|
| 199 | 199 | return ContainerScope::getContainer() !== $this |
| 200 | 200 | ? ContainerScope::runScope($this, $scope) |
| 201 | 201 | : $scope($this); |
| 202 | - } finally { |
|
| 202 | + }finally{ |
|
| 203 | 203 | // Remove new bindings |
| 204 | - foreach ($cleanup as $alias) { |
|
| 204 | + foreach ($cleanup as $alias){ |
|
| 205 | 205 | unset($binds[$alias], $singletons[$alias]); |
| 206 | 206 | } |
| 207 | 207 | // Restore previous bindings |
| 208 | - foreach ($previous as $alias => $resolver) { |
|
| 208 | + foreach ($previous as $alias => $resolver){ |
|
| 209 | 209 | $binds[$alias] = $resolver; |
| 210 | 210 | } |
| 211 | 211 | // Restore singletons |
| 212 | - foreach ($prevSin as $alias => $instance) { |
|
| 212 | + foreach ($prevSin as $alias => $instance){ |
|
| 213 | 213 | $singletons[$alias] = $instance; |
| 214 | 214 | } |
| 215 | 215 | } |
@@ -255,9 +255,9 @@ discard block |
||
| 255 | 255 | * @param bool $force If the value is false, an exception will be thrown when attempting |
| 256 | 256 | * to bind an already constructed singleton. |
| 257 | 257 | */ |
| 258 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
|
| 258 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, bool $force = true): void |
|
| 259 | 259 | { |
| 260 | - if ($force) { |
|
| 260 | + if ($force){ |
|
| 261 | 261 | $this->binder->removeBinding($alias); |
| 262 | 262 | } |
| 263 | 263 | |
@@ -328,8 +328,8 @@ discard block |
||
| 328 | 328 | ], $this->options); |
| 329 | 329 | |
| 330 | 330 | // Create container services |
| 331 | - foreach ($container->config as $property => $class) { |
|
| 332 | - if (\property_exists($container, $property)) { |
|
| 331 | + foreach ($container->config as $property => $class){ |
|
| 332 | + if (\property_exists($container, $property)){ |
|
| 333 | 333 | $container->$property = $constructor->get($property, $class); |
| 334 | 334 | } |
| 335 | 335 | } |
@@ -343,7 +343,7 @@ discard block |
||
| 343 | 343 | private function closeScope(): void |
| 344 | 344 | { |
| 345 | 345 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 346 | - if (!isset($this->scope)) { |
|
| 346 | + if (!isset($this->scope)){ |
|
| 347 | 347 | $this->destruct(); |
| 348 | 348 | return; |
| 349 | 349 | } |
@@ -352,10 +352,10 @@ discard block |
||
| 352 | 352 | |
| 353 | 353 | // Run finalizers |
| 354 | 354 | $errors = []; |
| 355 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 356 | - try { |
|
| 355 | + foreach ($this->state->finalizers as $finalizer){ |
|
| 356 | + try{ |
|
| 357 | 357 | $this->invoker->invoke($finalizer); |
| 358 | - } catch (\Throwable $e) { |
|
| 358 | + }catch (\Throwable $e){ |
|
| 359 | 359 | $errors[] = $e; |
| 360 | 360 | } |
| 361 | 361 | } |
@@ -364,7 +364,7 @@ discard block |
||
| 364 | 364 | $this->destruct(); |
| 365 | 365 | |
| 366 | 366 | // Throw collected errors |
| 367 | - if ($errors !== []) { |
|
| 367 | + if ($errors !== []){ |
|
| 368 | 368 | throw new FinalizersException($scopeName, $errors); |
| 369 | 369 | } |
| 370 | 370 | } |
@@ -386,18 +386,18 @@ discard block |
||
| 386 | 386 | $container->scope->setParent($this, $this->scope, $this->factory); |
| 387 | 387 | |
| 388 | 388 | // Add specific bindings |
| 389 | - foreach ($config->bindings as $alias => $resolver) { |
|
| 389 | + foreach ($config->bindings as $alias => $resolver){ |
|
| 390 | 390 | $container->binder->bind($alias, $resolver); |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | return ContainerScope::runScope( |
| 394 | 394 | $container, |
| 395 | 395 | static function (self $container) use ($config, $closure): mixed { |
| 396 | - try { |
|
| 396 | + try{ |
|
| 397 | 397 | return $config->autowire |
| 398 | 398 | ? $container->invoke($closure) |
| 399 | 399 | : $closure($container); |
| 400 | - } finally { |
|
| 400 | + }finally{ |
|
| 401 | 401 | $container->closeScope(); |
| 402 | 402 | } |
| 403 | 403 | } |
@@ -171,23 +171,29 @@ discard block |
||
| 171 | 171 | */ |
| 172 | 172 | public function runScope(Scope|array $bindings, callable $scope): mixed |
| 173 | 173 | { |
| 174 | - if (!\is_array($bindings)) { |
|
| 174 | + if (!\is_array($bindings)) |
|
| 175 | + { |
|
| 175 | 176 | return $this->runIsolatedScope($bindings, $scope); |
| 176 | 177 | } |
| 177 | 178 | |
| 178 | 179 | $binds = &$this->state->bindings; |
| 179 | 180 | $singletons = &$this->state->singletons; |
| 180 | 181 | $cleanup = $previous = $prevSin = []; |
| 181 | - foreach ($bindings as $alias => $resolver) { |
|
| 182 | + foreach ($bindings as $alias => $resolver) |
|
| 183 | + { |
|
| 182 | 184 | // Store previous bindings |
| 183 | - if (isset($binds[$alias])) { |
|
| 185 | + if (isset($binds[$alias])) |
|
| 186 | + { |
|
| 184 | 187 | $previous[$alias] = $binds[$alias]; |
| 185 | - } else { |
|
| 188 | + } |
|
| 189 | + else |
|
| 190 | + { |
|
| 186 | 191 | // Store bindings to be removed |
| 187 | 192 | $cleanup[] = $alias; |
| 188 | 193 | } |
| 189 | 194 | // Store previous singletons |
| 190 | - if (isset($singletons[$alias])) { |
|
| 195 | + if (isset($singletons[$alias])) |
|
| 196 | + { |
|
| 191 | 197 | $prevSin[$alias] = $singletons[$alias]; |
| 192 | 198 | unset($singletons[$alias]); |
| 193 | 199 | } |
@@ -195,21 +201,27 @@ discard block |
||
| 195 | 201 | $this->binder->bind($alias, $resolver); |
| 196 | 202 | } |
| 197 | 203 | |
| 198 | - try { |
|
| 204 | + try |
|
| 205 | + { |
|
| 199 | 206 | return ContainerScope::getContainer() !== $this |
| 200 | 207 | ? ContainerScope::runScope($this, $scope) |
| 201 | 208 | : $scope($this); |
| 202 | - } finally { |
|
| 209 | + } |
|
| 210 | + finally |
|
| 211 | + { |
|
| 203 | 212 | // Remove new bindings |
| 204 | - foreach ($cleanup as $alias) { |
|
| 213 | + foreach ($cleanup as $alias) |
|
| 214 | + { |
|
| 205 | 215 | unset($binds[$alias], $singletons[$alias]); |
| 206 | 216 | } |
| 207 | 217 | // Restore previous bindings |
| 208 | - foreach ($previous as $alias => $resolver) { |
|
| 218 | + foreach ($previous as $alias => $resolver) |
|
| 219 | + { |
|
| 209 | 220 | $binds[$alias] = $resolver; |
| 210 | 221 | } |
| 211 | 222 | // Restore singletons |
| 212 | - foreach ($prevSin as $alias => $instance) { |
|
| 223 | + foreach ($prevSin as $alias => $instance) |
|
| 224 | + { |
|
| 213 | 225 | $singletons[$alias] = $instance; |
| 214 | 226 | } |
| 215 | 227 | } |
@@ -257,7 +269,8 @@ discard block |
||
| 257 | 269 | */ |
| 258 | 270 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
| 259 | 271 | { |
| 260 | - if ($force) { |
|
| 272 | + if ($force) |
|
| 273 | + { |
|
| 261 | 274 | $this->binder->removeBinding($alias); |
| 262 | 275 | } |
| 263 | 276 | |
@@ -328,8 +341,10 @@ discard block |
||
| 328 | 341 | ], $this->options); |
| 329 | 342 | |
| 330 | 343 | // Create container services |
| 331 | - foreach ($container->config as $property => $class) { |
|
| 332 | - if (\property_exists($container, $property)) { |
|
| 344 | + foreach ($container->config as $property => $class) |
|
| 345 | + { |
|
| 346 | + if (\property_exists($container, $property)) |
|
| 347 | + { |
|
| 333 | 348 | $container->$property = $constructor->get($property, $class); |
| 334 | 349 | } |
| 335 | 350 | } |
@@ -343,7 +358,8 @@ discard block |
||
| 343 | 358 | private function closeScope(): void |
| 344 | 359 | { |
| 345 | 360 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 346 | - if (!isset($this->scope)) { |
|
| 361 | + if (!isset($this->scope)) |
|
| 362 | + { |
|
| 347 | 363 | $this->destruct(); |
| 348 | 364 | return; |
| 349 | 365 | } |
@@ -352,10 +368,14 @@ discard block |
||
| 352 | 368 | |
| 353 | 369 | // Run finalizers |
| 354 | 370 | $errors = []; |
| 355 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 356 | - try { |
|
| 371 | + foreach ($this->state->finalizers as $finalizer) |
|
| 372 | + { |
|
| 373 | + try |
|
| 374 | + { |
|
| 357 | 375 | $this->invoker->invoke($finalizer); |
| 358 | - } catch (\Throwable $e) { |
|
| 376 | + } |
|
| 377 | + catch (\Throwable $e) |
|
| 378 | + { |
|
| 359 | 379 | $errors[] = $e; |
| 360 | 380 | } |
| 361 | 381 | } |
@@ -364,7 +384,8 @@ discard block |
||
| 364 | 384 | $this->destruct(); |
| 365 | 385 | |
| 366 | 386 | // Throw collected errors |
| 367 | - if ($errors !== []) { |
|
| 387 | + if ($errors !== []) |
|
| 388 | + { |
|
| 368 | 389 | throw new FinalizersException($scopeName, $errors); |
| 369 | 390 | } |
| 370 | 391 | } |
@@ -386,18 +407,22 @@ discard block |
||
| 386 | 407 | $container->scope->setParent($this, $this->scope, $this->factory); |
| 387 | 408 | |
| 388 | 409 | // Add specific bindings |
| 389 | - foreach ($config->bindings as $alias => $resolver) { |
|
| 410 | + foreach ($config->bindings as $alias => $resolver) |
|
| 411 | + { |
|
| 390 | 412 | $container->binder->bind($alias, $resolver); |
| 391 | 413 | } |
| 392 | 414 | |
| 393 | 415 | return ContainerScope::runScope( |
| 394 | 416 | $container, |
| 395 | 417 | static function (self $container) use ($config, $closure): mixed { |
| 396 | - try { |
|
| 418 | + try |
|
| 419 | + { |
|
| 397 | 420 | return $config->autowire |
| 398 | 421 | ? $container->invoke($closure) |
| 399 | 422 | : $closure($container); |
| 400 | - } finally { |
|
| 423 | + } |
|
| 424 | + finally |
|
| 425 | + { |
|
| 401 | 426 | $container->closeScope(); |
| 402 | 427 | } |
| 403 | 428 | } |
@@ -21,8 +21,8 @@ discard block |
||
| 21 | 21 | { |
| 22 | 22 | $root = self::makeContainer(); |
| 23 | 23 | |
| 24 | - $obj = $root->runScoped(static function (Container $c1) { |
|
| 25 | - $obj = $c1->runScoped(static function (Container $c2) { |
|
| 24 | + $obj = $root->runScoped(static function (Container $c1){ |
|
| 25 | + $obj = $c1->runScoped(static function (Container $c2){ |
|
| 26 | 26 | $obj = $c2->get(AttrScopeFooFinalize::class); |
| 27 | 27 | |
| 28 | 28 | self::assertFalse($obj->finalized); |
@@ -124,12 +124,12 @@ discard block |
||
| 124 | 124 | self::expectException(FinalizersException::class); |
| 125 | 125 | self::expectExceptionMessage('An exception has been thrown during finalization of the scope `foo`'); |
| 126 | 126 | |
| 127 | - try { |
|
| 128 | - $root->runScoped(static function (Container $c1) { |
|
| 127 | + try{ |
|
| 128 | + $root->runScoped(static function (Container $c1){ |
|
| 129 | 129 | $obj = $c1->get(AttrScopeFooFinalize::class); |
| 130 | 130 | $obj->throwException = true; |
| 131 | 131 | }, name: 'foo'); |
| 132 | - } catch (FinalizersException $e) { |
|
| 132 | + }catch (FinalizersException $e){ |
|
| 133 | 133 | self::assertSame('foo', $e->getScope()); |
| 134 | 134 | self::assertCount(1, $e->getExceptions()); |
| 135 | 135 | // Contains the message from the inner exception. |
@@ -153,13 +153,13 @@ discard block |
||
| 153 | 153 | self::expectException(FinalizersException::class); |
| 154 | 154 | self::expectExceptionMessage('3 exceptions have been thrown during finalization of the scope `foo`'); |
| 155 | 155 | |
| 156 | - try { |
|
| 157 | - $root->runScoped(static function (Container $c1) { |
|
| 156 | + try{ |
|
| 157 | + $root->runScoped(static function (Container $c1){ |
|
| 158 | 158 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
| 159 | 159 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
| 160 | 160 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
| 161 | 161 | }, name: 'foo'); |
| 162 | - } catch (FinalizersException $e) { |
|
| 162 | + }catch (FinalizersException $e){ |
|
| 163 | 163 | self::assertSame('foo', $e->getScope()); |
| 164 | 164 | self::assertCount(3, $e->getExceptions()); |
| 165 | 165 | // Contains the message from the inner exception. |
@@ -21,8 +21,10 @@ discard block |
||
| 21 | 21 | { |
| 22 | 22 | $root = self::makeContainer(); |
| 23 | 23 | |
| 24 | - $obj = $root->runScoped(static function (Container $c1) { |
|
| 25 | - $obj = $c1->runScoped(static function (Container $c2) { |
|
| 24 | + $obj = $root->runScoped(static function (Container $c1) |
|
| 25 | + { |
|
| 26 | + $obj = $c1->runScoped(static function (Container $c2) |
|
| 27 | + { |
|
| 26 | 28 | $obj = $c2->get(AttrScopeFooFinalize::class); |
| 27 | 29 | |
| 28 | 30 | self::assertFalse($obj->finalized); |
@@ -46,8 +48,10 @@ discard block |
||
| 46 | 48 | $root->bindSingleton(LoggerInterface::class, FileLogger::class); |
| 47 | 49 | |
| 48 | 50 | $obj2 = null; |
| 49 | - $obj = $root->runScoped(static function (Container $c1) use (&$obj2) { |
|
| 50 | - $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) { |
|
| 51 | + $obj = $root->runScoped(static function (Container $c1) use (&$obj2) |
|
| 52 | + { |
|
| 53 | + $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) |
|
| 54 | + { |
|
| 51 | 55 | $obj = $c2->get(AttrScopeFooFinalize::class); |
| 52 | 56 | $obj2 = $c2->get(AttrScopeFooFinalize::class); |
| 53 | 57 | |
@@ -77,8 +81,10 @@ discard block |
||
| 77 | 81 | $root->bindSingleton(LoggerInterface::class, FileLogger::class); |
| 78 | 82 | |
| 79 | 83 | $obj2 = null; |
| 80 | - $obj = $root->runScoped(static function (Container $c1) use (&$obj2) { |
|
| 81 | - $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) { |
|
| 84 | + $obj = $root->runScoped(static function (Container $c1) use (&$obj2) |
|
| 85 | + { |
|
| 86 | + $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) |
|
| 87 | + { |
|
| 82 | 88 | $obj = $c2->get(AttrFinalize::class); |
| 83 | 89 | $obj2 = $c2->get(AttrFinalize::class); |
| 84 | 90 | |
@@ -124,12 +130,16 @@ discard block |
||
| 124 | 130 | self::expectException(FinalizersException::class); |
| 125 | 131 | self::expectExceptionMessage('An exception has been thrown during finalization of the scope `foo`'); |
| 126 | 132 | |
| 127 | - try { |
|
| 128 | - $root->runScoped(static function (Container $c1) { |
|
| 133 | + try |
|
| 134 | + { |
|
| 135 | + $root->runScoped(static function (Container $c1) |
|
| 136 | + { |
|
| 129 | 137 | $obj = $c1->get(AttrScopeFooFinalize::class); |
| 130 | 138 | $obj->throwException = true; |
| 131 | 139 | }, name: 'foo'); |
| 132 | - } catch (FinalizersException $e) { |
|
| 140 | + } |
|
| 141 | + catch (FinalizersException $e) |
|
| 142 | + { |
|
| 133 | 143 | self::assertSame('foo', $e->getScope()); |
| 134 | 144 | self::assertCount(1, $e->getExceptions()); |
| 135 | 145 | // Contains the message from the inner exception. |
@@ -153,13 +163,17 @@ discard block |
||
| 153 | 163 | self::expectException(FinalizersException::class); |
| 154 | 164 | self::expectExceptionMessage('3 exceptions have been thrown during finalization of the scope `foo`'); |
| 155 | 165 | |
| 156 | - try { |
|
| 157 | - $root->runScoped(static function (Container $c1) { |
|
| 166 | + try |
|
| 167 | + { |
|
| 168 | + $root->runScoped(static function (Container $c1) |
|
| 169 | + { |
|
| 158 | 170 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
| 159 | 171 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
| 160 | 172 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
| 161 | 173 | }, name: 'foo'); |
| 162 | - } catch (FinalizersException $e) { |
|
| 174 | + } |
|
| 175 | + catch (FinalizersException $e) |
|
| 176 | + { |
|
| 163 | 177 | self::assertSame('foo', $e->getScope()); |
| 164 | 178 | self::assertCount(3, $e->getExceptions()); |
| 165 | 179 | // Contains the message from the inner exception. |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | { |
| 40 | 40 | $root = self::makeContainer(); |
| 41 | 41 | |
| 42 | - $root->runScoped(static function (Container $c1) { |
|
| 42 | + $root->runScoped(static function (Container $c1){ |
|
| 43 | 43 | $c1->runScoped(static function (Container $c2) use ($c1) { |
| 44 | 44 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
| 45 | 45 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
@@ -54,8 +54,8 @@ discard block |
||
| 54 | 54 | $root = self::makeContainer(); |
| 55 | 55 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
| 56 | 56 | |
| 57 | - $root->runScoped(static function (Container $fooScope) { |
|
| 58 | - $fooScope->runScoped(static function (Container $container) { |
|
| 57 | + $root->runScoped(static function (Container $fooScope){ |
|
| 58 | + $fooScope->runScoped(static function (Container $container){ |
|
| 59 | 59 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
| 60 | 60 | }, name: 'bar'); |
| 61 | 61 | }, name: 'foo'); |
@@ -69,8 +69,8 @@ discard block |
||
| 69 | 69 | $root = self::makeContainer(); |
| 70 | 70 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
| 71 | 71 | |
| 72 | - $root->runScoped(static function (Container $fooScope) { |
|
| 73 | - $fooScope->runScoped(static function (Container $container) { |
|
| 72 | + $root->runScoped(static function (Container $fooScope){ |
|
| 73 | + $fooScope->runScoped(static function (Container $container){ |
|
| 74 | 74 | $container->get('binding'); |
| 75 | 75 | }, name: 'bar'); |
| 76 | 76 | }, name: 'baz'); |
@@ -81,8 +81,8 @@ discard block |
||
| 81 | 81 | $root = self::makeContainer(checkScope: false); |
| 82 | 82 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
| 83 | 83 | |
| 84 | - $root->runScoped(static function (Container $fooScope) { |
|
| 85 | - $fooScope->runScoped(static function (Container $container) { |
|
| 84 | + $root->runScoped(static function (Container $fooScope){ |
|
| 85 | + $fooScope->runScoped(static function (Container $container){ |
|
| 86 | 86 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
| 87 | 87 | }, name: 'bar'); |
| 88 | 88 | }, name: 'baz'); |
@@ -101,8 +101,8 @@ discard block |
||
| 101 | 101 | $root = self::makeContainer(); |
| 102 | 102 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 103 | 103 | |
| 104 | - $root->runScoped(static function (Container $c1) { |
|
| 105 | - $c1->runScoped(static function (Container $c2) { |
|
| 104 | + $root->runScoped(static function (Container $c1){ |
|
| 105 | + $c1->runScoped(static function (Container $c2){ |
|
| 106 | 106 | $c2->get('foo'); |
| 107 | 107 | }, name: 'foo'); |
| 108 | 108 | }); |
@@ -114,8 +114,8 @@ discard block |
||
| 114 | 114 | $root = self::makeContainer(checkScope: false); |
| 115 | 115 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 116 | 116 | |
| 117 | - $root->runScoped(static function (Container $c1) { |
|
| 118 | - $c1->runScoped(static function (Container $c2) { |
|
| 117 | + $root->runScoped(static function (Container $c1){ |
|
| 118 | + $c1->runScoped(static function (Container $c2){ |
|
| 119 | 119 | self::assertInstanceOf(AttrScopeFoo::class, $c2->get('foo')); |
| 120 | 120 | }, name: 'foo'); |
| 121 | 121 | }); |
@@ -133,8 +133,8 @@ discard block |
||
| 133 | 133 | $root = self::makeContainer(); |
| 134 | 134 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 135 | 135 | |
| 136 | - $root->runScoped(static function (Container $c1) { |
|
| 137 | - $c1->runScoped(static function (Container $c2) { |
|
| 136 | + $root->runScoped(static function (Container $c1){ |
|
| 137 | + $c1->runScoped(static function (Container $c2){ |
|
| 138 | 138 | $c2->get('foo'); |
| 139 | 139 | }); |
| 140 | 140 | }); |
@@ -146,8 +146,8 @@ discard block |
||
| 146 | 146 | $root = self::makeContainer(checkScope: false); |
| 147 | 147 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 148 | 148 | |
| 149 | - $root->runScoped(static function (Container $c1) { |
|
| 150 | - $c1->runScoped(static function (Container $c2) { |
|
| 149 | + $root->runScoped(static function (Container $c1){ |
|
| 150 | + $c1->runScoped(static function (Container $c2){ |
|
| 151 | 151 | self::assertInstanceOf(AttrScopeFoo::class, $c2->get('foo')); |
| 152 | 152 | }); |
| 153 | 153 | }); |
@@ -165,15 +165,15 @@ discard block |
||
| 165 | 165 | |
| 166 | 166 | $root = self::makeContainer(); |
| 167 | 167 | |
| 168 | - try { |
|
| 169 | - $root->runScoped(static function (Container $c1) { |
|
| 170 | - $c1->runScoped(static function (Container $c2) { |
|
| 171 | - $c2->runScoped(static function (Container $c3) { |
|
| 168 | + try{ |
|
| 169 | + $root->runScoped(static function (Container $c1){ |
|
| 170 | + $c1->runScoped(static function (Container $c2){ |
|
| 171 | + $c2->runScoped(static function (Container $c3){ |
|
| 172 | 172 | // do nothing |
| 173 | 173 | }, name: 'root'); |
| 174 | 174 | }); |
| 175 | 175 | }); |
| 176 | - } catch (NamedScopeDuplicationException $e) { |
|
| 176 | + }catch (NamedScopeDuplicationException $e){ |
|
| 177 | 177 | self::assertSame('root', $e->getScope()); |
| 178 | 178 | throw $e; |
| 179 | 179 | } |
@@ -188,14 +188,14 @@ discard block |
||
| 188 | 188 | { |
| 189 | 189 | self::expectException(BadScopeException::class); |
| 190 | 190 | |
| 191 | - try { |
|
| 191 | + try{ |
|
| 192 | 192 | $root = self::makeContainer(); |
| 193 | - $root->runScoped(static function (Container $c1) { |
|
| 194 | - $c1->runScoped(static function (Container $c2) { |
|
| 193 | + $root->runScoped(static function (Container $c1){ |
|
| 194 | + $c1->runScoped(static function (Container $c2){ |
|
| 195 | 195 | $c2->get(AttrScopeFoo::class); |
| 196 | 196 | }); |
| 197 | 197 | }, name: 'bar'); |
| 198 | - } catch (BadScopeException $e) { |
|
| 198 | + }catch (BadScopeException $e){ |
|
| 199 | 199 | self::assertSame('foo', $e->getScope()); |
| 200 | 200 | throw $e; |
| 201 | 201 | } |
@@ -39,8 +39,10 @@ discard block |
||
| 39 | 39 | { |
| 40 | 40 | $root = self::makeContainer(); |
| 41 | 41 | |
| 42 | - $root->runScoped(static function (Container $c1) { |
|
| 43 | - $c1->runScoped(static function (Container $c2) use ($c1) { |
|
| 42 | + $root->runScoped(static function (Container $c1) |
|
| 43 | + { |
|
| 44 | + $c1->runScoped(static function (Container $c2) use ($c1) |
|
| 45 | + { |
|
| 44 | 46 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
| 45 | 47 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
| 46 | 48 | |
@@ -54,8 +56,10 @@ discard block |
||
| 54 | 56 | $root = self::makeContainer(); |
| 55 | 57 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
| 56 | 58 | |
| 57 | - $root->runScoped(static function (Container $fooScope) { |
|
| 58 | - $fooScope->runScoped(static function (Container $container) { |
|
| 59 | + $root->runScoped(static function (Container $fooScope) |
|
| 60 | + { |
|
| 61 | + $fooScope->runScoped(static function (Container $container) |
|
| 62 | + { |
|
| 59 | 63 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
| 60 | 64 | }, name: 'bar'); |
| 61 | 65 | }, name: 'foo'); |
@@ -69,8 +73,10 @@ discard block |
||
| 69 | 73 | $root = self::makeContainer(); |
| 70 | 74 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
| 71 | 75 | |
| 72 | - $root->runScoped(static function (Container $fooScope) { |
|
| 73 | - $fooScope->runScoped(static function (Container $container) { |
|
| 76 | + $root->runScoped(static function (Container $fooScope) |
|
| 77 | + { |
|
| 78 | + $fooScope->runScoped(static function (Container $container) |
|
| 79 | + { |
|
| 74 | 80 | $container->get('binding'); |
| 75 | 81 | }, name: 'bar'); |
| 76 | 82 | }, name: 'baz'); |
@@ -81,8 +87,10 @@ discard block |
||
| 81 | 87 | $root = self::makeContainer(checkScope: false); |
| 82 | 88 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
| 83 | 89 | |
| 84 | - $root->runScoped(static function (Container $fooScope) { |
|
| 85 | - $fooScope->runScoped(static function (Container $container) { |
|
| 90 | + $root->runScoped(static function (Container $fooScope) |
|
| 91 | + { |
|
| 92 | + $fooScope->runScoped(static function (Container $container) |
|
| 93 | + { |
|
| 86 | 94 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
| 87 | 95 | }, name: 'bar'); |
| 88 | 96 | }, name: 'baz'); |
@@ -101,8 +109,10 @@ discard block |
||
| 101 | 109 | $root = self::makeContainer(); |
| 102 | 110 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 103 | 111 | |
| 104 | - $root->runScoped(static function (Container $c1) { |
|
| 105 | - $c1->runScoped(static function (Container $c2) { |
|
| 112 | + $root->runScoped(static function (Container $c1) |
|
| 113 | + { |
|
| 114 | + $c1->runScoped(static function (Container $c2) |
|
| 115 | + { |
|
| 106 | 116 | $c2->get('foo'); |
| 107 | 117 | }, name: 'foo'); |
| 108 | 118 | }); |
@@ -114,8 +124,10 @@ discard block |
||
| 114 | 124 | $root = self::makeContainer(checkScope: false); |
| 115 | 125 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 116 | 126 | |
| 117 | - $root->runScoped(static function (Container $c1) { |
|
| 118 | - $c1->runScoped(static function (Container $c2) { |
|
| 127 | + $root->runScoped(static function (Container $c1) |
|
| 128 | + { |
|
| 129 | + $c1->runScoped(static function (Container $c2) |
|
| 130 | + { |
|
| 119 | 131 | self::assertInstanceOf(AttrScopeFoo::class, $c2->get('foo')); |
| 120 | 132 | }, name: 'foo'); |
| 121 | 133 | }); |
@@ -133,8 +145,10 @@ discard block |
||
| 133 | 145 | $root = self::makeContainer(); |
| 134 | 146 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 135 | 147 | |
| 136 | - $root->runScoped(static function (Container $c1) { |
|
| 137 | - $c1->runScoped(static function (Container $c2) { |
|
| 148 | + $root->runScoped(static function (Container $c1) |
|
| 149 | + { |
|
| 150 | + $c1->runScoped(static function (Container $c2) |
|
| 151 | + { |
|
| 138 | 152 | $c2->get('foo'); |
| 139 | 153 | }); |
| 140 | 154 | }); |
@@ -146,8 +160,10 @@ discard block |
||
| 146 | 160 | $root = self::makeContainer(checkScope: false); |
| 147 | 161 | $root->bind('foo', self::makeFooScopeObject(...)); |
| 148 | 162 | |
| 149 | - $root->runScoped(static function (Container $c1) { |
|
| 150 | - $c1->runScoped(static function (Container $c2) { |
|
| 163 | + $root->runScoped(static function (Container $c1) |
|
| 164 | + { |
|
| 165 | + $c1->runScoped(static function (Container $c2) |
|
| 166 | + { |
|
| 151 | 167 | self::assertInstanceOf(AttrScopeFoo::class, $c2->get('foo')); |
| 152 | 168 | }); |
| 153 | 169 | }); |
@@ -165,15 +181,21 @@ discard block |
||
| 165 | 181 | |
| 166 | 182 | $root = self::makeContainer(); |
| 167 | 183 | |
| 168 | - try { |
|
| 169 | - $root->runScoped(static function (Container $c1) { |
|
| 170 | - $c1->runScoped(static function (Container $c2) { |
|
| 171 | - $c2->runScoped(static function (Container $c3) { |
|
| 184 | + try |
|
| 185 | + { |
|
| 186 | + $root->runScoped(static function (Container $c1) |
|
| 187 | + { |
|
| 188 | + $c1->runScoped(static function (Container $c2) |
|
| 189 | + { |
|
| 190 | + $c2->runScoped(static function (Container $c3) |
|
| 191 | + { |
|
| 172 | 192 | // do nothing |
| 173 | 193 | }, name: 'root'); |
| 174 | 194 | }); |
| 175 | 195 | }); |
| 176 | - } catch (NamedScopeDuplicationException $e) { |
|
| 196 | + } |
|
| 197 | + catch (NamedScopeDuplicationException $e) |
|
| 198 | + { |
|
| 177 | 199 | self::assertSame('root', $e->getScope()); |
| 178 | 200 | throw $e; |
| 179 | 201 | } |
@@ -188,14 +210,19 @@ discard block |
||
| 188 | 210 | { |
| 189 | 211 | self::expectException(BadScopeException::class); |
| 190 | 212 | |
| 191 | - try { |
|
| 213 | + try |
|
| 214 | + { |
|
| 192 | 215 | $root = self::makeContainer(); |
| 193 | - $root->runScoped(static function (Container $c1) { |
|
| 194 | - $c1->runScoped(static function (Container $c2) { |
|
| 216 | + $root->runScoped(static function (Container $c1) |
|
| 217 | + { |
|
| 218 | + $c1->runScoped(static function (Container $c2) |
|
| 219 | + { |
|
| 195 | 220 | $c2->get(AttrScopeFoo::class); |
| 196 | 221 | }); |
| 197 | 222 | }, name: 'bar'); |
| 198 | - } catch (BadScopeException $e) { |
|
| 223 | + } |
|
| 224 | + catch (BadScopeException $e) |
|
| 225 | + { |
|
| 199 | 226 | self::assertSame('foo', $e->getScope()); |
| 200 | 227 | throw $e; |
| 201 | 228 | } |
@@ -66,19 +66,19 @@ discard block |
||
| 66 | 66 | * |
| 67 | 67 | * @throws \Throwable |
| 68 | 68 | */ |
| 69 | - public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
|
| 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 | return $this->state->singletons[$alias]; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | $binding = $this->state->bindings[$alias] ?? null; |
| 76 | 76 | |
| 77 | - if ($binding === null) { |
|
| 77 | + if ($binding === null){ |
|
| 78 | 78 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - try { |
|
| 81 | + try{ |
|
| 82 | 82 | $this->tracer->push( |
| 83 | 83 | false, |
| 84 | 84 | action: 'resolve from binding', |
@@ -108,7 +108,7 @@ discard block |
||
| 108 | 108 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 109 | 109 | default => $binding, |
| 110 | 110 | }; |
| 111 | - } finally { |
|
| 111 | + }finally{ |
|
| 112 | 112 | $this->state->bindings[$alias] ??= $binding; |
| 113 | 113 | $this->tracer->pop(true); |
| 114 | 114 | $this->tracer->pop(false); |
@@ -122,18 +122,18 @@ discard block |
||
| 122 | 122 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
| 123 | 123 | { |
| 124 | 124 | $context = $ctx->context; |
| 125 | - try { |
|
| 125 | + try{ |
|
| 126 | 126 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 127 | - } catch (\ReflectionException $e) { |
|
| 127 | + }catch (\ReflectionException $e){ |
|
| 128 | 128 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | $injector = $binding->injector; |
| 132 | 132 | |
| 133 | - try { |
|
| 133 | + try{ |
|
| 134 | 134 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 135 | 135 | |
| 136 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 136 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 137 | 137 | throw new InjectionException( |
| 138 | 138 | \sprintf( |
| 139 | 139 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | default => (string)$context, |
| 161 | 161 | }); |
| 162 | 162 | |
| 163 | - if (!$reflection->isInstance($instance)) { |
|
| 163 | + if (!$reflection->isInstance($instance)){ |
|
| 164 | 164 | throw new InjectionException( |
| 165 | 165 | \sprintf( |
| 166 | 166 | "Invalid injection response for '%s'.", |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | return $instance; |
| 173 | - } finally { |
|
| 173 | + }finally{ |
|
| 174 | 174 | $this->state->bindings[$ctx->class] ??= $binding; |
| 175 | 175 | } |
| 176 | 176 | } |
@@ -178,29 +178,29 @@ discard block |
||
| 178 | 178 | private function resolveAlias( |
| 179 | 179 | Config\Alias $binding, |
| 180 | 180 | string $alias, |
| 181 | - Stringable|string|null $context, |
|
| 181 | + Stringable | string | null $context, |
|
| 182 | 182 | array $arguments, |
| 183 | 183 | ): mixed { |
| 184 | 184 | $result = $binding->alias === $alias |
| 185 | 185 | ? $this->autowire( |
| 186 | - new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton), |
|
| 186 | + new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton), |
|
| 187 | 187 | $arguments, |
| 188 | 188 | ) |
| 189 | 189 | //Binding is pointing to something else |
| 190 | 190 | : $this->make($binding->alias, $arguments, $context); |
| 191 | 191 | |
| 192 | - if ($binding->singleton && $arguments === []) { |
|
| 192 | + if ($binding->singleton && $arguments === []){ |
|
| 193 | 193 | $this->state->singletons[$alias] = $result; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | return $result; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed |
|
| 199 | + private function resolveProxy(Config\Proxy $binding, string $alias, Stringable | string | null $context): mixed |
|
| 200 | 200 | { |
| 201 | 201 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
| 202 | 202 | |
| 203 | - if ($binding->singleton) { |
|
| 203 | + if ($binding->singleton){ |
|
| 204 | 204 | $this->state->singletons[$alias] = $result; |
| 205 | 205 | } |
| 206 | 206 | |
@@ -210,13 +210,13 @@ discard block |
||
| 210 | 210 | private function resolveShared( |
| 211 | 211 | Config\Shared $binding, |
| 212 | 212 | string $alias, |
| 213 | - Stringable|string|null $context, |
|
| 213 | + Stringable | string | null $context, |
|
| 214 | 214 | array $arguments, |
| 215 | 215 | ): object { |
| 216 | 216 | $avoidCache = $arguments !== []; |
| 217 | 217 | return $avoidCache |
| 218 | 218 | ? $this->createInstance( |
| 219 | - new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
|
| 219 | + new Ctx(alias : $alias, class : $binding->value::class, context : $context), |
|
| 220 | 220 | $arguments, |
| 221 | 221 | ) |
| 222 | 222 | : $binding->value; |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | private function resolveAutowire( |
| 226 | 226 | Config\Autowire $binding, |
| 227 | 227 | string $alias, |
| 228 | - Stringable|string|null $context, |
|
| 228 | + Stringable | string | null $context, |
|
| 229 | 229 | array $arguments, |
| 230 | 230 | ): mixed { |
| 231 | 231 | $instance = $binding->autowire->resolve($this, $arguments); |
@@ -235,17 +235,17 @@ discard block |
||
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | private function resolveFactory( |
| 238 | - Config\Factory|Config\DeferredFactory $binding, |
|
| 238 | + Config\Factory | Config\DeferredFactory $binding, |
|
| 239 | 239 | string $alias, |
| 240 | - Stringable|string|null $context, |
|
| 240 | + Stringable | string | null $context, |
|
| 241 | 241 | array $arguments, |
| 242 | 242 | ): mixed { |
| 243 | 243 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
| 244 | - try { |
|
| 244 | + try{ |
|
| 245 | 245 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 246 | 246 | ? ($binding->factory)() |
| 247 | 247 | : $this->invoker->invoke($binding->factory, $arguments); |
| 248 | - } catch (NotCallableException $e) { |
|
| 248 | + }catch (NotCallableException $e){ |
|
| 249 | 249 | throw new ContainerException( |
| 250 | 250 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 251 | 251 | $e->getCode(), |
@@ -259,24 +259,24 @@ discard block |
||
| 259 | 259 | private function resolveWeakReference( |
| 260 | 260 | Config\WeakReference $binding, |
| 261 | 261 | string $alias, |
| 262 | - Stringable|string|null $context, |
|
| 262 | + Stringable | string | null $context, |
|
| 263 | 263 | array $arguments, |
| 264 | 264 | ): ?object { |
| 265 | 265 | $avoidCache = $arguments !== []; |
| 266 | 266 | |
| 267 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 268 | - try { |
|
| 267 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
| 268 | + try{ |
|
| 269 | 269 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 270 | 270 | |
| 271 | 271 | $object = $this->createInstance( |
| 272 | 272 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 273 | 273 | $arguments, |
| 274 | 274 | ); |
| 275 | - if ($avoidCache) { |
|
| 275 | + if ($avoidCache){ |
|
| 276 | 276 | return $object; |
| 277 | 277 | } |
| 278 | 278 | $binding->reference = WeakReference::create($object); |
| 279 | - } catch (\Throwable) { |
|
| 279 | + }catch (\Throwable){ |
|
| 280 | 280 | throw new ContainerException( |
| 281 | 281 | $this->tracer->combineTraceMessage( |
| 282 | 282 | \sprintf( |
@@ -286,7 +286,7 @@ discard block |
||
| 286 | 286 | ) |
| 287 | 287 | ) |
| 288 | 288 | ); |
| 289 | - } finally { |
|
| 289 | + }finally{ |
|
| 290 | 290 | $this->tracer->pop(); |
| 291 | 291 | } |
| 292 | 292 | } |
@@ -297,23 +297,23 @@ discard block |
||
| 297 | 297 | private function resolveWithoutBinding( |
| 298 | 298 | string $alias, |
| 299 | 299 | array $parameters = [], |
| 300 | - Stringable|string|null $context = null |
|
| 300 | + Stringable | string | null $context = null |
|
| 301 | 301 | ): mixed { |
| 302 | 302 | $parent = $this->scope->getParentFactory(); |
| 303 | 303 | |
| 304 | - if ($parent !== null) { |
|
| 305 | - try { |
|
| 304 | + if ($parent !== null){ |
|
| 305 | + try{ |
|
| 306 | 306 | $this->tracer->push(false, ...[ |
| 307 | 307 | 'current scope' => $this->scope->getScopeName(), |
| 308 | 308 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 309 | 309 | ]); |
| 310 | 310 | /** @psalm-suppress TooManyArguments */ |
| 311 | 311 | return $parent->make($alias, $parameters, $context); |
| 312 | - } catch (BadScopeException $e) { |
|
| 313 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 312 | + }catch (BadScopeException $e){ |
|
| 313 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
| 314 | 314 | throw $e; |
| 315 | 315 | } |
| 316 | - } catch (ContainerExceptionInterface $e) { |
|
| 316 | + }catch (ContainerExceptionInterface $e){ |
|
| 317 | 317 | $className = match (true) { |
| 318 | 318 | $e instanceof NotFoundException => NotFoundException::class, |
| 319 | 319 | default => ContainerException::class, |
@@ -322,19 +322,19 @@ discard block |
||
| 322 | 322 | 'Can\'t resolve `%s`.', |
| 323 | 323 | $alias, |
| 324 | 324 | )), previous: $e); |
| 325 | - } finally { |
|
| 325 | + }finally{ |
|
| 326 | 326 | $this->tracer->pop(false); |
| 327 | 327 | } |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 331 | - try { |
|
| 331 | + try{ |
|
| 332 | 332 | //No direct instructions how to construct class, make is automatically |
| 333 | 333 | return $this->autowire( |
| 334 | 334 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 335 | 335 | $parameters, |
| 336 | 336 | ); |
| 337 | - } finally { |
|
| 337 | + }finally{ |
|
| 338 | 338 | $this->tracer->pop(false); |
| 339 | 339 | } |
| 340 | 340 | } |
@@ -355,7 +355,7 @@ discard block |
||
| 355 | 355 | && |
| 356 | 356 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
| 357 | 357 | )) |
| 358 | - ) { |
|
| 358 | + ){ |
|
| 359 | 359 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 360 | 360 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 361 | 361 | $this->tracer->getRootAlias(), |
@@ -381,13 +381,13 @@ discard block |
||
| 381 | 381 | Ctx $ctx, |
| 382 | 382 | array $arguments, |
| 383 | 383 | ): object { |
| 384 | - if ($this->options->checkScope) { |
|
| 384 | + if ($this->options->checkScope){ |
|
| 385 | 385 | // Check scope name |
| 386 | 386 | $ctx->reflection = new \ReflectionClass($instance); |
| 387 | 387 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 388 | - if ($scopeName !== null) { |
|
| 388 | + if ($scopeName !== null){ |
|
| 389 | 389 | $scope = $this->scope; |
| 390 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 390 | + while ($scope->getScopeName() !== $scopeName){ |
|
| 391 | 391 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 392 | 392 | } |
| 393 | 393 | } |
@@ -416,26 +416,26 @@ discard block |
||
| 416 | 416 | array $arguments, |
| 417 | 417 | ): object { |
| 418 | 418 | $class = $ctx->class; |
| 419 | - try { |
|
| 419 | + try{ |
|
| 420 | 420 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 421 | - } catch (\ReflectionException $e) { |
|
| 421 | + }catch (\ReflectionException $e){ |
|
| 422 | 422 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 423 | 423 | } |
| 424 | 424 | |
| 425 | 425 | // Check scope name |
| 426 | - if ($this->options->checkScope) { |
|
| 426 | + if ($this->options->checkScope){ |
|
| 427 | 427 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 428 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 428 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
| 429 | 429 | throw new BadScopeException($scope, $class); |
| 430 | 430 | } |
| 431 | 431 | } |
| 432 | 432 | |
| 433 | 433 | // We have to construct class using external injector when we know the exact context |
| 434 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
| 434 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
| 435 | 435 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | - if (!$reflection->isInstantiable()) { |
|
| 438 | + if (!$reflection->isInstantiable()){ |
|
| 439 | 439 | $itIs = match (true) { |
| 440 | 440 | $reflection->isEnum() => 'Enum', |
| 441 | 441 | $reflection->isAbstract() => 'Abstract class', |
@@ -448,12 +448,12 @@ discard block |
||
| 448 | 448 | |
| 449 | 449 | $constructor = $reflection->getConstructor(); |
| 450 | 450 | |
| 451 | - if ($constructor !== null) { |
|
| 452 | - try { |
|
| 451 | + if ($constructor !== null){ |
|
| 452 | + try{ |
|
| 453 | 453 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 454 | 454 | $this->tracer->push(true); |
| 455 | 455 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
| 456 | - } catch (ValidationException $e) { |
|
| 456 | + }catch (ValidationException $e){ |
|
| 457 | 457 | throw new ContainerException( |
| 458 | 458 | $this->tracer->combineTraceMessage( |
| 459 | 459 | \sprintf( |
@@ -463,22 +463,22 @@ discard block |
||
| 463 | 463 | ) |
| 464 | 464 | ), |
| 465 | 465 | ); |
| 466 | - } finally { |
|
| 466 | + }finally{ |
|
| 467 | 467 | $this->tracer->pop(true); |
| 468 | 468 | $this->tracer->pop(false); |
| 469 | 469 | } |
| 470 | - try { |
|
| 470 | + try{ |
|
| 471 | 471 | // Using constructor with resolved arguments |
| 472 | 472 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
| 473 | 473 | $this->tracer->push(true); |
| 474 | 474 | $instance = new $class(...$args); |
| 475 | - } catch (\TypeError $e) { |
|
| 475 | + }catch (\TypeError $e){ |
|
| 476 | 476 | throw new WrongTypeException($constructor, $e); |
| 477 | - } finally { |
|
| 477 | + }finally{ |
|
| 478 | 478 | $this->tracer->pop(true); |
| 479 | 479 | $this->tracer->pop(false); |
| 480 | 480 | } |
| 481 | - } else { |
|
| 481 | + }else{ |
|
| 482 | 482 | // No constructor specified |
| 483 | 483 | $instance = $reflection->newInstance(); |
| 484 | 484 | } |
@@ -496,13 +496,13 @@ discard block |
||
| 496 | 496 | $instance = $this->runInflector($instance); |
| 497 | 497 | |
| 498 | 498 | //Declarative singletons |
| 499 | - if ($this->isSingleton($ctx)) { |
|
| 499 | + if ($this->isSingleton($ctx)){ |
|
| 500 | 500 | $this->state->singletons[$ctx->alias] = $instance; |
| 501 | 501 | } |
| 502 | 502 | |
| 503 | 503 | // Register finalizer |
| 504 | 504 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 505 | - if ($finalizer !== null) { |
|
| 505 | + if ($finalizer !== null){ |
|
| 506 | 506 | $this->state->finalizers[] = $finalizer; |
| 507 | 507 | } |
| 508 | 508 | |
@@ -514,12 +514,12 @@ discard block |
||
| 514 | 514 | */ |
| 515 | 515 | private function isSingleton(Ctx $ctx): bool |
| 516 | 516 | { |
| 517 | - if ($ctx->singleton === true) { |
|
| 517 | + if ($ctx->singleton === true){ |
|
| 518 | 518 | return true; |
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 522 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 522 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
| 523 | 523 | return true; |
| 524 | 524 | } |
| 525 | 525 | |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | * @var Attribute\Finalize|null $attribute |
| 534 | 534 | */ |
| 535 | 535 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 536 | - if ($attribute === null) { |
|
| 536 | + if ($attribute === null){ |
|
| 537 | 537 | return null; |
| 538 | 538 | } |
| 539 | 539 | |
@@ -547,10 +547,10 @@ discard block |
||
| 547 | 547 | { |
| 548 | 548 | $scope = $this->scope; |
| 549 | 549 | |
| 550 | - while ($scope !== null) { |
|
| 551 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 552 | - if ($instance instanceof $class) { |
|
| 553 | - foreach ($inflectors as $inflector) { |
|
| 550 | + while ($scope !== null){ |
|
| 551 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
| 552 | + if ($instance instanceof $class){ |
|
| 553 | + foreach ($inflectors as $inflector){ |
|
| 554 | 554 | $instance = $inflector->getParametersCount() > 1 |
| 555 | 555 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 556 | 556 | : ($inflector->inflector)($instance); |
@@ -566,9 +566,9 @@ discard block |
||
| 566 | 566 | |
| 567 | 567 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 568 | 568 | { |
| 569 | - try { |
|
| 569 | + try{ |
|
| 570 | 570 | $this->resolver->validateArguments($reflection, $arguments); |
| 571 | - } catch (\Throwable) { |
|
| 571 | + }catch (\Throwable){ |
|
| 572 | 572 | return false; |
| 573 | 573 | } |
| 574 | 574 | |
@@ -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', |
@@ -108,7 +111,9 @@ discard block |
||
| 108 | 111 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 109 | 112 | default => $binding, |
| 110 | 113 | }; |
| 111 | - } finally { |
|
| 114 | + } |
|
| 115 | + finally |
|
| 116 | + { |
|
| 112 | 117 | $this->state->bindings[$alias] ??= $binding; |
| 113 | 118 | $this->tracer->pop(true); |
| 114 | 119 | $this->tracer->pop(false); |
@@ -122,18 +127,23 @@ discard block |
||
| 122 | 127 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
| 123 | 128 | { |
| 124 | 129 | $context = $ctx->context; |
| 125 | - try { |
|
| 130 | + try |
|
| 131 | + { |
|
| 126 | 132 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 127 | - } catch (\ReflectionException $e) { |
|
| 133 | + } |
|
| 134 | + catch (\ReflectionException $e) |
|
| 135 | + { |
|
| 128 | 136 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 129 | 137 | } |
| 130 | 138 | |
| 131 | 139 | $injector = $binding->injector; |
| 132 | 140 | |
| 133 | - try { |
|
| 141 | + try |
|
| 142 | + { |
|
| 134 | 143 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 135 | 144 | |
| 136 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 145 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 146 | + { |
|
| 137 | 147 | throw new InjectionException( |
| 138 | 148 | \sprintf( |
| 139 | 149 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -160,7 +170,8 @@ discard block |
||
| 160 | 170 | default => (string)$context, |
| 161 | 171 | }); |
| 162 | 172 | |
| 163 | - if (!$reflection->isInstance($instance)) { |
|
| 173 | + if (!$reflection->isInstance($instance)) |
|
| 174 | + { |
|
| 164 | 175 | throw new InjectionException( |
| 165 | 176 | \sprintf( |
| 166 | 177 | "Invalid injection response for '%s'.", |
@@ -170,7 +181,9 @@ discard block |
||
| 170 | 181 | } |
| 171 | 182 | |
| 172 | 183 | return $instance; |
| 173 | - } finally { |
|
| 184 | + } |
|
| 185 | + finally |
|
| 186 | + { |
|
| 174 | 187 | $this->state->bindings[$ctx->class] ??= $binding; |
| 175 | 188 | } |
| 176 | 189 | } |
@@ -189,7 +202,8 @@ discard block |
||
| 189 | 202 | //Binding is pointing to something else |
| 190 | 203 | : $this->make($binding->alias, $arguments, $context); |
| 191 | 204 | |
| 192 | - if ($binding->singleton && $arguments === []) { |
|
| 205 | + if ($binding->singleton && $arguments === []) |
|
| 206 | + { |
|
| 193 | 207 | $this->state->singletons[$alias] = $result; |
| 194 | 208 | } |
| 195 | 209 | |
@@ -200,7 +214,8 @@ discard block |
||
| 200 | 214 | { |
| 201 | 215 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
| 202 | 216 | |
| 203 | - if ($binding->singleton) { |
|
| 217 | + if ($binding->singleton) |
|
| 218 | + { |
|
| 204 | 219 | $this->state->singletons[$alias] = $result; |
| 205 | 220 | } |
| 206 | 221 | |
@@ -241,11 +256,14 @@ discard block |
||
| 241 | 256 | array $arguments, |
| 242 | 257 | ): mixed { |
| 243 | 258 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
| 244 | - try { |
|
| 259 | + try |
|
| 260 | + { |
|
| 245 | 261 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 246 | 262 | ? ($binding->factory)() |
| 247 | 263 | : $this->invoker->invoke($binding->factory, $arguments); |
| 248 | - } catch (NotCallableException $e) { |
|
| 264 | + } |
|
| 265 | + catch (NotCallableException $e) |
|
| 266 | + { |
|
| 249 | 267 | throw new ContainerException( |
| 250 | 268 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 251 | 269 | $e->getCode(), |
@@ -264,19 +282,24 @@ discard block |
||
| 264 | 282 | ): ?object { |
| 265 | 283 | $avoidCache = $arguments !== []; |
| 266 | 284 | |
| 267 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 268 | - try { |
|
| 285 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 286 | + { |
|
| 287 | + try |
|
| 288 | + { |
|
| 269 | 289 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 270 | 290 | |
| 271 | 291 | $object = $this->createInstance( |
| 272 | 292 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 273 | 293 | $arguments, |
| 274 | 294 | ); |
| 275 | - if ($avoidCache) { |
|
| 295 | + if ($avoidCache) |
|
| 296 | + { |
|
| 276 | 297 | return $object; |
| 277 | 298 | } |
| 278 | 299 | $binding->reference = WeakReference::create($object); |
| 279 | - } catch (\Throwable) { |
|
| 300 | + } |
|
| 301 | + catch (\Throwable) |
|
| 302 | + { |
|
| 280 | 303 | throw new ContainerException( |
| 281 | 304 | $this->tracer->combineTraceMessage( |
| 282 | 305 | \sprintf( |
@@ -286,7 +309,9 @@ discard block |
||
| 286 | 309 | ) |
| 287 | 310 | ) |
| 288 | 311 | ); |
| 289 | - } finally { |
|
| 312 | + } |
|
| 313 | + finally |
|
| 314 | + { |
|
| 290 | 315 | $this->tracer->pop(); |
| 291 | 316 | } |
| 292 | 317 | } |
@@ -301,19 +326,26 @@ discard block |
||
| 301 | 326 | ): mixed { |
| 302 | 327 | $parent = $this->scope->getParentFactory(); |
| 303 | 328 | |
| 304 | - if ($parent !== null) { |
|
| 305 | - try { |
|
| 329 | + if ($parent !== null) |
|
| 330 | + { |
|
| 331 | + try |
|
| 332 | + { |
|
| 306 | 333 | $this->tracer->push(false, ...[ |
| 307 | 334 | 'current scope' => $this->scope->getScopeName(), |
| 308 | 335 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 309 | 336 | ]); |
| 310 | 337 | /** @psalm-suppress TooManyArguments */ |
| 311 | 338 | return $parent->make($alias, $parameters, $context); |
| 312 | - } catch (BadScopeException $e) { |
|
| 313 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 339 | + } |
|
| 340 | + catch (BadScopeException $e) |
|
| 341 | + { |
|
| 342 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 343 | + { |
|
| 314 | 344 | throw $e; |
| 315 | 345 | } |
| 316 | - } catch (ContainerExceptionInterface $e) { |
|
| 346 | + } |
|
| 347 | + catch (ContainerExceptionInterface $e) |
|
| 348 | + { |
|
| 317 | 349 | $className = match (true) { |
| 318 | 350 | $e instanceof NotFoundException => NotFoundException::class, |
| 319 | 351 | default => ContainerException::class, |
@@ -322,19 +354,24 @@ discard block |
||
| 322 | 354 | 'Can\'t resolve `%s`.', |
| 323 | 355 | $alias, |
| 324 | 356 | )), previous: $e); |
| 325 | - } finally { |
|
| 357 | + } |
|
| 358 | + finally |
|
| 359 | + { |
|
| 326 | 360 | $this->tracer->pop(false); |
| 327 | 361 | } |
| 328 | 362 | } |
| 329 | 363 | |
| 330 | 364 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 331 | - try { |
|
| 365 | + try |
|
| 366 | + { |
|
| 332 | 367 | //No direct instructions how to construct class, make is automatically |
| 333 | 368 | return $this->autowire( |
| 334 | 369 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 335 | 370 | $parameters, |
| 336 | 371 | ); |
| 337 | - } finally { |
|
| 372 | + } |
|
| 373 | + finally |
|
| 374 | + { |
|
| 338 | 375 | $this->tracer->pop(false); |
| 339 | 376 | } |
| 340 | 377 | } |
@@ -381,13 +418,16 @@ discard block |
||
| 381 | 418 | Ctx $ctx, |
| 382 | 419 | array $arguments, |
| 383 | 420 | ): object { |
| 384 | - if ($this->options->checkScope) { |
|
| 421 | + if ($this->options->checkScope) |
|
| 422 | + { |
|
| 385 | 423 | // Check scope name |
| 386 | 424 | $ctx->reflection = new \ReflectionClass($instance); |
| 387 | 425 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 388 | - if ($scopeName !== null) { |
|
| 426 | + if ($scopeName !== null) |
|
| 427 | + { |
|
| 389 | 428 | $scope = $this->scope; |
| 390 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 429 | + while ($scope->getScopeName() !== $scopeName) |
|
| 430 | + { |
|
| 391 | 431 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 392 | 432 | } |
| 393 | 433 | } |
@@ -416,26 +456,33 @@ discard block |
||
| 416 | 456 | array $arguments, |
| 417 | 457 | ): object { |
| 418 | 458 | $class = $ctx->class; |
| 419 | - try { |
|
| 459 | + try |
|
| 460 | + { |
|
| 420 | 461 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 421 | - } catch (\ReflectionException $e) { |
|
| 462 | + } |
|
| 463 | + catch (\ReflectionException $e) |
|
| 464 | + { |
|
| 422 | 465 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 423 | 466 | } |
| 424 | 467 | |
| 425 | 468 | // Check scope name |
| 426 | - if ($this->options->checkScope) { |
|
| 469 | + if ($this->options->checkScope) |
|
| 470 | + { |
|
| 427 | 471 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 428 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 472 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 473 | + { |
|
| 429 | 474 | throw new BadScopeException($scope, $class); |
| 430 | 475 | } |
| 431 | 476 | } |
| 432 | 477 | |
| 433 | 478 | // We have to construct class using external injector when we know the exact context |
| 434 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
| 479 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
| 480 | + { |
|
| 435 | 481 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
| 436 | 482 | } |
| 437 | 483 | |
| 438 | - if (!$reflection->isInstantiable()) { |
|
| 484 | + if (!$reflection->isInstantiable()) |
|
| 485 | + { |
|
| 439 | 486 | $itIs = match (true) { |
| 440 | 487 | $reflection->isEnum() => 'Enum', |
| 441 | 488 | $reflection->isAbstract() => 'Abstract class', |
@@ -448,12 +495,16 @@ discard block |
||
| 448 | 495 | |
| 449 | 496 | $constructor = $reflection->getConstructor(); |
| 450 | 497 | |
| 451 | - if ($constructor !== null) { |
|
| 452 | - try { |
|
| 498 | + if ($constructor !== null) |
|
| 499 | + { |
|
| 500 | + try |
|
| 501 | + { |
|
| 453 | 502 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 454 | 503 | $this->tracer->push(true); |
| 455 | 504 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
| 456 | - } catch (ValidationException $e) { |
|
| 505 | + } |
|
| 506 | + catch (ValidationException $e) |
|
| 507 | + { |
|
| 457 | 508 | throw new ContainerException( |
| 458 | 509 | $this->tracer->combineTraceMessage( |
| 459 | 510 | \sprintf( |
@@ -463,22 +514,31 @@ discard block |
||
| 463 | 514 | ) |
| 464 | 515 | ), |
| 465 | 516 | ); |
| 466 | - } finally { |
|
| 517 | + } |
|
| 518 | + finally |
|
| 519 | + { |
|
| 467 | 520 | $this->tracer->pop(true); |
| 468 | 521 | $this->tracer->pop(false); |
| 469 | 522 | } |
| 470 | - try { |
|
| 523 | + try |
|
| 524 | + { |
|
| 471 | 525 | // Using constructor with resolved arguments |
| 472 | 526 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
| 473 | 527 | $this->tracer->push(true); |
| 474 | 528 | $instance = new $class(...$args); |
| 475 | - } catch (\TypeError $e) { |
|
| 529 | + } |
|
| 530 | + catch (\TypeError $e) |
|
| 531 | + { |
|
| 476 | 532 | throw new WrongTypeException($constructor, $e); |
| 477 | - } finally { |
|
| 533 | + } |
|
| 534 | + finally |
|
| 535 | + { |
|
| 478 | 536 | $this->tracer->pop(true); |
| 479 | 537 | $this->tracer->pop(false); |
| 480 | 538 | } |
| 481 | - } else { |
|
| 539 | + } |
|
| 540 | + else |
|
| 541 | + { |
|
| 482 | 542 | // No constructor specified |
| 483 | 543 | $instance = $reflection->newInstance(); |
| 484 | 544 | } |
@@ -496,13 +556,15 @@ discard block |
||
| 496 | 556 | $instance = $this->runInflector($instance); |
| 497 | 557 | |
| 498 | 558 | //Declarative singletons |
| 499 | - if ($this->isSingleton($ctx)) { |
|
| 559 | + if ($this->isSingleton($ctx)) |
|
| 560 | + { |
|
| 500 | 561 | $this->state->singletons[$ctx->alias] = $instance; |
| 501 | 562 | } |
| 502 | 563 | |
| 503 | 564 | // Register finalizer |
| 504 | 565 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 505 | - if ($finalizer !== null) { |
|
| 566 | + if ($finalizer !== null) |
|
| 567 | + { |
|
| 506 | 568 | $this->state->finalizers[] = $finalizer; |
| 507 | 569 | } |
| 508 | 570 | |
@@ -514,12 +576,14 @@ discard block |
||
| 514 | 576 | */ |
| 515 | 577 | private function isSingleton(Ctx $ctx): bool |
| 516 | 578 | { |
| 517 | - if ($ctx->singleton === true) { |
|
| 579 | + if ($ctx->singleton === true) |
|
| 580 | + { |
|
| 518 | 581 | return true; |
| 519 | 582 | } |
| 520 | 583 | |
| 521 | 584 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 522 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 585 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 586 | + { |
|
| 523 | 587 | return true; |
| 524 | 588 | } |
| 525 | 589 | |
@@ -533,7 +597,8 @@ discard block |
||
| 533 | 597 | * @var Attribute\Finalize|null $attribute |
| 534 | 598 | */ |
| 535 | 599 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 536 | - if ($attribute === null) { |
|
| 600 | + if ($attribute === null) |
|
| 601 | + { |
|
| 537 | 602 | return null; |
| 538 | 603 | } |
| 539 | 604 | |
@@ -547,10 +612,14 @@ discard block |
||
| 547 | 612 | { |
| 548 | 613 | $scope = $this->scope; |
| 549 | 614 | |
| 550 | - while ($scope !== null) { |
|
| 551 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 552 | - if ($instance instanceof $class) { |
|
| 553 | - foreach ($inflectors as $inflector) { |
|
| 615 | + while ($scope !== null) |
|
| 616 | + { |
|
| 617 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 618 | + { |
|
| 619 | + if ($instance instanceof $class) |
|
| 620 | + { |
|
| 621 | + foreach ($inflectors as $inflector) |
|
| 622 | + { |
|
| 554 | 623 | $instance = $inflector->getParametersCount() > 1 |
| 555 | 624 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 556 | 625 | : ($inflector->inflector)($instance); |
@@ -566,9 +635,12 @@ discard block |
||
| 566 | 635 | |
| 567 | 636 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 568 | 637 | { |
| 569 | - try { |
|
| 638 | + try |
|
| 639 | + { |
|
| 570 | 640 | $this->resolver->validateArguments($reflection, $arguments); |
| 571 | - } catch (\Throwable) { |
|
| 641 | + } |
|
| 642 | + catch (\Throwable) |
|
| 643 | + { |
|
| 572 | 644 | return false; |
| 573 | 645 | } |
| 574 | 646 | |