@@ -13,7 +13,7 @@ |
||
| 13 | 13 | { |
| 14 | 14 | public function __construct( |
| 15 | 15 | private LoggerInterface $logger, |
| 16 | - ) { |
|
| 16 | + ){ |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | 19 | public function createInjection(\ReflectionClass $class, ?string $context = null): LoggerInterface |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | |
| 37 | 37 | public function testInvalidInjector(): void |
| 38 | 38 | { |
| 39 | - $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " . |
|
| 39 | + $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an ". |
|
| 40 | 40 | "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'"; |
| 41 | 41 | $this->expectException(InjectionException::class); |
| 42 | 42 | $this->expectExceptionMessage($excepted); |
@@ -62,7 +62,7 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | public function testInvalidRuntimeInjector(): void |
| 64 | 64 | { |
| 65 | - $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " . |
|
| 65 | + $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an ". |
|
| 66 | 66 | "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'"; |
| 67 | 67 | $this->expectException(InjectionException::class); |
| 68 | 68 | $this->expectExceptionMessage($excepted); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | $container->bind(ConfigsInterface::class, $configurator); |
| 100 | 100 | |
| 101 | 101 | $configurator->shouldReceive('createInjection') |
| 102 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 102 | + ->with(m::on(static function (ReflectionClass $r){ |
|
| 103 | 103 | return $r->getName() === TestConfig::class; |
| 104 | 104 | }), null) |
| 105 | 105 | ->andReturn($expected); |
@@ -116,7 +116,7 @@ discard block |
||
| 116 | 116 | $container->bind(ConfigsInterface::class, $configurator); |
| 117 | 117 | |
| 118 | 118 | $configurator->shouldReceive('createInjection') |
| 119 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 119 | + ->with(m::on(static function (ReflectionClass $r){ |
|
| 120 | 120 | return $r->getName() === TestConfig::class; |
| 121 | 121 | }), 'context') |
| 122 | 122 | ->andReturn($expected); |
@@ -99,7 +99,8 @@ discard block |
||
| 99 | 99 | $container->bind(ConfigsInterface::class, $configurator); |
| 100 | 100 | |
| 101 | 101 | $configurator->shouldReceive('createInjection') |
| 102 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 102 | + ->with(m::on(static function (ReflectionClass $r) |
|
| 103 | + { |
|
| 103 | 104 | return $r->getName() === TestConfig::class; |
| 104 | 105 | }), null) |
| 105 | 106 | ->andReturn($expected); |
@@ -116,7 +117,8 @@ discard block |
||
| 116 | 117 | $container->bind(ConfigsInterface::class, $configurator); |
| 117 | 118 | |
| 118 | 119 | $configurator->shouldReceive('createInjection') |
| 119 | - ->with(m::on(static function (ReflectionClass $r) { |
|
| 120 | + ->with(m::on(static function (ReflectionClass $r) |
|
| 121 | + { |
|
| 120 | 122 | return $r->getName() === TestConfig::class; |
| 121 | 123 | }), 'context') |
| 122 | 124 | ->andReturn($expected); |
@@ -135,9 +135,9 @@ discard block |
||
| 135 | 135 | { |
| 136 | 136 | $container = new Container(); |
| 137 | 137 | |
| 138 | - try { |
|
| 138 | + try{ |
|
| 139 | 139 | $container->get('invalid'); |
| 140 | - } catch (ContainerException $e) { |
|
| 140 | + }catch (ContainerException $e){ |
|
| 141 | 141 | $this->assertSame( |
| 142 | 142 | <<<MARKDOWN |
| 143 | 143 | Can't resolve `invalid`: undefined class or binding `invalid`. |
@@ -169,9 +169,9 @@ discard block |
||
| 169 | 169 | { |
| 170 | 170 | $this->expectException(ContainerException::class); |
| 171 | 171 | |
| 172 | - try { |
|
| 172 | + try{ |
|
| 173 | 173 | $container->get(ClassWithUndefinedDependency::class); |
| 174 | - } catch (ContainerException $e) { |
|
| 174 | + }catch (ContainerException $e){ |
|
| 175 | 175 | self::assertSame($message, $e->getMessage()); |
| 176 | 176 | |
| 177 | 177 | throw $e; |
@@ -135,9 +135,12 @@ discard block |
||
| 135 | 135 | { |
| 136 | 136 | $container = new Container(); |
| 137 | 137 | |
| 138 | - try { |
|
| 138 | + try |
|
| 139 | + { |
|
| 139 | 140 | $container->get('invalid'); |
| 140 | - } catch (ContainerException $e) { |
|
| 141 | + } |
|
| 142 | + catch (ContainerException $e) |
|
| 143 | + { |
|
| 141 | 144 | $this->assertSame( |
| 142 | 145 | <<<MARKDOWN |
| 143 | 146 | Can't resolve `invalid`: undefined class or binding `invalid`. |
@@ -169,9 +172,12 @@ discard block |
||
| 169 | 172 | { |
| 170 | 173 | $this->expectException(ContainerException::class); |
| 171 | 174 | |
| 172 | - try { |
|
| 175 | + try |
|
| 176 | + { |
|
| 173 | 177 | $container->get(ClassWithUndefinedDependency::class); |
| 174 | - } catch (ContainerException $e) { |
|
| 178 | + } |
|
| 179 | + catch (ContainerException $e) |
|
| 180 | + { |
|
| 175 | 181 | self::assertSame($message, $e->getMessage()); |
| 176 | 182 | |
| 177 | 183 | throw $e; |
@@ -28,7 +28,7 @@ |
||
| 28 | 28 | |
| 29 | 29 | public function hasInstance(string $alias): bool |
| 30 | 30 | { |
| 31 | - if (!$this->container->has($alias)) { |
|
| 31 | + if (!$this->container->has($alias)){ |
|
| 32 | 32 | return false; |
| 33 | 33 | } |
| 34 | 34 | |
@@ -28,7 +28,8 @@ |
||
| 28 | 28 | |
| 29 | 29 | public function hasInstance(string $alias): bool |
| 30 | 30 | { |
| 31 | - if (!$this->container->has($alias)) { |
|
| 31 | + if (!$this->container->has($alias)) |
|
| 32 | + { |
|
| 32 | 33 | return false; |
| 33 | 34 | } |
| 34 | 35 | |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | { |
| 32 | 32 | public function __construct( |
| 33 | 33 | protected readonly State $state, |
| 34 | - ) { |
|
| 34 | + ){ |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
@@ -39,14 +39,14 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function bind(string $alias, mixed $resolver): void |
| 41 | 41 | { |
| 42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 42 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))){ |
|
| 43 | 43 | $this->state->inflectors[$alias][] = $resolver; |
| 44 | 44 | return; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - try { |
|
| 47 | + try{ |
|
| 48 | 48 | $config = $this->makeConfig($resolver, false); |
| 49 | - } catch (\Throwable $e) { |
|
| 49 | + }catch (\Throwable $e){ |
|
| 50 | 50 | throw $this->invalidBindingException($alias, $resolver, $e); |
| 51 | 51 | } |
| 52 | 52 | |
@@ -58,9 +58,9 @@ discard block |
||
| 58 | 58 | */ |
| 59 | 59 | public function bindSingleton(string $alias, mixed $resolver): void |
| 60 | 60 | { |
| 61 | - try { |
|
| 61 | + try{ |
|
| 62 | 62 | $config = $this->makeConfig($resolver, true); |
| 63 | - } catch (\Throwable $e) { |
|
| 63 | + }catch (\Throwable $e){ |
|
| 64 | 64 | throw $this->invalidBindingException($alias, $resolver, $e); |
| 65 | 65 | } |
| 66 | 66 | |
@@ -72,9 +72,9 @@ discard block |
||
| 72 | 72 | $bindings = &$this->state->bindings; |
| 73 | 73 | |
| 74 | 74 | $flags = []; |
| 75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 75 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class){ |
|
| 76 | 76 | //Checking alias tree |
| 77 | - if ($flags[$binding->alias] ?? false) { |
|
| 77 | + if ($flags[$binding->alias] ?? false){ |
|
| 78 | 78 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 79 | 79 | } |
| 80 | 80 | |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | public function removeInjector(string $class): void |
| 100 | 100 | { |
| 101 | 101 | unset($this->state->injectors[$class]); |
| 102 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 102 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class){ |
|
| 103 | 103 | return; |
| 104 | 104 | } |
| 105 | 105 | unset($this->state->bindings[$class]); |
@@ -107,20 +107,20 @@ discard block |
||
| 107 | 107 | |
| 108 | 108 | public function hasInjector(string $class): bool |
| 109 | 109 | { |
| 110 | - try { |
|
| 110 | + try{ |
|
| 111 | 111 | $reflection = new \ReflectionClass($class); |
| 112 | - } catch (\ReflectionException $e) { |
|
| 112 | + }catch (\ReflectionException $e){ |
|
| 113 | 113 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 116 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
| 117 | 117 | return true; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | if ( |
| 121 | 121 | $reflection->implementsInterface(InjectableInterface::class) |
| 122 | 122 | && $reflection->hasConstant('INJECTOR') |
| 123 | - ) { |
|
| 123 | + ){ |
|
| 124 | 124 | $const = $reflection->getConstant('INJECTOR'); |
| 125 | 125 | $this->bindInjector($class, $const); |
| 126 | 126 | |
@@ -128,12 +128,12 @@ discard block |
||
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // check interfaces |
| 131 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 131 | + foreach ($this->state->injectors as $target => $injector){ |
|
| 132 | 132 | if ( |
| 133 | 133 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 134 | 134 | || |
| 135 | 135 | (\interface_exists($target, true) && $reflection->implementsInterface($target)) |
| 136 | - ) { |
|
| 136 | + ){ |
|
| 137 | 137 | $this->state->bindings[$class] = new Injectable($injector); |
| 138 | 138 | |
| 139 | 139 | return true; |
@@ -160,15 +160,15 @@ discard block |
||
| 160 | 160 | |
| 161 | 161 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 162 | 162 | { |
| 163 | - if (\is_callable($resolver)) { |
|
| 163 | + if (\is_callable($resolver)){ |
|
| 164 | 164 | return new Factory($resolver, $singleton); |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Validate lazy invokable array |
| 168 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 168 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === ''){ |
|
| 169 | 169 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 170 | 170 | } |
| 171 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 171 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === ''){ |
|
| 172 | 172 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 173 | 173 | } |
| 174 | 174 | |
@@ -39,14 +39,18 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function bind(string $alias, mixed $resolver): void |
| 41 | 41 | { |
| 42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
| 42 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
| 43 | + { |
|
| 43 | 44 | $this->state->inflectors[$alias][] = $resolver; |
| 44 | 45 | return; |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | - try { |
|
| 48 | + try |
|
| 49 | + { |
|
| 48 | 50 | $config = $this->makeConfig($resolver, false); |
| 49 | - } catch (\Throwable $e) { |
|
| 51 | + } |
|
| 52 | + catch (\Throwable $e) |
|
| 53 | + { |
|
| 50 | 54 | throw $this->invalidBindingException($alias, $resolver, $e); |
| 51 | 55 | } |
| 52 | 56 | |
@@ -58,9 +62,12 @@ discard block |
||
| 58 | 62 | */ |
| 59 | 63 | public function bindSingleton(string $alias, mixed $resolver): void |
| 60 | 64 | { |
| 61 | - try { |
|
| 65 | + try |
|
| 66 | + { |
|
| 62 | 67 | $config = $this->makeConfig($resolver, true); |
| 63 | - } catch (\Throwable $e) { |
|
| 68 | + } |
|
| 69 | + catch (\Throwable $e) |
|
| 70 | + { |
|
| 64 | 71 | throw $this->invalidBindingException($alias, $resolver, $e); |
| 65 | 72 | } |
| 66 | 73 | |
@@ -72,9 +79,11 @@ discard block |
||
| 72 | 79 | $bindings = &$this->state->bindings; |
| 73 | 80 | |
| 74 | 81 | $flags = []; |
| 75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
| 82 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
| 83 | + { |
|
| 76 | 84 | //Checking alias tree |
| 77 | - if ($flags[$binding->alias] ?? false) { |
|
| 85 | + if ($flags[$binding->alias] ?? false) |
|
| 86 | + { |
|
| 78 | 87 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
| 79 | 88 | } |
| 80 | 89 | |
@@ -99,7 +108,8 @@ discard block |
||
| 99 | 108 | public function removeInjector(string $class): void |
| 100 | 109 | { |
| 101 | 110 | unset($this->state->injectors[$class]); |
| 102 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
| 111 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
| 112 | + { |
|
| 103 | 113 | return; |
| 104 | 114 | } |
| 105 | 115 | unset($this->state->bindings[$class]); |
@@ -107,13 +117,17 @@ discard block |
||
| 107 | 117 | |
| 108 | 118 | public function hasInjector(string $class): bool |
| 109 | 119 | { |
| 110 | - try { |
|
| 120 | + try |
|
| 121 | + { |
|
| 111 | 122 | $reflection = new \ReflectionClass($class); |
| 112 | - } catch (\ReflectionException $e) { |
|
| 123 | + } |
|
| 124 | + catch (\ReflectionException $e) |
|
| 125 | + { |
|
| 113 | 126 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 114 | 127 | } |
| 115 | 128 | |
| 116 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 129 | + if (\array_key_exists($class, $this->state->injectors)) |
|
| 130 | + { |
|
| 117 | 131 | return true; |
| 118 | 132 | } |
| 119 | 133 | |
@@ -128,7 +142,8 @@ discard block |
||
| 128 | 142 | } |
| 129 | 143 | |
| 130 | 144 | // check interfaces |
| 131 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 145 | + foreach ($this->state->injectors as $target => $injector) |
|
| 146 | + { |
|
| 132 | 147 | if ( |
| 133 | 148 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
| 134 | 149 | || |
@@ -160,15 +175,18 @@ discard block |
||
| 160 | 175 | |
| 161 | 176 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
| 162 | 177 | { |
| 163 | - if (\is_callable($resolver)) { |
|
| 178 | + if (\is_callable($resolver)) |
|
| 179 | + { |
|
| 164 | 180 | return new Factory($resolver, $singleton); |
| 165 | 181 | } |
| 166 | 182 | |
| 167 | 183 | // Validate lazy invokable array |
| 168 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
| 184 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
| 185 | + { |
|
| 169 | 186 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 170 | 187 | } |
| 171 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
| 188 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
| 189 | + { |
|
| 172 | 190 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
| 173 | 191 | } |
| 174 | 192 | |
@@ -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) => $item instanceof \Stringable ? (string) $item : 'instance of ' . $item::class, |
|
| 42 | + $item instanceof \UnitEnum => $item::class."::$item->name", |
|
| 43 | + \is_object($item) => $item instanceof \Stringable ? (string)$item : 'instance of '.$item::class, |
|
| 44 | 44 | \is_array($item) => $this->renderArray($item), |
| 45 | 45 | default => \get_debug_type($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 | } |
@@ -66,17 +66,17 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 68 | 68 | { |
| 69 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 69 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)){ |
|
| 70 | 70 | return $this->state->singletons[$alias]; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $binding = $this->state->bindings[$alias] ?? null; |
| 74 | 74 | |
| 75 | - if ($binding === null) { |
|
| 75 | + if ($binding === null){ |
|
| 76 | 76 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - try { |
|
| 79 | + try{ |
|
| 80 | 80 | $this->tracer->push( |
| 81 | 81 | false, |
| 82 | 82 | action: 'resolve from binding', |
@@ -100,7 +100,7 @@ discard block |
||
| 100 | 100 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 101 | 101 | default => $binding, |
| 102 | 102 | }; |
| 103 | - } finally { |
|
| 103 | + }finally{ |
|
| 104 | 104 | $this->state->bindings[$alias] ??= $binding; |
| 105 | 105 | $this->tracer->pop(true); |
| 106 | 106 | $this->tracer->pop(false); |
@@ -112,27 +112,27 @@ discard block |
||
| 112 | 112 | string $alias, |
| 113 | 113 | ?string $context, |
| 114 | 114 | array $arguments |
| 115 | - ) { |
|
| 116 | - $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
|
| 115 | + ){ |
|
| 116 | + $ctx = new Ctx(alias : $alias, class : $alias, parameter : $context); |
|
| 117 | 117 | |
| 118 | 118 | // We have to construct class using external injector when we know exact context |
| 119 | - if ($arguments !== []) { |
|
| 119 | + if ($arguments !== []){ |
|
| 120 | 120 | // todo factory? |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | $class = $ctx->class; |
| 124 | - try { |
|
| 124 | + try{ |
|
| 125 | 125 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 126 | - } catch (\ReflectionException $e) { |
|
| 126 | + }catch (\ReflectionException $e){ |
|
| 127 | 127 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | $injector = $binding->injector; |
| 131 | 131 | |
| 132 | - try { |
|
| 132 | + try{ |
|
| 133 | 133 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 134 | 134 | |
| 135 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 135 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 136 | 136 | throw new InjectionException( |
| 137 | 137 | \sprintf( |
| 138 | 138 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | * @psalm-suppress RedundantCondition |
| 148 | 148 | */ |
| 149 | 149 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
| 150 | - if (!$reflection->isInstance($instance)) { |
|
| 150 | + if (!$reflection->isInstance($instance)){ |
|
| 151 | 151 | throw new InjectionException( |
| 152 | 152 | \sprintf( |
| 153 | 153 | "Invalid injection response for '%s'.", |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | return $instance; |
| 160 | - } finally { |
|
| 160 | + }finally{ |
|
| 161 | 161 | $this->state->bindings[$reflection->getName()] ??= $binding; //new Injector($injector); |
| 162 | 162 | } |
| 163 | 163 | } |
@@ -170,13 +170,13 @@ discard block |
||
| 170 | 170 | ): mixed { |
| 171 | 171 | $result = $binding->alias === $alias |
| 172 | 172 | ? $this->autowire( |
| 173 | - new Ctx(alias: $alias, class: $binding->alias, parameter: $context, singleton: $binding->singleton), |
|
| 173 | + new Ctx(alias : $alias, class : $binding->alias, parameter : $context, singleton : $binding->singleton), |
|
| 174 | 174 | $arguments, |
| 175 | 175 | ) |
| 176 | 176 | //Binding is pointing to something else |
| 177 | 177 | : $this->make($binding->alias, $arguments, $context); |
| 178 | 178 | |
| 179 | - if ($binding->singleton && $arguments === []) { |
|
| 179 | + if ($binding->singleton && $arguments === []){ |
|
| 180 | 180 | $this->state->singletons[$alias] = $result; |
| 181 | 181 | } |
| 182 | 182 | |
@@ -192,7 +192,7 @@ discard block |
||
| 192 | 192 | $avoidCache = $arguments !== []; |
| 193 | 193 | return $avoidCache |
| 194 | 194 | ? $this->createInstance( |
| 195 | - new Ctx(alias: $alias, class: $binding->value::class, parameter: $context), |
|
| 195 | + new Ctx(alias : $alias, class : $binding->value::class, parameter : $context), |
|
| 196 | 196 | $arguments, |
| 197 | 197 | ) |
| 198 | 198 | : $binding->value; |
@@ -211,17 +211,17 @@ discard block |
||
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | private function resolveFactory( |
| 214 | - \Spiral\Core\Config\Factory|DeferredFactory $binding, |
|
| 214 | + \Spiral\Core\Config\Factory | DeferredFactory $binding, |
|
| 215 | 215 | string $alias, |
| 216 | 216 | ?string $context, |
| 217 | 217 | array $arguments, |
| 218 | 218 | ): mixed { |
| 219 | - $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context, singleton: $binding->singleton); |
|
| 220 | - try { |
|
| 219 | + $ctx = new Ctx(alias : $alias, class : $alias, parameter : $context, singleton : $binding->singleton); |
|
| 220 | + try{ |
|
| 221 | 221 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
| 222 | 222 | ? ($binding->factory)() |
| 223 | 223 | : $this->invoker->invoke($binding->factory, $arguments); |
| 224 | - } catch (NotCallableException $e) { |
|
| 224 | + }catch (NotCallableException $e){ |
|
| 225 | 225 | throw new ContainerException( |
| 226 | 226 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 227 | 227 | $e->getCode(), |
@@ -240,19 +240,19 @@ discard block |
||
| 240 | 240 | ): ?object { |
| 241 | 241 | $avoidCache = $arguments !== []; |
| 242 | 242 | |
| 243 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 244 | - try { |
|
| 243 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
| 244 | + try{ |
|
| 245 | 245 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 246 | 246 | |
| 247 | 247 | $object = $this->createInstance( |
| 248 | 248 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 249 | 249 | $arguments, |
| 250 | 250 | ); |
| 251 | - if ($avoidCache) { |
|
| 251 | + if ($avoidCache){ |
|
| 252 | 252 | return $object; |
| 253 | 253 | } |
| 254 | 254 | $binding->reference = WeakReference::create($object); |
| 255 | - } catch (\Throwable) { |
|
| 255 | + }catch (\Throwable){ |
|
| 256 | 256 | throw new ContainerException( |
| 257 | 257 | $this->tracer->combineTraceMessage( |
| 258 | 258 | \sprintf( |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | ) |
| 263 | 263 | ) |
| 264 | 264 | ); |
| 265 | - } finally { |
|
| 265 | + }finally{ |
|
| 266 | 266 | $this->tracer->pop(); |
| 267 | 267 | } |
| 268 | 268 | } |
@@ -274,18 +274,18 @@ discard block |
||
| 274 | 274 | { |
| 275 | 275 | $parent = $this->scope->getParent(); |
| 276 | 276 | |
| 277 | - if ($parent !== null) { |
|
| 278 | - try { |
|
| 277 | + if ($parent !== null){ |
|
| 278 | + try{ |
|
| 279 | 279 | $this->tracer->push(false, ...[ |
| 280 | 280 | 'current scope' => $this->scope->getScopeName(), |
| 281 | 281 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 282 | 282 | ]); |
| 283 | 283 | return $parent->make($alias, $parameters, $context); |
| 284 | - } catch (BadScopeException $e) { |
|
| 285 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 284 | + }catch (BadScopeException $e){ |
|
| 285 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
| 286 | 286 | throw $e; |
| 287 | 287 | } |
| 288 | - } catch (ContainerExceptionInterface $e) { |
|
| 288 | + }catch (ContainerExceptionInterface $e){ |
|
| 289 | 289 | $className = match (true) { |
| 290 | 290 | $e instanceof NotFoundException => NotFoundException::class, |
| 291 | 291 | default => ContainerException::class, |
@@ -294,19 +294,19 @@ discard block |
||
| 294 | 294 | 'Can\'t resolve `%s`.', |
| 295 | 295 | $alias, |
| 296 | 296 | )), previous: $e); |
| 297 | - } finally { |
|
| 297 | + }finally{ |
|
| 298 | 298 | $this->tracer->pop(false); |
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 303 | - try { |
|
| 303 | + try{ |
|
| 304 | 304 | //No direct instructions how to construct class, make is automatically |
| 305 | 305 | return $this->autowire( |
| 306 | 306 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 307 | 307 | $parameters, |
| 308 | 308 | ); |
| 309 | - } finally { |
|
| 309 | + }finally{ |
|
| 310 | 310 | $this->tracer->pop(false); |
| 311 | 311 | } |
| 312 | 312 | } |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | && |
| 329 | 329 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
| 330 | 330 | )) |
| 331 | - ) { |
|
| 331 | + ){ |
|
| 332 | 332 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 333 | 333 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 334 | 334 | $this->tracer->getRootAlias(), |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | // Check scope name |
| 358 | 358 | $ctx->reflection = new \ReflectionClass($instance); |
| 359 | 359 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 360 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 360 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()){ |
|
| 361 | 361 | throw new BadScopeException($scopeName, $instance::class); |
| 362 | 362 | } |
| 363 | 363 | |
@@ -384,24 +384,24 @@ discard block |
||
| 384 | 384 | array $parameters, |
| 385 | 385 | ): object { |
| 386 | 386 | $class = $ctx->class; |
| 387 | - try { |
|
| 387 | + try{ |
|
| 388 | 388 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 389 | - } catch (\ReflectionException $e) { |
|
| 389 | + }catch (\ReflectionException $e){ |
|
| 390 | 390 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | 393 | // Check scope name |
| 394 | 394 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 395 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 395 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
| 396 | 396 | throw new BadScopeException($scope, $class); |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | //We have to construct class using external injector when we know exact context |
| 400 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 400 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
| 401 | 401 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx->class, $ctx->parameter, $parameters); |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | - if (!$reflection->isInstantiable()) { |
|
| 404 | + if (!$reflection->isInstantiable()){ |
|
| 405 | 405 | $itIs = match (true) { |
| 406 | 406 | $reflection->isEnum() => 'Enum', |
| 407 | 407 | $reflection->isAbstract() => 'Abstract class', |
@@ -414,12 +414,12 @@ discard block |
||
| 414 | 414 | |
| 415 | 415 | $constructor = $reflection->getConstructor(); |
| 416 | 416 | |
| 417 | - if ($constructor !== null) { |
|
| 418 | - try { |
|
| 417 | + if ($constructor !== null){ |
|
| 418 | + try{ |
|
| 419 | 419 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 420 | 420 | $this->tracer->push(true); |
| 421 | 421 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 422 | - } catch (ValidationException $e) { |
|
| 422 | + }catch (ValidationException $e){ |
|
| 423 | 423 | throw new ContainerException( |
| 424 | 424 | $this->tracer->combineTraceMessage( |
| 425 | 425 | \sprintf( |
@@ -429,22 +429,22 @@ discard block |
||
| 429 | 429 | ) |
| 430 | 430 | ), |
| 431 | 431 | ); |
| 432 | - } finally { |
|
| 432 | + }finally{ |
|
| 433 | 433 | $this->tracer->pop(true); |
| 434 | 434 | $this->tracer->pop(false); |
| 435 | 435 | } |
| 436 | - try { |
|
| 436 | + try{ |
|
| 437 | 437 | // Using constructor with resolved arguments |
| 438 | 438 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 439 | 439 | $this->tracer->push(true); |
| 440 | 440 | $instance = new $class(...$arguments); |
| 441 | - } catch (\TypeError $e) { |
|
| 441 | + }catch (\TypeError $e){ |
|
| 442 | 442 | throw new WrongTypeException($constructor, $e); |
| 443 | - } finally { |
|
| 443 | + }finally{ |
|
| 444 | 444 | $this->tracer->pop(true); |
| 445 | 445 | $this->tracer->pop(false); |
| 446 | 446 | } |
| 447 | - } else { |
|
| 447 | + }else{ |
|
| 448 | 448 | // No constructor specified |
| 449 | 449 | $instance = $reflection->newInstance(); |
| 450 | 450 | } |
@@ -470,13 +470,13 @@ discard block |
||
| 470 | 470 | $instance = $this->runInflector($instance); |
| 471 | 471 | |
| 472 | 472 | //Declarative singletons |
| 473 | - if ($this->isSingleton($ctx)) { |
|
| 473 | + if ($this->isSingleton($ctx)){ |
|
| 474 | 474 | $this->state->singletons[$ctx->alias] = $instance; |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | // Register finalizer |
| 478 | 478 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 479 | - if ($finalizer !== null) { |
|
| 479 | + if ($finalizer !== null){ |
|
| 480 | 480 | $this->state->finalizers[] = $finalizer; |
| 481 | 481 | } |
| 482 | 482 | |
@@ -488,12 +488,12 @@ discard block |
||
| 488 | 488 | */ |
| 489 | 489 | private function isSingleton(Ctx $ctx): bool |
| 490 | 490 | { |
| 491 | - if ($ctx->singleton === true) { |
|
| 491 | + if ($ctx->singleton === true){ |
|
| 492 | 492 | return true; |
| 493 | 493 | } |
| 494 | 494 | |
| 495 | 495 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 496 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 496 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
| 497 | 497 | return true; |
| 498 | 498 | } |
| 499 | 499 | |
@@ -507,7 +507,7 @@ discard block |
||
| 507 | 507 | * @var Finalize|null $attribute |
| 508 | 508 | */ |
| 509 | 509 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 510 | - if ($attribute === null) { |
|
| 510 | + if ($attribute === null){ |
|
| 511 | 511 | return null; |
| 512 | 512 | } |
| 513 | 513 | |
@@ -521,10 +521,10 @@ discard block |
||
| 521 | 521 | { |
| 522 | 522 | $scope = $this->scope; |
| 523 | 523 | |
| 524 | - while ($scope !== null) { |
|
| 525 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 526 | - if ($instance instanceof $class) { |
|
| 527 | - foreach ($inflectors as $inflector) { |
|
| 524 | + while ($scope !== null){ |
|
| 525 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
| 526 | + if ($instance instanceof $class){ |
|
| 527 | + foreach ($inflectors as $inflector){ |
|
| 528 | 528 | $instance = $inflector->getParametersCount() > 1 |
| 529 | 529 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 530 | 530 | : ($inflector->inflector)($instance); |
@@ -66,17 +66,20 @@ discard block |
||
| 66 | 66 | */ |
| 67 | 67 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 68 | 68 | { |
| 69 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 69 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
| 70 | + { |
|
| 70 | 71 | return $this->state->singletons[$alias]; |
| 71 | 72 | } |
| 72 | 73 | |
| 73 | 74 | $binding = $this->state->bindings[$alias] ?? null; |
| 74 | 75 | |
| 75 | - if ($binding === null) { |
|
| 76 | + if ($binding === null) |
|
| 77 | + { |
|
| 76 | 78 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
| 77 | 79 | } |
| 78 | 80 | |
| 79 | - try { |
|
| 81 | + try |
|
| 82 | + { |
|
| 80 | 83 | $this->tracer->push( |
| 81 | 84 | false, |
| 82 | 85 | action: 'resolve from binding', |
@@ -100,7 +103,9 @@ discard block |
||
| 100 | 103 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
| 101 | 104 | default => $binding, |
| 102 | 105 | }; |
| 103 | - } finally { |
|
| 106 | + } |
|
| 107 | + finally |
|
| 108 | + { |
|
| 104 | 109 | $this->state->bindings[$alias] ??= $binding; |
| 105 | 110 | $this->tracer->pop(true); |
| 106 | 111 | $this->tracer->pop(false); |
@@ -116,23 +121,29 @@ discard block |
||
| 116 | 121 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
| 117 | 122 | |
| 118 | 123 | // We have to construct class using external injector when we know exact context |
| 119 | - if ($arguments !== []) { |
|
| 124 | + if ($arguments !== []) |
|
| 125 | + { |
|
| 120 | 126 | // todo factory? |
| 121 | 127 | } |
| 122 | 128 | |
| 123 | 129 | $class = $ctx->class; |
| 124 | - try { |
|
| 130 | + try |
|
| 131 | + { |
|
| 125 | 132 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 126 | - } catch (\ReflectionException $e) { |
|
| 133 | + } |
|
| 134 | + catch (\ReflectionException $e) |
|
| 135 | + { |
|
| 127 | 136 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 128 | 137 | } |
| 129 | 138 | |
| 130 | 139 | $injector = $binding->injector; |
| 131 | 140 | |
| 132 | - try { |
|
| 141 | + try |
|
| 142 | + { |
|
| 133 | 143 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 134 | 144 | |
| 135 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 145 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 146 | + { |
|
| 136 | 147 | throw new InjectionException( |
| 137 | 148 | \sprintf( |
| 138 | 149 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -147,7 +158,8 @@ discard block |
||
| 147 | 158 | * @psalm-suppress RedundantCondition |
| 148 | 159 | */ |
| 149 | 160 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
| 150 | - if (!$reflection->isInstance($instance)) { |
|
| 161 | + if (!$reflection->isInstance($instance)) |
|
| 162 | + { |
|
| 151 | 163 | throw new InjectionException( |
| 152 | 164 | \sprintf( |
| 153 | 165 | "Invalid injection response for '%s'.", |
@@ -157,7 +169,9 @@ discard block |
||
| 157 | 169 | } |
| 158 | 170 | |
| 159 | 171 | return $instance; |
| 160 | - } finally { |
|
| 172 | + } |
|
| 173 | + finally |
|
| 174 | + { |
|
| 161 | 175 | $this->state->bindings[$reflection->getName()] ??= $binding; //new Injector($injector); |
| 162 | 176 | } |
| 163 | 177 | } |
@@ -176,7 +190,8 @@ discard block |
||
| 176 | 190 | //Binding is pointing to something else |
| 177 | 191 | : $this->make($binding->alias, $arguments, $context); |
| 178 | 192 | |
| 179 | - if ($binding->singleton && $arguments === []) { |
|
| 193 | + if ($binding->singleton && $arguments === []) |
|
| 194 | + { |
|
| 180 | 195 | $this->state->singletons[$alias] = $result; |
| 181 | 196 | } |
| 182 | 197 | |
@@ -217,11 +232,14 @@ discard block |
||
| 217 | 232 | array $arguments, |
| 218 | 233 | ): mixed { |
| 219 | 234 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context, singleton: $binding->singleton); |
| 220 | - try { |
|
| 235 | + try |
|
| 236 | + { |
|
| 221 | 237 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
| 222 | 238 | ? ($binding->factory)() |
| 223 | 239 | : $this->invoker->invoke($binding->factory, $arguments); |
| 224 | - } catch (NotCallableException $e) { |
|
| 240 | + } |
|
| 241 | + catch (NotCallableException $e) |
|
| 242 | + { |
|
| 225 | 243 | throw new ContainerException( |
| 226 | 244 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
| 227 | 245 | $e->getCode(), |
@@ -240,19 +258,24 @@ discard block |
||
| 240 | 258 | ): ?object { |
| 241 | 259 | $avoidCache = $arguments !== []; |
| 242 | 260 | |
| 243 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 244 | - try { |
|
| 261 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 262 | + { |
|
| 263 | + try |
|
| 264 | + { |
|
| 245 | 265 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 246 | 266 | |
| 247 | 267 | $object = $this->createInstance( |
| 248 | 268 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 249 | 269 | $arguments, |
| 250 | 270 | ); |
| 251 | - if ($avoidCache) { |
|
| 271 | + if ($avoidCache) |
|
| 272 | + { |
|
| 252 | 273 | return $object; |
| 253 | 274 | } |
| 254 | 275 | $binding->reference = WeakReference::create($object); |
| 255 | - } catch (\Throwable) { |
|
| 276 | + } |
|
| 277 | + catch (\Throwable) |
|
| 278 | + { |
|
| 256 | 279 | throw new ContainerException( |
| 257 | 280 | $this->tracer->combineTraceMessage( |
| 258 | 281 | \sprintf( |
@@ -262,7 +285,9 @@ discard block |
||
| 262 | 285 | ) |
| 263 | 286 | ) |
| 264 | 287 | ); |
| 265 | - } finally { |
|
| 288 | + } |
|
| 289 | + finally |
|
| 290 | + { |
|
| 266 | 291 | $this->tracer->pop(); |
| 267 | 292 | } |
| 268 | 293 | } |
@@ -274,18 +299,25 @@ discard block |
||
| 274 | 299 | { |
| 275 | 300 | $parent = $this->scope->getParent(); |
| 276 | 301 | |
| 277 | - if ($parent !== null) { |
|
| 278 | - try { |
|
| 302 | + if ($parent !== null) |
|
| 303 | + { |
|
| 304 | + try |
|
| 305 | + { |
|
| 279 | 306 | $this->tracer->push(false, ...[ |
| 280 | 307 | 'current scope' => $this->scope->getScopeName(), |
| 281 | 308 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
| 282 | 309 | ]); |
| 283 | 310 | return $parent->make($alias, $parameters, $context); |
| 284 | - } catch (BadScopeException $e) { |
|
| 285 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
| 311 | + } |
|
| 312 | + catch (BadScopeException $e) |
|
| 313 | + { |
|
| 314 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
| 315 | + { |
|
| 286 | 316 | throw $e; |
| 287 | 317 | } |
| 288 | - } catch (ContainerExceptionInterface $e) { |
|
| 318 | + } |
|
| 319 | + catch (ContainerExceptionInterface $e) |
|
| 320 | + { |
|
| 289 | 321 | $className = match (true) { |
| 290 | 322 | $e instanceof NotFoundException => NotFoundException::class, |
| 291 | 323 | default => ContainerException::class, |
@@ -294,19 +326,24 @@ discard block |
||
| 294 | 326 | 'Can\'t resolve `%s`.', |
| 295 | 327 | $alias, |
| 296 | 328 | )), previous: $e); |
| 297 | - } finally { |
|
| 329 | + } |
|
| 330 | + finally |
|
| 331 | + { |
|
| 298 | 332 | $this->tracer->pop(false); |
| 299 | 333 | } |
| 300 | 334 | } |
| 301 | 335 | |
| 302 | 336 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 303 | - try { |
|
| 337 | + try |
|
| 338 | + { |
|
| 304 | 339 | //No direct instructions how to construct class, make is automatically |
| 305 | 340 | return $this->autowire( |
| 306 | 341 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
| 307 | 342 | $parameters, |
| 308 | 343 | ); |
| 309 | - } finally { |
|
| 344 | + } |
|
| 345 | + finally |
|
| 346 | + { |
|
| 310 | 347 | $this->tracer->pop(false); |
| 311 | 348 | } |
| 312 | 349 | } |
@@ -357,7 +394,8 @@ discard block |
||
| 357 | 394 | // Check scope name |
| 358 | 395 | $ctx->reflection = new \ReflectionClass($instance); |
| 359 | 396 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 360 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
| 397 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
| 398 | + { |
|
| 361 | 399 | throw new BadScopeException($scopeName, $instance::class); |
| 362 | 400 | } |
| 363 | 401 | |
@@ -384,24 +422,30 @@ discard block |
||
| 384 | 422 | array $parameters, |
| 385 | 423 | ): object { |
| 386 | 424 | $class = $ctx->class; |
| 387 | - try { |
|
| 425 | + try |
|
| 426 | + { |
|
| 388 | 427 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 389 | - } catch (\ReflectionException $e) { |
|
| 428 | + } |
|
| 429 | + catch (\ReflectionException $e) |
|
| 430 | + { |
|
| 390 | 431 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 391 | 432 | } |
| 392 | 433 | |
| 393 | 434 | // Check scope name |
| 394 | 435 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
| 395 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
| 436 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
| 437 | + { |
|
| 396 | 438 | throw new BadScopeException($scope, $class); |
| 397 | 439 | } |
| 398 | 440 | |
| 399 | 441 | //We have to construct class using external injector when we know exact context |
| 400 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 442 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 443 | + { |
|
| 401 | 444 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx->class, $ctx->parameter, $parameters); |
| 402 | 445 | } |
| 403 | 446 | |
| 404 | - if (!$reflection->isInstantiable()) { |
|
| 447 | + if (!$reflection->isInstantiable()) |
|
| 448 | + { |
|
| 405 | 449 | $itIs = match (true) { |
| 406 | 450 | $reflection->isEnum() => 'Enum', |
| 407 | 451 | $reflection->isAbstract() => 'Abstract class', |
@@ -414,12 +458,16 @@ discard block |
||
| 414 | 458 | |
| 415 | 459 | $constructor = $reflection->getConstructor(); |
| 416 | 460 | |
| 417 | - if ($constructor !== null) { |
|
| 418 | - try { |
|
| 461 | + if ($constructor !== null) |
|
| 462 | + { |
|
| 463 | + try |
|
| 464 | + { |
|
| 419 | 465 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 420 | 466 | $this->tracer->push(true); |
| 421 | 467 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 422 | - } catch (ValidationException $e) { |
|
| 468 | + } |
|
| 469 | + catch (ValidationException $e) |
|
| 470 | + { |
|
| 423 | 471 | throw new ContainerException( |
| 424 | 472 | $this->tracer->combineTraceMessage( |
| 425 | 473 | \sprintf( |
@@ -429,22 +477,31 @@ discard block |
||
| 429 | 477 | ) |
| 430 | 478 | ), |
| 431 | 479 | ); |
| 432 | - } finally { |
|
| 480 | + } |
|
| 481 | + finally |
|
| 482 | + { |
|
| 433 | 483 | $this->tracer->pop(true); |
| 434 | 484 | $this->tracer->pop(false); |
| 435 | 485 | } |
| 436 | - try { |
|
| 486 | + try |
|
| 487 | + { |
|
| 437 | 488 | // Using constructor with resolved arguments |
| 438 | 489 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 439 | 490 | $this->tracer->push(true); |
| 440 | 491 | $instance = new $class(...$arguments); |
| 441 | - } catch (\TypeError $e) { |
|
| 492 | + } |
|
| 493 | + catch (\TypeError $e) |
|
| 494 | + { |
|
| 442 | 495 | throw new WrongTypeException($constructor, $e); |
| 443 | - } finally { |
|
| 496 | + } |
|
| 497 | + finally |
|
| 498 | + { |
|
| 444 | 499 | $this->tracer->pop(true); |
| 445 | 500 | $this->tracer->pop(false); |
| 446 | 501 | } |
| 447 | - } else { |
|
| 502 | + } |
|
| 503 | + else |
|
| 504 | + { |
|
| 448 | 505 | // No constructor specified |
| 449 | 506 | $instance = $reflection->newInstance(); |
| 450 | 507 | } |
@@ -470,13 +527,15 @@ discard block |
||
| 470 | 527 | $instance = $this->runInflector($instance); |
| 471 | 528 | |
| 472 | 529 | //Declarative singletons |
| 473 | - if ($this->isSingleton($ctx)) { |
|
| 530 | + if ($this->isSingleton($ctx)) |
|
| 531 | + { |
|
| 474 | 532 | $this->state->singletons[$ctx->alias] = $instance; |
| 475 | 533 | } |
| 476 | 534 | |
| 477 | 535 | // Register finalizer |
| 478 | 536 | $finalizer = $this->getFinalizer($ctx, $instance); |
| 479 | - if ($finalizer !== null) { |
|
| 537 | + if ($finalizer !== null) |
|
| 538 | + { |
|
| 480 | 539 | $this->state->finalizers[] = $finalizer; |
| 481 | 540 | } |
| 482 | 541 | |
@@ -488,12 +547,14 @@ discard block |
||
| 488 | 547 | */ |
| 489 | 548 | private function isSingleton(Ctx $ctx): bool |
| 490 | 549 | { |
| 491 | - if ($ctx->singleton === true) { |
|
| 550 | + if ($ctx->singleton === true) |
|
| 551 | + { |
|
| 492 | 552 | return true; |
| 493 | 553 | } |
| 494 | 554 | |
| 495 | 555 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 496 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 556 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 557 | + { |
|
| 497 | 558 | return true; |
| 498 | 559 | } |
| 499 | 560 | |
@@ -507,7 +568,8 @@ discard block |
||
| 507 | 568 | * @var Finalize|null $attribute |
| 508 | 569 | */ |
| 509 | 570 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
| 510 | - if ($attribute === null) { |
|
| 571 | + if ($attribute === null) |
|
| 572 | + { |
|
| 511 | 573 | return null; |
| 512 | 574 | } |
| 513 | 575 | |
@@ -521,10 +583,14 @@ discard block |
||
| 521 | 583 | { |
| 522 | 584 | $scope = $this->scope; |
| 523 | 585 | |
| 524 | - while ($scope !== null) { |
|
| 525 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 526 | - if ($instance instanceof $class) { |
|
| 527 | - foreach ($inflectors as $inflector) { |
|
| 586 | + while ($scope !== null) |
|
| 587 | + { |
|
| 588 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 589 | + { |
|
| 590 | + if ($instance instanceof $class) |
|
| 591 | + { |
|
| 592 | + foreach ($inflectors as $inflector) |
|
| 593 | + { |
|
| 528 | 594 | $instance = $inflector->getParametersCount() > 1 |
| 529 | 595 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 530 | 596 | : ($inflector->inflector)($instance); |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | ): array { |
| 53 | 53 | $state = new ResolvingState($reflection, $parameters); |
| 54 | 54 | |
| 55 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 55 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 56 | 56 | $this->resolveParameter($parameter, $state, $validate) |
| 57 | 57 | or |
| 58 | 58 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -66,47 +66,47 @@ discard block |
||
| 66 | 66 | $positional = true; |
| 67 | 67 | $variadic = false; |
| 68 | 68 | $parameters = $reflection->getParameters(); |
| 69 | - if (\count($parameters) === 0) { |
|
| 69 | + if (\count($parameters) === 0){ |
|
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | $parameter = null; |
| 74 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 74 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
| 75 | 75 | // get related argument value |
| 76 | 76 | $key = \key($arguments); |
| 77 | 77 | |
| 78 | 78 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 79 | 79 | // But you can't send positional argument after named in any case |
| 80 | - if (\is_int($key) && !$positional) { |
|
| 80 | + if (\is_int($key) && !$positional){ |
|
| 81 | 81 | throw new PositionalArgumentException($reflection, $key); |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | $positional = $positional && \is_int($key); |
| 85 | 85 | |
| 86 | - if (!$variadic) { |
|
| 86 | + if (!$variadic){ |
|
| 87 | 87 | $parameter = \array_shift($parameters); |
| 88 | 88 | $variadic = $parameter?->isVariadic() ?? false; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - if ($parameter === null) { |
|
| 91 | + if ($parameter === null){ |
|
| 92 | 92 | throw new UnknownParameterException($reflection, $key); |
| 93 | 93 | } |
| 94 | 94 | $name = $parameter->getName(); |
| 95 | 95 | |
| 96 | - if (($positional || $variadic) && $key !== null) { |
|
| 96 | + if (($positional || $variadic) && $key !== null){ |
|
| 97 | 97 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 98 | 98 | $value = \array_shift($arguments); |
| 99 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 100 | - if ($parameter->isOptional()) { |
|
| 99 | + } elseif ($key === null || !\array_key_exists($name, $arguments)){ |
|
| 100 | + if ($parameter->isOptional()){ |
|
| 101 | 101 | continue; |
| 102 | 102 | } |
| 103 | 103 | throw new MissingRequiredArgumentException($reflection, $name); |
| 104 | - } else { |
|
| 104 | + }else{ |
|
| 105 | 105 | $value = &$arguments[$name]; |
| 106 | 106 | unset($arguments[$name]); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 109 | + if (!$this->validateValueToParameter($parameter, $value)){ |
|
| 110 | 110 | throw new InvalidArgumentException($reflection, $name); |
| 111 | 111 | } |
| 112 | 112 | } |
@@ -114,7 +114,7 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 116 | 116 | { |
| 117 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 117 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)){ |
|
| 118 | 118 | return true; |
| 119 | 119 | } |
| 120 | 120 | $type = $parameter->getType(); |
@@ -125,17 +125,17 @@ discard block |
||
| 125 | 125 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 126 | 126 | }; |
| 127 | 127 | |
| 128 | - foreach ($types as $t) { |
|
| 128 | + foreach ($types as $t){ |
|
| 129 | 129 | \assert($t instanceof ReflectionNamedType); |
| 130 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 130 | + if (!$this->validateValueNamedType($t, $value)){ |
|
| 131 | 131 | // If it is TypeIntersection |
| 132 | - if ($or) { |
|
| 132 | + if ($or){ |
|
| 133 | 133 | continue; |
| 134 | 134 | } |
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | 137 | // If it is not type intersection then we can skip that value after first successful check |
| 138 | - if ($or) { |
|
| 138 | + if ($or){ |
|
| 139 | 139 | return true; |
| 140 | 140 | } |
| 141 | 141 | } |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | { |
| 151 | 151 | $name = $type->getName(); |
| 152 | 152 | |
| 153 | - if ($type->isBuiltin()) { |
|
| 153 | + if ($type->isBuiltin()){ |
|
| 154 | 154 | return match ($name) { |
| 155 | 155 | 'mixed' => true, |
| 156 | 156 | 'string' => \is_string($value), |
@@ -183,13 +183,13 @@ discard block |
||
| 183 | 183 | |
| 184 | 184 | // Try to resolve parameter by name |
| 185 | 185 | $res = $state->resolveParameterByNameOrPosition($parameter, $isVariadic); |
| 186 | - if ($res !== [] || $isVariadic) { |
|
| 186 | + if ($res !== [] || $isVariadic){ |
|
| 187 | 187 | // validate |
| 188 | - if ($isVariadic) { |
|
| 189 | - foreach ($res as $k => &$v) { |
|
| 190 | - $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key: $k); |
|
| 188 | + if ($isVariadic){ |
|
| 189 | + foreach ($res as $k => &$v){ |
|
| 190 | + $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key : $k); |
|
| 191 | 191 | } |
| 192 | - } else { |
|
| 192 | + }else{ |
|
| 193 | 193 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
| 194 | 194 | } |
| 195 | 195 | |
@@ -197,39 +197,39 @@ discard block |
||
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | $error = null; |
| 200 | - if ($hasType) { |
|
| 200 | + if ($hasType){ |
|
| 201 | 201 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
| 202 | 202 | $reflectionType = $parameter->getType(); |
| 203 | 203 | |
| 204 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
| 204 | + if ($reflectionType instanceof ReflectionIntersectionType){ |
|
| 205 | 205 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
| 209 | - foreach ($types as $namedType) { |
|
| 210 | - try { |
|
| 211 | - if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) { |
|
| 209 | + foreach ($types as $namedType){ |
|
| 210 | + try{ |
|
| 211 | + if ($this->resolveNamedType($state, $parameter, $namedType, $validate)){ |
|
| 212 | 212 | return true; |
| 213 | 213 | } |
| 214 | - } catch (Throwable $e) { |
|
| 214 | + }catch (Throwable $e){ |
|
| 215 | 215 | $error = $e; |
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 220 | + if ($parameter->isDefaultValueAvailable()){ |
|
| 221 | 221 | $argument = $parameter->getDefaultValue(); |
| 222 | 222 | $this->processArgument($state, $argument); |
| 223 | 223 | return true; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - if ($hasType && $parameter->allowsNull()) { |
|
| 226 | + if ($hasType && $parameter->allowsNull()){ |
|
| 227 | 227 | $argument = null; |
| 228 | 228 | $this->processArgument($state, $argument); |
| 229 | 229 | return true; |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - if ($error === null) { |
|
| 232 | + if ($error === null){ |
|
| 233 | 233 | return false; |
| 234 | 234 | } |
| 235 | 235 | |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | ReflectionParameter $parameter, |
| 251 | 251 | ReflectionNamedType $typeRef, |
| 252 | 252 | bool $validate, |
| 253 | - ) { |
|
| 253 | + ){ |
|
| 254 | 254 | return !$typeRef->isBuiltin() && $this->resolveObjectParameter( |
| 255 | 255 | $state, |
| 256 | 256 | $typeRef->getName(), |
@@ -293,15 +293,15 @@ discard block |
||
| 293 | 293 | ResolvingState $state, |
| 294 | 294 | mixed &$value, |
| 295 | 295 | ReflectionParameter $validateWith = null, |
| 296 | - int|string $key = null |
|
| 296 | + int | string $key = null |
|
| 297 | 297 | ): void { |
| 298 | 298 | // Resolve Autowire objects |
| 299 | - if ($value instanceof Autowire) { |
|
| 299 | + if ($value instanceof Autowire){ |
|
| 300 | 300 | $value = $value->resolve($this->factory); |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | // Validation |
| 304 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 304 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
| 305 | 305 | throw new InvalidArgumentException( |
| 306 | 306 | $validateWith->getDeclaringFunction(), |
| 307 | 307 | $validateWith->getName() |
@@ -7,8 +7,8 @@ |
||
| 7 | 7 | final class Scalar extends Binding |
| 8 | 8 | { |
| 9 | 9 | public function __construct( |
| 10 | - public readonly bool|int|string|float $value, |
|
| 11 | - ) { |
|
| 10 | + public readonly bool | int | string | float $value, |
|
| 11 | + ){ |
|
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function __toString(): string |