@@ -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 | } |
@@ -131,9 +131,9 @@ |
||
| 131 | 131 | { |
| 132 | 132 | $container = new Container(); |
| 133 | 133 | |
| 134 | - try { |
|
| 134 | + try{ |
|
| 135 | 135 | $container->get('invalid'); |
| 136 | - } catch (ContainerException $e) { |
|
| 136 | + }catch (ContainerException $e){ |
|
| 137 | 137 | $this->assertSame( |
| 138 | 138 | <<<MARKDOWN |
| 139 | 139 | Can't resolve `invalid`: undefined class or binding `invalid`. |
@@ -131,9 +131,12 @@ |
||
| 131 | 131 | { |
| 132 | 132 | $container = new Container(); |
| 133 | 133 | |
| 134 | - try { |
|
| 134 | + try |
|
| 135 | + { |
|
| 135 | 136 | $container->get('invalid'); |
| 136 | - } catch (ContainerException $e) { |
|
| 137 | + } |
|
| 138 | + catch (ContainerException $e) |
|
| 139 | + { |
|
| 137 | 140 | $this->assertSame( |
| 138 | 141 | <<<MARKDOWN |
| 139 | 142 | Can't resolve `invalid`: undefined class or binding `invalid`. |
@@ -27,7 +27,8 @@ discard block |
||
| 27 | 27 | public function __toString(): string |
| 28 | 28 | { |
| 29 | 29 | $info = []; |
| 30 | - foreach ($this->info as $key => $item) { |
|
| 30 | + foreach ($this->info as $key => $item) |
|
| 31 | + { |
|
| 31 | 32 | $info[] = "$key: {$this->stringifyValue($item)}"; |
| 32 | 33 | } |
| 33 | 34 | return \implode("\n", $info); |
@@ -47,14 +48,17 @@ discard block |
||
| 47 | 48 | |
| 48 | 49 | private function renderArray(array $array, int $level = 0): string |
| 49 | 50 | { |
| 50 | - if ($array === []) { |
|
| 51 | + if ($array === []) |
|
| 52 | + { |
|
| 51 | 53 | return '[]'; |
| 52 | 54 | } |
| 53 | - if ($level >= self::ARRAY_MAX_LEVEL) { |
|
| 55 | + if ($level >= self::ARRAY_MAX_LEVEL) |
|
| 56 | + { |
|
| 54 | 57 | return 'array'; |
| 55 | 58 | } |
| 56 | 59 | $result = []; |
| 57 | - foreach ($array as $key => $value) { |
|
| 60 | + foreach ($array as $key => $value) |
|
| 61 | + { |
|
| 58 | 62 | $result[] = \sprintf( |
| 59 | 63 | '%s: %s', |
| 60 | 64 | $key, |
@@ -19,14 +19,14 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | public function __construct( |
| 21 | 21 | public array $info = [], |
| 22 | - ) { |
|
| 22 | + ){ |
|
| 23 | 23 | $this->alias = $info['alias'] ?? null; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | 26 | public function __toString(): string |
| 27 | 27 | { |
| 28 | 28 | $info = []; |
| 29 | - foreach ($this->info as $key => $item) { |
|
| 29 | + foreach ($this->info as $key => $item){ |
|
| 30 | 30 | $info[] = "$key: {$this->stringifyValue($item)}"; |
| 31 | 31 | } |
| 32 | 32 | return \implode("\n", $info); |
@@ -39,8 +39,8 @@ discard block |
||
| 39 | 39 | \is_scalar($item) => \var_export($item, true), |
| 40 | 40 | $item instanceof \Closure => $this->renderClosureSignature(new \ReflectionFunction($item)), |
| 41 | 41 | $item instanceof \ReflectionFunctionAbstract => $this->renderClosureSignature($item), |
| 42 | - $item instanceof \UnitEnum => $item::class . "::$item->name", |
|
| 43 | - \is_object($item) => 'instance of ' . $item::class, |
|
| 42 | + $item instanceof \UnitEnum => $item::class."::$item->name", |
|
| 43 | + \is_object($item) => 'instance of '.$item::class, |
|
| 44 | 44 | \is_array($item) => $this->renderArray($item), |
| 45 | 45 | default => \gettype($item), |
| 46 | 46 | }; |
@@ -48,14 +48,14 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | private function renderArray(array $array, int $level = 0): string |
| 50 | 50 | { |
| 51 | - if ($array === []) { |
|
| 51 | + if ($array === []){ |
|
| 52 | 52 | return '[]'; |
| 53 | 53 | } |
| 54 | - if ($level >= self::ARRAY_MAX_LEVEL) { |
|
| 54 | + if ($level >= self::ARRAY_MAX_LEVEL){ |
|
| 55 | 55 | return 'array'; |
| 56 | 56 | } |
| 57 | 57 | $result = []; |
| 58 | - foreach ($array as $key => $value) { |
|
| 58 | + foreach ($array as $key => $value){ |
|
| 59 | 59 | $result[] = \sprintf( |
| 60 | 60 | '%s: %s', |
| 61 | 61 | $key, |
@@ -66,6 +66,6 @@ discard block |
||
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | $pad = \str_repeat(' ', $level); |
| 69 | - return "[\n $pad" . \implode(",\n $pad", $result) . "\n$pad]"; |
|
| 69 | + return "[\n $pad".\implode(",\n $pad", $result)."\n$pad]"; |
|
| 70 | 70 | } |
| 71 | 71 | } |
@@ -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,19 +34,19 @@ discard block |
||
| 34 | 34 | public function push(bool $nextLevel, mixed ...$details): void |
| 35 | 35 | { |
| 36 | 36 | $trace = $details === [] ? null : new Trace($details); |
| 37 | - if ($nextLevel || $this->traces === []) { |
|
| 37 | + if ($nextLevel || $this->traces === []){ |
|
| 38 | 38 | $this->traces[] = $trace === null ? [] : [$trace]; |
| 39 | - } elseif ($trace !== null) { |
|
| 39 | + } elseif ($trace !== null){ |
|
| 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 | } |
@@ -67,7 +67,7 @@ discard block |
||
| 67 | 67 | { |
| 68 | 68 | $result = []; |
| 69 | 69 | $i = 0; |
| 70 | - foreach ($blocks as $block) { |
|
| 70 | + foreach ($blocks as $block){ |
|
| 71 | 71 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
| 72 | 72 | } |
| 73 | 73 | return \implode("\n", $result); |
@@ -87,8 +87,8 @@ discard block |
||
| 87 | 87 | // Separator |
| 88 | 88 | $s = "\n"; |
| 89 | 89 | $nexPrefix = "$s$padding "; |
| 90 | - foreach ($items as $item) { |
|
| 91 | - $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
|
| 90 | + foreach ($items as $item){ |
|
| 91 | + $result[] = $firstPrefix.\str_replace($s, $nexPrefix, (string)$item); |
|
| 92 | 92 | } |
| 93 | 93 | return $result; |
| 94 | 94 | } |
@@ -34,19 +34,24 @@ discard block |
||
| 34 | 34 | public function push(bool $nextLevel, mixed ...$details): void |
| 35 | 35 | { |
| 36 | 36 | $trace = $details === [] ? null : new Trace($details); |
| 37 | - if ($nextLevel || $this->traces === []) { |
|
| 37 | + if ($nextLevel || $this->traces === []) |
|
| 38 | + { |
|
| 38 | 39 | $this->traces[] = $trace === null ? [] : [$trace]; |
| 39 | - } elseif ($trace !== null) { |
|
| 40 | + } |
|
| 41 | + elseif ($trace !== null) |
|
| 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 | } |
@@ -67,7 +72,8 @@ discard block |
||
| 67 | 72 | { |
| 68 | 73 | $result = []; |
| 69 | 74 | $i = 0; |
| 70 | - foreach ($blocks as $block) { |
|
| 75 | + foreach ($blocks as $block) |
|
| 76 | + { |
|
| 71 | 77 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
| 72 | 78 | } |
| 73 | 79 | return \implode("\n", $result); |
@@ -87,7 +93,8 @@ discard block |
||
| 87 | 93 | // Separator |
| 88 | 94 | $s = "\n"; |
| 89 | 95 | $nexPrefix = "$s$padding "; |
| 90 | - foreach ($items as $item) { |
|
| 96 | + foreach ($items as $item) |
|
| 97 | + { |
|
| 91 | 98 | $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
| 92 | 99 | } |
| 93 | 100 | return $result; |
@@ -54,34 +54,34 @@ 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(false, action: 'autowire', alias: $alias, 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 | } |
| 66 | 66 | |
| 67 | 67 | $binding = $this->state->bindings[$alias]; |
| 68 | - try { |
|
| 68 | + try{ |
|
| 69 | 69 | $this->tracer->push(false, action: 'get from binding', alias: $alias, context: $context, binding: $binding); |
| 70 | 70 | |
| 71 | - if (\is_object($binding)) { |
|
| 72 | - if ($binding::class === WeakReference::class) { |
|
| 73 | - if ($binding->get() === null && \class_exists($alias)) { |
|
| 74 | - try { |
|
| 71 | + if (\is_object($binding)){ |
|
| 72 | + if ($binding::class === WeakReference::class){ |
|
| 73 | + if ($binding->get() === null && \class_exists($alias)){ |
|
| 74 | + try{ |
|
| 75 | 75 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 76 | 76 | $object = $this->createInstance($alias, $parameters, $context); |
| 77 | 77 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 78 | - } catch (\Throwable) { |
|
| 78 | + }catch (\Throwable){ |
|
| 79 | 79 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 80 | 80 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 81 | 81 | $this->tracer->getRootAlias(), |
| 82 | 82 | $alias, |
| 83 | 83 | ))); |
| 84 | - } finally { |
|
| 84 | + }finally{ |
|
| 85 | 85 | $this->tracer->pop(); |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -91,25 +91,25 @@ discard block |
||
| 91 | 91 | return $binding; |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - if (\is_string($binding)) { |
|
| 94 | + if (\is_string($binding)){ |
|
| 95 | 95 | //Binding is pointing to something else |
| 96 | 96 | return $this->make($binding, $parameters, $context); |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | unset($this->state->bindings[$alias]); |
| 100 | - try { |
|
| 100 | + try{ |
|
| 101 | 101 | $instance = $binding[0] === $alias |
| 102 | 102 | ? $this->autowire($alias, $parameters, $context) |
| 103 | 103 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 104 | - } finally { |
|
| 104 | + }finally{ |
|
| 105 | 105 | /** @psalm-var class-string $alias */ |
| 106 | 106 | $this->state->bindings[$alias] = $binding; |
| 107 | 107 | } |
| 108 | - } finally { |
|
| 108 | + }finally{ |
|
| 109 | 109 | $this->tracer->pop(false); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | - if ($binding[1]) { |
|
| 112 | + if ($binding[1]){ |
|
| 113 | 113 | // Indicates singleton |
| 114 | 114 | /** @psalm-var class-string $alias */ |
| 115 | 115 | $this->state->bindings[$alias] = $instance; |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | */ |
| 129 | 129 | private function autowire(string $class, array $parameters, string $context = null): object |
| 130 | 130 | { |
| 131 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
| 131 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])){ |
|
| 132 | 132 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 133 | 133 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 134 | 134 | $this->tracer->getRootAlias(), |
@@ -155,18 +155,18 @@ discard block |
||
| 155 | 155 | array $parameters, |
| 156 | 156 | string $context = null |
| 157 | 157 | ): mixed { |
| 158 | - if (\is_string($target)) { |
|
| 158 | + if (\is_string($target)){ |
|
| 159 | 159 | // Reference |
| 160 | 160 | return $this->make($target, $parameters, $context); |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | - if ($target instanceof Autowire) { |
|
| 163 | + if ($target instanceof Autowire){ |
|
| 164 | 164 | return $target->resolve($this, $parameters); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - try { |
|
| 167 | + try{ |
|
| 168 | 168 | return $this->invoker->invoke($target, $parameters); |
| 169 | - } catch (NotCallableException $e) { |
|
| 169 | + }catch (NotCallableException $e){ |
|
| 170 | 170 | throw new ContainerException( |
| 171 | 171 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 172 | 172 | $e->getCode(), |
@@ -190,20 +190,20 @@ discard block |
||
| 190 | 190 | */ |
| 191 | 191 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 192 | 192 | { |
| 193 | - try { |
|
| 193 | + try{ |
|
| 194 | 194 | $reflection = new \ReflectionClass($class); |
| 195 | - } catch (\ReflectionException $e) { |
|
| 195 | + }catch (\ReflectionException $e){ |
|
| 196 | 196 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | //We have to construct class using external injector when we know exact context |
| 200 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 200 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
| 201 | 201 | $injector = $this->state->injectors[$reflection->getName()]; |
| 202 | 202 | |
| 203 | - try { |
|
| 203 | + try{ |
|
| 204 | 204 | $injectorInstance = $this->container->get($injector); |
| 205 | 205 | |
| 206 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 206 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 207 | 207 | throw new InjectionException( |
| 208 | 208 | \sprintf( |
| 209 | 209 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | |
| 216 | 216 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 217 | 217 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 218 | - if (!$reflection->isInstance($instance)) { |
|
| 218 | + if (!$reflection->isInstance($instance)){ |
|
| 219 | 219 | throw new InjectionException( |
| 220 | 220 | \sprintf( |
| 221 | 221 | "Invalid injection response for '%s'.", |
@@ -225,12 +225,12 @@ discard block |
||
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | return $instance; |
| 228 | - } finally { |
|
| 228 | + }finally{ |
|
| 229 | 229 | $this->state->injectors[$reflection->getName()] = $injector; |
| 230 | 230 | } |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | - if (!$reflection->isInstantiable()) { |
|
| 233 | + if (!$reflection->isInstantiable()){ |
|
| 234 | 234 | $itIs = match (true) { |
| 235 | 235 | $reflection->isEnum() => 'Enum', |
| 236 | 236 | $reflection->isAbstract() => 'Abstract class', |
@@ -243,12 +243,12 @@ discard block |
||
| 243 | 243 | |
| 244 | 244 | $constructor = $reflection->getConstructor(); |
| 245 | 245 | |
| 246 | - if ($constructor !== null) { |
|
| 247 | - try { |
|
| 246 | + if ($constructor !== null){ |
|
| 247 | + try{ |
|
| 248 | 248 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 249 | 249 | $this->tracer->push(true); |
| 250 | 250 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 251 | - } catch (ValidationException $e) { |
|
| 251 | + }catch (ValidationException $e){ |
|
| 252 | 252 | throw new ContainerException( |
| 253 | 253 | $this->tracer->combineTraceMessage( |
| 254 | 254 | \sprintf( |
@@ -258,22 +258,22 @@ discard block |
||
| 258 | 258 | ) |
| 259 | 259 | ), |
| 260 | 260 | ); |
| 261 | - } finally { |
|
| 261 | + }finally{ |
|
| 262 | 262 | $this->tracer->pop(true); |
| 263 | 263 | $this->tracer->pop(false); |
| 264 | 264 | } |
| 265 | - try { |
|
| 265 | + try{ |
|
| 266 | 266 | // Using constructor with resolved arguments |
| 267 | 267 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 268 | 268 | $this->tracer->push(true); |
| 269 | 269 | $instance = new $class(...$arguments); |
| 270 | - } catch (\TypeError $e) { |
|
| 270 | + }catch (\TypeError $e){ |
|
| 271 | 271 | throw new WrongTypeException($constructor, $e); |
| 272 | - } finally { |
|
| 272 | + }finally{ |
|
| 273 | 273 | $this->tracer->pop(true); |
| 274 | 274 | $this->tracer->pop(false); |
| 275 | 275 | } |
| 276 | - } else { |
|
| 276 | + }else{ |
|
| 277 | 277 | // No constructor specified |
| 278 | 278 | $instance = $reflection->newInstance(); |
| 279 | 279 | } |
@@ -291,9 +291,9 @@ discard block |
||
| 291 | 291 | private function registerInstance(object $instance, array $parameters): object |
| 292 | 292 | { |
| 293 | 293 | //Declarative singletons (only when class received via direct get) |
| 294 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 294 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
| 295 | 295 | $alias = $instance::class; |
| 296 | - if (!isset($this->state->bindings[$alias])) { |
|
| 296 | + if (!isset($this->state->bindings[$alias])){ |
|
| 297 | 297 | $this->state->bindings[$alias] = $instance; |
| 298 | 298 | } |
| 299 | 299 | } |
@@ -54,34 +54,47 @@ 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(false, action: 'autowire', alias: $alias, 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 | } |
| 66 | 70 | |
| 67 | 71 | $binding = $this->state->bindings[$alias]; |
| 68 | - try { |
|
| 72 | + try |
|
| 73 | + { |
|
| 69 | 74 | $this->tracer->push(false, action: 'get from binding', alias: $alias, context: $context, binding: $binding); |
| 70 | 75 | |
| 71 | - if (\is_object($binding)) { |
|
| 72 | - if ($binding::class === WeakReference::class) { |
|
| 73 | - if ($binding->get() === null && \class_exists($alias)) { |
|
| 74 | - try { |
|
| 76 | + if (\is_object($binding)) |
|
| 77 | + { |
|
| 78 | + if ($binding::class === WeakReference::class) |
|
| 79 | + { |
|
| 80 | + if ($binding->get() === null && \class_exists($alias)) |
|
| 81 | + { |
|
| 82 | + try |
|
| 83 | + { |
|
| 75 | 84 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 76 | 85 | $object = $this->createInstance($alias, $parameters, $context); |
| 77 | 86 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 78 | - } catch (\Throwable) { |
|
| 87 | + } |
|
| 88 | + catch (\Throwable) |
|
| 89 | + { |
|
| 79 | 90 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 80 | 91 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 81 | 92 | $this->tracer->getRootAlias(), |
| 82 | 93 | $alias, |
| 83 | 94 | ))); |
| 84 | - } finally { |
|
| 95 | + } |
|
| 96 | + finally |
|
| 97 | + { |
|
| 85 | 98 | $this->tracer->pop(); |
| 86 | 99 | } |
| 87 | 100 | } |
@@ -91,25 +104,32 @@ discard block |
||
| 91 | 104 | return $binding; |
| 92 | 105 | } |
| 93 | 106 | |
| 94 | - if (\is_string($binding)) { |
|
| 107 | + if (\is_string($binding)) |
|
| 108 | + { |
|
| 95 | 109 | //Binding is pointing to something else |
| 96 | 110 | return $this->make($binding, $parameters, $context); |
| 97 | 111 | } |
| 98 | 112 | |
| 99 | 113 | unset($this->state->bindings[$alias]); |
| 100 | - try { |
|
| 114 | + try |
|
| 115 | + { |
|
| 101 | 116 | $instance = $binding[0] === $alias |
| 102 | 117 | ? $this->autowire($alias, $parameters, $context) |
| 103 | 118 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 104 | - } finally { |
|
| 119 | + } |
|
| 120 | + finally |
|
| 121 | + { |
|
| 105 | 122 | /** @psalm-var class-string $alias */ |
| 106 | 123 | $this->state->bindings[$alias] = $binding; |
| 107 | 124 | } |
| 108 | - } finally { |
|
| 125 | + } |
|
| 126 | + finally |
|
| 127 | + { |
|
| 109 | 128 | $this->tracer->pop(false); |
| 110 | 129 | } |
| 111 | 130 | |
| 112 | - if ($binding[1]) { |
|
| 131 | + if ($binding[1]) |
|
| 132 | + { |
|
| 113 | 133 | // Indicates singleton |
| 114 | 134 | /** @psalm-var class-string $alias */ |
| 115 | 135 | $this->state->bindings[$alias] = $instance; |
@@ -128,7 +148,8 @@ discard block |
||
| 128 | 148 | */ |
| 129 | 149 | private function autowire(string $class, array $parameters, string $context = null): object |
| 130 | 150 | { |
| 131 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
| 151 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])) |
|
| 152 | + { |
|
| 132 | 153 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 133 | 154 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 134 | 155 | $this->tracer->getRootAlias(), |
@@ -155,18 +176,23 @@ discard block |
||
| 155 | 176 | array $parameters, |
| 156 | 177 | string $context = null |
| 157 | 178 | ): mixed { |
| 158 | - if (\is_string($target)) { |
|
| 179 | + if (\is_string($target)) |
|
| 180 | + { |
|
| 159 | 181 | // Reference |
| 160 | 182 | return $this->make($target, $parameters, $context); |
| 161 | 183 | } |
| 162 | 184 | |
| 163 | - if ($target instanceof Autowire) { |
|
| 185 | + if ($target instanceof Autowire) |
|
| 186 | + { |
|
| 164 | 187 | return $target->resolve($this, $parameters); |
| 165 | 188 | } |
| 166 | 189 | |
| 167 | - try { |
|
| 190 | + try |
|
| 191 | + { |
|
| 168 | 192 | return $this->invoker->invoke($target, $parameters); |
| 169 | - } catch (NotCallableException $e) { |
|
| 193 | + } |
|
| 194 | + catch (NotCallableException $e) |
|
| 195 | + { |
|
| 170 | 196 | throw new ContainerException( |
| 171 | 197 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 172 | 198 | $e->getCode(), |
@@ -190,20 +216,26 @@ discard block |
||
| 190 | 216 | */ |
| 191 | 217 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 192 | 218 | { |
| 193 | - try { |
|
| 219 | + try |
|
| 220 | + { |
|
| 194 | 221 | $reflection = new \ReflectionClass($class); |
| 195 | - } catch (\ReflectionException $e) { |
|
| 222 | + } |
|
| 223 | + catch (\ReflectionException $e) |
|
| 224 | + { |
|
| 196 | 225 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 197 | 226 | } |
| 198 | 227 | |
| 199 | 228 | //We have to construct class using external injector when we know exact context |
| 200 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 229 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 230 | + { |
|
| 201 | 231 | $injector = $this->state->injectors[$reflection->getName()]; |
| 202 | 232 | |
| 203 | - try { |
|
| 233 | + try |
|
| 234 | + { |
|
| 204 | 235 | $injectorInstance = $this->container->get($injector); |
| 205 | 236 | |
| 206 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 237 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 238 | + { |
|
| 207 | 239 | throw new InjectionException( |
| 208 | 240 | \sprintf( |
| 209 | 241 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -215,7 +247,8 @@ discard block |
||
| 215 | 247 | |
| 216 | 248 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 217 | 249 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 218 | - if (!$reflection->isInstance($instance)) { |
|
| 250 | + if (!$reflection->isInstance($instance)) |
|
| 251 | + { |
|
| 219 | 252 | throw new InjectionException( |
| 220 | 253 | \sprintf( |
| 221 | 254 | "Invalid injection response for '%s'.", |
@@ -225,12 +258,15 @@ discard block |
||
| 225 | 258 | } |
| 226 | 259 | |
| 227 | 260 | return $instance; |
| 228 | - } finally { |
|
| 261 | + } |
|
| 262 | + finally |
|
| 263 | + { |
|
| 229 | 264 | $this->state->injectors[$reflection->getName()] = $injector; |
| 230 | 265 | } |
| 231 | 266 | } |
| 232 | 267 | |
| 233 | - if (!$reflection->isInstantiable()) { |
|
| 268 | + if (!$reflection->isInstantiable()) |
|
| 269 | + { |
|
| 234 | 270 | $itIs = match (true) { |
| 235 | 271 | $reflection->isEnum() => 'Enum', |
| 236 | 272 | $reflection->isAbstract() => 'Abstract class', |
@@ -243,12 +279,16 @@ discard block |
||
| 243 | 279 | |
| 244 | 280 | $constructor = $reflection->getConstructor(); |
| 245 | 281 | |
| 246 | - if ($constructor !== null) { |
|
| 247 | - try { |
|
| 282 | + if ($constructor !== null) |
|
| 283 | + { |
|
| 284 | + try |
|
| 285 | + { |
|
| 248 | 286 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 249 | 287 | $this->tracer->push(true); |
| 250 | 288 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 251 | - } catch (ValidationException $e) { |
|
| 289 | + } |
|
| 290 | + catch (ValidationException $e) |
|
| 291 | + { |
|
| 252 | 292 | throw new ContainerException( |
| 253 | 293 | $this->tracer->combineTraceMessage( |
| 254 | 294 | \sprintf( |
@@ -258,22 +298,31 @@ discard block |
||
| 258 | 298 | ) |
| 259 | 299 | ), |
| 260 | 300 | ); |
| 261 | - } finally { |
|
| 301 | + } |
|
| 302 | + finally |
|
| 303 | + { |
|
| 262 | 304 | $this->tracer->pop(true); |
| 263 | 305 | $this->tracer->pop(false); |
| 264 | 306 | } |
| 265 | - try { |
|
| 307 | + try |
|
| 308 | + { |
|
| 266 | 309 | // Using constructor with resolved arguments |
| 267 | 310 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 268 | 311 | $this->tracer->push(true); |
| 269 | 312 | $instance = new $class(...$arguments); |
| 270 | - } catch (\TypeError $e) { |
|
| 313 | + } |
|
| 314 | + catch (\TypeError $e) |
|
| 315 | + { |
|
| 271 | 316 | throw new WrongTypeException($constructor, $e); |
| 272 | - } finally { |
|
| 317 | + } |
|
| 318 | + finally |
|
| 319 | + { |
|
| 273 | 320 | $this->tracer->pop(true); |
| 274 | 321 | $this->tracer->pop(false); |
| 275 | 322 | } |
| 276 | - } else { |
|
| 323 | + } |
|
| 324 | + else |
|
| 325 | + { |
|
| 277 | 326 | // No constructor specified |
| 278 | 327 | $instance = $reflection->newInstance(); |
| 279 | 328 | } |
@@ -291,9 +340,11 @@ discard block |
||
| 291 | 340 | private function registerInstance(object $instance, array $parameters): object |
| 292 | 341 | { |
| 293 | 342 | //Declarative singletons (only when class received via direct get) |
| 294 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 343 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
| 344 | + { |
|
| 295 | 345 | $alias = $instance::class; |
| 296 | - if (!isset($this->state->bindings[$alias])) { |
|
| 346 | + if (!isset($this->state->bindings[$alias])) |
|
| 347 | + { |
|
| 297 | 348 | $this->state->bindings[$alias] = $instance; |
| 298 | 349 | } |
| 299 | 350 | } |