@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | |
40 | 40 | public function testInvalidInjector(): void |
41 | 41 | { |
42 | - $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " . |
|
42 | + $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an ". |
|
43 | 43 | "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'"; |
44 | 44 | $this->expectException(InjectionException::class); |
45 | 45 | $this->expectExceptionMessage($excepted); |
@@ -65,7 +65,7 @@ discard block |
||
65 | 65 | |
66 | 66 | public function testInvalidRuntimeInjector(): void |
67 | 67 | { |
68 | - $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an " . |
|
68 | + $excepted = "Class 'Spiral\Tests\Core\Fixtures\InvalidInjector' must be an ". |
|
69 | 69 | "instance of InjectorInterface for 'Spiral\Tests\Core\Fixtures\TestConfig'"; |
70 | 70 | $this->expectException(InjectionException::class); |
71 | 71 | $this->expectExceptionMessage($excepted); |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | $container->bind(ConfigsInterface::class, $configurator); |
103 | 103 | |
104 | 104 | $configurator->shouldReceive('createInjection') |
105 | - ->with(m::on(static function (ReflectionClass $r) { |
|
105 | + ->with(m::on(static function (ReflectionClass $r){ |
|
106 | 106 | return $r->getName() === TestConfig::class; |
107 | 107 | }), null) |
108 | 108 | ->andReturn($expected); |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | $container->bind(ConfigsInterface::class, $configurator); |
120 | 120 | |
121 | 121 | $configurator->shouldReceive('createInjection') |
122 | - ->with(m::on(static function (ReflectionClass $r) { |
|
122 | + ->with(m::on(static function (ReflectionClass $r){ |
|
123 | 123 | return $r->getName() === TestConfig::class; |
124 | 124 | }), 'context') |
125 | 125 | ->andReturn($expected); |
@@ -207,8 +207,8 @@ discard block |
||
207 | 207 | public function testExtendedInjectorAnonClassObjectParam(): void |
208 | 208 | { |
209 | 209 | $container = new Container(); |
210 | - $container->bind(stdClass::class, new Injectable(new class implements Container\InjectorInterface { |
|
211 | - public function createInjection(\ReflectionClass $class, object|string|null $context = null): object |
|
210 | + $container->bind(stdClass::class, new Injectable(new class implements Container\InjectorInterface{ |
|
211 | + public function createInjection(\ReflectionClass $class, object | string | null $context = null): object |
|
212 | 212 | { |
213 | 213 | return (object)['context' => $context]; |
214 | 214 | } |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | public function testExtendedInjectorAnonClassMixedParam(): void |
224 | 224 | { |
225 | 225 | $container = new Container(); |
226 | - $container->bind(stdClass::class, new Injectable(new class implements Container\InjectorInterface { |
|
226 | + $container->bind(stdClass::class, new Injectable(new class implements Container\InjectorInterface{ |
|
227 | 227 | public function createInjection(\ReflectionClass $class, mixed $context = null): object |
228 | 228 | { |
229 | 229 | return (object)['context' => $context]; |
@@ -12,7 +12,7 @@ |
||
12 | 12 | */ |
13 | 13 | class ExtendedContextInjector implements InjectorInterface |
14 | 14 | { |
15 | - public function createInjection(\ReflectionClass $class, \ReflectionParameter|string|null $context = null): object |
|
15 | + public function createInjection(\ReflectionClass $class, \ReflectionParameter | string | null $context = null): object |
|
16 | 16 | { |
17 | 17 | return (object)['context' => $context]; |
18 | 18 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | use DestructorTrait; |
20 | 20 | |
21 | 21 | private State $state; |
22 | - private FactoryInterface|Factory $factory; |
|
22 | + private FactoryInterface | Factory $factory; |
|
23 | 23 | |
24 | 24 | public function __construct(Registry $constructor) |
25 | 25 | { |
@@ -47,9 +47,9 @@ discard block |
||
47 | 47 | * @throws ContainerException |
48 | 48 | * @throws \Throwable |
49 | 49 | */ |
50 | - public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
50 | + public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed |
|
51 | 51 | { |
52 | - if ($id instanceof Autowire) { |
|
52 | + if ($id instanceof Autowire){ |
|
53 | 53 | return $id->resolve($this->factory); |
54 | 54 | } |
55 | 55 |
@@ -49,7 +49,8 @@ |
||
49 | 49 | */ |
50 | 50 | public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
51 | 51 | { |
52 | - if ($id instanceof Autowire) { |
|
52 | + if ($id instanceof Autowire) |
|
53 | + { |
|
53 | 54 | return $id->resolve($this->factory); |
54 | 55 | } |
55 | 56 |
@@ -66,19 +66,19 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @throws \Throwable |
68 | 68 | */ |
69 | - public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
|
69 | + public function make(string $alias, array $parameters = [], Stringable | string | null $context = null): mixed |
|
70 | 70 | { |
71 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
71 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)){ |
|
72 | 72 | return $this->state->singletons[$alias]; |
73 | 73 | } |
74 | 74 | |
75 | 75 | $binding = $this->state->bindings[$alias] ?? null; |
76 | 76 | |
77 | - if ($binding === null) { |
|
77 | + if ($binding === null){ |
|
78 | 78 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
79 | 79 | } |
80 | 80 | |
81 | - try { |
|
81 | + try{ |
|
82 | 82 | $this->tracer->push( |
83 | 83 | false, |
84 | 84 | action: 'resolve from binding', |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
107 | 107 | default => $binding, |
108 | 108 | }; |
109 | - } finally { |
|
109 | + }finally{ |
|
110 | 110 | $this->state->bindings[$alias] ??= $binding; |
111 | 111 | $this->tracer->pop(true); |
112 | 112 | $this->tracer->pop(false); |
@@ -116,18 +116,18 @@ discard block |
||
116 | 116 | private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments) |
117 | 117 | { |
118 | 118 | $context = $ctx->context; |
119 | - try { |
|
119 | + try{ |
|
120 | 120 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
121 | - } catch (\ReflectionException $e) { |
|
121 | + }catch (\ReflectionException $e){ |
|
122 | 122 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
123 | 123 | } |
124 | 124 | |
125 | 125 | $injector = $binding->injector; |
126 | 126 | |
127 | - try { |
|
127 | + try{ |
|
128 | 128 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
129 | 129 | |
130 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
130 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
131 | 131 | throw new InjectionException( |
132 | 132 | \sprintf( |
133 | 133 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -155,7 +155,7 @@ discard block |
||
155 | 155 | default => (string)$context, |
156 | 156 | }); |
157 | 157 | |
158 | - if (!$reflection->isInstance($instance)) { |
|
158 | + if (!$reflection->isInstance($instance)){ |
|
159 | 159 | throw new InjectionException( |
160 | 160 | \sprintf( |
161 | 161 | "Invalid injection response for '%s'.", |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | } |
166 | 166 | |
167 | 167 | return $instance; |
168 | - } finally { |
|
168 | + }finally{ |
|
169 | 169 | $this->state->bindings[$reflection->getName()] ??= $binding; |
170 | 170 | } |
171 | 171 | } |
@@ -173,18 +173,18 @@ discard block |
||
173 | 173 | private function resolveAlias( |
174 | 174 | \Spiral\Core\Config\Alias $binding, |
175 | 175 | string $alias, |
176 | - Stringable|string|null $context, |
|
176 | + Stringable | string | null $context, |
|
177 | 177 | array $arguments, |
178 | 178 | ): mixed { |
179 | 179 | $result = $binding->alias === $alias |
180 | 180 | ? $this->autowire( |
181 | - new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton), |
|
181 | + new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton), |
|
182 | 182 | $arguments, |
183 | 183 | ) |
184 | 184 | //Binding is pointing to something else |
185 | 185 | : $this->make($binding->alias, $arguments, $context); |
186 | 186 | |
187 | - if ($binding->singleton && $arguments === []) { |
|
187 | + if ($binding->singleton && $arguments === []){ |
|
188 | 188 | $this->state->singletons[$alias] = $result; |
189 | 189 | } |
190 | 190 | |
@@ -194,13 +194,13 @@ discard block |
||
194 | 194 | private function resolveShared( |
195 | 195 | \Spiral\Core\Config\Shared $binding, |
196 | 196 | string $alias, |
197 | - Stringable|string|null $context, |
|
197 | + Stringable | string | null $context, |
|
198 | 198 | array $arguments, |
199 | 199 | ): object { |
200 | 200 | $avoidCache = $arguments !== []; |
201 | 201 | return $avoidCache |
202 | 202 | ? $this->createInstance( |
203 | - new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
|
203 | + new Ctx(alias : $alias, class : $binding->value::class, context : $context), |
|
204 | 204 | $arguments, |
205 | 205 | ) |
206 | 206 | : $binding->value; |
@@ -209,7 +209,7 @@ discard block |
||
209 | 209 | private function resolveAutowire( |
210 | 210 | \Spiral\Core\Config\Autowire $binding, |
211 | 211 | string $alias, |
212 | - Stringable|string|null $context, |
|
212 | + Stringable | string | null $context, |
|
213 | 213 | array $arguments, |
214 | 214 | ): mixed { |
215 | 215 | $instance = $binding->autowire->resolve($this, $arguments); |
@@ -219,17 +219,17 @@ discard block |
||
219 | 219 | } |
220 | 220 | |
221 | 221 | private function resolveFactory( |
222 | - \Spiral\Core\Config\Factory|DeferredFactory $binding, |
|
222 | + \Spiral\Core\Config\Factory | DeferredFactory $binding, |
|
223 | 223 | string $alias, |
224 | - Stringable|string|null $context, |
|
224 | + Stringable | string | null $context, |
|
225 | 225 | array $arguments, |
226 | 226 | ): mixed { |
227 | 227 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
228 | - try { |
|
228 | + try{ |
|
229 | 229 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
230 | 230 | ? ($binding->factory)() |
231 | 231 | : $this->invoker->invoke($binding->factory, $arguments); |
232 | - } catch (NotCallableException $e) { |
|
232 | + }catch (NotCallableException $e){ |
|
233 | 233 | throw new ContainerException( |
234 | 234 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
235 | 235 | $e->getCode(), |
@@ -243,24 +243,24 @@ discard block |
||
243 | 243 | private function resolveWeakReference( |
244 | 244 | \Spiral\Core\Config\WeakReference $binding, |
245 | 245 | string $alias, |
246 | - Stringable|string|null $context, |
|
246 | + Stringable | string | null $context, |
|
247 | 247 | array $arguments, |
248 | 248 | ): ?object { |
249 | 249 | $avoidCache = $arguments !== []; |
250 | 250 | |
251 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
252 | - try { |
|
251 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
252 | + try{ |
|
253 | 253 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
254 | 254 | |
255 | 255 | $object = $this->createInstance( |
256 | 256 | new Ctx(alias: $alias, class: $alias, context: $context), |
257 | 257 | $arguments, |
258 | 258 | ); |
259 | - if ($avoidCache) { |
|
259 | + if ($avoidCache){ |
|
260 | 260 | return $object; |
261 | 261 | } |
262 | 262 | $binding->reference = WeakReference::create($object); |
263 | - } catch (\Throwable) { |
|
263 | + }catch (\Throwable){ |
|
264 | 264 | throw new ContainerException( |
265 | 265 | $this->tracer->combineTraceMessage( |
266 | 266 | \sprintf( |
@@ -270,7 +270,7 @@ discard block |
||
270 | 270 | ) |
271 | 271 | ) |
272 | 272 | ); |
273 | - } finally { |
|
273 | + }finally{ |
|
274 | 274 | $this->tracer->pop(); |
275 | 275 | } |
276 | 276 | } |
@@ -281,22 +281,22 @@ discard block |
||
281 | 281 | private function resolveWithoutBinding( |
282 | 282 | string $alias, |
283 | 283 | array $parameters = [], |
284 | - Stringable|string|null $context = null |
|
284 | + Stringable | string | null $context = null |
|
285 | 285 | ): mixed { |
286 | 286 | $parent = $this->scope->getParent(); |
287 | 287 | |
288 | - if ($parent !== null) { |
|
289 | - try { |
|
288 | + if ($parent !== null){ |
|
289 | + try{ |
|
290 | 290 | $this->tracer->push(false, ...[ |
291 | 291 | 'current scope' => $this->scope->getScopeName(), |
292 | 292 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
293 | 293 | ]); |
294 | 294 | return $parent->make($alias, $parameters, $context); |
295 | - } catch (BadScopeException $e) { |
|
296 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
295 | + }catch (BadScopeException $e){ |
|
296 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
297 | 297 | throw $e; |
298 | 298 | } |
299 | - } catch (ContainerExceptionInterface $e) { |
|
299 | + }catch (ContainerExceptionInterface $e){ |
|
300 | 300 | $className = match (true) { |
301 | 301 | $e instanceof NotFoundException => NotFoundException::class, |
302 | 302 | default => ContainerException::class, |
@@ -305,19 +305,19 @@ discard block |
||
305 | 305 | 'Can\'t resolve `%s`.', |
306 | 306 | $alias, |
307 | 307 | )), previous: $e); |
308 | - } finally { |
|
308 | + }finally{ |
|
309 | 309 | $this->tracer->pop(false); |
310 | 310 | } |
311 | 311 | } |
312 | 312 | |
313 | 313 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
314 | - try { |
|
314 | + try{ |
|
315 | 315 | //No direct instructions how to construct class, make is automatically |
316 | 316 | return $this->autowire( |
317 | 317 | new Ctx(alias: $alias, class: $alias, context: $context), |
318 | 318 | $parameters, |
319 | 319 | ); |
320 | - } finally { |
|
320 | + }finally{ |
|
321 | 321 | $this->tracer->pop(false); |
322 | 322 | } |
323 | 323 | } |
@@ -338,7 +338,7 @@ discard block |
||
338 | 338 | && |
339 | 339 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
340 | 340 | )) |
341 | - ) { |
|
341 | + ){ |
|
342 | 342 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
343 | 343 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
344 | 344 | $this->tracer->getRootAlias(), |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | // Check scope name |
368 | 368 | $ctx->reflection = new \ReflectionClass($instance); |
369 | 369 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
370 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
370 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()){ |
|
371 | 371 | throw new BadScopeException($scopeName, $instance::class); |
372 | 372 | } |
373 | 373 | |
@@ -394,24 +394,24 @@ discard block |
||
394 | 394 | array $parameters, |
395 | 395 | ): object { |
396 | 396 | $class = $ctx->class; |
397 | - try { |
|
397 | + try{ |
|
398 | 398 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
399 | - } catch (\ReflectionException $e) { |
|
399 | + }catch (\ReflectionException $e){ |
|
400 | 400 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
401 | 401 | } |
402 | 402 | |
403 | 403 | // Check scope name |
404 | 404 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
405 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
405 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
406 | 406 | throw new BadScopeException($scope, $class); |
407 | 407 | } |
408 | 408 | |
409 | 409 | //We have to construct class using external injector when we know exact context |
410 | - if ($this->binder->hasInjector($class)) { |
|
410 | + if ($this->binder->hasInjector($class)){ |
|
411 | 411 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $parameters); |
412 | 412 | } |
413 | 413 | |
414 | - if (!$reflection->isInstantiable()) { |
|
414 | + if (!$reflection->isInstantiable()){ |
|
415 | 415 | $itIs = match (true) { |
416 | 416 | $reflection->isEnum() => 'Enum', |
417 | 417 | $reflection->isAbstract() => 'Abstract class', |
@@ -424,12 +424,12 @@ discard block |
||
424 | 424 | |
425 | 425 | $constructor = $reflection->getConstructor(); |
426 | 426 | |
427 | - if ($constructor !== null) { |
|
428 | - try { |
|
427 | + if ($constructor !== null){ |
|
428 | + try{ |
|
429 | 429 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
430 | 430 | $this->tracer->push(true); |
431 | 431 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
432 | - } catch (ValidationException $e) { |
|
432 | + }catch (ValidationException $e){ |
|
433 | 433 | throw new ContainerException( |
434 | 434 | $this->tracer->combineTraceMessage( |
435 | 435 | \sprintf( |
@@ -439,22 +439,22 @@ discard block |
||
439 | 439 | ) |
440 | 440 | ), |
441 | 441 | ); |
442 | - } finally { |
|
442 | + }finally{ |
|
443 | 443 | $this->tracer->pop(true); |
444 | 444 | $this->tracer->pop(false); |
445 | 445 | } |
446 | - try { |
|
446 | + try{ |
|
447 | 447 | // Using constructor with resolved arguments |
448 | 448 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
449 | 449 | $this->tracer->push(true); |
450 | 450 | $instance = new $class(...$arguments); |
451 | - } catch (\TypeError $e) { |
|
451 | + }catch (\TypeError $e){ |
|
452 | 452 | throw new WrongTypeException($constructor, $e); |
453 | - } finally { |
|
453 | + }finally{ |
|
454 | 454 | $this->tracer->pop(true); |
455 | 455 | $this->tracer->pop(false); |
456 | 456 | } |
457 | - } else { |
|
457 | + }else{ |
|
458 | 458 | // No constructor specified |
459 | 459 | $instance = $reflection->newInstance(); |
460 | 460 | } |
@@ -472,13 +472,13 @@ discard block |
||
472 | 472 | $instance = $this->runInflector($instance); |
473 | 473 | |
474 | 474 | //Declarative singletons |
475 | - if ($this->isSingleton($ctx)) { |
|
475 | + if ($this->isSingleton($ctx)){ |
|
476 | 476 | $this->state->singletons[$ctx->alias] = $instance; |
477 | 477 | } |
478 | 478 | |
479 | 479 | // Register finalizer |
480 | 480 | $finalizer = $this->getFinalizer($ctx, $instance); |
481 | - if ($finalizer !== null) { |
|
481 | + if ($finalizer !== null){ |
|
482 | 482 | $this->state->finalizers[] = $finalizer; |
483 | 483 | } |
484 | 484 | |
@@ -490,12 +490,12 @@ discard block |
||
490 | 490 | */ |
491 | 491 | private function isSingleton(Ctx $ctx): bool |
492 | 492 | { |
493 | - if ($ctx->singleton === true) { |
|
493 | + if ($ctx->singleton === true){ |
|
494 | 494 | return true; |
495 | 495 | } |
496 | 496 | |
497 | 497 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
498 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
498 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
499 | 499 | return true; |
500 | 500 | } |
501 | 501 | |
@@ -509,7 +509,7 @@ discard block |
||
509 | 509 | * @var Finalize|null $attribute |
510 | 510 | */ |
511 | 511 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
512 | - if ($attribute === null) { |
|
512 | + if ($attribute === null){ |
|
513 | 513 | return null; |
514 | 514 | } |
515 | 515 | |
@@ -523,10 +523,10 @@ discard block |
||
523 | 523 | { |
524 | 524 | $scope = $this->scope; |
525 | 525 | |
526 | - while ($scope !== null) { |
|
527 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
528 | - if ($instance instanceof $class) { |
|
529 | - foreach ($inflectors as $inflector) { |
|
526 | + while ($scope !== null){ |
|
527 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
528 | + if ($instance instanceof $class){ |
|
529 | + foreach ($inflectors as $inflector){ |
|
530 | 530 | $instance = $inflector->getParametersCount() > 1 |
531 | 531 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
532 | 532 | : ($inflector->inflector)($instance); |
@@ -542,9 +542,9 @@ discard block |
||
542 | 542 | |
543 | 543 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
544 | 544 | { |
545 | - try { |
|
545 | + try{ |
|
546 | 546 | $this->resolver->validateArguments($reflection, $arguments); |
547 | - } catch (\Throwable) { |
|
547 | + }catch (\Throwable){ |
|
548 | 548 | return false; |
549 | 549 | } |
550 | 550 |
@@ -68,17 +68,20 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
70 | 70 | { |
71 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
71 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
72 | + { |
|
72 | 73 | return $this->state->singletons[$alias]; |
73 | 74 | } |
74 | 75 | |
75 | 76 | $binding = $this->state->bindings[$alias] ?? null; |
76 | 77 | |
77 | - if ($binding === null) { |
|
78 | + if ($binding === null) |
|
79 | + { |
|
78 | 80 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
79 | 81 | } |
80 | 82 | |
81 | - try { |
|
83 | + try |
|
84 | + { |
|
82 | 85 | $this->tracer->push( |
83 | 86 | false, |
84 | 87 | action: 'resolve from binding', |
@@ -106,7 +109,9 @@ discard block |
||
106 | 109 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
107 | 110 | default => $binding, |
108 | 111 | }; |
109 | - } finally { |
|
112 | + } |
|
113 | + finally |
|
114 | + { |
|
110 | 115 | $this->state->bindings[$alias] ??= $binding; |
111 | 116 | $this->tracer->pop(true); |
112 | 117 | $this->tracer->pop(false); |
@@ -116,18 +121,23 @@ discard block |
||
116 | 121 | private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments) |
117 | 122 | { |
118 | 123 | $context = $ctx->context; |
119 | - try { |
|
124 | + try |
|
125 | + { |
|
120 | 126 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
121 | - } catch (\ReflectionException $e) { |
|
127 | + } |
|
128 | + catch (\ReflectionException $e) |
|
129 | + { |
|
122 | 130 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
123 | 131 | } |
124 | 132 | |
125 | 133 | $injector = $binding->injector; |
126 | 134 | |
127 | - try { |
|
135 | + try |
|
136 | + { |
|
128 | 137 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
129 | 138 | |
130 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
139 | + if (!$injectorInstance instanceof InjectorInterface) |
|
140 | + { |
|
131 | 141 | throw new InjectionException( |
132 | 142 | \sprintf( |
133 | 143 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -155,7 +165,8 @@ discard block |
||
155 | 165 | default => (string)$context, |
156 | 166 | }); |
157 | 167 | |
158 | - if (!$reflection->isInstance($instance)) { |
|
168 | + if (!$reflection->isInstance($instance)) |
|
169 | + { |
|
159 | 170 | throw new InjectionException( |
160 | 171 | \sprintf( |
161 | 172 | "Invalid injection response for '%s'.", |
@@ -165,7 +176,9 @@ discard block |
||
165 | 176 | } |
166 | 177 | |
167 | 178 | return $instance; |
168 | - } finally { |
|
179 | + } |
|
180 | + finally |
|
181 | + { |
|
169 | 182 | $this->state->bindings[$reflection->getName()] ??= $binding; |
170 | 183 | } |
171 | 184 | } |
@@ -184,7 +197,8 @@ discard block |
||
184 | 197 | //Binding is pointing to something else |
185 | 198 | : $this->make($binding->alias, $arguments, $context); |
186 | 199 | |
187 | - if ($binding->singleton && $arguments === []) { |
|
200 | + if ($binding->singleton && $arguments === []) |
|
201 | + { |
|
188 | 202 | $this->state->singletons[$alias] = $result; |
189 | 203 | } |
190 | 204 | |
@@ -225,11 +239,14 @@ discard block |
||
225 | 239 | array $arguments, |
226 | 240 | ): mixed { |
227 | 241 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
228 | - try { |
|
242 | + try |
|
243 | + { |
|
229 | 244 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
230 | 245 | ? ($binding->factory)() |
231 | 246 | : $this->invoker->invoke($binding->factory, $arguments); |
232 | - } catch (NotCallableException $e) { |
|
247 | + } |
|
248 | + catch (NotCallableException $e) |
|
249 | + { |
|
233 | 250 | throw new ContainerException( |
234 | 251 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
235 | 252 | $e->getCode(), |
@@ -248,19 +265,24 @@ discard block |
||
248 | 265 | ): ?object { |
249 | 266 | $avoidCache = $arguments !== []; |
250 | 267 | |
251 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
252 | - try { |
|
268 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
269 | + { |
|
270 | + try |
|
271 | + { |
|
253 | 272 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
254 | 273 | |
255 | 274 | $object = $this->createInstance( |
256 | 275 | new Ctx(alias: $alias, class: $alias, context: $context), |
257 | 276 | $arguments, |
258 | 277 | ); |
259 | - if ($avoidCache) { |
|
278 | + if ($avoidCache) |
|
279 | + { |
|
260 | 280 | return $object; |
261 | 281 | } |
262 | 282 | $binding->reference = WeakReference::create($object); |
263 | - } catch (\Throwable) { |
|
283 | + } |
|
284 | + catch (\Throwable) |
|
285 | + { |
|
264 | 286 | throw new ContainerException( |
265 | 287 | $this->tracer->combineTraceMessage( |
266 | 288 | \sprintf( |
@@ -270,7 +292,9 @@ discard block |
||
270 | 292 | ) |
271 | 293 | ) |
272 | 294 | ); |
273 | - } finally { |
|
295 | + } |
|
296 | + finally |
|
297 | + { |
|
274 | 298 | $this->tracer->pop(); |
275 | 299 | } |
276 | 300 | } |
@@ -285,18 +309,25 @@ discard block |
||
285 | 309 | ): mixed { |
286 | 310 | $parent = $this->scope->getParent(); |
287 | 311 | |
288 | - if ($parent !== null) { |
|
289 | - try { |
|
312 | + if ($parent !== null) |
|
313 | + { |
|
314 | + try |
|
315 | + { |
|
290 | 316 | $this->tracer->push(false, ...[ |
291 | 317 | 'current scope' => $this->scope->getScopeName(), |
292 | 318 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
293 | 319 | ]); |
294 | 320 | return $parent->make($alias, $parameters, $context); |
295 | - } catch (BadScopeException $e) { |
|
296 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
321 | + } |
|
322 | + catch (BadScopeException $e) |
|
323 | + { |
|
324 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
325 | + { |
|
297 | 326 | throw $e; |
298 | 327 | } |
299 | - } catch (ContainerExceptionInterface $e) { |
|
328 | + } |
|
329 | + catch (ContainerExceptionInterface $e) |
|
330 | + { |
|
300 | 331 | $className = match (true) { |
301 | 332 | $e instanceof NotFoundException => NotFoundException::class, |
302 | 333 | default => ContainerException::class, |
@@ -305,19 +336,24 @@ discard block |
||
305 | 336 | 'Can\'t resolve `%s`.', |
306 | 337 | $alias, |
307 | 338 | )), previous: $e); |
308 | - } finally { |
|
339 | + } |
|
340 | + finally |
|
341 | + { |
|
309 | 342 | $this->tracer->pop(false); |
310 | 343 | } |
311 | 344 | } |
312 | 345 | |
313 | 346 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
314 | - try { |
|
347 | + try |
|
348 | + { |
|
315 | 349 | //No direct instructions how to construct class, make is automatically |
316 | 350 | return $this->autowire( |
317 | 351 | new Ctx(alias: $alias, class: $alias, context: $context), |
318 | 352 | $parameters, |
319 | 353 | ); |
320 | - } finally { |
|
354 | + } |
|
355 | + finally |
|
356 | + { |
|
321 | 357 | $this->tracer->pop(false); |
322 | 358 | } |
323 | 359 | } |
@@ -367,7 +403,8 @@ discard block |
||
367 | 403 | // Check scope name |
368 | 404 | $ctx->reflection = new \ReflectionClass($instance); |
369 | 405 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
370 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
406 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
407 | + { |
|
371 | 408 | throw new BadScopeException($scopeName, $instance::class); |
372 | 409 | } |
373 | 410 | |
@@ -394,24 +431,30 @@ discard block |
||
394 | 431 | array $parameters, |
395 | 432 | ): object { |
396 | 433 | $class = $ctx->class; |
397 | - try { |
|
434 | + try |
|
435 | + { |
|
398 | 436 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
399 | - } catch (\ReflectionException $e) { |
|
437 | + } |
|
438 | + catch (\ReflectionException $e) |
|
439 | + { |
|
400 | 440 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
401 | 441 | } |
402 | 442 | |
403 | 443 | // Check scope name |
404 | 444 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
405 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
445 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
446 | + { |
|
406 | 447 | throw new BadScopeException($scope, $class); |
407 | 448 | } |
408 | 449 | |
409 | 450 | //We have to construct class using external injector when we know exact context |
410 | - if ($this->binder->hasInjector($class)) { |
|
451 | + if ($this->binder->hasInjector($class)) |
|
452 | + { |
|
411 | 453 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $parameters); |
412 | 454 | } |
413 | 455 | |
414 | - if (!$reflection->isInstantiable()) { |
|
456 | + if (!$reflection->isInstantiable()) |
|
457 | + { |
|
415 | 458 | $itIs = match (true) { |
416 | 459 | $reflection->isEnum() => 'Enum', |
417 | 460 | $reflection->isAbstract() => 'Abstract class', |
@@ -424,12 +467,16 @@ discard block |
||
424 | 467 | |
425 | 468 | $constructor = $reflection->getConstructor(); |
426 | 469 | |
427 | - if ($constructor !== null) { |
|
428 | - try { |
|
470 | + if ($constructor !== null) |
|
471 | + { |
|
472 | + try |
|
473 | + { |
|
429 | 474 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
430 | 475 | $this->tracer->push(true); |
431 | 476 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
432 | - } catch (ValidationException $e) { |
|
477 | + } |
|
478 | + catch (ValidationException $e) |
|
479 | + { |
|
433 | 480 | throw new ContainerException( |
434 | 481 | $this->tracer->combineTraceMessage( |
435 | 482 | \sprintf( |
@@ -439,22 +486,31 @@ discard block |
||
439 | 486 | ) |
440 | 487 | ), |
441 | 488 | ); |
442 | - } finally { |
|
489 | + } |
|
490 | + finally |
|
491 | + { |
|
443 | 492 | $this->tracer->pop(true); |
444 | 493 | $this->tracer->pop(false); |
445 | 494 | } |
446 | - try { |
|
495 | + try |
|
496 | + { |
|
447 | 497 | // Using constructor with resolved arguments |
448 | 498 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
449 | 499 | $this->tracer->push(true); |
450 | 500 | $instance = new $class(...$arguments); |
451 | - } catch (\TypeError $e) { |
|
501 | + } |
|
502 | + catch (\TypeError $e) |
|
503 | + { |
|
452 | 504 | throw new WrongTypeException($constructor, $e); |
453 | - } finally { |
|
505 | + } |
|
506 | + finally |
|
507 | + { |
|
454 | 508 | $this->tracer->pop(true); |
455 | 509 | $this->tracer->pop(false); |
456 | 510 | } |
457 | - } else { |
|
511 | + } |
|
512 | + else |
|
513 | + { |
|
458 | 514 | // No constructor specified |
459 | 515 | $instance = $reflection->newInstance(); |
460 | 516 | } |
@@ -472,13 +528,15 @@ discard block |
||
472 | 528 | $instance = $this->runInflector($instance); |
473 | 529 | |
474 | 530 | //Declarative singletons |
475 | - if ($this->isSingleton($ctx)) { |
|
531 | + if ($this->isSingleton($ctx)) |
|
532 | + { |
|
476 | 533 | $this->state->singletons[$ctx->alias] = $instance; |
477 | 534 | } |
478 | 535 | |
479 | 536 | // Register finalizer |
480 | 537 | $finalizer = $this->getFinalizer($ctx, $instance); |
481 | - if ($finalizer !== null) { |
|
538 | + if ($finalizer !== null) |
|
539 | + { |
|
482 | 540 | $this->state->finalizers[] = $finalizer; |
483 | 541 | } |
484 | 542 | |
@@ -490,12 +548,14 @@ discard block |
||
490 | 548 | */ |
491 | 549 | private function isSingleton(Ctx $ctx): bool |
492 | 550 | { |
493 | - if ($ctx->singleton === true) { |
|
551 | + if ($ctx->singleton === true) |
|
552 | + { |
|
494 | 553 | return true; |
495 | 554 | } |
496 | 555 | |
497 | 556 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
498 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
557 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
558 | + { |
|
499 | 559 | return true; |
500 | 560 | } |
501 | 561 | |
@@ -509,7 +569,8 @@ discard block |
||
509 | 569 | * @var Finalize|null $attribute |
510 | 570 | */ |
511 | 571 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
512 | - if ($attribute === null) { |
|
572 | + if ($attribute === null) |
|
573 | + { |
|
513 | 574 | return null; |
514 | 575 | } |
515 | 576 | |
@@ -523,10 +584,14 @@ discard block |
||
523 | 584 | { |
524 | 585 | $scope = $this->scope; |
525 | 586 | |
526 | - while ($scope !== null) { |
|
527 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
528 | - if ($instance instanceof $class) { |
|
529 | - foreach ($inflectors as $inflector) { |
|
587 | + while ($scope !== null) |
|
588 | + { |
|
589 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
590 | + { |
|
591 | + if ($instance instanceof $class) |
|
592 | + { |
|
593 | + foreach ($inflectors as $inflector) |
|
594 | + { |
|
530 | 595 | $instance = $inflector->getParametersCount() > 1 |
531 | 596 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
532 | 597 | : ($inflector->inflector)($instance); |
@@ -542,9 +607,12 @@ discard block |
||
542 | 607 | |
543 | 608 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
544 | 609 | { |
545 | - try { |
|
610 | + try |
|
611 | + { |
|
546 | 612 | $this->resolver->validateArguments($reflection, $arguments); |
547 | - } catch (\Throwable) { |
|
613 | + } |
|
614 | + catch (\Throwable) |
|
615 | + { |
|
548 | 616 | return false; |
549 | 617 | } |
550 | 618 |
@@ -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 (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate)) { |
|
209 | + foreach ($types as $namedType){ |
|
210 | + try{ |
|
211 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $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 | |
@@ -267,15 +267,15 @@ discard block |
||
267 | 267 | ResolvingState $state, |
268 | 268 | mixed &$value, |
269 | 269 | ReflectionParameter $validateWith = null, |
270 | - int|string $key = null |
|
270 | + int | string $key = null |
|
271 | 271 | ): void { |
272 | 272 | // Resolve Autowire objects |
273 | - if ($value instanceof Autowire) { |
|
273 | + if ($value instanceof Autowire){ |
|
274 | 274 | $value = $value->resolve($this->factory); |
275 | 275 | } |
276 | 276 | |
277 | 277 | // Validation |
278 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
278 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
279 | 279 | throw new InvalidArgumentException( |
280 | 280 | $validateWith->getDeclaringFunction(), |
281 | 281 | $validateWith->getName() |
@@ -52,7 +52,8 @@ 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 | 57 | $this->resolveParameter($parameter, $state, $validate) |
57 | 58 | or |
58 | 59 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -66,47 +67,59 @@ discard block |
||
66 | 67 | $positional = true; |
67 | 68 | $variadic = false; |
68 | 69 | $parameters = $reflection->getParameters(); |
69 | - if (\count($parameters) === 0) { |
|
70 | + if (\count($parameters) === 0) |
|
71 | + { |
|
70 | 72 | return; |
71 | 73 | } |
72 | 74 | |
73 | 75 | $parameter = null; |
74 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
76 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
77 | + { |
|
75 | 78 | // get related argument value |
76 | 79 | $key = \key($arguments); |
77 | 80 | |
78 | 81 | // For a variadic parameter it's no sense - named or positional argument will be sent |
79 | 82 | // But you can't send positional argument after named in any case |
80 | - if (\is_int($key) && !$positional) { |
|
83 | + if (\is_int($key) && !$positional) |
|
84 | + { |
|
81 | 85 | throw new PositionalArgumentException($reflection, $key); |
82 | 86 | } |
83 | 87 | |
84 | 88 | $positional = $positional && \is_int($key); |
85 | 89 | |
86 | - if (!$variadic) { |
|
90 | + if (!$variadic) |
|
91 | + { |
|
87 | 92 | $parameter = \array_shift($parameters); |
88 | 93 | $variadic = $parameter?->isVariadic() ?? false; |
89 | 94 | } |
90 | 95 | |
91 | - if ($parameter === null) { |
|
96 | + if ($parameter === null) |
|
97 | + { |
|
92 | 98 | throw new UnknownParameterException($reflection, $key); |
93 | 99 | } |
94 | 100 | $name = $parameter->getName(); |
95 | 101 | |
96 | - if (($positional || $variadic) && $key !== null) { |
|
102 | + if (($positional || $variadic) && $key !== null) |
|
103 | + { |
|
97 | 104 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
98 | 105 | $value = \array_shift($arguments); |
99 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
100 | - if ($parameter->isOptional()) { |
|
106 | + } |
|
107 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
108 | + { |
|
109 | + if ($parameter->isOptional()) |
|
110 | + { |
|
101 | 111 | continue; |
102 | 112 | } |
103 | 113 | throw new MissingRequiredArgumentException($reflection, $name); |
104 | - } else { |
|
114 | + } |
|
115 | + else |
|
116 | + { |
|
105 | 117 | $value = &$arguments[$name]; |
106 | 118 | unset($arguments[$name]); |
107 | 119 | } |
108 | 120 | |
109 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
121 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
122 | + { |
|
110 | 123 | throw new InvalidArgumentException($reflection, $name); |
111 | 124 | } |
112 | 125 | } |
@@ -114,7 +127,8 @@ discard block |
||
114 | 127 | |
115 | 128 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
116 | 129 | { |
117 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
130 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
131 | + { |
|
118 | 132 | return true; |
119 | 133 | } |
120 | 134 | $type = $parameter->getType(); |
@@ -125,17 +139,21 @@ discard block |
||
125 | 139 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
126 | 140 | }; |
127 | 141 | |
128 | - foreach ($types as $t) { |
|
142 | + foreach ($types as $t) |
|
143 | + { |
|
129 | 144 | \assert($t instanceof ReflectionNamedType); |
130 | - if (!$this->validateValueNamedType($t, $value)) { |
|
145 | + if (!$this->validateValueNamedType($t, $value)) |
|
146 | + { |
|
131 | 147 | // If it is TypeIntersection |
132 | - if ($or) { |
|
148 | + if ($or) |
|
149 | + { |
|
133 | 150 | continue; |
134 | 151 | } |
135 | 152 | return false; |
136 | 153 | } |
137 | 154 | // If it is not type intersection then we can skip that value after first successful check |
138 | - if ($or) { |
|
155 | + if ($or) |
|
156 | + { |
|
139 | 157 | return true; |
140 | 158 | } |
141 | 159 | } |
@@ -150,7 +168,8 @@ discard block |
||
150 | 168 | { |
151 | 169 | $name = $type->getName(); |
152 | 170 | |
153 | - if ($type->isBuiltin()) { |
|
171 | + if ($type->isBuiltin()) |
|
172 | + { |
|
154 | 173 | return match ($name) { |
155 | 174 | 'mixed' => true, |
156 | 175 | 'string' => \is_string($value), |
@@ -183,13 +202,18 @@ discard block |
||
183 | 202 | |
184 | 203 | // Try to resolve parameter by name |
185 | 204 | $res = $state->resolveParameterByNameOrPosition($parameter, $isVariadic); |
186 | - if ($res !== [] || $isVariadic) { |
|
205 | + if ($res !== [] || $isVariadic) |
|
206 | + { |
|
187 | 207 | // validate |
188 | - if ($isVariadic) { |
|
189 | - foreach ($res as $k => &$v) { |
|
208 | + if ($isVariadic) |
|
209 | + { |
|
210 | + foreach ($res as $k => &$v) |
|
211 | + { |
|
190 | 212 | $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key: $k); |
191 | 213 | } |
192 | - } else { |
|
214 | + } |
|
215 | + else |
|
216 | + { |
|
193 | 217 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
194 | 218 | } |
195 | 219 | |
@@ -197,39 +221,49 @@ discard block |
||
197 | 221 | } |
198 | 222 | |
199 | 223 | $error = null; |
200 | - if ($hasType) { |
|
224 | + if ($hasType) |
|
225 | + { |
|
201 | 226 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
202 | 227 | $reflectionType = $parameter->getType(); |
203 | 228 | |
204 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
229 | + if ($reflectionType instanceof ReflectionIntersectionType) |
|
230 | + { |
|
205 | 231 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
206 | 232 | } |
207 | 233 | |
208 | 234 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
209 | - foreach ($types as $namedType) { |
|
210 | - try { |
|
211 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate)) { |
|
235 | + foreach ($types as $namedType) |
|
236 | + { |
|
237 | + try |
|
238 | + { |
|
239 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $parameter, $validate)) |
|
240 | + { |
|
212 | 241 | return true; |
213 | 242 | } |
214 | - } catch (Throwable $e) { |
|
243 | + } |
|
244 | + catch (Throwable $e) |
|
245 | + { |
|
215 | 246 | $error = $e; |
216 | 247 | } |
217 | 248 | } |
218 | 249 | } |
219 | 250 | |
220 | - if ($parameter->isDefaultValueAvailable()) { |
|
251 | + if ($parameter->isDefaultValueAvailable()) |
|
252 | + { |
|
221 | 253 | $argument = $parameter->getDefaultValue(); |
222 | 254 | $this->processArgument($state, $argument); |
223 | 255 | return true; |
224 | 256 | } |
225 | 257 | |
226 | - if ($hasType && $parameter->allowsNull()) { |
|
258 | + if ($hasType && $parameter->allowsNull()) |
|
259 | + { |
|
227 | 260 | $argument = null; |
228 | 261 | $this->processArgument($state, $argument); |
229 | 262 | return true; |
230 | 263 | } |
231 | 264 | |
232 | - if ($error === null) { |
|
265 | + if ($error === null) |
|
266 | + { |
|
233 | 267 | return false; |
234 | 268 | } |
235 | 269 | |
@@ -270,12 +304,14 @@ discard block |
||
270 | 304 | int|string $key = null |
271 | 305 | ): void { |
272 | 306 | // Resolve Autowire objects |
273 | - if ($value instanceof Autowire) { |
|
307 | + if ($value instanceof Autowire) |
|
308 | + { |
|
274 | 309 | $value = $value->resolve($this->factory); |
275 | 310 | } |
276 | 311 | |
277 | 312 | // Validation |
278 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
313 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
314 | + { |
|
279 | 315 | throw new InvalidArgumentException( |
280 | 316 | $validateWith->getDeclaringFunction(), |
281 | 317 | $validateWith->getName() |
@@ -17,9 +17,9 @@ |
||
17 | 17 | public function __construct( |
18 | 18 | public readonly string $alias, |
19 | 19 | public string $class, |
20 | - public \Stringable|string|null $context = null, |
|
20 | + public \Stringable | string | null $context = null, |
|
21 | 21 | public ?bool $singleton = null, |
22 | 22 | public ?\ReflectionClass $reflection = null, |
23 | - ) { |
|
23 | + ){ |
|
24 | 24 | } |
25 | 25 | } |
@@ -49,11 +49,11 @@ discard block |
||
49 | 49 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
50 | 50 | |
51 | 51 | private Internal\State $state; |
52 | - private ResolverInterface|Internal\Resolver $resolver; |
|
53 | - private FactoryInterface|Internal\Factory $factory; |
|
54 | - private ContainerInterface|Internal\Container $container; |
|
55 | - private BinderInterface|Internal\Binder $binder; |
|
56 | - private InvokerInterface|Internal\Invoker $invoker; |
|
52 | + private ResolverInterface | Internal\Resolver $resolver; |
|
53 | + private FactoryInterface | Internal\Factory $factory; |
|
54 | + private ContainerInterface | Internal\Container $container; |
|
55 | + private BinderInterface | Internal\Binder $binder; |
|
56 | + private InvokerInterface | Internal\Invoker $invoker; |
|
57 | 57 | private Internal\Scope $scope; |
58 | 58 | |
59 | 59 | /** |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | public function __construct( |
63 | 63 | private Config $config = new Config(), |
64 | 64 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
65 | - ) { |
|
65 | + ){ |
|
66 | 66 | $this->initServices($this, $scopeName); |
67 | 67 | |
68 | 68 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | * @throws ContainerException |
115 | 115 | * @throws \Throwable |
116 | 116 | */ |
117 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
117 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
118 | 118 | { |
119 | 119 | /** @psalm-suppress TooManyArguments */ |
120 | 120 | return $this->factory->make($alias, $parameters, $context); |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @throws ContainerException |
139 | 139 | * @throws \Throwable |
140 | 140 | */ |
141 | - public function get(string|Autowire $id, string $context = null): mixed |
|
141 | + public function get(string | Autowire $id, string $context = null): mixed |
|
142 | 142 | { |
143 | 143 | /** @psalm-suppress TooManyArguments */ |
144 | 144 | return $this->container->get($id, $context); |
@@ -164,16 +164,16 @@ discard block |
||
164 | 164 | $binds = &$this->state->bindings; |
165 | 165 | $singletons = &$this->state->singletons; |
166 | 166 | $cleanup = $previous = $prevSin = []; |
167 | - foreach ($bindings as $alias => $resolver) { |
|
167 | + foreach ($bindings as $alias => $resolver){ |
|
168 | 168 | // Store previous bindings |
169 | - if (isset($binds[$alias])) { |
|
169 | + if (isset($binds[$alias])){ |
|
170 | 170 | $previous[$alias] = $binds[$alias]; |
171 | - } else { |
|
171 | + }else{ |
|
172 | 172 | // Store bindings to be removed |
173 | 173 | $cleanup[] = $alias; |
174 | 174 | } |
175 | 175 | // Store previous singletons |
176 | - if (isset($singletons[$alias])) { |
|
176 | + if (isset($singletons[$alias])){ |
|
177 | 177 | $prevSin[$alias] = $singletons[$alias]; |
178 | 178 | unset($singletons[$alias]); |
179 | 179 | } |
@@ -181,21 +181,21 @@ discard block |
||
181 | 181 | $this->binder->bind($alias, $resolver); |
182 | 182 | } |
183 | 183 | |
184 | - try { |
|
184 | + try{ |
|
185 | 185 | return ContainerScope::getContainer() !== $this |
186 | 186 | ? ContainerScope::runScope($this, $scope) |
187 | 187 | : $scope($this); |
188 | - } finally { |
|
188 | + }finally{ |
|
189 | 189 | // Remove new bindings |
190 | - foreach ($cleanup as $alias) { |
|
190 | + foreach ($cleanup as $alias){ |
|
191 | 191 | unset($binds[$alias], $singletons[$alias]); |
192 | 192 | } |
193 | 193 | // Restore previous bindings |
194 | - foreach ($previous as $alias => $resolver) { |
|
194 | + foreach ($previous as $alias => $resolver){ |
|
195 | 195 | $binds[$alias] = $resolver; |
196 | 196 | } |
197 | 197 | // Restore singletons |
198 | - foreach ($prevSin as $alias => $instance) { |
|
198 | + foreach ($prevSin as $alias => $instance){ |
|
199 | 199 | $singletons[$alias] = $instance; |
200 | 200 | } |
201 | 201 | } |
@@ -213,18 +213,18 @@ discard block |
||
213 | 213 | $container->scope->setParent($this, $this->scope); |
214 | 214 | |
215 | 215 | // Add specific bindings |
216 | - foreach ($bindings as $alias => $resolver) { |
|
216 | + foreach ($bindings as $alias => $resolver){ |
|
217 | 217 | $container->binder->bind($alias, $resolver); |
218 | 218 | } |
219 | 219 | |
220 | 220 | return ContainerScope::runScope( |
221 | 221 | $container, |
222 | 222 | static function (self $container) use ($autowire, $closure): mixed { |
223 | - try { |
|
223 | + try{ |
|
224 | 224 | return $autowire |
225 | 225 | ? $container->invoke($closure) |
226 | 226 | : $closure($container); |
227 | - } finally { |
|
227 | + }finally{ |
|
228 | 228 | $container->closeScope(); |
229 | 229 | } |
230 | 230 | } |
@@ -259,9 +259,9 @@ discard block |
||
259 | 259 | * @param bool $force If the value is false, an exception will be thrown when attempting |
260 | 260 | * to bind an already constructed singleton. |
261 | 261 | */ |
262 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
|
262 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, bool $force = true): void |
|
263 | 263 | { |
264 | - if ($force) { |
|
264 | + if ($force){ |
|
265 | 265 | $this->binder->removeBinding($alias); |
266 | 266 | } |
267 | 267 | |
@@ -330,8 +330,8 @@ discard block |
||
330 | 330 | ]); |
331 | 331 | |
332 | 332 | // Create container services |
333 | - foreach ($container->config as $property => $class) { |
|
334 | - if (\property_exists($container, $property)) { |
|
333 | + foreach ($container->config as $property => $class){ |
|
334 | + if (\property_exists($container, $property)){ |
|
335 | 335 | $container->$property = $constructor->get($property, $class); |
336 | 336 | } |
337 | 337 | } |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | private function closeScope(): void |
346 | 346 | { |
347 | 347 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
348 | - if (!isset($this->scope)) { |
|
348 | + if (!isset($this->scope)){ |
|
349 | 349 | $this->destruct(); |
350 | 350 | return; |
351 | 351 | } |
@@ -354,10 +354,10 @@ discard block |
||
354 | 354 | |
355 | 355 | // Run finalizers |
356 | 356 | $errors = []; |
357 | - foreach ($this->state->finalizers as $finalizer) { |
|
358 | - try { |
|
357 | + foreach ($this->state->finalizers as $finalizer){ |
|
358 | + try{ |
|
359 | 359 | $this->invoker->invoke($finalizer); |
360 | - } catch (\Throwable $e) { |
|
360 | + }catch (\Throwable $e){ |
|
361 | 361 | $errors[] = $e; |
362 | 362 | } |
363 | 363 | } |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | $this->destruct(); |
367 | 367 | |
368 | 368 | // Throw collected errors |
369 | - if ($errors !== []) { |
|
369 | + if ($errors !== []){ |
|
370 | 370 | throw new FinalizersException($scopeName, $errors); |
371 | 371 | } |
372 | 372 | } |