@@ -64,13 +64,13 @@ discard block |
||
| 64 | 64 | */ |
| 65 | 65 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 66 | 66 | { |
| 67 | - if (!isset($this->state->bindings[$alias])) { |
|
| 67 | + if (!isset($this->state->bindings[$alias])){ |
|
| 68 | 68 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | $avoidCache = $parameters !== []; |
| 72 | 72 | $binding = $this->state->bindings[$alias]; |
| 73 | - try { |
|
| 73 | + try{ |
|
| 74 | 74 | $this->tracer->push( |
| 75 | 75 | false, |
| 76 | 76 | action: 'resolve from binding', |
@@ -81,22 +81,22 @@ discard block |
||
| 81 | 81 | ); |
| 82 | 82 | $this->tracer->push(true); |
| 83 | 83 | |
| 84 | - if (\is_object($binding)) { |
|
| 85 | - if ($binding::class === WeakReference::class) { |
|
| 84 | + if (\is_object($binding)){ |
|
| 85 | + if ($binding::class === WeakReference::class){ |
|
| 86 | 86 | return $this->resolveWeakReference($binding, $alias, $context, $parameters); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | // When binding is instance, assuming singleton |
| 90 | 90 | return $avoidCache |
| 91 | 91 | ? $this->createInstance( |
| 92 | - new Ctx(alias: $alias, class: $binding::class, parameter: $context), |
|
| 92 | + new Ctx(alias : $alias, class : $binding::class, parameter : $context), |
|
| 93 | 93 | $parameters, |
| 94 | 94 | ) |
| 95 | 95 | : $binding; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
| 99 | - if (\is_string($binding)) { |
|
| 99 | + if (\is_string($binding)){ |
|
| 100 | 100 | $ctx->class = $binding; |
| 101 | 101 | return $binding === $alias |
| 102 | 102 | ? $this->autowire($ctx, $parameters) |
@@ -104,18 +104,18 @@ discard block |
||
| 104 | 104 | : $this->make($binding, $parameters, $context); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - if ($binding[1] === true) { |
|
| 107 | + if ($binding[1] === true){ |
|
| 108 | 108 | $ctx->singleton = true; |
| 109 | 109 | } |
| 110 | 110 | unset($this->state->bindings[$alias]); |
| 111 | - try { |
|
| 111 | + try{ |
|
| 112 | 112 | return $binding[0] === $alias |
| 113 | 113 | ? $this->autowire($ctx, $parameters) |
| 114 | 114 | : $this->evaluateBinding($ctx, $binding[0], $parameters); |
| 115 | - } finally { |
|
| 115 | + }finally{ |
|
| 116 | 116 | $this->state->bindings[$alias] ??= $binding; |
| 117 | 117 | } |
| 118 | - } finally { |
|
| 118 | + }finally{ |
|
| 119 | 119 | $this->tracer->pop(true); |
| 120 | 120 | $this->tracer->pop(false); |
| 121 | 121 | } |
@@ -129,19 +129,19 @@ discard block |
||
| 129 | 129 | ): ?object { |
| 130 | 130 | $avoidCache = $parameters !== []; |
| 131 | 131 | |
| 132 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
| 133 | - try { |
|
| 132 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)){ |
|
| 133 | + try{ |
|
| 134 | 134 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 135 | 135 | |
| 136 | 136 | $object = $this->createInstance( |
| 137 | 137 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 138 | 138 | $parameters, |
| 139 | 139 | ); |
| 140 | - if ($avoidCache) { |
|
| 140 | + if ($avoidCache){ |
|
| 141 | 141 | return $object; |
| 142 | 142 | } |
| 143 | 143 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 144 | - } catch (\Throwable) { |
|
| 144 | + }catch (\Throwable){ |
|
| 145 | 145 | throw new ContainerException( |
| 146 | 146 | $this->tracer->combineTraceMessage( |
| 147 | 147 | \sprintf( |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | ) |
| 152 | 152 | ) |
| 153 | 153 | ); |
| 154 | - } finally { |
|
| 154 | + }finally{ |
|
| 155 | 155 | $this->tracer->pop(); |
| 156 | 156 | } |
| 157 | 157 | } |
@@ -163,18 +163,18 @@ discard block |
||
| 163 | 163 | { |
| 164 | 164 | $parent = $this->scope->getParent(); |
| 165 | 165 | |
| 166 | - if ($parent !== null) { |
|
| 167 | - try { |
|
| 166 | + if ($parent !== null){ |
|
| 167 | + try{ |
|
| 168 | 168 | $this->tracer->push(false, ...[ |
| 169 | 169 | 'current scope' => $this->scope->getScopeName(), |
| 170 | 170 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 171 | 171 | ]); |
| 172 | 172 | return $parent->make($alias, $parameters, $context); |
| 173 | - } catch (BadScopeException $e) { |
|
| 174 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 173 | + }catch (BadScopeException $e){ |
|
| 174 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
| 175 | 175 | throw $e; |
| 176 | 176 | } |
| 177 | - } catch (ContainerExceptionInterface $e) { |
|
| 177 | + }catch (ContainerExceptionInterface $e){ |
|
| 178 | 178 | $className = match (true) { |
| 179 | 179 | $e instanceof NotFoundException => NotFoundException::class, |
| 180 | 180 | default => ContainerException::class, |
@@ -183,19 +183,19 @@ discard block |
||
| 183 | 183 | 'Can\'t resolve `%s`.', |
| 184 | 184 | $alias, |
| 185 | 185 | )), previous: $e); |
| 186 | - } finally { |
|
| 186 | + }finally{ |
|
| 187 | 187 | $this->tracer->pop(false); |
| 188 | 188 | } |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 192 | - try { |
|
| 192 | + try{ |
|
| 193 | 193 | //No direct instructions how to construct class, make is automatically |
| 194 | 194 | return $this->autowire( |
| 195 | 195 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 196 | 196 | $parameters, |
| 197 | 197 | ); |
| 198 | - } finally { |
|
| 198 | + }finally{ |
|
| 199 | 199 | $this->tracer->pop(false); |
| 200 | 200 | } |
| 201 | 201 | } |
@@ -217,7 +217,7 @@ discard block |
||
| 217 | 217 | && |
| 218 | 218 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
| 219 | 219 | )) |
| 220 | - ) { |
|
| 220 | + ){ |
|
| 221 | 221 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 222 | 222 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 223 | 223 | $this->tracer->getRootAlias(), |
@@ -245,16 +245,16 @@ discard block |
||
| 245 | 245 | mixed $target, |
| 246 | 246 | array $arguments, |
| 247 | 247 | ): mixed { |
| 248 | - if (\is_string($target)) { |
|
| 248 | + if (\is_string($target)){ |
|
| 249 | 249 | // Reference |
| 250 | 250 | $instance = $this->make($target, $arguments, $ctx->parameter); |
| 251 | - } else { |
|
| 252 | - if ($target instanceof Autowire) { |
|
| 251 | + }else{ |
|
| 252 | + if ($target instanceof Autowire){ |
|
| 253 | 253 | $instance = $target->resolve($this, $arguments); |
| 254 | - } else { |
|
| 255 | - try { |
|
| 254 | + }else{ |
|
| 255 | + try{ |
|
| 256 | 256 | $instance = $this->invoker->invoke($target, $arguments); |
| 257 | - } catch (NotCallableException $e) { |
|
| 257 | + }catch (NotCallableException $e){ |
|
| 258 | 258 | throw new ContainerException( |
| 259 | 259 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 260 | 260 | $e->getCode(), |
@@ -264,10 +264,10 @@ discard block |
||
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | // Check scope name |
| 267 | - if (\is_object($instance)) { |
|
| 267 | + if (\is_object($instance)){ |
|
| 268 | 268 | $ctx->reflection = new \ReflectionClass($instance); |
| 269 | 269 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 270 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 270 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()){ |
|
| 271 | 271 | throw new BadScopeException($scopeName, $instance::class); |
| 272 | 272 | } |
| 273 | 273 | } |
@@ -296,26 +296,26 @@ discard block |
||
| 296 | 296 | array $parameters, |
| 297 | 297 | ): object { |
| 298 | 298 | $class = $ctx->class; |
| 299 | - try { |
|
| 299 | + try{ |
|
| 300 | 300 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 301 | - } catch (\ReflectionException $e) { |
|
| 301 | + }catch (\ReflectionException $e){ |
|
| 302 | 302 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | // Check scope name |
| 306 | 306 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 307 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 307 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
| 308 | 308 | throw new BadScopeException($scope, $class); |
| 309 | 309 | } |
| 310 | 310 | |
| 311 | 311 | //We have to construct class using external injector when we know exact context |
| 312 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 312 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
| 313 | 313 | $injector = $this->state->injectors[$reflection->getName()]; |
| 314 | 314 | |
| 315 | - try { |
|
| 315 | + try{ |
|
| 316 | 316 | $injectorInstance = $this->container->get($injector); |
| 317 | 317 | |
| 318 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 318 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 319 | 319 | throw new InjectionException( |
| 320 | 320 | \sprintf( |
| 321 | 321 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -330,7 +330,7 @@ discard block |
||
| 330 | 330 | * @psalm-suppress RedundantCondition |
| 331 | 331 | */ |
| 332 | 332 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
| 333 | - if (!$reflection->isInstance($instance)) { |
|
| 333 | + if (!$reflection->isInstance($instance)){ |
|
| 334 | 334 | throw new InjectionException( |
| 335 | 335 | \sprintf( |
| 336 | 336 | "Invalid injection response for '%s'.", |
@@ -340,12 +340,12 @@ discard block |
||
| 340 | 340 | } |
| 341 | 341 | |
| 342 | 342 | return $instance; |
| 343 | - } finally { |
|
| 343 | + }finally{ |
|
| 344 | 344 | $this->state->injectors[$reflection->getName()] = $injector; |
| 345 | 345 | } |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - if (!$reflection->isInstantiable()) { |
|
| 348 | + if (!$reflection->isInstantiable()){ |
|
| 349 | 349 | $itIs = match (true) { |
| 350 | 350 | $reflection->isEnum() => 'Enum', |
| 351 | 351 | $reflection->isAbstract() => 'Abstract class', |
@@ -358,12 +358,12 @@ discard block |
||
| 358 | 358 | |
| 359 | 359 | $constructor = $reflection->getConstructor(); |
| 360 | 360 | |
| 361 | - if ($constructor !== null) { |
|
| 362 | - try { |
|
| 361 | + if ($constructor !== null){ |
|
| 362 | + try{ |
|
| 363 | 363 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 364 | 364 | $this->tracer->push(true); |
| 365 | 365 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 366 | - } catch (ValidationException $e) { |
|
| 366 | + }catch (ValidationException $e){ |
|
| 367 | 367 | throw new ContainerException( |
| 368 | 368 | $this->tracer->combineTraceMessage( |
| 369 | 369 | \sprintf( |
@@ -373,22 +373,22 @@ discard block |
||
| 373 | 373 | ) |
| 374 | 374 | ), |
| 375 | 375 | ); |
| 376 | - } finally { |
|
| 376 | + }finally{ |
|
| 377 | 377 | $this->tracer->pop(true); |
| 378 | 378 | $this->tracer->pop(false); |
| 379 | 379 | } |
| 380 | - try { |
|
| 380 | + try{ |
|
| 381 | 381 | // Using constructor with resolved arguments |
| 382 | 382 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 383 | 383 | $this->tracer->push(true); |
| 384 | 384 | $instance = new $class(...$arguments); |
| 385 | - } catch (\TypeError $e) { |
|
| 385 | + }catch (\TypeError $e){ |
|
| 386 | 386 | throw new WrongTypeException($constructor, $e); |
| 387 | - } finally { |
|
| 387 | + }finally{ |
|
| 388 | 388 | $this->tracer->pop(true); |
| 389 | 389 | $this->tracer->pop(false); |
| 390 | 390 | } |
| 391 | - } else { |
|
| 391 | + }else{ |
|
| 392 | 392 | // No constructor specified |
| 393 | 393 | $instance = $reflection->newInstance(); |
| 394 | 394 | } |
@@ -412,13 +412,13 @@ discard block |
||
| 412 | 412 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 413 | 413 | |
| 414 | 414 | //Declarative singletons |
| 415 | - if ($this->isSingleton($ctx)) { |
|
| 415 | + if ($this->isSingleton($ctx)){ |
|
| 416 | 416 | $this->state->bindings[$ctx->alias] = $instance; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | // Register finalizer |
| 420 | 420 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 421 | - if ($finalizer !== null) { |
|
| 421 | + if ($finalizer !== null){ |
|
| 422 | 422 | $this->state->finalizers[] = $finalizer; |
| 423 | 423 | } |
| 424 | 424 | |
@@ -430,11 +430,11 @@ discard block |
||
| 430 | 430 | */ |
| 431 | 431 | private function isSingleton(Ctx $ctx): bool |
| 432 | 432 | { |
| 433 | - if ($ctx->singleton === true) { |
|
| 433 | + if ($ctx->singleton === true){ |
|
| 434 | 434 | return true; |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 437 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
| 438 | 438 | return true; |
| 439 | 439 | } |
| 440 | 440 | |
@@ -448,7 +448,7 @@ discard block |
||
| 448 | 448 | * @var Finalize|null $attribute |
| 449 | 449 | */ |
| 450 | 450 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 451 | - if ($attribute === null) { |
|
| 451 | + if ($attribute === null){ |
|
| 452 | 452 | return null; |
| 453 | 453 | } |
| 454 | 454 | |
@@ -64,13 +64,15 @@ discard block |
||
| 64 | 64 | */ |
| 65 | 65 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 66 | 66 | { |
| 67 | - if (!isset($this->state->bindings[$alias])) { |
|
| 67 | + if (!isset($this->state->bindings[$alias])) |
|
| 68 | + { |
|
| 68 | 69 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 69 | 70 | } |
| 70 | 71 | |
| 71 | 72 | $avoidCache = $parameters !== []; |
| 72 | 73 | $binding = $this->state->bindings[$alias]; |
| 73 | - try { |
|
| 74 | + try |
|
| 75 | + { |
|
| 74 | 76 | $this->tracer->push( |
| 75 | 77 | false, |
| 76 | 78 | action: 'resolve from binding', |
@@ -81,8 +83,10 @@ discard block |
||
| 81 | 83 | ); |
| 82 | 84 | $this->tracer->push(true); |
| 83 | 85 | |
| 84 | - if (\is_object($binding)) { |
|
| 85 | - if ($binding::class === WeakReference::class) { |
|
| 86 | + if (\is_object($binding)) |
|
| 87 | + { |
|
| 88 | + if ($binding::class === WeakReference::class) |
|
| 89 | + { |
|
| 86 | 90 | return $this->resolveWeakReference($binding, $alias, $context, $parameters); |
| 87 | 91 | } |
| 88 | 92 | |
@@ -96,7 +100,8 @@ discard block |
||
| 96 | 100 | } |
| 97 | 101 | |
| 98 | 102 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
| 99 | - if (\is_string($binding)) { |
|
| 103 | + if (\is_string($binding)) |
|
| 104 | + { |
|
| 100 | 105 | $ctx->class = $binding; |
| 101 | 106 | return $binding === $alias |
| 102 | 107 | ? $this->autowire($ctx, $parameters) |
@@ -104,18 +109,24 @@ discard block |
||
| 104 | 109 | : $this->make($binding, $parameters, $context); |
| 105 | 110 | } |
| 106 | 111 | |
| 107 | - if ($binding[1] === true) { |
|
| 112 | + if ($binding[1] === true) |
|
| 113 | + { |
|
| 108 | 114 | $ctx->singleton = true; |
| 109 | 115 | } |
| 110 | 116 | unset($this->state->bindings[$alias]); |
| 111 | - try { |
|
| 117 | + try |
|
| 118 | + { |
|
| 112 | 119 | return $binding[0] === $alias |
| 113 | 120 | ? $this->autowire($ctx, $parameters) |
| 114 | 121 | : $this->evaluateBinding($ctx, $binding[0], $parameters); |
| 115 | - } finally { |
|
| 122 | + } |
|
| 123 | + finally |
|
| 124 | + { |
|
| 116 | 125 | $this->state->bindings[$alias] ??= $binding; |
| 117 | 126 | } |
| 118 | - } finally { |
|
| 127 | + } |
|
| 128 | + finally |
|
| 129 | + { |
|
| 119 | 130 | $this->tracer->pop(true); |
| 120 | 131 | $this->tracer->pop(false); |
| 121 | 132 | } |
@@ -129,19 +140,24 @@ discard block |
||
| 129 | 140 | ): ?object { |
| 130 | 141 | $avoidCache = $parameters !== []; |
| 131 | 142 | |
| 132 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
| 133 | - try { |
|
| 143 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)) |
|
| 144 | + { |
|
| 145 | + try |
|
| 146 | + { |
|
| 134 | 147 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 135 | 148 | |
| 136 | 149 | $object = $this->createInstance( |
| 137 | 150 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 138 | 151 | $parameters, |
| 139 | 152 | ); |
| 140 | - if ($avoidCache) { |
|
| 153 | + if ($avoidCache) |
|
| 154 | + { |
|
| 141 | 155 | return $object; |
| 142 | 156 | } |
| 143 | 157 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 144 | - } catch (\Throwable) { |
|
| 158 | + } |
|
| 159 | + catch (\Throwable) |
|
| 160 | + { |
|
| 145 | 161 | throw new ContainerException( |
| 146 | 162 | $this->tracer->combineTraceMessage( |
| 147 | 163 | \sprintf( |
@@ -151,7 +167,9 @@ discard block |
||
| 151 | 167 | ) |
| 152 | 168 | ) |
| 153 | 169 | ); |
| 154 | - } finally { |
|
| 170 | + } |
|
| 171 | + finally |
|
| 172 | + { |
|
| 155 | 173 | $this->tracer->pop(); |
| 156 | 174 | } |
| 157 | 175 | } |
@@ -163,18 +181,25 @@ discard block |
||
| 163 | 181 | { |
| 164 | 182 | $parent = $this->scope->getParent(); |
| 165 | 183 | |
| 166 | - if ($parent !== null) { |
|
| 167 | - try { |
|
| 184 | + if ($parent !== null) |
|
| 185 | + { |
|
| 186 | + try |
|
| 187 | + { |
|
| 168 | 188 | $this->tracer->push(false, ...[ |
| 169 | 189 | 'current scope' => $this->scope->getScopeName(), |
| 170 | 190 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 171 | 191 | ]); |
| 172 | 192 | return $parent->make($alias, $parameters, $context); |
| 173 | - } catch (BadScopeException $e) { |
|
| 174 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 193 | + } |
|
| 194 | + catch (BadScopeException $e) |
|
| 195 | + { |
|
| 196 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 197 | + { |
|
| 175 | 198 | throw $e; |
| 176 | 199 | } |
| 177 | - } catch (ContainerExceptionInterface $e) { |
|
| 200 | + } |
|
| 201 | + catch (ContainerExceptionInterface $e) |
|
| 202 | + { |
|
| 178 | 203 | $className = match (true) { |
| 179 | 204 | $e instanceof NotFoundException => NotFoundException::class, |
| 180 | 205 | default => ContainerException::class, |
@@ -183,19 +208,24 @@ discard block |
||
| 183 | 208 | 'Can\'t resolve `%s`.', |
| 184 | 209 | $alias, |
| 185 | 210 | )), previous: $e); |
| 186 | - } finally { |
|
| 211 | + } |
|
| 212 | + finally |
|
| 213 | + { |
|
| 187 | 214 | $this->tracer->pop(false); |
| 188 | 215 | } |
| 189 | 216 | } |
| 190 | 217 | |
| 191 | 218 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 192 | - try { |
|
| 219 | + try |
|
| 220 | + { |
|
| 193 | 221 | //No direct instructions how to construct class, make is automatically |
| 194 | 222 | return $this->autowire( |
| 195 | 223 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 196 | 224 | $parameters, |
| 197 | 225 | ); |
| 198 | - } finally { |
|
| 226 | + } |
|
| 227 | + finally |
|
| 228 | + { |
|
| 199 | 229 | $this->tracer->pop(false); |
| 200 | 230 | } |
| 201 | 231 | } |
@@ -245,16 +275,25 @@ discard block |
||
| 245 | 275 | mixed $target, |
| 246 | 276 | array $arguments, |
| 247 | 277 | ): mixed { |
| 248 | - if (\is_string($target)) { |
|
| 278 | + if (\is_string($target)) |
|
| 279 | + { |
|
| 249 | 280 | // Reference |
| 250 | 281 | $instance = $this->make($target, $arguments, $ctx->parameter); |
| 251 | - } else { |
|
| 252 | - if ($target instanceof Autowire) { |
|
| 282 | + } |
|
| 283 | + else |
|
| 284 | + { |
|
| 285 | + if ($target instanceof Autowire) |
|
| 286 | + { |
|
| 253 | 287 | $instance = $target->resolve($this, $arguments); |
| 254 | - } else { |
|
| 255 | - try { |
|
| 288 | + } |
|
| 289 | + else |
|
| 290 | + { |
|
| 291 | + try |
|
| 292 | + { |
|
| 256 | 293 | $instance = $this->invoker->invoke($target, $arguments); |
| 257 | - } catch (NotCallableException $e) { |
|
| 294 | + } |
|
| 295 | + catch (NotCallableException $e) |
|
| 296 | + { |
|
| 258 | 297 | throw new ContainerException( |
| 259 | 298 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 260 | 299 | $e->getCode(), |
@@ -264,10 +303,12 @@ discard block |
||
| 264 | 303 | } |
| 265 | 304 | |
| 266 | 305 | // Check scope name |
| 267 | - if (\is_object($instance)) { |
|
| 306 | + if (\is_object($instance)) |
|
| 307 | + { |
|
| 268 | 308 | $ctx->reflection = new \ReflectionClass($instance); |
| 269 | 309 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 270 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 310 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
| 311 | + { |
|
| 271 | 312 | throw new BadScopeException($scopeName, $instance::class); |
| 272 | 313 | } |
| 273 | 314 | } |
@@ -296,26 +337,33 @@ discard block |
||
| 296 | 337 | array $parameters, |
| 297 | 338 | ): object { |
| 298 | 339 | $class = $ctx->class; |
| 299 | - try { |
|
| 340 | + try |
|
| 341 | + { |
|
| 300 | 342 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 301 | - } catch (\ReflectionException $e) { |
|
| 343 | + } |
|
| 344 | + catch (\ReflectionException $e) |
|
| 345 | + { |
|
| 302 | 346 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 303 | 347 | } |
| 304 | 348 | |
| 305 | 349 | // Check scope name |
| 306 | 350 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 307 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 351 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 352 | + { |
|
| 308 | 353 | throw new BadScopeException($scope, $class); |
| 309 | 354 | } |
| 310 | 355 | |
| 311 | 356 | //We have to construct class using external injector when we know exact context |
| 312 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 357 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 358 | + { |
|
| 313 | 359 | $injector = $this->state->injectors[$reflection->getName()]; |
| 314 | 360 | |
| 315 | - try { |
|
| 361 | + try |
|
| 362 | + { |
|
| 316 | 363 | $injectorInstance = $this->container->get($injector); |
| 317 | 364 | |
| 318 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 365 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 366 | + { |
|
| 319 | 367 | throw new InjectionException( |
| 320 | 368 | \sprintf( |
| 321 | 369 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -330,7 +378,8 @@ discard block |
||
| 330 | 378 | * @psalm-suppress RedundantCondition |
| 331 | 379 | */ |
| 332 | 380 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
| 333 | - if (!$reflection->isInstance($instance)) { |
|
| 381 | + if (!$reflection->isInstance($instance)) |
|
| 382 | + { |
|
| 334 | 383 | throw new InjectionException( |
| 335 | 384 | \sprintf( |
| 336 | 385 | "Invalid injection response for '%s'.", |
@@ -340,12 +389,15 @@ discard block |
||
| 340 | 389 | } |
| 341 | 390 | |
| 342 | 391 | return $instance; |
| 343 | - } finally { |
|
| 392 | + } |
|
| 393 | + finally |
|
| 394 | + { |
|
| 344 | 395 | $this->state->injectors[$reflection->getName()] = $injector; |
| 345 | 396 | } |
| 346 | 397 | } |
| 347 | 398 | |
| 348 | - if (!$reflection->isInstantiable()) { |
|
| 399 | + if (!$reflection->isInstantiable()) |
|
| 400 | + { |
|
| 349 | 401 | $itIs = match (true) { |
| 350 | 402 | $reflection->isEnum() => 'Enum', |
| 351 | 403 | $reflection->isAbstract() => 'Abstract class', |
@@ -358,12 +410,16 @@ discard block |
||
| 358 | 410 | |
| 359 | 411 | $constructor = $reflection->getConstructor(); |
| 360 | 412 | |
| 361 | - if ($constructor !== null) { |
|
| 362 | - try { |
|
| 413 | + if ($constructor !== null) |
|
| 414 | + { |
|
| 415 | + try |
|
| 416 | + { |
|
| 363 | 417 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 364 | 418 | $this->tracer->push(true); |
| 365 | 419 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 366 | - } catch (ValidationException $e) { |
|
| 420 | + } |
|
| 421 | + catch (ValidationException $e) |
|
| 422 | + { |
|
| 367 | 423 | throw new ContainerException( |
| 368 | 424 | $this->tracer->combineTraceMessage( |
| 369 | 425 | \sprintf( |
@@ -373,22 +429,31 @@ discard block |
||
| 373 | 429 | ) |
| 374 | 430 | ), |
| 375 | 431 | ); |
| 376 | - } finally { |
|
| 432 | + } |
|
| 433 | + finally |
|
| 434 | + { |
|
| 377 | 435 | $this->tracer->pop(true); |
| 378 | 436 | $this->tracer->pop(false); |
| 379 | 437 | } |
| 380 | - try { |
|
| 438 | + try |
|
| 439 | + { |
|
| 381 | 440 | // Using constructor with resolved arguments |
| 382 | 441 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 383 | 442 | $this->tracer->push(true); |
| 384 | 443 | $instance = new $class(...$arguments); |
| 385 | - } catch (\TypeError $e) { |
|
| 444 | + } |
|
| 445 | + catch (\TypeError $e) |
|
| 446 | + { |
|
| 386 | 447 | throw new WrongTypeException($constructor, $e); |
| 387 | - } finally { |
|
| 448 | + } |
|
| 449 | + finally |
|
| 450 | + { |
|
| 388 | 451 | $this->tracer->pop(true); |
| 389 | 452 | $this->tracer->pop(false); |
| 390 | 453 | } |
| 391 | - } else { |
|
| 454 | + } |
|
| 455 | + else |
|
| 456 | + { |
|
| 392 | 457 | // No constructor specified |
| 393 | 458 | $instance = $reflection->newInstance(); |
| 394 | 459 | } |
@@ -412,13 +477,15 @@ discard block |
||
| 412 | 477 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 413 | 478 | |
| 414 | 479 | //Declarative singletons |
| 415 | - if ($this->isSingleton($ctx)) { |
|
| 480 | + if ($this->isSingleton($ctx)) |
|
| 481 | + { |
|
| 416 | 482 | $this->state->bindings[$ctx->alias] = $instance; |
| 417 | 483 | } |
| 418 | 484 | |
| 419 | 485 | // Register finalizer |
| 420 | 486 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 421 | - if ($finalizer !== null) { |
|
| 487 | + if ($finalizer !== null) |
|
| 488 | + { |
|
| 422 | 489 | $this->state->finalizers[] = $finalizer; |
| 423 | 490 | } |
| 424 | 491 | |
@@ -430,11 +497,13 @@ discard block |
||
| 430 | 497 | */ |
| 431 | 498 | private function isSingleton(Ctx $ctx): bool |
| 432 | 499 | { |
| 433 | - if ($ctx->singleton === true) { |
|
| 500 | + if ($ctx->singleton === true) |
|
| 501 | + { |
|
| 434 | 502 | return true; |
| 435 | 503 | } |
| 436 | 504 | |
| 437 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 505 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 506 | + { |
|
| 438 | 507 | return true; |
| 439 | 508 | } |
| 440 | 509 | |
@@ -448,7 +517,8 @@ discard block |
||
| 448 | 517 | * @var Finalize|null $attribute |
| 449 | 518 | */ |
| 450 | 519 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 451 | - if ($attribute === null) { |
|
| 520 | + if ($attribute === null) |
|
| 521 | + { |
|
| 452 | 522 | return null; |
| 453 | 523 | } |
| 454 | 524 | |
@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | { |
| 21 | 21 | public function __construct( |
| 22 | 22 | private readonly QueueInterface $connection |
| 23 | - ) { |
|
| 23 | + ){ |
|
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | /** |
@@ -33,15 +33,15 @@ discard block |
||
| 33 | 33 | ): string { |
| 34 | 34 | \assert(\is_array($parameters['payload'])); |
| 35 | 35 | |
| 36 | - if ($parameters['options'] === null) { |
|
| 36 | + if ($parameters['options'] === null){ |
|
| 37 | 37 | $parameters['options'] = new Options(); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | $tracer = $this->getTracer(); |
| 41 | 41 | |
| 42 | 42 | /** @psalm-suppress RedundantCondition */ |
| 43 | - if (\method_exists($parameters['options'], 'withHeader')) { |
|
| 44 | - foreach ($tracer->getContext() as $key => $data) { |
|
| 43 | + if (\method_exists($parameters['options'], 'withHeader')){ |
|
| 44 | + foreach ($tracer->getContext() as $key => $data){ |
|
| 45 | 45 | $parameters['options'] = $parameters['options']->withHeader($key, $data); |
| 46 | 46 | } |
| 47 | 47 | } |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | private function getTracer(): TracerInterface |
| 63 | 63 | { |
| 64 | - try { |
|
| 64 | + try{ |
|
| 65 | 65 | return ContainerScope::getContainer()->get(TracerInterface::class); |
| 66 | - } catch (\Throwable $e) { |
|
| 66 | + }catch (\Throwable $e){ |
|
| 67 | 67 | return new NullTracer(); |
| 68 | 68 | } |
| 69 | 69 | } |
@@ -33,15 +33,18 @@ discard block |
||
| 33 | 33 | ): string { |
| 34 | 34 | \assert(\is_array($parameters['payload'])); |
| 35 | 35 | |
| 36 | - if ($parameters['options'] === null) { |
|
| 36 | + if ($parameters['options'] === null) |
|
| 37 | + { |
|
| 37 | 38 | $parameters['options'] = new Options(); |
| 38 | 39 | } |
| 39 | 40 | |
| 40 | 41 | $tracer = $this->getTracer(); |
| 41 | 42 | |
| 42 | 43 | /** @psalm-suppress RedundantCondition */ |
| 43 | - if (\method_exists($parameters['options'], 'withHeader')) { |
|
| 44 | - foreach ($tracer->getContext() as $key => $data) { |
|
| 44 | + if (\method_exists($parameters['options'], 'withHeader')) |
|
| 45 | + { |
|
| 46 | + foreach ($tracer->getContext() as $key => $data) |
|
| 47 | + { |
|
| 45 | 48 | $parameters['options'] = $parameters['options']->withHeader($key, $data); |
| 46 | 49 | } |
| 47 | 50 | } |
@@ -61,9 +64,12 @@ discard block |
||
| 61 | 64 | |
| 62 | 65 | private function getTracer(): TracerInterface |
| 63 | 66 | { |
| 64 | - try { |
|
| 67 | + try |
|
| 68 | + { |
|
| 65 | 69 | return ContainerScope::getContainer()->get(TracerInterface::class); |
| 66 | - } catch (\Throwable $e) { |
|
| 70 | + } |
|
| 71 | + catch (\Throwable $e) |
|
| 72 | + { |
|
| 67 | 73 | return new NullTracer(); |
| 68 | 74 | } |
| 69 | 75 | } |