@@ -32,21 +32,26 @@ discard block |
||
| 32 | 32 | { |
| 33 | 33 | public function __construct( |
| 34 | 34 | protected readonly State $state, |
| 35 | - ) {} |
|
| 35 | + ) { |
|
| 36 | +} |
|
| 36 | 37 | |
| 37 | 38 | /** |
| 38 | 39 | * @param TResolver|object $resolver |
| 39 | 40 | */ |
| 40 | 41 | public function bind(string $alias, mixed $resolver): void |
| 41 | 42 | { |
| 42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 43 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
| 44 | + { |
|
| 43 | 45 | $this->state->inflectors[$alias][] = $resolver; |
| 44 | 46 | return; |
| 45 | 47 | } |
| 46 | 48 | |
| 47 | - try { |
|
| 49 | + try |
|
| 50 | + { |
|
| 48 | 51 | $config = $this->makeConfig($resolver, false); |
| 49 | - } catch (\Throwable $e) { |
|
| 52 | + } |
|
| 53 | + catch (\Throwable $e) |
|
| 54 | + { |
|
| 50 | 55 | throw $this->invalidBindingException($alias, $e); |
| 51 | 56 | } |
| 52 | 57 | |
@@ -58,9 +63,12 @@ discard block |
||
| 58 | 63 | */ |
| 59 | 64 | public function bindSingleton(string $alias, mixed $resolver): void |
| 60 | 65 | { |
| 61 | - try { |
|
| 66 | + try |
|
| 67 | + { |
|
| 62 | 68 | $config = $this->makeConfig($resolver, true); |
| 63 | - } catch (\Throwable $e) { |
|
| 69 | + } |
|
| 70 | + catch (\Throwable $e) |
|
| 71 | + { |
|
| 64 | 72 | throw $this->invalidBindingException($alias, $e); |
| 65 | 73 | } |
| 66 | 74 | |
@@ -72,13 +80,16 @@ discard block |
||
| 72 | 80 | $bindings = &$this->state->bindings; |
| 73 | 81 | |
| 74 | 82 | $flags = []; |
| 75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 83 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
| 84 | + { |
|
| 76 | 85 | //Checking alias tree |
| 77 | - if ($flags[$binding->alias] ?? false) { |
|
| 86 | + if ($flags[$binding->alias] ?? false) |
|
| 87 | + { |
|
| 78 | 88 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 79 | 89 | } |
| 80 | 90 | |
| 81 | - if (\array_key_exists($alias, $this->state->singletons)) { |
|
| 91 | + if (\array_key_exists($alias, $this->state->singletons)) |
|
| 92 | + { |
|
| 82 | 93 | return true; |
| 83 | 94 | } |
| 84 | 95 | |
@@ -102,7 +113,8 @@ discard block |
||
| 102 | 113 | public function removeInjector(string $class): void |
| 103 | 114 | { |
| 104 | 115 | unset($this->state->injectors[$class]); |
| 105 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 116 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
| 117 | + { |
|
| 106 | 118 | return; |
| 107 | 119 | } |
| 108 | 120 | unset($this->state->bindings[$class]); |
@@ -110,13 +122,17 @@ discard block |
||
| 110 | 122 | |
| 111 | 123 | public function hasInjector(string $class): bool |
| 112 | 124 | { |
| 113 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 125 | + if (\array_key_exists($class, $this->state->injectors)) |
|
| 126 | + { |
|
| 114 | 127 | return true; |
| 115 | 128 | } |
| 116 | 129 | |
| 117 | - try { |
|
| 130 | + try |
|
| 131 | + { |
|
| 118 | 132 | $reflection = new \ReflectionClass($class); |
| 119 | - } catch (\ReflectionException $e) { |
|
| 133 | + } |
|
| 134 | + catch (\ReflectionException $e) |
|
| 135 | + { |
|
| 120 | 136 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 121 | 137 | } |
| 122 | 138 | |
@@ -131,7 +147,8 @@ discard block |
||
| 131 | 147 | } |
| 132 | 148 | |
| 133 | 149 | // check interfaces |
| 134 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 150 | + foreach ($this->state->injectors as $target => $injector) |
|
| 151 | + { |
|
| 135 | 152 | if ( |
| 136 | 153 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 137 | 154 | || |
@@ -163,15 +180,18 @@ discard block |
||
| 163 | 180 | |
| 164 | 181 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 165 | 182 | { |
| 166 | - if (\is_callable($resolver)) { |
|
| 183 | + if (\is_callable($resolver)) |
|
| 184 | + { |
|
| 167 | 185 | return new Factory($resolver, $singleton); |
| 168 | 186 | } |
| 169 | 187 | |
| 170 | 188 | // Validate lazy invokable array |
| 171 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 189 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
| 190 | + { |
|
| 172 | 191 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 173 | 192 | } |
| 174 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 193 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
| 194 | + { |
|
| 175 | 195 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 176 | 196 | } |
| 177 | 197 | |
@@ -191,13 +211,15 @@ discard block |
||
| 191 | 211 | |
| 192 | 212 | private function setBinding(string $alias, Binding $config): void |
| 193 | 213 | { |
| 194 | - if (isset($this->state->singletons[$alias])) { |
|
| 214 | + if (isset($this->state->singletons[$alias])) |
|
| 215 | + { |
|
| 195 | 216 | throw new SingletonOverloadException($alias); |
| 196 | 217 | } |
| 197 | 218 | |
| 198 | 219 | $this->state->bindings[$alias] = $config; |
| 199 | 220 | |
| 200 | - if ($config instanceof Injectable) { |
|
| 221 | + if ($config instanceof Injectable) |
|
| 222 | + { |
|
| 201 | 223 | $this->state->injectors[$alias] = $config->injector; |
| 202 | 224 | } |
| 203 | 225 | } |
@@ -63,7 +63,8 @@ discard block |
||
| 63 | 63 | string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
| 64 | 64 | private Options $options = new Options(), |
| 65 | 65 | ) { |
| 66 | - if (\is_object($scopeName)) { |
|
| 66 | + if (\is_object($scopeName)) |
|
| 67 | + { |
|
| 67 | 68 | $scopeName = (string) $scopeName->value; |
| 68 | 69 | } |
| 69 | 70 | |
@@ -178,23 +179,29 @@ discard block |
||
| 178 | 179 | */ |
| 179 | 180 | public function runScope(Scope|array $bindings, callable $scope): mixed |
| 180 | 181 | { |
| 181 | - if (!\is_array($bindings)) { |
|
| 182 | + if (!\is_array($bindings)) |
|
| 183 | + { |
|
| 182 | 184 | return $this->runIsolatedScope($bindings, $scope); |
| 183 | 185 | } |
| 184 | 186 | |
| 185 | 187 | $binds = &$this->state->bindings; |
| 186 | 188 | $singletons = &$this->state->singletons; |
| 187 | 189 | $cleanup = $previous = $prevSin = []; |
| 188 | - foreach ($bindings as $alias => $resolver) { |
|
| 190 | + foreach ($bindings as $alias => $resolver) |
|
| 191 | + { |
|
| 189 | 192 | // Store previous bindings |
| 190 | - if (isset($binds[$alias])) { |
|
| 193 | + if (isset($binds[$alias])) |
|
| 194 | + { |
|
| 191 | 195 | $previous[$alias] = $binds[$alias]; |
| 192 | - } else { |
|
| 196 | + } |
|
| 197 | + else |
|
| 198 | + { |
|
| 193 | 199 | // Store bindings to be removed |
| 194 | 200 | $cleanup[] = $alias; |
| 195 | 201 | } |
| 196 | 202 | // Store previous singletons |
| 197 | - if (isset($singletons[$alias])) { |
|
| 203 | + if (isset($singletons[$alias])) |
|
| 204 | + { |
|
| 198 | 205 | $prevSin[$alias] = $singletons[$alias]; |
| 199 | 206 | unset($singletons[$alias]); |
| 200 | 207 | } |
@@ -202,21 +209,27 @@ discard block |
||
| 202 | 209 | $this->binder->bind($alias, $resolver); |
| 203 | 210 | } |
| 204 | 211 | |
| 205 | - try { |
|
| 212 | + try |
|
| 213 | + { |
|
| 206 | 214 | return ContainerScope::getContainer() !== $this |
| 207 | 215 | ? ContainerScope::runScope($this, $scope) |
| 208 | 216 | : $scope($this); |
| 209 | - } finally { |
|
| 217 | + } |
|
| 218 | + finally |
|
| 219 | + { |
|
| 210 | 220 | // Remove new bindings |
| 211 | - foreach ($cleanup as $alias) { |
|
| 221 | + foreach ($cleanup as $alias) |
|
| 222 | + { |
|
| 212 | 223 | unset($binds[$alias], $singletons[$alias]); |
| 213 | 224 | } |
| 214 | 225 | // Restore previous bindings |
| 215 | - foreach ($previous as $alias => $resolver) { |
|
| 226 | + foreach ($previous as $alias => $resolver) |
|
| 227 | + { |
|
| 216 | 228 | $binds[$alias] = $resolver; |
| 217 | 229 | } |
| 218 | 230 | // Restore singletons |
| 219 | - foreach ($prevSin as $alias => $instance) { |
|
| 231 | + foreach ($prevSin as $alias => $instance) |
|
| 232 | + { |
|
| 220 | 233 | $singletons[$alias] = $instance; |
| 221 | 234 | } |
| 222 | 235 | } |
@@ -266,7 +279,8 @@ discard block |
||
| 266 | 279 | */ |
| 267 | 280 | public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
| 268 | 281 | { |
| 269 | - if ($force ?? $this->options->allowSingletonsRebind) { |
|
| 282 | + if ($force ?? $this->options->allowSingletonsRebind) |
|
| 283 | + { |
|
| 270 | 284 | $this->binder->removeBinding($alias); |
| 271 | 285 | } |
| 272 | 286 | |
@@ -337,8 +351,10 @@ discard block |
||
| 337 | 351 | ], $this->options); |
| 338 | 352 | |
| 339 | 353 | // Create container services |
| 340 | - foreach ($container->config as $property => $class) { |
|
| 341 | - if (\property_exists($container, $property)) { |
|
| 354 | + foreach ($container->config as $property => $class) |
|
| 355 | + { |
|
| 356 | + if (\property_exists($container, $property)) |
|
| 357 | + { |
|
| 342 | 358 | $container->$property = $constructor->get($property, $class); |
| 343 | 359 | } |
| 344 | 360 | } |
@@ -352,7 +368,8 @@ discard block |
||
| 352 | 368 | private function closeScope(): void |
| 353 | 369 | { |
| 354 | 370 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 355 | - if (!isset($this->scope)) { |
|
| 371 | + if (!isset($this->scope)) |
|
| 372 | + { |
|
| 356 | 373 | $this->destruct(); |
| 357 | 374 | return; |
| 358 | 375 | } |
@@ -361,10 +378,14 @@ discard block |
||
| 361 | 378 | |
| 362 | 379 | // Run finalizers |
| 363 | 380 | $errors = []; |
| 364 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 365 | - try { |
|
| 381 | + foreach ($this->state->finalizers as $finalizer) |
|
| 382 | + { |
|
| 383 | + try |
|
| 384 | + { |
|
| 366 | 385 | $this->invoker->invoke($finalizer); |
| 367 | - } catch (\Throwable $e) { |
|
| 386 | + } |
|
| 387 | + catch (\Throwable $e) |
|
| 388 | + { |
|
| 368 | 389 | $errors[] = $e; |
| 369 | 390 | } |
| 370 | 391 | } |
@@ -373,7 +394,8 @@ discard block |
||
| 373 | 394 | $this->destruct(); |
| 374 | 395 | |
| 375 | 396 | // Throw collected errors |
| 376 | - if ($errors !== []) { |
|
| 397 | + if ($errors !== []) |
|
| 398 | + { |
|
| 377 | 399 | throw new FinalizersException($scopeName, $errors); |
| 378 | 400 | } |
| 379 | 401 | } |
@@ -395,18 +417,22 @@ discard block |
||
| 395 | 417 | $container->scope->setParent($this, $this->scope, $this->factory); |
| 396 | 418 | |
| 397 | 419 | // Add specific bindings |
| 398 | - foreach ($config->bindings as $alias => $resolver) { |
|
| 420 | + foreach ($config->bindings as $alias => $resolver) |
|
| 421 | + { |
|
| 399 | 422 | $container->binder->bind($alias, $resolver); |
| 400 | 423 | } |
| 401 | 424 | |
| 402 | 425 | return ContainerScope::runScope( |
| 403 | 426 | $container, |
| 404 | 427 | static function (self $container) use ($config, $closure): mixed { |
| 405 | - try { |
|
| 428 | + try |
|
| 429 | + { |
|
| 406 | 430 | return $config->autowire |
| 407 | 431 | ? $container->invoke($closure) |
| 408 | 432 | : $closure($container); |
| 409 | - } finally { |
|
| 433 | + } |
|
| 434 | + finally |
|
| 435 | + { |
|
| 410 | 436 | $container->closeScope(); |
| 411 | 437 | } |
| 412 | 438 | } |
@@ -12,7 +12,8 @@ |
||
| 12 | 12 | public function __construct( |
| 13 | 13 | public readonly object $value, |
| 14 | 14 | public readonly bool $singleton = false, |
| 15 | - ) {} |
|
| 15 | + ) { |
|
| 16 | +} |
|
| 16 | 17 | |
| 17 | 18 | public function __toString(): string |
| 18 | 19 | { |
@@ -70,17 +70,20 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
| 72 | 72 | { |
| 73 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 73 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
| 74 | + { |
|
| 74 | 75 | return $this->state->singletons[$alias]; |
| 75 | 76 | } |
| 76 | 77 | |
| 77 | 78 | $binding = $this->state->bindings[$alias] ?? null; |
| 78 | 79 | |
| 79 | - if ($binding === null) { |
|
| 80 | + if ($binding === null) |
|
| 81 | + { |
|
| 80 | 82 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 81 | 83 | } |
| 82 | 84 | |
| 83 | - try { |
|
| 85 | + try |
|
| 86 | + { |
|
| 84 | 87 | $this->tracer->push( |
| 85 | 88 | false, |
| 86 | 89 | action: 'resolve from binding', |
@@ -110,7 +113,9 @@ discard block |
||
| 110 | 113 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 111 | 114 | default => $binding, |
| 112 | 115 | }; |
| 113 | - } finally { |
|
| 116 | + } |
|
| 117 | + finally |
|
| 118 | + { |
|
| 114 | 119 | $this->state->bindings[$alias] ??= $binding; |
| 115 | 120 | $this->tracer->pop(true); |
| 116 | 121 | $this->tracer->pop(false); |
@@ -124,18 +129,23 @@ discard block |
||
| 124 | 129 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
| 125 | 130 | { |
| 126 | 131 | $context = $ctx->context; |
| 127 | - try { |
|
| 132 | + try |
|
| 133 | + { |
|
| 128 | 134 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 129 | - } catch (\ReflectionException $e) { |
|
| 135 | + } |
|
| 136 | + catch (\ReflectionException $e) |
|
| 137 | + { |
|
| 130 | 138 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 131 | 139 | } |
| 132 | 140 | |
| 133 | 141 | $injector = $binding->injector; |
| 134 | 142 | |
| 135 | - try { |
|
| 143 | + try |
|
| 144 | + { |
|
| 136 | 145 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 137 | 146 | |
| 138 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 147 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 148 | + { |
|
| 139 | 149 | throw new InjectionException( |
| 140 | 150 | \sprintf( |
| 141 | 151 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -162,7 +172,8 @@ discard block |
||
| 162 | 172 | default => (string)$context, |
| 163 | 173 | }); |
| 164 | 174 | |
| 165 | - if (!$reflection->isInstance($instance)) { |
|
| 175 | + if (!$reflection->isInstance($instance)) |
|
| 176 | + { |
|
| 166 | 177 | throw new InjectionException( |
| 167 | 178 | \sprintf( |
| 168 | 179 | "Invalid injection response for '%s'.", |
@@ -172,7 +183,9 @@ discard block |
||
| 172 | 183 | } |
| 173 | 184 | |
| 174 | 185 | return $instance; |
| 175 | - } finally { |
|
| 186 | + } |
|
| 187 | + finally |
|
| 188 | + { |
|
| 176 | 189 | $this->state->bindings[$ctx->class] ??= $binding; |
| 177 | 190 | } |
| 178 | 191 | } |
@@ -191,7 +204,8 @@ discard block |
||
| 191 | 204 | //Binding is pointing to something else |
| 192 | 205 | : $this->make($binding->alias, $arguments, $context); |
| 193 | 206 | |
| 194 | - if ($binding->singleton && $arguments === []) { |
|
| 207 | + if ($binding->singleton && $arguments === []) |
|
| 208 | + { |
|
| 195 | 209 | $this->state->singletons[$alias] = $result; |
| 196 | 210 | } |
| 197 | 211 | |
@@ -200,7 +214,8 @@ discard block |
||
| 200 | 214 | |
| 201 | 215 | private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed |
| 202 | 216 | { |
| 203 | - if ($context instanceof RetryContext) { |
|
| 217 | + if ($context instanceof RetryContext) |
|
| 218 | + { |
|
| 204 | 219 | return $binding->fallbackFactory === null |
| 205 | 220 | ? throw new RecursiveProxyException( |
| 206 | 221 | $alias, |
@@ -211,7 +226,8 @@ discard block |
||
| 211 | 226 | |
| 212 | 227 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
| 213 | 228 | |
| 214 | - if ($binding->singleton) { |
|
| 229 | + if ($binding->singleton) |
|
| 230 | + { |
|
| 215 | 231 | $this->state->singletons[$alias] = $result; |
| 216 | 232 | } |
| 217 | 233 | |
@@ -226,14 +242,16 @@ discard block |
||
| 226 | 242 | ): object { |
| 227 | 243 | $avoidCache = $arguments !== []; |
| 228 | 244 | |
| 229 | - if ($avoidCache) { |
|
| 245 | + if ($avoidCache) |
|
| 246 | + { |
|
| 230 | 247 | return $this->createInstance( |
| 231 | 248 | new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
| 232 | 249 | $arguments, |
| 233 | 250 | ); |
| 234 | 251 | } |
| 235 | 252 | |
| 236 | - if ($binding->singleton) { |
|
| 253 | + if ($binding->singleton) |
|
| 254 | + { |
|
| 237 | 255 | $this->state->singletons[$alias] = $binding->value; |
| 238 | 256 | } |
| 239 | 257 | |
@@ -263,11 +281,14 @@ discard block |
||
| 263 | 281 | array $arguments, |
| 264 | 282 | ): mixed { |
| 265 | 283 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
| 266 | - try { |
|
| 284 | + try |
|
| 285 | + { |
|
| 267 | 286 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 268 | 287 | ? ($binding->factory)() |
| 269 | 288 | : $this->invoker->invoke($binding->factory, $arguments); |
| 270 | - } catch (NotCallableException $e) { |
|
| 289 | + } |
|
| 290 | + catch (NotCallableException $e) |
|
| 291 | + { |
|
| 271 | 292 | throw new ContainerException( |
| 272 | 293 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 273 | 294 | $e->getCode(), |
@@ -286,19 +307,24 @@ discard block |
||
| 286 | 307 | ): ?object { |
| 287 | 308 | $avoidCache = $arguments !== []; |
| 288 | 309 | |
| 289 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 290 | - try { |
|
| 310 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 311 | + { |
|
| 312 | + try |
|
| 313 | + { |
|
| 291 | 314 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 292 | 315 | |
| 293 | 316 | $object = $this->createInstance( |
| 294 | 317 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 295 | 318 | $arguments, |
| 296 | 319 | ); |
| 297 | - if ($avoidCache) { |
|
| 320 | + if ($avoidCache) |
|
| 321 | + { |
|
| 298 | 322 | return $object; |
| 299 | 323 | } |
| 300 | 324 | $binding->reference = WeakReference::create($object); |
| 301 | - } catch (\Throwable) { |
|
| 325 | + } |
|
| 326 | + catch (\Throwable) |
|
| 327 | + { |
|
| 302 | 328 | throw new ContainerException( |
| 303 | 329 | $this->tracer->combineTraceMessage( |
| 304 | 330 | \sprintf( |
@@ -308,7 +334,9 @@ discard block |
||
| 308 | 334 | ) |
| 309 | 335 | ) |
| 310 | 336 | ); |
| 311 | - } finally { |
|
| 337 | + } |
|
| 338 | + finally |
|
| 339 | + { |
|
| 312 | 340 | $this->tracer->pop(); |
| 313 | 341 | } |
| 314 | 342 | } |
@@ -323,19 +351,26 @@ discard block |
||
| 323 | 351 | ): mixed { |
| 324 | 352 | $parent = $this->scope->getParentFactory(); |
| 325 | 353 | |
| 326 | - if ($parent !== null) { |
|
| 327 | - try { |
|
| 354 | + if ($parent !== null) |
|
| 355 | + { |
|
| 356 | + try |
|
| 357 | + { |
|
| 328 | 358 | $this->tracer->push(false, ...[ |
| 329 | 359 | 'current scope' => $this->scope->getScopeName(), |
| 330 | 360 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 331 | 361 | ]); |
| 332 | 362 | /** @psalm-suppress TooManyArguments */ |
| 333 | 363 | return $parent->make($alias, $parameters, $context); |
| 334 | - } catch (BadScopeException $e) { |
|
| 335 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 364 | + } |
|
| 365 | + catch (BadScopeException $e) |
|
| 366 | + { |
|
| 367 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 368 | + { |
|
| 336 | 369 | throw $e; |
| 337 | 370 | } |
| 338 | - } catch (ContainerExceptionInterface $e) { |
|
| 371 | + } |
|
| 372 | + catch (ContainerExceptionInterface $e) |
|
| 373 | + { |
|
| 339 | 374 | $className = match (true) { |
| 340 | 375 | $e instanceof NotFoundException => NotFoundException::class, |
| 341 | 376 | $e instanceof RecursiveProxyException => throw $e, |
@@ -345,19 +380,24 @@ discard block |
||
| 345 | 380 | 'Can\'t resolve `%s`.', |
| 346 | 381 | $alias, |
| 347 | 382 | )), previous: $e); |
| 348 | - } finally { |
|
| 383 | + } |
|
| 384 | + finally |
|
| 385 | + { |
|
| 349 | 386 | $this->tracer->pop(false); |
| 350 | 387 | } |
| 351 | 388 | } |
| 352 | 389 | |
| 353 | 390 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 354 | - try { |
|
| 391 | + try |
|
| 392 | + { |
|
| 355 | 393 | //No direct instructions how to construct class, make is automatically |
| 356 | 394 | return $this->autowire( |
| 357 | 395 | new Ctx(alias: $alias, class: $alias, context: $context), |
| 358 | 396 | $parameters, |
| 359 | 397 | ); |
| 360 | - } finally { |
|
| 398 | + } |
|
| 399 | + finally |
|
| 400 | + { |
|
| 361 | 401 | $this->tracer->pop(false); |
| 362 | 402 | } |
| 363 | 403 | } |
@@ -404,13 +444,16 @@ discard block |
||
| 404 | 444 | Ctx $ctx, |
| 405 | 445 | array $arguments, |
| 406 | 446 | ): object { |
| 407 | - if ($this->options->checkScope) { |
|
| 447 | + if ($this->options->checkScope) |
|
| 448 | + { |
|
| 408 | 449 | // Check scope name |
| 409 | 450 | $ctx->reflection = new \ReflectionClass($instance); |
| 410 | 451 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 411 | - if ($scopeName !== null) { |
|
| 452 | + if ($scopeName !== null) |
|
| 453 | + { |
|
| 412 | 454 | $scope = $this->scope; |
| 413 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 455 | + while ($scope->getScopeName() !== $scopeName) |
|
| 456 | + { |
|
| 414 | 457 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 415 | 458 | } |
| 416 | 459 | } |
@@ -439,26 +482,33 @@ discard block |
||
| 439 | 482 | array $arguments, |
| 440 | 483 | ): object { |
| 441 | 484 | $class = $ctx->class; |
| 442 | - try { |
|
| 485 | + try |
|
| 486 | + { |
|
| 443 | 487 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 444 | - } catch (\ReflectionException $e) { |
|
| 488 | + } |
|
| 489 | + catch (\ReflectionException $e) |
|
| 490 | + { |
|
| 445 | 491 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 446 | 492 | } |
| 447 | 493 | |
| 448 | 494 | // Check scope name |
| 449 | - if ($this->options->checkScope) { |
|
| 495 | + if ($this->options->checkScope) |
|
| 496 | + { |
|
| 450 | 497 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 451 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 498 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 499 | + { |
|
| 452 | 500 | throw new BadScopeException($scope, $class); |
| 453 | 501 | } |
| 454 | 502 | } |
| 455 | 503 | |
| 456 | 504 | // We have to construct class using external injector when we know the exact context |
| 457 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
| 505 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
| 506 | + { |
|
| 458 | 507 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
| 459 | 508 | } |
| 460 | 509 | |
| 461 | - if (!$reflection->isInstantiable()) { |
|
| 510 | + if (!$reflection->isInstantiable()) |
|
| 511 | + { |
|
| 462 | 512 | $itIs = match (true) { |
| 463 | 513 | $reflection->isEnum() => 'Enum', |
| 464 | 514 | $reflection->isAbstract() => 'Abstract class', |
@@ -471,12 +521,16 @@ discard block |
||
| 471 | 521 | |
| 472 | 522 | $constructor = $reflection->getConstructor(); |
| 473 | 523 | |
| 474 | - if ($constructor !== null) { |
|
| 475 | - try { |
|
| 524 | + if ($constructor !== null) |
|
| 525 | + { |
|
| 526 | + try |
|
| 527 | + { |
|
| 476 | 528 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 477 | 529 | $this->tracer->push(true); |
| 478 | 530 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
| 479 | - } catch (ValidationException $e) { |
|
| 531 | + } |
|
| 532 | + catch (ValidationException $e) |
|
| 533 | + { |
|
| 480 | 534 | throw new ContainerException( |
| 481 | 535 | $this->tracer->combineTraceMessage( |
| 482 | 536 | \sprintf( |
@@ -486,22 +540,31 @@ discard block |
||
| 486 | 540 | ) |
| 487 | 541 | ), |
| 488 | 542 | ); |
| 489 | - } finally { |
|
| 543 | + } |
|
| 544 | + finally |
|
| 545 | + { |
|
| 490 | 546 | $this->tracer->pop(true); |
| 491 | 547 | $this->tracer->pop(false); |
| 492 | 548 | } |
| 493 | - try { |
|
| 549 | + try |
|
| 550 | + { |
|
| 494 | 551 | // Using constructor with resolved arguments |
| 495 | 552 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
| 496 | 553 | $this->tracer->push(true); |
| 497 | 554 | $instance = new $class(...$args); |
| 498 | - } catch (\TypeError $e) { |
|
| 555 | + } |
|
| 556 | + catch (\TypeError $e) |
|
| 557 | + { |
|
| 499 | 558 | throw new WrongTypeException($constructor, $e); |
| 500 | - } finally { |
|
| 559 | + } |
|
| 560 | + finally |
|
| 561 | + { |
|
| 501 | 562 | $this->tracer->pop(true); |
| 502 | 563 | $this->tracer->pop(false); |
| 503 | 564 | } |
| 504 | - } else { |
|
| 565 | + } |
|
| 566 | + else |
|
| 567 | + { |
|
| 505 | 568 | // No constructor specified |
| 506 | 569 | $instance = $reflection->newInstance(); |
| 507 | 570 | } |
@@ -519,13 +582,15 @@ discard block |
||
| 519 | 582 | $instance = $this->runInflector($instance); |
| 520 | 583 | |
| 521 | 584 | //Declarative singletons |
| 522 | - if ($this->isSingleton($ctx)) { |
|
| 585 | + if ($this->isSingleton($ctx)) |
|
| 586 | + { |
|
| 523 | 587 | $this->state->singletons[$ctx->alias] = $instance; |
| 524 | 588 | } |
| 525 | 589 | |
| 526 | 590 | // Register finalizer |
| 527 | 591 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 528 | - if ($finalizer !== null) { |
|
| 592 | + if ($finalizer !== null) |
|
| 593 | + { |
|
| 529 | 594 | $this->state->finalizers[] = $finalizer; |
| 530 | 595 | } |
| 531 | 596 | |
@@ -537,12 +602,14 @@ discard block |
||
| 537 | 602 | */ |
| 538 | 603 | private function isSingleton(Ctx $ctx): bool |
| 539 | 604 | { |
| 540 | - if ($ctx->singleton === true) { |
|
| 605 | + if ($ctx->singleton === true) |
|
| 606 | + { |
|
| 541 | 607 | return true; |
| 542 | 608 | } |
| 543 | 609 | |
| 544 | 610 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 545 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 611 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 612 | + { |
|
| 546 | 613 | return true; |
| 547 | 614 | } |
| 548 | 615 | |
@@ -556,7 +623,8 @@ discard block |
||
| 556 | 623 | * @var Attribute\Finalize|null $attribute |
| 557 | 624 | */ |
| 558 | 625 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 559 | - if ($attribute === null) { |
|
| 626 | + if ($attribute === null) |
|
| 627 | + { |
|
| 560 | 628 | return null; |
| 561 | 629 | } |
| 562 | 630 | |
@@ -570,10 +638,14 @@ discard block |
||
| 570 | 638 | { |
| 571 | 639 | $scope = $this->scope; |
| 572 | 640 | |
| 573 | - while ($scope !== null) { |
|
| 574 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 575 | - if ($instance instanceof $class) { |
|
| 576 | - foreach ($inflectors as $inflector) { |
|
| 641 | + while ($scope !== null) |
|
| 642 | + { |
|
| 643 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 644 | + { |
|
| 645 | + if ($instance instanceof $class) |
|
| 646 | + { |
|
| 647 | + foreach ($inflectors as $inflector) |
|
| 648 | + { |
|
| 577 | 649 | $instance = $inflector->getParametersCount() > 1 |
| 578 | 650 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 579 | 651 | : ($inflector->inflector)($instance); |
@@ -589,9 +661,12 @@ discard block |
||
| 589 | 661 | |
| 590 | 662 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 591 | 663 | { |
| 592 | - try { |
|
| 664 | + try |
|
| 665 | + { |
|
| 593 | 666 | $this->resolver->validateArguments($reflection, $arguments); |
| 594 | - } catch (\Throwable) { |
|
| 667 | + } |
|
| 668 | + catch (\Throwable) |
|
| 669 | + { |
|
| 595 | 670 | return false; |
| 596 | 671 | } |
| 597 | 672 | |