@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | $this->expectExceptionMessage('Enum `Spiral\Tests\Core\Stub\EnumObject` can not be constructed.'); |
| 64 | 64 | |
| 65 | 65 | $this->resolveClosure( |
| 66 | - static function (EnumObject $enum) {}, |
|
| 66 | + static function (EnumObject $enum){}, |
|
| 67 | 67 | ); |
| 68 | 68 | } |
| 69 | 69 | |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | ); |
| 76 | 76 | |
| 77 | 77 | $this->resolveClosure( |
| 78 | - static function (TestTrait $enum) {}, |
|
| 78 | + static function (TestTrait $enum){}, |
|
| 79 | 79 | ); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -63,7 +63,9 @@ discard block |
||
| 63 | 63 | $this->expectExceptionMessage('Enum `Spiral\Tests\Core\Stub\EnumObject` can not be constructed.'); |
| 64 | 64 | |
| 65 | 65 | $this->resolveClosure( |
| 66 | - static function (EnumObject $enum) {}, |
|
| 66 | + static function (EnumObject $enum) |
|
| 67 | + { |
|
| 68 | +}, |
|
| 67 | 69 | ); |
| 68 | 70 | } |
| 69 | 71 | |
@@ -75,7 +77,9 @@ discard block |
||
| 75 | 77 | ); |
| 76 | 78 | |
| 77 | 79 | $this->resolveClosure( |
| 78 | - static function (TestTrait $enum) {}, |
|
| 80 | + static function (TestTrait $enum) |
|
| 81 | + { |
|
| 82 | +}, |
|
| 79 | 83 | ); |
| 80 | 84 | } |
| 81 | 85 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | public readonly string $binder = Binder::class, |
| 41 | 41 | public readonly string $invoker = Invoker::class, |
| 42 | 42 | public readonly string $tracer = Tracer::class, |
| 43 | - ) { |
|
| 43 | + ){ |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | public function getIterator(): Traversable |
@@ -13,7 +13,7 @@ |
||
| 13 | 13 | public function __construct( |
| 14 | 14 | \ReflectionFunctionAbstract $reflection, |
| 15 | 15 | private readonly string $parameter |
| 16 | - ) { |
|
| 16 | + ){ |
|
| 17 | 17 | $pattern = "Invalid argument value type for the `$parameter` parameter when validating arguments for `%s`."; |
| 18 | 18 | parent::__construct($this->renderFunctionAndParameter($reflection, $pattern)); |
| 19 | 19 | } |
@@ -18,13 +18,13 @@ discard block |
||
| 18 | 18 | public function __construct( |
| 19 | 19 | public readonly string $alias, |
| 20 | 20 | public array $info, |
| 21 | - ) { |
|
| 21 | + ){ |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | public function __toString(): string |
| 25 | 25 | { |
| 26 | 26 | $info = [$this->alias]; |
| 27 | - foreach ($this->info as $key => $item) { |
|
| 27 | + foreach ($this->info as $key => $item){ |
|
| 28 | 28 | $info[] = "$key: {$this->stringifyValue($item)}"; |
| 29 | 29 | } |
| 30 | 30 | return \implode("\n", $info); |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | \is_string($item) => "'$item'", |
| 37 | 37 | \is_scalar($item) => (string)$item, |
| 38 | 38 | $item instanceof Closure => $this->renderClosureSignature(new ReflectionFunction($item)), |
| 39 | - \is_object($item) => 'instance of ' . $item::class, |
|
| 39 | + \is_object($item) => 'instance of '.$item::class, |
|
| 40 | 40 | default => \gettype($item), |
| 41 | 41 | }; |
| 42 | 42 | } |
@@ -24,7 +24,8 @@ |
||
| 24 | 24 | public function __toString(): string |
| 25 | 25 | { |
| 26 | 26 | $info = [$this->alias]; |
| 27 | - foreach ($this->info as $key => $item) { |
|
| 27 | + foreach ($this->info as $key => $item) |
|
| 28 | + { |
|
| 28 | 29 | $info[] = "$key: {$this->stringifyValue($item)}"; |
| 29 | 30 | } |
| 30 | 31 | return \implode("\n", $info); |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | |
| 19 | 19 | public function __toString(): string |
| 20 | 20 | { |
| 21 | - return $this->traces === [] ? '' : "Container trace list:\n" . $this->renderTraceList($this->traces); |
|
| 21 | + return $this->traces === [] ? '' : "Container trace list:\n".$this->renderTraceList($this->traces); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
@@ -34,26 +34,26 @@ discard block |
||
| 34 | 34 | public function push(string $alias, bool $nextLevel = false, mixed ...$details): void |
| 35 | 35 | { |
| 36 | 36 | $trace = new Trace($alias, $details); |
| 37 | - if ($nextLevel || $this->traces === []) { |
|
| 37 | + if ($nextLevel || $this->traces === []){ |
|
| 38 | 38 | $this->traces[] = [$trace]; |
| 39 | - } else { |
|
| 39 | + }else{ |
|
| 40 | 40 | $this->traces[\array_key_last($this->traces)][] = $trace; |
| 41 | 41 | } |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | public function pop(bool $previousLevel = false): void |
| 45 | 45 | { |
| 46 | - if ($this->traces === []) { |
|
| 46 | + if ($this->traces === []){ |
|
| 47 | 47 | return; |
| 48 | 48 | } |
| 49 | - if ($previousLevel) { |
|
| 49 | + if ($previousLevel){ |
|
| 50 | 50 | \array_pop($this->traces); |
| 51 | 51 | return; |
| 52 | 52 | } |
| 53 | 53 | $key = \array_key_last($this->traces); |
| 54 | 54 | $list = &$this->traces[$key]; |
| 55 | 55 | \array_pop($list); |
| 56 | - if ($list === []) { |
|
| 56 | + if ($list === []){ |
|
| 57 | 57 | unset($this->traces[$key]); |
| 58 | 58 | } |
| 59 | 59 | } |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | { |
| 71 | 71 | $result = []; |
| 72 | 72 | $i = 0; |
| 73 | - foreach ($blocks as $block) { |
|
| 73 | + foreach ($blocks as $block){ |
|
| 74 | 74 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
| 75 | 75 | } |
| 76 | 76 | return \implode("\n", $result); |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | // Separator |
| 91 | 91 | $s = "\n"; |
| 92 | 92 | $nexPrefix = "$s$padding "; |
| 93 | - foreach ($items as $item) { |
|
| 94 | - $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
|
| 93 | + foreach ($items as $item){ |
|
| 94 | + $result[] = $firstPrefix.\str_replace($s, $nexPrefix, (string)$item); |
|
| 95 | 95 | } |
| 96 | 96 | return $result; |
| 97 | 97 | } |
@@ -34,26 +34,32 @@ discard block |
||
| 34 | 34 | public function push(string $alias, bool $nextLevel = false, mixed ...$details): void |
| 35 | 35 | { |
| 36 | 36 | $trace = new Trace($alias, $details); |
| 37 | - if ($nextLevel || $this->traces === []) { |
|
| 37 | + if ($nextLevel || $this->traces === []) |
|
| 38 | + { |
|
| 38 | 39 | $this->traces[] = [$trace]; |
| 39 | - } else { |
|
| 40 | + } |
|
| 41 | + else |
|
| 42 | + { |
|
| 40 | 43 | $this->traces[\array_key_last($this->traces)][] = $trace; |
| 41 | 44 | } |
| 42 | 45 | } |
| 43 | 46 | |
| 44 | 47 | public function pop(bool $previousLevel = false): void |
| 45 | 48 | { |
| 46 | - if ($this->traces === []) { |
|
| 49 | + if ($this->traces === []) |
|
| 50 | + { |
|
| 47 | 51 | return; |
| 48 | 52 | } |
| 49 | - if ($previousLevel) { |
|
| 53 | + if ($previousLevel) |
|
| 54 | + { |
|
| 50 | 55 | \array_pop($this->traces); |
| 51 | 56 | return; |
| 52 | 57 | } |
| 53 | 58 | $key = \array_key_last($this->traces); |
| 54 | 59 | $list = &$this->traces[$key]; |
| 55 | 60 | \array_pop($list); |
| 56 | - if ($list === []) { |
|
| 61 | + if ($list === []) |
|
| 62 | + { |
|
| 57 | 63 | unset($this->traces[$key]); |
| 58 | 64 | } |
| 59 | 65 | } |
@@ -70,7 +76,8 @@ discard block |
||
| 70 | 76 | { |
| 71 | 77 | $result = []; |
| 72 | 78 | $i = 0; |
| 73 | - foreach ($blocks as $block) { |
|
| 79 | + foreach ($blocks as $block) |
|
| 80 | + { |
|
| 74 | 81 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
| 75 | 82 | } |
| 76 | 83 | return \implode("\n", $result); |
@@ -90,7 +97,8 @@ discard block |
||
| 90 | 97 | // Separator |
| 91 | 98 | $s = "\n"; |
| 92 | 99 | $nexPrefix = "$s$padding "; |
| 93 | - foreach ($items as $item) { |
|
| 100 | + foreach ($items as $item) |
|
| 101 | + { |
|
| 94 | 102 | $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
| 95 | 103 | } |
| 96 | 104 | return $result; |
@@ -143,9 +143,9 @@ |
||
| 143 | 143 | { |
| 144 | 144 | $container = new Container(); |
| 145 | 145 | |
| 146 | - try { |
|
| 146 | + try{ |
|
| 147 | 147 | $container->get('invalid'); |
| 148 | - } catch (ContainerException $e) { |
|
| 148 | + }catch (ContainerException $e){ |
|
| 149 | 149 | $this->assertSame( |
| 150 | 150 | $e->getMessage(), |
| 151 | 151 | <<<MARKDOWN |
@@ -143,9 +143,12 @@ |
||
| 143 | 143 | { |
| 144 | 144 | $container = new Container(); |
| 145 | 145 | |
| 146 | - try { |
|
| 146 | + try |
|
| 147 | + { |
|
| 147 | 148 | $container->get('invalid'); |
| 148 | - } catch (ContainerException $e) { |
|
| 149 | + } |
|
| 150 | + catch (ContainerException $e) |
|
| 151 | + { |
|
| 149 | 152 | $this->assertSame( |
| 150 | 153 | $e->getMessage(), |
| 151 | 154 | <<<MARKDOWN |
@@ -54,12 +54,12 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 56 | 56 | { |
| 57 | - if (!isset($this->state->bindings[$alias])) { |
|
| 57 | + if (!isset($this->state->bindings[$alias])){ |
|
| 58 | 58 | $this->tracer->push($alias, false, source: 'autowiring', context: $context); |
| 59 | - try { |
|
| 59 | + try{ |
|
| 60 | 60 | //No direct instructions how to construct class, make is automatically |
| 61 | 61 | return $this->autowire($alias, $parameters, $context); |
| 62 | - } finally { |
|
| 62 | + }finally{ |
|
| 63 | 63 | $this->tracer->pop(false); |
| 64 | 64 | } |
| 65 | 65 | } |
@@ -67,20 +67,20 @@ discard block |
||
| 67 | 67 | $binding = $this->state->bindings[$alias]; |
| 68 | 68 | $this->tracer->push($alias, false, source: 'binding', binding: $binding, context: $context); |
| 69 | 69 | |
| 70 | - if (\is_object($binding)) { |
|
| 71 | - if ($binding::class === WeakReference::class) { |
|
| 72 | - if ($binding->get() === null && \class_exists($alias)) { |
|
| 73 | - try { |
|
| 70 | + if (\is_object($binding)){ |
|
| 71 | + if ($binding::class === WeakReference::class){ |
|
| 72 | + if ($binding->get() === null && \class_exists($alias)){ |
|
| 73 | + try{ |
|
| 74 | 74 | $this->tracer->push($alias, false, source: WeakReference::class, context: $context); |
| 75 | 75 | $object = $this->createInstance($alias, $parameters, $context); |
| 76 | 76 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 77 | - } catch (\Throwable) { |
|
| 77 | + }catch (\Throwable){ |
|
| 78 | 78 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 79 | 79 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 80 | 80 | $this->tracer->getRootAlias(), |
| 81 | 81 | $alias, |
| 82 | 82 | ))); |
| 83 | - } finally { |
|
| 83 | + }finally{ |
|
| 84 | 84 | $this->tracer->pop(); |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -90,22 +90,22 @@ discard block |
||
| 90 | 90 | return $binding; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if (\is_string($binding)) { |
|
| 93 | + if (\is_string($binding)){ |
|
| 94 | 94 | //Binding is pointing to something else |
| 95 | 95 | return $this->make($binding, $parameters, $context); |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | unset($this->state->bindings[$alias]); |
| 99 | - try { |
|
| 99 | + try{ |
|
| 100 | 100 | $instance = $binding[0] === $alias |
| 101 | 101 | ? $this->autowire($alias, $parameters, $context) |
| 102 | 102 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 103 | - } finally { |
|
| 103 | + }finally{ |
|
| 104 | 104 | /** @psalm-var class-string $alias */ |
| 105 | 105 | $this->state->bindings[$alias] = $binding; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - if ($binding[1]) { |
|
| 108 | + if ($binding[1]){ |
|
| 109 | 109 | // Indicates singleton |
| 110 | 110 | /** @psalm-var class-string $alias */ |
| 111 | 111 | $this->state->bindings[$alias] = $instance; |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | */ |
| 125 | 125 | private function autowire(string $class, array $parameters, string $context = null): object |
| 126 | 126 | { |
| 127 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
| 127 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])){ |
|
| 128 | 128 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 129 | 129 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 130 | 130 | $this->tracer->getRootAlias(), |
@@ -151,18 +151,18 @@ discard block |
||
| 151 | 151 | array $parameters, |
| 152 | 152 | string $context = null |
| 153 | 153 | ): mixed { |
| 154 | - if (\is_string($target)) { |
|
| 154 | + if (\is_string($target)){ |
|
| 155 | 155 | // Reference |
| 156 | 156 | return $this->make($target, $parameters, $context); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - if ($target instanceof Autowire) { |
|
| 159 | + if ($target instanceof Autowire){ |
|
| 160 | 160 | return $target->resolve($this, $parameters); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - try { |
|
| 163 | + try{ |
|
| 164 | 164 | return $this->invoker->invoke($target, $parameters); |
| 165 | - } catch (NotCallableException $e) { |
|
| 165 | + }catch (NotCallableException $e){ |
|
| 166 | 166 | throw new ContainerException( |
| 167 | 167 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 168 | 168 | $e->getCode(), |
@@ -186,20 +186,20 @@ discard block |
||
| 186 | 186 | */ |
| 187 | 187 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 188 | 188 | { |
| 189 | - try { |
|
| 189 | + try{ |
|
| 190 | 190 | $reflection = new \ReflectionClass($class); |
| 191 | - } catch (\ReflectionException $e) { |
|
| 191 | + }catch (\ReflectionException $e){ |
|
| 192 | 192 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | //We have to construct class using external injector when we know exact context |
| 196 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 196 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
| 197 | 197 | $injector = $this->state->injectors[$reflection->getName()]; |
| 198 | 198 | |
| 199 | - try { |
|
| 199 | + try{ |
|
| 200 | 200 | $injectorInstance = $this->container->get($injector); |
| 201 | 201 | |
| 202 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 202 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 203 | 203 | throw new InjectionException( |
| 204 | 204 | \sprintf( |
| 205 | 205 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -211,7 +211,7 @@ discard block |
||
| 211 | 211 | |
| 212 | 212 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 213 | 213 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 214 | - if (!$reflection->isInstance($instance)) { |
|
| 214 | + if (!$reflection->isInstance($instance)){ |
|
| 215 | 215 | throw new InjectionException( |
| 216 | 216 | \sprintf( |
| 217 | 217 | "Invalid injection response for '%s'.", |
@@ -221,12 +221,12 @@ discard block |
||
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | return $instance; |
| 224 | - } finally { |
|
| 224 | + }finally{ |
|
| 225 | 225 | $this->state->injectors[$reflection->getName()] = $injector; |
| 226 | 226 | } |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - if (!$reflection->isInstantiable()) { |
|
| 229 | + if (!$reflection->isInstantiable()){ |
|
| 230 | 230 | $itIs = match (true) { |
| 231 | 231 | $reflection->isEnum() => 'Enum', |
| 232 | 232 | $reflection->isAbstract() => 'Abstract class', |
@@ -239,10 +239,10 @@ discard block |
||
| 239 | 239 | |
| 240 | 240 | $constructor = $reflection->getConstructor(); |
| 241 | 241 | |
| 242 | - if ($constructor !== null) { |
|
| 243 | - try { |
|
| 242 | + if ($constructor !== null){ |
|
| 243 | + try{ |
|
| 244 | 244 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 245 | - } catch (ValidationException $e) { |
|
| 245 | + }catch (ValidationException $e){ |
|
| 246 | 246 | throw new ContainerException( |
| 247 | 247 | $this->tracer->combineTraceMessage( |
| 248 | 248 | \sprintf( |
@@ -253,13 +253,13 @@ discard block |
||
| 253 | 253 | ), |
| 254 | 254 | ); |
| 255 | 255 | } |
| 256 | - try { |
|
| 256 | + try{ |
|
| 257 | 257 | // Using constructor with resolved arguments |
| 258 | 258 | $instance = new $class(...$arguments); |
| 259 | - } catch (\TypeError $e) { |
|
| 259 | + }catch (\TypeError $e){ |
|
| 260 | 260 | throw new WrongTypeException($constructor, $e); |
| 261 | 261 | } |
| 262 | - } else { |
|
| 262 | + }else{ |
|
| 263 | 263 | // No constructor specified |
| 264 | 264 | $instance = $reflection->newInstance(); |
| 265 | 265 | } |
@@ -277,9 +277,9 @@ discard block |
||
| 277 | 277 | private function registerInstance(object $instance, array $parameters): object |
| 278 | 278 | { |
| 279 | 279 | //Declarative singletons (only when class received via direct get) |
| 280 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 280 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
| 281 | 281 | $alias = $instance::class; |
| 282 | - if (!isset($this->state->bindings[$alias])) { |
|
| 282 | + if (!isset($this->state->bindings[$alias])){ |
|
| 283 | 283 | $this->state->bindings[$alias] = $instance; |
| 284 | 284 | } |
| 285 | 285 | } |
@@ -54,12 +54,16 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 56 | 56 | { |
| 57 | - if (!isset($this->state->bindings[$alias])) { |
|
| 57 | + if (!isset($this->state->bindings[$alias])) |
|
| 58 | + { |
|
| 58 | 59 | $this->tracer->push($alias, false, source: 'autowiring', context: $context); |
| 59 | - try { |
|
| 60 | + try |
|
| 61 | + { |
|
| 60 | 62 | //No direct instructions how to construct class, make is automatically |
| 61 | 63 | return $this->autowire($alias, $parameters, $context); |
| 62 | - } finally { |
|
| 64 | + } |
|
| 65 | + finally |
|
| 66 | + { |
|
| 63 | 67 | $this->tracer->pop(false); |
| 64 | 68 | } |
| 65 | 69 | } |
@@ -67,20 +71,28 @@ discard block |
||
| 67 | 71 | $binding = $this->state->bindings[$alias]; |
| 68 | 72 | $this->tracer->push($alias, false, source: 'binding', binding: $binding, context: $context); |
| 69 | 73 | |
| 70 | - if (\is_object($binding)) { |
|
| 71 | - if ($binding::class === WeakReference::class) { |
|
| 72 | - if ($binding->get() === null && \class_exists($alias)) { |
|
| 73 | - try { |
|
| 74 | + if (\is_object($binding)) |
|
| 75 | + { |
|
| 76 | + if ($binding::class === WeakReference::class) |
|
| 77 | + { |
|
| 78 | + if ($binding->get() === null && \class_exists($alias)) |
|
| 79 | + { |
|
| 80 | + try |
|
| 81 | + { |
|
| 74 | 82 | $this->tracer->push($alias, false, source: WeakReference::class, context: $context); |
| 75 | 83 | $object = $this->createInstance($alias, $parameters, $context); |
| 76 | 84 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 77 | - } catch (\Throwable) { |
|
| 85 | + } |
|
| 86 | + catch (\Throwable) |
|
| 87 | + { |
|
| 78 | 88 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 79 | 89 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 80 | 90 | $this->tracer->getRootAlias(), |
| 81 | 91 | $alias, |
| 82 | 92 | ))); |
| 83 | - } finally { |
|
| 93 | + } |
|
| 94 | + finally |
|
| 95 | + { |
|
| 84 | 96 | $this->tracer->pop(); |
| 85 | 97 | } |
| 86 | 98 | } |
@@ -90,22 +102,27 @@ discard block |
||
| 90 | 102 | return $binding; |
| 91 | 103 | } |
| 92 | 104 | |
| 93 | - if (\is_string($binding)) { |
|
| 105 | + if (\is_string($binding)) |
|
| 106 | + { |
|
| 94 | 107 | //Binding is pointing to something else |
| 95 | 108 | return $this->make($binding, $parameters, $context); |
| 96 | 109 | } |
| 97 | 110 | |
| 98 | 111 | unset($this->state->bindings[$alias]); |
| 99 | - try { |
|
| 112 | + try |
|
| 113 | + { |
|
| 100 | 114 | $instance = $binding[0] === $alias |
| 101 | 115 | ? $this->autowire($alias, $parameters, $context) |
| 102 | 116 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 103 | - } finally { |
|
| 117 | + } |
|
| 118 | + finally |
|
| 119 | + { |
|
| 104 | 120 | /** @psalm-var class-string $alias */ |
| 105 | 121 | $this->state->bindings[$alias] = $binding; |
| 106 | 122 | } |
| 107 | 123 | |
| 108 | - if ($binding[1]) { |
|
| 124 | + if ($binding[1]) |
|
| 125 | + { |
|
| 109 | 126 | // Indicates singleton |
| 110 | 127 | /** @psalm-var class-string $alias */ |
| 111 | 128 | $this->state->bindings[$alias] = $instance; |
@@ -124,7 +141,8 @@ discard block |
||
| 124 | 141 | */ |
| 125 | 142 | private function autowire(string $class, array $parameters, string $context = null): object |
| 126 | 143 | { |
| 127 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
| 144 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])) |
|
| 145 | + { |
|
| 128 | 146 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 129 | 147 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 130 | 148 | $this->tracer->getRootAlias(), |
@@ -151,18 +169,23 @@ discard block |
||
| 151 | 169 | array $parameters, |
| 152 | 170 | string $context = null |
| 153 | 171 | ): mixed { |
| 154 | - if (\is_string($target)) { |
|
| 172 | + if (\is_string($target)) |
|
| 173 | + { |
|
| 155 | 174 | // Reference |
| 156 | 175 | return $this->make($target, $parameters, $context); |
| 157 | 176 | } |
| 158 | 177 | |
| 159 | - if ($target instanceof Autowire) { |
|
| 178 | + if ($target instanceof Autowire) |
|
| 179 | + { |
|
| 160 | 180 | return $target->resolve($this, $parameters); |
| 161 | 181 | } |
| 162 | 182 | |
| 163 | - try { |
|
| 183 | + try |
|
| 184 | + { |
|
| 164 | 185 | return $this->invoker->invoke($target, $parameters); |
| 165 | - } catch (NotCallableException $e) { |
|
| 186 | + } |
|
| 187 | + catch (NotCallableException $e) |
|
| 188 | + { |
|
| 166 | 189 | throw new ContainerException( |
| 167 | 190 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 168 | 191 | $e->getCode(), |
@@ -186,20 +209,26 @@ discard block |
||
| 186 | 209 | */ |
| 187 | 210 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 188 | 211 | { |
| 189 | - try { |
|
| 212 | + try |
|
| 213 | + { |
|
| 190 | 214 | $reflection = new \ReflectionClass($class); |
| 191 | - } catch (\ReflectionException $e) { |
|
| 215 | + } |
|
| 216 | + catch (\ReflectionException $e) |
|
| 217 | + { |
|
| 192 | 218 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 193 | 219 | } |
| 194 | 220 | |
| 195 | 221 | //We have to construct class using external injector when we know exact context |
| 196 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 222 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 223 | + { |
|
| 197 | 224 | $injector = $this->state->injectors[$reflection->getName()]; |
| 198 | 225 | |
| 199 | - try { |
|
| 226 | + try |
|
| 227 | + { |
|
| 200 | 228 | $injectorInstance = $this->container->get($injector); |
| 201 | 229 | |
| 202 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 230 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 231 | + { |
|
| 203 | 232 | throw new InjectionException( |
| 204 | 233 | \sprintf( |
| 205 | 234 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -211,7 +240,8 @@ discard block |
||
| 211 | 240 | |
| 212 | 241 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 213 | 242 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 214 | - if (!$reflection->isInstance($instance)) { |
|
| 243 | + if (!$reflection->isInstance($instance)) |
|
| 244 | + { |
|
| 215 | 245 | throw new InjectionException( |
| 216 | 246 | \sprintf( |
| 217 | 247 | "Invalid injection response for '%s'.", |
@@ -221,12 +251,15 @@ discard block |
||
| 221 | 251 | } |
| 222 | 252 | |
| 223 | 253 | return $instance; |
| 224 | - } finally { |
|
| 254 | + } |
|
| 255 | + finally |
|
| 256 | + { |
|
| 225 | 257 | $this->state->injectors[$reflection->getName()] = $injector; |
| 226 | 258 | } |
| 227 | 259 | } |
| 228 | 260 | |
| 229 | - if (!$reflection->isInstantiable()) { |
|
| 261 | + if (!$reflection->isInstantiable()) |
|
| 262 | + { |
|
| 230 | 263 | $itIs = match (true) { |
| 231 | 264 | $reflection->isEnum() => 'Enum', |
| 232 | 265 | $reflection->isAbstract() => 'Abstract class', |
@@ -239,10 +272,14 @@ discard block |
||
| 239 | 272 | |
| 240 | 273 | $constructor = $reflection->getConstructor(); |
| 241 | 274 | |
| 242 | - if ($constructor !== null) { |
|
| 243 | - try { |
|
| 275 | + if ($constructor !== null) |
|
| 276 | + { |
|
| 277 | + try |
|
| 278 | + { |
|
| 244 | 279 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 245 | - } catch (ValidationException $e) { |
|
| 280 | + } |
|
| 281 | + catch (ValidationException $e) |
|
| 282 | + { |
|
| 246 | 283 | throw new ContainerException( |
| 247 | 284 | $this->tracer->combineTraceMessage( |
| 248 | 285 | \sprintf( |
@@ -253,13 +290,18 @@ discard block |
||
| 253 | 290 | ), |
| 254 | 291 | ); |
| 255 | 292 | } |
| 256 | - try { |
|
| 293 | + try |
|
| 294 | + { |
|
| 257 | 295 | // Using constructor with resolved arguments |
| 258 | 296 | $instance = new $class(...$arguments); |
| 259 | - } catch (\TypeError $e) { |
|
| 297 | + } |
|
| 298 | + catch (\TypeError $e) |
|
| 299 | + { |
|
| 260 | 300 | throw new WrongTypeException($constructor, $e); |
| 261 | 301 | } |
| 262 | - } else { |
|
| 302 | + } |
|
| 303 | + else |
|
| 304 | + { |
|
| 263 | 305 | // No constructor specified |
| 264 | 306 | $instance = $reflection->newInstance(); |
| 265 | 307 | } |
@@ -277,9 +319,11 @@ discard block |
||
| 277 | 319 | private function registerInstance(object $instance, array $parameters): object |
| 278 | 320 | { |
| 279 | 321 | //Declarative singletons (only when class received via direct get) |
| 280 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 322 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
| 323 | + { |
|
| 281 | 324 | $alias = $instance::class; |
| 282 | - if (!isset($this->state->bindings[$alias])) { |
|
| 325 | + if (!isset($this->state->bindings[$alias])) |
|
| 326 | + { |
|
| 283 | 327 | $this->state->bindings[$alias] = $instance; |
| 284 | 328 | } |
| 285 | 329 | } |