@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | |
17 | 17 | public function __construct( |
18 | 18 | private readonly ?string $scopeName = null, |
19 | - ) { |
|
19 | + ){ |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public function getScopeName(): ?string |
@@ -35,9 +35,9 @@ discard block |
||
35 | 35 | $this->parentScope = $parentScope; |
36 | 36 | |
37 | 37 | // Check a scope with the same name is not already registered |
38 | - if ($this->scopeName !== null) { |
|
38 | + if ($this->scopeName !== null){ |
|
39 | 39 | $tmp = $this; |
40 | - while ($tmp->parentScope !== null) { |
|
40 | + while ($tmp->parentScope !== null){ |
|
41 | 41 | $tmp = $tmp->parentScope; |
42 | 42 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
43 | 43 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $result = [$this->scopeName]; |
61 | 61 | |
62 | 62 | $parent = $this; |
63 | - while ($parent->parentScope !== null) { |
|
63 | + while ($parent->parentScope !== null){ |
|
64 | 64 | $parent = $parent->parentScope; |
65 | 65 | $result[] = $parent->scopeName; |
66 | 66 | } |
@@ -35,9 +35,11 @@ discard block |
||
35 | 35 | $this->parentScope = $parentScope; |
36 | 36 | |
37 | 37 | // Check a scope with the same name is not already registered |
38 | - if ($this->scopeName !== null) { |
|
38 | + if ($this->scopeName !== null) |
|
39 | + { |
|
39 | 40 | $tmp = $this; |
40 | - while ($tmp->parentScope !== null) { |
|
41 | + while ($tmp->parentScope !== null) |
|
42 | + { |
|
41 | 43 | $tmp = $tmp->parentScope; |
42 | 44 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
43 | 45 | } |
@@ -60,7 +62,8 @@ discard block |
||
60 | 62 | $result = [$this->scopeName]; |
61 | 63 | |
62 | 64 | $parent = $this; |
63 | - while ($parent->parentScope !== null) { |
|
65 | + while ($parent->parentScope !== null) |
|
66 | + { |
|
64 | 67 | $parent = $parent->parentScope; |
65 | 68 | $result[] = $parent->scopeName; |
66 | 69 | } |
@@ -11,7 +11,7 @@ |
||
11 | 11 | { |
12 | 12 | public function __construct( |
13 | 13 | string $scope, |
14 | - ) { |
|
14 | + ){ |
|
15 | 15 | parent::__construct( |
16 | 16 | $scope, |
17 | 17 | "Error on a scope allocation with the name `{$scope}`. A scope with the same name already exists.", |
@@ -12,7 +12,7 @@ |
||
12 | 12 | public function __construct( |
13 | 13 | string $scope, |
14 | 14 | protected string $className, |
15 | - ) { |
|
15 | + ){ |
|
16 | 16 | parent::__construct( |
17 | 17 | $scope, |
18 | 18 | \sprintf('Class `%s` can be resolved only in `%s` scope.', $className, $scope), |
@@ -16,7 +16,7 @@ |
||
16 | 16 | string $message = '', |
17 | 17 | int $code = 0, |
18 | 18 | ?\Throwable $previous = null, |
19 | - ) { |
|
19 | + ){ |
|
20 | 20 | parent::__construct( |
21 | 21 | $message, |
22 | 22 | $code, |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | public function __construct( |
16 | 16 | ?string $scope, |
17 | 17 | protected array $exceptions, |
18 | - ) { |
|
18 | + ){ |
|
19 | 19 | $count = \count($exceptions); |
20 | 20 | parent::__construct( |
21 | 21 | $scope, |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | $count === 1 ? 'An exception has been' : "$count exceptions have been", |
25 | 25 | $scope === null ? 'an unnamed scope' : "the scope `$scope`", |
26 | 26 | \implode("\n\n", \array_map( |
27 | - static fn (Exception $e): string => \sprintf( |
|
27 | + static fn (Exception $e) : string => \sprintf( |
|
28 | 28 | "# %s\n%s", |
29 | 29 | $e::class, |
30 | 30 | $e->getMessage(), |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | public readonly string $binder = Binder::class, |
51 | 51 | public readonly string $invoker = Invoker::class, |
52 | 52 | public readonly string $tracer = Tracer::class, |
53 | - ) { |
|
53 | + ){ |
|
54 | 54 | $this->scope = Scope::class; |
55 | 55 | $this->scopedBindings = new Internal\Config\StateStorage(); |
56 | 56 | } |
@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function lockRoot(): bool |
76 | 76 | { |
77 | - try { |
|
77 | + try{ |
|
78 | 78 | return $this->rootLocked; |
79 | - } finally { |
|
79 | + }finally{ |
|
80 | 80 | $this->rootLocked = false; |
81 | 81 | } |
82 | 82 | } |
@@ -74,9 +74,12 @@ |
||
74 | 74 | */ |
75 | 75 | public function lockRoot(): bool |
76 | 76 | { |
77 | - try { |
|
77 | + try |
|
78 | + { |
|
78 | 79 | return $this->rootLocked; |
79 | - } finally { |
|
80 | + } |
|
81 | + finally |
|
82 | + { |
|
80 | 83 | $this->rootLocked = false; |
81 | 84 | } |
82 | 85 | } |
@@ -13,9 +13,9 @@ |
||
13 | 13 | public function __construct( |
14 | 14 | ?string $scope, |
15 | 15 | array $parents, |
16 | - ) { |
|
16 | + ){ |
|
17 | 17 | $scopes = \implode('->', \array_map( |
18 | - static fn (?string $scope): string => $scope === null ? 'null' : "\"$scope\"", |
|
18 | + static fn (?string $scope) : string => $scope === null ? 'null' : "\"$scope\"", |
|
19 | 19 | [...\array_reverse($parents), $scope], |
20 | 20 | )); |
21 | 21 | parent::__construct( |
@@ -20,7 +20,7 @@ |
||
20 | 20 | { |
21 | 21 | $this->logger = $logger; |
22 | 22 | $this->finalized = true; |
23 | - if ($this->throwException) { |
|
23 | + if ($this->throwException){ |
|
24 | 24 | throw new RuntimeException('Test exception from finalize method.'); |
25 | 25 | } |
26 | 26 | } |
@@ -20,7 +20,8 @@ |
||
20 | 20 | { |
21 | 21 | $this->logger = $logger; |
22 | 22 | $this->finalized = true; |
23 | - if ($this->throwException) { |
|
23 | + if ($this->throwException) |
|
24 | + { |
|
24 | 25 | throw new RuntimeException('Test exception from finalize method.'); |
25 | 26 | } |
26 | 27 | } |
@@ -64,13 +64,13 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
66 | 66 | { |
67 | - if (!isset($this->state->bindings[$alias])) { |
|
67 | + if (!isset($this->state->bindings[$alias])){ |
|
68 | 68 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
69 | 69 | } |
70 | 70 | |
71 | 71 | $avoidCache = $parameters !== []; |
72 | 72 | $binding = $this->state->bindings[$alias]; |
73 | - try { |
|
73 | + try{ |
|
74 | 74 | $this->tracer->push( |
75 | 75 | false, |
76 | 76 | action: 'resolve from binding', |
@@ -81,22 +81,22 @@ discard block |
||
81 | 81 | ); |
82 | 82 | $this->tracer->push(true); |
83 | 83 | |
84 | - if (\is_object($binding)) { |
|
85 | - if ($binding::class === WeakReference::class) { |
|
84 | + if (\is_object($binding)){ |
|
85 | + if ($binding::class === WeakReference::class){ |
|
86 | 86 | return $this->resolveWeakReference($binding, $alias, $context, $parameters); |
87 | 87 | } |
88 | 88 | |
89 | 89 | // When binding is instance, assuming singleton |
90 | 90 | return $avoidCache |
91 | 91 | ? $this->createInstance( |
92 | - new Ctx(alias: $alias, class: $binding::class, parameter: $context), |
|
92 | + new Ctx(alias : $alias, class : $binding::class, parameter : $context), |
|
93 | 93 | $parameters, |
94 | 94 | ) |
95 | 95 | : $binding; |
96 | 96 | } |
97 | 97 | |
98 | 98 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
99 | - if (\is_string($binding)) { |
|
99 | + if (\is_string($binding)){ |
|
100 | 100 | $ctx->class = $binding; |
101 | 101 | return $binding === $alias |
102 | 102 | ? $this->autowire($ctx, $parameters) |
@@ -104,18 +104,18 @@ discard block |
||
104 | 104 | : $this->make($binding, $parameters, $context); |
105 | 105 | } |
106 | 106 | |
107 | - if ($binding[1] === true) { |
|
107 | + if ($binding[1] === true){ |
|
108 | 108 | $ctx->singleton = true; |
109 | 109 | } |
110 | 110 | unset($this->state->bindings[$alias]); |
111 | - try { |
|
111 | + try{ |
|
112 | 112 | return $binding[0] === $alias |
113 | 113 | ? $this->autowire($ctx, $parameters) |
114 | 114 | : $this->evaluateBinding($ctx, $binding[0], $parameters); |
115 | - } finally { |
|
115 | + }finally{ |
|
116 | 116 | $this->state->bindings[$alias] ??= $binding; |
117 | 117 | } |
118 | - } finally { |
|
118 | + }finally{ |
|
119 | 119 | $this->tracer->pop(true); |
120 | 120 | $this->tracer->pop(false); |
121 | 121 | } |
@@ -129,19 +129,19 @@ discard block |
||
129 | 129 | ): ?object { |
130 | 130 | $avoidCache = $parameters !== []; |
131 | 131 | |
132 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
133 | - try { |
|
132 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)){ |
|
133 | + try{ |
|
134 | 134 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
135 | 135 | /** @psalm-suppress NoValue */ |
136 | 136 | $object = $this->createInstance( |
137 | 137 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
138 | 138 | $parameters, |
139 | 139 | ); |
140 | - if ($avoidCache) { |
|
140 | + if ($avoidCache){ |
|
141 | 141 | return $object; |
142 | 142 | } |
143 | 143 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
144 | - } catch (\Throwable) { |
|
144 | + }catch (\Throwable){ |
|
145 | 145 | throw new ContainerException( |
146 | 146 | $this->tracer->combineTraceMessage( |
147 | 147 | \sprintf( |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | ) |
152 | 152 | ) |
153 | 153 | ); |
154 | - } finally { |
|
154 | + }finally{ |
|
155 | 155 | $this->tracer->pop(); |
156 | 156 | } |
157 | 157 | } |
@@ -163,18 +163,18 @@ discard block |
||
163 | 163 | { |
164 | 164 | $parent = $this->scope->getParent(); |
165 | 165 | |
166 | - if ($parent !== null) { |
|
167 | - try { |
|
166 | + if ($parent !== null){ |
|
167 | + try{ |
|
168 | 168 | $this->tracer->push(false, ...[ |
169 | 169 | 'current scope' => $this->scope->getScopeName(), |
170 | 170 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
171 | 171 | ]); |
172 | 172 | return $parent->make($alias, $parameters, $context); |
173 | - } catch (BadScopeException $e) { |
|
174 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
173 | + }catch (BadScopeException $e){ |
|
174 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
175 | 175 | throw $e; |
176 | 176 | } |
177 | - } catch (ContainerExceptionInterface $e) { |
|
177 | + }catch (ContainerExceptionInterface $e){ |
|
178 | 178 | $className = match (true) { |
179 | 179 | $e instanceof NotFoundException => NotFoundException::class, |
180 | 180 | default => ContainerException::class, |
@@ -183,19 +183,19 @@ discard block |
||
183 | 183 | 'Can\'t resolve `%s`.', |
184 | 184 | $alias, |
185 | 185 | )), previous: $e); |
186 | - } finally { |
|
186 | + }finally{ |
|
187 | 187 | $this->tracer->pop(false); |
188 | 188 | } |
189 | 189 | } |
190 | 190 | |
191 | 191 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
192 | - try { |
|
192 | + try{ |
|
193 | 193 | //No direct instructions how to construct class, make is automatically |
194 | 194 | return $this->autowire( |
195 | 195 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
196 | 196 | $parameters, |
197 | 197 | ); |
198 | - } finally { |
|
198 | + }finally{ |
|
199 | 199 | $this->tracer->pop(false); |
200 | 200 | } |
201 | 201 | } |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | && |
217 | 217 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
218 | 218 | )) |
219 | - ) { |
|
219 | + ){ |
|
220 | 220 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
221 | 221 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
222 | 222 | $this->tracer->getRootAlias(), |
@@ -244,16 +244,16 @@ discard block |
||
244 | 244 | mixed $target, |
245 | 245 | array $arguments, |
246 | 246 | ): mixed { |
247 | - if (\is_string($target)) { |
|
247 | + if (\is_string($target)){ |
|
248 | 248 | // Reference |
249 | 249 | $instance = $this->make($target, $arguments, $ctx->parameter); |
250 | - } else { |
|
251 | - if ($target instanceof Autowire) { |
|
250 | + }else{ |
|
251 | + if ($target instanceof Autowire){ |
|
252 | 252 | $instance = $target->resolve($this, $arguments); |
253 | - } else { |
|
254 | - try { |
|
253 | + }else{ |
|
254 | + try{ |
|
255 | 255 | $instance = $this->invoker->invoke($target, $arguments); |
256 | - } catch (NotCallableException $e) { |
|
256 | + }catch (NotCallableException $e){ |
|
257 | 257 | throw new ContainerException( |
258 | 258 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
259 | 259 | $e->getCode(), |
@@ -263,10 +263,10 @@ discard block |
||
263 | 263 | } |
264 | 264 | |
265 | 265 | // Check scope name |
266 | - if (\is_object($instance)) { |
|
266 | + if (\is_object($instance)){ |
|
267 | 267 | $ctx->reflection = new \ReflectionClass($instance); |
268 | 268 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
269 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
269 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()){ |
|
270 | 270 | throw new BadScopeException($scopeName, $instance::class); |
271 | 271 | } |
272 | 272 | } |
@@ -295,26 +295,26 @@ discard block |
||
295 | 295 | array $parameters, |
296 | 296 | ): object { |
297 | 297 | $class = $ctx->class; |
298 | - try { |
|
298 | + try{ |
|
299 | 299 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
300 | - } catch (\ReflectionException $e) { |
|
300 | + }catch (\ReflectionException $e){ |
|
301 | 301 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
302 | 302 | } |
303 | 303 | |
304 | 304 | // Check scope name |
305 | 305 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
306 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
306 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
307 | 307 | throw new BadScopeException($scope, $class); |
308 | 308 | } |
309 | 309 | |
310 | 310 | //We have to construct class using external injector when we know exact context |
311 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
311 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
312 | 312 | $injector = $this->state->injectors[$reflection->getName()]; |
313 | 313 | |
314 | - try { |
|
314 | + try{ |
|
315 | 315 | $injectorInstance = $this->container->get($injector); |
316 | 316 | |
317 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
317 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
318 | 318 | throw new InjectionException( |
319 | 319 | \sprintf( |
320 | 320 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | * @psalm-suppress RedundantCondition |
330 | 330 | */ |
331 | 331 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
332 | - if (!$reflection->isInstance($instance)) { |
|
332 | + if (!$reflection->isInstance($instance)){ |
|
333 | 333 | throw new InjectionException( |
334 | 334 | \sprintf( |
335 | 335 | "Invalid injection response for '%s'.", |
@@ -339,12 +339,12 @@ discard block |
||
339 | 339 | } |
340 | 340 | |
341 | 341 | return $instance; |
342 | - } finally { |
|
342 | + }finally{ |
|
343 | 343 | $this->state->injectors[$reflection->getName()] = $injector; |
344 | 344 | } |
345 | 345 | } |
346 | 346 | |
347 | - if (!$reflection->isInstantiable()) { |
|
347 | + if (!$reflection->isInstantiable()){ |
|
348 | 348 | $itIs = match (true) { |
349 | 349 | $reflection->isEnum() => 'Enum', |
350 | 350 | $reflection->isAbstract() => 'Abstract class', |
@@ -357,12 +357,12 @@ discard block |
||
357 | 357 | |
358 | 358 | $constructor = $reflection->getConstructor(); |
359 | 359 | |
360 | - if ($constructor !== null) { |
|
361 | - try { |
|
360 | + if ($constructor !== null){ |
|
361 | + try{ |
|
362 | 362 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
363 | 363 | $this->tracer->push(true); |
364 | 364 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
365 | - } catch (ValidationException $e) { |
|
365 | + }catch (ValidationException $e){ |
|
366 | 366 | throw new ContainerException( |
367 | 367 | $this->tracer->combineTraceMessage( |
368 | 368 | \sprintf( |
@@ -372,22 +372,22 @@ discard block |
||
372 | 372 | ) |
373 | 373 | ), |
374 | 374 | ); |
375 | - } finally { |
|
375 | + }finally{ |
|
376 | 376 | $this->tracer->pop(true); |
377 | 377 | $this->tracer->pop(false); |
378 | 378 | } |
379 | - try { |
|
379 | + try{ |
|
380 | 380 | // Using constructor with resolved arguments |
381 | 381 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
382 | 382 | $this->tracer->push(true); |
383 | 383 | $instance = new $class(...$arguments); |
384 | - } catch (\TypeError $e) { |
|
384 | + }catch (\TypeError $e){ |
|
385 | 385 | throw new WrongTypeException($constructor, $e); |
386 | - } finally { |
|
386 | + }finally{ |
|
387 | 387 | $this->tracer->pop(true); |
388 | 388 | $this->tracer->pop(false); |
389 | 389 | } |
390 | - } else { |
|
390 | + }else{ |
|
391 | 391 | // No constructor specified |
392 | 392 | $instance = $reflection->newInstance(); |
393 | 393 | } |
@@ -411,13 +411,13 @@ discard block |
||
411 | 411 | $ctx->reflection ??= new \ReflectionClass($instance); |
412 | 412 | |
413 | 413 | //Declarative singletons |
414 | - if ($this->isSingleton($ctx)) { |
|
414 | + if ($this->isSingleton($ctx)){ |
|
415 | 415 | $this->state->bindings[$ctx->alias] = $instance; |
416 | 416 | } |
417 | 417 | |
418 | 418 | // Register finalizer |
419 | 419 | $finalizer = $this->getFinalizer($ctx, $instance); |
420 | - if ($finalizer !== null) { |
|
420 | + if ($finalizer !== null){ |
|
421 | 421 | $this->state->finalizers[] = $finalizer; |
422 | 422 | } |
423 | 423 | |
@@ -429,11 +429,11 @@ discard block |
||
429 | 429 | */ |
430 | 430 | private function isSingleton(Ctx $ctx): bool |
431 | 431 | { |
432 | - if ($ctx->singleton === true) { |
|
432 | + if ($ctx->singleton === true){ |
|
433 | 433 | return true; |
434 | 434 | } |
435 | 435 | |
436 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
436 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
437 | 437 | return true; |
438 | 438 | } |
439 | 439 | |
@@ -444,7 +444,7 @@ discard block |
||
444 | 444 | { |
445 | 445 | /** @var Finalize|null $attribute */ |
446 | 446 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
447 | - if ($attribute === null) { |
|
447 | + if ($attribute === null){ |
|
448 | 448 | return null; |
449 | 449 | } |
450 | 450 |
@@ -64,13 +64,15 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
66 | 66 | { |
67 | - if (!isset($this->state->bindings[$alias])) { |
|
67 | + if (!isset($this->state->bindings[$alias])) |
|
68 | + { |
|
68 | 69 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
69 | 70 | } |
70 | 71 | |
71 | 72 | $avoidCache = $parameters !== []; |
72 | 73 | $binding = $this->state->bindings[$alias]; |
73 | - try { |
|
74 | + try |
|
75 | + { |
|
74 | 76 | $this->tracer->push( |
75 | 77 | false, |
76 | 78 | action: 'resolve from binding', |
@@ -81,8 +83,10 @@ discard block |
||
81 | 83 | ); |
82 | 84 | $this->tracer->push(true); |
83 | 85 | |
84 | - if (\is_object($binding)) { |
|
85 | - if ($binding::class === WeakReference::class) { |
|
86 | + if (\is_object($binding)) |
|
87 | + { |
|
88 | + if ($binding::class === WeakReference::class) |
|
89 | + { |
|
86 | 90 | return $this->resolveWeakReference($binding, $alias, $context, $parameters); |
87 | 91 | } |
88 | 92 | |
@@ -96,7 +100,8 @@ discard block |
||
96 | 100 | } |
97 | 101 | |
98 | 102 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
99 | - if (\is_string($binding)) { |
|
103 | + if (\is_string($binding)) |
|
104 | + { |
|
100 | 105 | $ctx->class = $binding; |
101 | 106 | return $binding === $alias |
102 | 107 | ? $this->autowire($ctx, $parameters) |
@@ -104,18 +109,24 @@ discard block |
||
104 | 109 | : $this->make($binding, $parameters, $context); |
105 | 110 | } |
106 | 111 | |
107 | - if ($binding[1] === true) { |
|
112 | + if ($binding[1] === true) |
|
113 | + { |
|
108 | 114 | $ctx->singleton = true; |
109 | 115 | } |
110 | 116 | unset($this->state->bindings[$alias]); |
111 | - try { |
|
117 | + try |
|
118 | + { |
|
112 | 119 | return $binding[0] === $alias |
113 | 120 | ? $this->autowire($ctx, $parameters) |
114 | 121 | : $this->evaluateBinding($ctx, $binding[0], $parameters); |
115 | - } finally { |
|
122 | + } |
|
123 | + finally |
|
124 | + { |
|
116 | 125 | $this->state->bindings[$alias] ??= $binding; |
117 | 126 | } |
118 | - } finally { |
|
127 | + } |
|
128 | + finally |
|
129 | + { |
|
119 | 130 | $this->tracer->pop(true); |
120 | 131 | $this->tracer->pop(false); |
121 | 132 | } |
@@ -129,19 +140,24 @@ discard block |
||
129 | 140 | ): ?object { |
130 | 141 | $avoidCache = $parameters !== []; |
131 | 142 | |
132 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
133 | - try { |
|
143 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)) |
|
144 | + { |
|
145 | + try |
|
146 | + { |
|
134 | 147 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
135 | 148 | /** @psalm-suppress NoValue */ |
136 | 149 | $object = $this->createInstance( |
137 | 150 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
138 | 151 | $parameters, |
139 | 152 | ); |
140 | - if ($avoidCache) { |
|
153 | + if ($avoidCache) |
|
154 | + { |
|
141 | 155 | return $object; |
142 | 156 | } |
143 | 157 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
144 | - } catch (\Throwable) { |
|
158 | + } |
|
159 | + catch (\Throwable) |
|
160 | + { |
|
145 | 161 | throw new ContainerException( |
146 | 162 | $this->tracer->combineTraceMessage( |
147 | 163 | \sprintf( |
@@ -151,7 +167,9 @@ discard block |
||
151 | 167 | ) |
152 | 168 | ) |
153 | 169 | ); |
154 | - } finally { |
|
170 | + } |
|
171 | + finally |
|
172 | + { |
|
155 | 173 | $this->tracer->pop(); |
156 | 174 | } |
157 | 175 | } |
@@ -163,18 +181,25 @@ discard block |
||
163 | 181 | { |
164 | 182 | $parent = $this->scope->getParent(); |
165 | 183 | |
166 | - if ($parent !== null) { |
|
167 | - try { |
|
184 | + if ($parent !== null) |
|
185 | + { |
|
186 | + try |
|
187 | + { |
|
168 | 188 | $this->tracer->push(false, ...[ |
169 | 189 | 'current scope' => $this->scope->getScopeName(), |
170 | 190 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
171 | 191 | ]); |
172 | 192 | return $parent->make($alias, $parameters, $context); |
173 | - } catch (BadScopeException $e) { |
|
174 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
193 | + } |
|
194 | + catch (BadScopeException $e) |
|
195 | + { |
|
196 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
197 | + { |
|
175 | 198 | throw $e; |
176 | 199 | } |
177 | - } catch (ContainerExceptionInterface $e) { |
|
200 | + } |
|
201 | + catch (ContainerExceptionInterface $e) |
|
202 | + { |
|
178 | 203 | $className = match (true) { |
179 | 204 | $e instanceof NotFoundException => NotFoundException::class, |
180 | 205 | default => ContainerException::class, |
@@ -183,19 +208,24 @@ discard block |
||
183 | 208 | 'Can\'t resolve `%s`.', |
184 | 209 | $alias, |
185 | 210 | )), previous: $e); |
186 | - } finally { |
|
211 | + } |
|
212 | + finally |
|
213 | + { |
|
187 | 214 | $this->tracer->pop(false); |
188 | 215 | } |
189 | 216 | } |
190 | 217 | |
191 | 218 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
192 | - try { |
|
219 | + try |
|
220 | + { |
|
193 | 221 | //No direct instructions how to construct class, make is automatically |
194 | 222 | return $this->autowire( |
195 | 223 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
196 | 224 | $parameters, |
197 | 225 | ); |
198 | - } finally { |
|
226 | + } |
|
227 | + finally |
|
228 | + { |
|
199 | 229 | $this->tracer->pop(false); |
200 | 230 | } |
201 | 231 | } |
@@ -244,16 +274,25 @@ discard block |
||
244 | 274 | mixed $target, |
245 | 275 | array $arguments, |
246 | 276 | ): mixed { |
247 | - if (\is_string($target)) { |
|
277 | + if (\is_string($target)) |
|
278 | + { |
|
248 | 279 | // Reference |
249 | 280 | $instance = $this->make($target, $arguments, $ctx->parameter); |
250 | - } else { |
|
251 | - if ($target instanceof Autowire) { |
|
281 | + } |
|
282 | + else |
|
283 | + { |
|
284 | + if ($target instanceof Autowire) |
|
285 | + { |
|
252 | 286 | $instance = $target->resolve($this, $arguments); |
253 | - } else { |
|
254 | - try { |
|
287 | + } |
|
288 | + else |
|
289 | + { |
|
290 | + try |
|
291 | + { |
|
255 | 292 | $instance = $this->invoker->invoke($target, $arguments); |
256 | - } catch (NotCallableException $e) { |
|
293 | + } |
|
294 | + catch (NotCallableException $e) |
|
295 | + { |
|
257 | 296 | throw new ContainerException( |
258 | 297 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
259 | 298 | $e->getCode(), |
@@ -263,10 +302,12 @@ discard block |
||
263 | 302 | } |
264 | 303 | |
265 | 304 | // Check scope name |
266 | - if (\is_object($instance)) { |
|
305 | + if (\is_object($instance)) |
|
306 | + { |
|
267 | 307 | $ctx->reflection = new \ReflectionClass($instance); |
268 | 308 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
269 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
309 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
310 | + { |
|
270 | 311 | throw new BadScopeException($scopeName, $instance::class); |
271 | 312 | } |
272 | 313 | } |
@@ -295,26 +336,33 @@ discard block |
||
295 | 336 | array $parameters, |
296 | 337 | ): object { |
297 | 338 | $class = $ctx->class; |
298 | - try { |
|
339 | + try |
|
340 | + { |
|
299 | 341 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
300 | - } catch (\ReflectionException $e) { |
|
342 | + } |
|
343 | + catch (\ReflectionException $e) |
|
344 | + { |
|
301 | 345 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
302 | 346 | } |
303 | 347 | |
304 | 348 | // Check scope name |
305 | 349 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
306 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
350 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
351 | + { |
|
307 | 352 | throw new BadScopeException($scope, $class); |
308 | 353 | } |
309 | 354 | |
310 | 355 | //We have to construct class using external injector when we know exact context |
311 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
356 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
357 | + { |
|
312 | 358 | $injector = $this->state->injectors[$reflection->getName()]; |
313 | 359 | |
314 | - try { |
|
360 | + try |
|
361 | + { |
|
315 | 362 | $injectorInstance = $this->container->get($injector); |
316 | 363 | |
317 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
364 | + if (!$injectorInstance instanceof InjectorInterface) |
|
365 | + { |
|
318 | 366 | throw new InjectionException( |
319 | 367 | \sprintf( |
320 | 368 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -329,7 +377,8 @@ discard block |
||
329 | 377 | * @psalm-suppress RedundantCondition |
330 | 378 | */ |
331 | 379 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
332 | - if (!$reflection->isInstance($instance)) { |
|
380 | + if (!$reflection->isInstance($instance)) |
|
381 | + { |
|
333 | 382 | throw new InjectionException( |
334 | 383 | \sprintf( |
335 | 384 | "Invalid injection response for '%s'.", |
@@ -339,12 +388,15 @@ discard block |
||
339 | 388 | } |
340 | 389 | |
341 | 390 | return $instance; |
342 | - } finally { |
|
391 | + } |
|
392 | + finally |
|
393 | + { |
|
343 | 394 | $this->state->injectors[$reflection->getName()] = $injector; |
344 | 395 | } |
345 | 396 | } |
346 | 397 | |
347 | - if (!$reflection->isInstantiable()) { |
|
398 | + if (!$reflection->isInstantiable()) |
|
399 | + { |
|
348 | 400 | $itIs = match (true) { |
349 | 401 | $reflection->isEnum() => 'Enum', |
350 | 402 | $reflection->isAbstract() => 'Abstract class', |
@@ -357,12 +409,16 @@ discard block |
||
357 | 409 | |
358 | 410 | $constructor = $reflection->getConstructor(); |
359 | 411 | |
360 | - if ($constructor !== null) { |
|
361 | - try { |
|
412 | + if ($constructor !== null) |
|
413 | + { |
|
414 | + try |
|
415 | + { |
|
362 | 416 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
363 | 417 | $this->tracer->push(true); |
364 | 418 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
365 | - } catch (ValidationException $e) { |
|
419 | + } |
|
420 | + catch (ValidationException $e) |
|
421 | + { |
|
366 | 422 | throw new ContainerException( |
367 | 423 | $this->tracer->combineTraceMessage( |
368 | 424 | \sprintf( |
@@ -372,22 +428,31 @@ discard block |
||
372 | 428 | ) |
373 | 429 | ), |
374 | 430 | ); |
375 | - } finally { |
|
431 | + } |
|
432 | + finally |
|
433 | + { |
|
376 | 434 | $this->tracer->pop(true); |
377 | 435 | $this->tracer->pop(false); |
378 | 436 | } |
379 | - try { |
|
437 | + try |
|
438 | + { |
|
380 | 439 | // Using constructor with resolved arguments |
381 | 440 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
382 | 441 | $this->tracer->push(true); |
383 | 442 | $instance = new $class(...$arguments); |
384 | - } catch (\TypeError $e) { |
|
443 | + } |
|
444 | + catch (\TypeError $e) |
|
445 | + { |
|
385 | 446 | throw new WrongTypeException($constructor, $e); |
386 | - } finally { |
|
447 | + } |
|
448 | + finally |
|
449 | + { |
|
387 | 450 | $this->tracer->pop(true); |
388 | 451 | $this->tracer->pop(false); |
389 | 452 | } |
390 | - } else { |
|
453 | + } |
|
454 | + else |
|
455 | + { |
|
391 | 456 | // No constructor specified |
392 | 457 | $instance = $reflection->newInstance(); |
393 | 458 | } |
@@ -411,13 +476,15 @@ discard block |
||
411 | 476 | $ctx->reflection ??= new \ReflectionClass($instance); |
412 | 477 | |
413 | 478 | //Declarative singletons |
414 | - if ($this->isSingleton($ctx)) { |
|
479 | + if ($this->isSingleton($ctx)) |
|
480 | + { |
|
415 | 481 | $this->state->bindings[$ctx->alias] = $instance; |
416 | 482 | } |
417 | 483 | |
418 | 484 | // Register finalizer |
419 | 485 | $finalizer = $this->getFinalizer($ctx, $instance); |
420 | - if ($finalizer !== null) { |
|
486 | + if ($finalizer !== null) |
|
487 | + { |
|
421 | 488 | $this->state->finalizers[] = $finalizer; |
422 | 489 | } |
423 | 490 | |
@@ -429,11 +496,13 @@ discard block |
||
429 | 496 | */ |
430 | 497 | private function isSingleton(Ctx $ctx): bool |
431 | 498 | { |
432 | - if ($ctx->singleton === true) { |
|
499 | + if ($ctx->singleton === true) |
|
500 | + { |
|
433 | 501 | return true; |
434 | 502 | } |
435 | 503 | |
436 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
504 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
505 | + { |
|
437 | 506 | return true; |
438 | 507 | } |
439 | 508 | |
@@ -444,7 +513,8 @@ discard block |
||
444 | 513 | { |
445 | 514 | /** @var Finalize|null $attribute */ |
446 | 515 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
447 | - if ($attribute === null) { |
|
516 | + if ($attribute === null) |
|
517 | + { |
|
448 | 518 | return null; |
449 | 519 | } |
450 | 520 |