@@ -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 | } |
@@ -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); |
@@ -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,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,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 | } |