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