@@ -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 | } |
@@ -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, |
@@ -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,35 +54,48 @@ 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: 'resolve from binding', alias: $alias, context: $context, binding: $binding); |
| 70 | 75 | $this->tracer->push(true); |
| 71 | 76 | |
| 72 | - if (\is_object($binding)) { |
|
| 73 | - if ($binding::class === WeakReference::class) { |
|
| 74 | - if ($binding->get() === null && \class_exists($alias)) { |
|
| 75 | - try { |
|
| 77 | + if (\is_object($binding)) |
|
| 78 | + { |
|
| 79 | + if ($binding::class === WeakReference::class) |
|
| 80 | + { |
|
| 81 | + if ($binding->get() === null && \class_exists($alias)) |
|
| 82 | + { |
|
| 83 | + try |
|
| 84 | + { |
|
| 76 | 85 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 77 | 86 | $object = $this->createInstance($alias, $parameters, $context); |
| 78 | 87 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 79 | - } catch (\Throwable) { |
|
| 88 | + } |
|
| 89 | + catch (\Throwable) |
|
| 90 | + { |
|
| 80 | 91 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 81 | 92 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 82 | 93 | $this->tracer->getRootAlias(), |
| 83 | 94 | $alias, |
| 84 | 95 | ))); |
| 85 | - } finally { |
|
| 96 | + } |
|
| 97 | + finally |
|
| 98 | + { |
|
| 86 | 99 | $this->tracer->pop(); |
| 87 | 100 | } |
| 88 | 101 | } |
@@ -92,26 +105,33 @@ discard block |
||
| 92 | 105 | return $binding; |
| 93 | 106 | } |
| 94 | 107 | |
| 95 | - if (\is_string($binding)) { |
|
| 108 | + if (\is_string($binding)) |
|
| 109 | + { |
|
| 96 | 110 | //Binding is pointing to something else |
| 97 | 111 | return $this->make($binding, $parameters, $context); |
| 98 | 112 | } |
| 99 | 113 | |
| 100 | 114 | unset($this->state->bindings[$alias]); |
| 101 | - try { |
|
| 115 | + try |
|
| 116 | + { |
|
| 102 | 117 | $instance = $binding[0] === $alias |
| 103 | 118 | ? $this->autowire($alias, $parameters, $context) |
| 104 | 119 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 105 | - } finally { |
|
| 120 | + } |
|
| 121 | + finally |
|
| 122 | + { |
|
| 106 | 123 | /** @psalm-var class-string $alias */ |
| 107 | 124 | $this->state->bindings[$alias] = $binding; |
| 108 | 125 | } |
| 109 | - } finally { |
|
| 126 | + } |
|
| 127 | + finally |
|
| 128 | + { |
|
| 110 | 129 | $this->tracer->pop(true); |
| 111 | 130 | $this->tracer->pop(false); |
| 112 | 131 | } |
| 113 | 132 | |
| 114 | - if ($binding[1]) { |
|
| 133 | + if ($binding[1]) |
|
| 134 | + { |
|
| 115 | 135 | // Indicates singleton |
| 116 | 136 | /** @psalm-var class-string $alias */ |
| 117 | 137 | $this->state->bindings[$alias] = $instance; |
@@ -130,7 +150,8 @@ discard block |
||
| 130 | 150 | */ |
| 131 | 151 | private function autowire(string $class, array $parameters, string $context = null): object |
| 132 | 152 | { |
| 133 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
| 153 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])) |
|
| 154 | + { |
|
| 134 | 155 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 135 | 156 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 136 | 157 | $this->tracer->getRootAlias(), |
@@ -157,18 +178,23 @@ discard block |
||
| 157 | 178 | array $parameters, |
| 158 | 179 | string $context = null |
| 159 | 180 | ): mixed { |
| 160 | - if (\is_string($target)) { |
|
| 181 | + if (\is_string($target)) |
|
| 182 | + { |
|
| 161 | 183 | // Reference |
| 162 | 184 | return $this->make($target, $parameters, $context); |
| 163 | 185 | } |
| 164 | 186 | |
| 165 | - if ($target instanceof Autowire) { |
|
| 187 | + if ($target instanceof Autowire) |
|
| 188 | + { |
|
| 166 | 189 | return $target->resolve($this, $parameters); |
| 167 | 190 | } |
| 168 | 191 | |
| 169 | - try { |
|
| 192 | + try |
|
| 193 | + { |
|
| 170 | 194 | return $this->invoker->invoke($target, $parameters); |
| 171 | - } catch (NotCallableException $e) { |
|
| 195 | + } |
|
| 196 | + catch (NotCallableException $e) |
|
| 197 | + { |
|
| 172 | 198 | throw new ContainerException( |
| 173 | 199 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 174 | 200 | $e->getCode(), |
@@ -192,20 +218,26 @@ discard block |
||
| 192 | 218 | */ |
| 193 | 219 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 194 | 220 | { |
| 195 | - try { |
|
| 221 | + try |
|
| 222 | + { |
|
| 196 | 223 | $reflection = new \ReflectionClass($class); |
| 197 | - } catch (\ReflectionException $e) { |
|
| 224 | + } |
|
| 225 | + catch (\ReflectionException $e) |
|
| 226 | + { |
|
| 198 | 227 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 199 | 228 | } |
| 200 | 229 | |
| 201 | 230 | //We have to construct class using external injector when we know exact context |
| 202 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 231 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 232 | + { |
|
| 203 | 233 | $injector = $this->state->injectors[$reflection->getName()]; |
| 204 | 234 | |
| 205 | - try { |
|
| 235 | + try |
|
| 236 | + { |
|
| 206 | 237 | $injectorInstance = $this->container->get($injector); |
| 207 | 238 | |
| 208 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 239 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 240 | + { |
|
| 209 | 241 | throw new InjectionException( |
| 210 | 242 | \sprintf( |
| 211 | 243 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -217,7 +249,8 @@ discard block |
||
| 217 | 249 | |
| 218 | 250 | /** @var InjectorInterface<TObject> $injectorInstance */ |
| 219 | 251 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 220 | - if (!$reflection->isInstance($instance)) { |
|
| 252 | + if (!$reflection->isInstance($instance)) |
|
| 253 | + { |
|
| 221 | 254 | throw new InjectionException( |
| 222 | 255 | \sprintf( |
| 223 | 256 | "Invalid injection response for '%s'.", |
@@ -227,12 +260,15 @@ discard block |
||
| 227 | 260 | } |
| 228 | 261 | |
| 229 | 262 | return $instance; |
| 230 | - } finally { |
|
| 263 | + } |
|
| 264 | + finally |
|
| 265 | + { |
|
| 231 | 266 | $this->state->injectors[$reflection->getName()] = $injector; |
| 232 | 267 | } |
| 233 | 268 | } |
| 234 | 269 | |
| 235 | - if (!$reflection->isInstantiable()) { |
|
| 270 | + if (!$reflection->isInstantiable()) |
|
| 271 | + { |
|
| 236 | 272 | $itIs = match (true) { |
| 237 | 273 | $reflection->isEnum() => 'Enum', |
| 238 | 274 | $reflection->isAbstract() => 'Abstract class', |
@@ -245,12 +281,16 @@ discard block |
||
| 245 | 281 | |
| 246 | 282 | $constructor = $reflection->getConstructor(); |
| 247 | 283 | |
| 248 | - if ($constructor !== null) { |
|
| 249 | - try { |
|
| 284 | + if ($constructor !== null) |
|
| 285 | + { |
|
| 286 | + try |
|
| 287 | + { |
|
| 250 | 288 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 251 | 289 | $this->tracer->push(true); |
| 252 | 290 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 253 | - } catch (ValidationException $e) { |
|
| 291 | + } |
|
| 292 | + catch (ValidationException $e) |
|
| 293 | + { |
|
| 254 | 294 | throw new ContainerException( |
| 255 | 295 | $this->tracer->combineTraceMessage( |
| 256 | 296 | \sprintf( |
@@ -260,22 +300,31 @@ discard block |
||
| 260 | 300 | ) |
| 261 | 301 | ), |
| 262 | 302 | ); |
| 263 | - } finally { |
|
| 303 | + } |
|
| 304 | + finally |
|
| 305 | + { |
|
| 264 | 306 | $this->tracer->pop(true); |
| 265 | 307 | $this->tracer->pop(false); |
| 266 | 308 | } |
| 267 | - try { |
|
| 309 | + try |
|
| 310 | + { |
|
| 268 | 311 | // Using constructor with resolved arguments |
| 269 | 312 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 270 | 313 | $this->tracer->push(true); |
| 271 | 314 | $instance = new $class(...$arguments); |
| 272 | - } catch (\TypeError $e) { |
|
| 315 | + } |
|
| 316 | + catch (\TypeError $e) |
|
| 317 | + { |
|
| 273 | 318 | throw new WrongTypeException($constructor, $e); |
| 274 | - } finally { |
|
| 319 | + } |
|
| 320 | + finally |
|
| 321 | + { |
|
| 275 | 322 | $this->tracer->pop(true); |
| 276 | 323 | $this->tracer->pop(false); |
| 277 | 324 | } |
| 278 | - } else { |
|
| 325 | + } |
|
| 326 | + else |
|
| 327 | + { |
|
| 279 | 328 | // No constructor specified |
| 280 | 329 | $instance = $reflection->newInstance(); |
| 281 | 330 | } |
@@ -293,9 +342,11 @@ discard block |
||
| 293 | 342 | private function registerInstance(object $instance, array $parameters): object |
| 294 | 343 | { |
| 295 | 344 | //Declarative singletons (only when class received via direct get) |
| 296 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
| 345 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
| 346 | + { |
|
| 297 | 347 | $alias = $instance::class; |
| 298 | - if (!isset($this->state->bindings[$alias])) { |
|
| 348 | + if (!isset($this->state->bindings[$alias])) |
|
| 349 | + { |
|
| 299 | 350 | $this->state->bindings[$alias] = $instance; |
| 300 | 351 | } |
| 301 | 352 | } |