@@ -63,7 +63,7 @@ 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 | 67 | ); |
68 | 68 | } |
69 | 69 | |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | ); |
76 | 76 | |
77 | 77 | $this->resolveClosure( |
78 | - static function (TestTrait $enum) {}, |
|
78 | + static function (TestTrait $enum){}, |
|
79 | 79 | ); |
80 | 80 | } |
81 | 81 | } |
@@ -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 | } |
@@ -40,7 +40,7 @@ |
||
40 | 40 | public readonly string $binder = Binder::class, |
41 | 41 | public readonly string $invoker = Invoker::class, |
42 | 42 | public readonly string $tracer = Tracer::class, |
43 | - ) { |
|
43 | + ){ |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | public function getIterator(): Traversable |
@@ -13,7 +13,7 @@ |
||
13 | 13 | public function __construct( |
14 | 14 | \ReflectionFunctionAbstract $reflection, |
15 | 15 | private readonly string $parameter |
16 | - ) { |
|
16 | + ){ |
|
17 | 17 | $pattern = "Invalid argument value type for the `$parameter` parameter when validating arguments for `%s`."; |
18 | 18 | parent::__construct($this->renderFunctionAndParameter($reflection, $pattern)); |
19 | 19 | } |
@@ -131,9 +131,9 @@ |
||
131 | 131 | { |
132 | 132 | $container = new Container(); |
133 | 133 | |
134 | - try { |
|
134 | + try{ |
|
135 | 135 | $container->get('invalid'); |
136 | - } catch (ContainerException $e) { |
|
136 | + }catch (ContainerException $e){ |
|
137 | 137 | $this->assertSame( |
138 | 138 | <<<MARKDOWN |
139 | 139 | Can't resolve `invalid`: undefined class or binding `invalid`. |
@@ -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, |
@@ -19,14 +19,14 @@ discard block |
||
19 | 19 | |
20 | 20 | public function __construct( |
21 | 21 | public array $info = [], |
22 | - ) { |
|
22 | + ){ |
|
23 | 23 | $this->alias = $info['alias'] ?? null; |
24 | 24 | } |
25 | 25 | |
26 | 26 | public function __toString(): string |
27 | 27 | { |
28 | 28 | $info = []; |
29 | - foreach ($this->info as $key => $item) { |
|
29 | + foreach ($this->info as $key => $item){ |
|
30 | 30 | $info[] = "$key: {$this->stringifyValue($item)}"; |
31 | 31 | } |
32 | 32 | return \implode("\n", $info); |
@@ -39,8 +39,8 @@ discard block |
||
39 | 39 | \is_scalar($item) => \var_export($item, true), |
40 | 40 | $item instanceof \Closure => $this->renderClosureSignature(new \ReflectionFunction($item)), |
41 | 41 | $item instanceof \ReflectionFunctionAbstract => $this->renderClosureSignature($item), |
42 | - $item instanceof \UnitEnum => $item::class . "::$item->name", |
|
43 | - \is_object($item) => 'instance of ' . $item::class, |
|
42 | + $item instanceof \UnitEnum => $item::class."::$item->name", |
|
43 | + \is_object($item) => 'instance of '.$item::class, |
|
44 | 44 | \is_array($item) => $this->renderArray($item), |
45 | 45 | default => \gettype($item), |
46 | 46 | }; |
@@ -48,14 +48,14 @@ discard block |
||
48 | 48 | |
49 | 49 | private function renderArray(array $array, int $level = 0): string |
50 | 50 | { |
51 | - if ($array === []) { |
|
51 | + if ($array === []){ |
|
52 | 52 | return '[]'; |
53 | 53 | } |
54 | - if ($level >= self::ARRAY_MAX_LEVEL) { |
|
54 | + if ($level >= self::ARRAY_MAX_LEVEL){ |
|
55 | 55 | return 'array'; |
56 | 56 | } |
57 | 57 | $result = []; |
58 | - foreach ($array as $key => $value) { |
|
58 | + foreach ($array as $key => $value){ |
|
59 | 59 | $result[] = \sprintf( |
60 | 60 | '%s: %s', |
61 | 61 | $key, |
@@ -66,6 +66,6 @@ discard block |
||
66 | 66 | } |
67 | 67 | |
68 | 68 | $pad = \str_repeat(' ', $level); |
69 | - return "[\n $pad" . \implode(",\n $pad", $result) . "\n$pad]"; |
|
69 | + return "[\n $pad".\implode(",\n $pad", $result)."\n$pad]"; |
|
70 | 70 | } |
71 | 71 | } |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | |
19 | 19 | public function __toString(): string |
20 | 20 | { |
21 | - return $this->traces === [] ? '' : "Container trace list:\n" . $this->renderTraceList($this->traces); |
|
21 | + return $this->traces === [] ? '' : "Container trace list:\n".$this->renderTraceList($this->traces); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -34,19 +34,19 @@ 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 | $this->traces[] = $trace === null ? [] : [$trace]; |
39 | - } elseif ($trace !== null) { |
|
39 | + } elseif ($trace !== null){ |
|
40 | 40 | $this->traces[\array_key_last($this->traces)][] = $trace; |
41 | 41 | } |
42 | 42 | } |
43 | 43 | |
44 | 44 | public function pop(bool $previousLevel = false): void |
45 | 45 | { |
46 | - if ($this->traces === []) { |
|
46 | + if ($this->traces === []){ |
|
47 | 47 | return; |
48 | 48 | } |
49 | - if ($previousLevel) { |
|
49 | + if ($previousLevel){ |
|
50 | 50 | \array_pop($this->traces); |
51 | 51 | return; |
52 | 52 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | { |
68 | 68 | $result = []; |
69 | 69 | $i = 0; |
70 | - foreach ($blocks as $block) { |
|
70 | + foreach ($blocks as $block){ |
|
71 | 71 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
72 | 72 | } |
73 | 73 | return \implode("\n", $result); |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | // Separator |
88 | 88 | $s = "\n"; |
89 | 89 | $nexPrefix = "$s$padding "; |
90 | - foreach ($items as $item) { |
|
91 | - $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
|
90 | + foreach ($items as $item){ |
|
91 | + $result[] = $firstPrefix.\str_replace($s, $nexPrefix, (string)$item); |
|
92 | 92 | } |
93 | 93 | return $result; |
94 | 94 | } |
@@ -34,19 +34,24 @@ discard block |
||
34 | 34 | public function push(bool $nextLevel, mixed ...$details): void |
35 | 35 | { |
36 | 36 | $trace = $details === [] ? null : new Trace($details); |
37 | - if ($nextLevel || $this->traces === []) { |
|
37 | + if ($nextLevel || $this->traces === []) |
|
38 | + { |
|
38 | 39 | $this->traces[] = $trace === null ? [] : [$trace]; |
39 | - } elseif ($trace !== null) { |
|
40 | + } |
|
41 | + elseif ($trace !== null) |
|
42 | + { |
|
40 | 43 | $this->traces[\array_key_last($this->traces)][] = $trace; |
41 | 44 | } |
42 | 45 | } |
43 | 46 | |
44 | 47 | public function pop(bool $previousLevel = false): void |
45 | 48 | { |
46 | - if ($this->traces === []) { |
|
49 | + if ($this->traces === []) |
|
50 | + { |
|
47 | 51 | return; |
48 | 52 | } |
49 | - if ($previousLevel) { |
|
53 | + if ($previousLevel) |
|
54 | + { |
|
50 | 55 | \array_pop($this->traces); |
51 | 56 | return; |
52 | 57 | } |
@@ -67,7 +72,8 @@ discard block |
||
67 | 72 | { |
68 | 73 | $result = []; |
69 | 74 | $i = 0; |
70 | - foreach ($blocks as $block) { |
|
75 | + foreach ($blocks as $block) |
|
76 | + { |
|
71 | 77 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
72 | 78 | } |
73 | 79 | return \implode("\n", $result); |
@@ -87,7 +93,8 @@ discard block |
||
87 | 93 | // Separator |
88 | 94 | $s = "\n"; |
89 | 95 | $nexPrefix = "$s$padding "; |
90 | - foreach ($items as $item) { |
|
96 | + foreach ($items as $item) |
|
97 | + { |
|
91 | 98 | $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
92 | 99 | } |
93 | 100 | return $result; |
@@ -54,35 +54,35 @@ 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 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
59 | - try { |
|
59 | + try{ |
|
60 | 60 | //No direct instructions how to construct class, make is automatically |
61 | 61 | return $this->autowire($alias, $parameters, $context); |
62 | - } finally { |
|
62 | + }finally{ |
|
63 | 63 | $this->tracer->pop(false); |
64 | 64 | } |
65 | 65 | } |
66 | 66 | |
67 | 67 | $binding = $this->state->bindings[$alias]; |
68 | - try { |
|
68 | + try{ |
|
69 | 69 | $this->tracer->push(false, action: 'resolve from binding', alias: $alias, context: $context, binding: $binding); |
70 | 70 | $this->tracer->push(true); |
71 | 71 | |
72 | - if (\is_object($binding)) { |
|
73 | - if ($binding::class === WeakReference::class) { |
|
74 | - if ($binding->get() === null && \class_exists($alias)) { |
|
75 | - try { |
|
72 | + if (\is_object($binding)){ |
|
73 | + if ($binding::class === WeakReference::class){ |
|
74 | + if ($binding->get() === null && \class_exists($alias)){ |
|
75 | + try{ |
|
76 | 76 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
77 | 77 | $object = $this->createInstance($alias, $parameters, $context); |
78 | 78 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
79 | - } catch (\Throwable) { |
|
79 | + }catch (\Throwable){ |
|
80 | 80 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
81 | 81 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
82 | 82 | $this->tracer->getRootAlias(), |
83 | 83 | $alias, |
84 | 84 | ))); |
85 | - } finally { |
|
85 | + }finally{ |
|
86 | 86 | $this->tracer->pop(); |
87 | 87 | } |
88 | 88 | } |
@@ -92,26 +92,26 @@ discard block |
||
92 | 92 | return $binding; |
93 | 93 | } |
94 | 94 | |
95 | - if (\is_string($binding)) { |
|
95 | + if (\is_string($binding)){ |
|
96 | 96 | //Binding is pointing to something else |
97 | 97 | return $this->make($binding, $parameters, $context); |
98 | 98 | } |
99 | 99 | |
100 | 100 | unset($this->state->bindings[$alias]); |
101 | - try { |
|
101 | + try{ |
|
102 | 102 | $instance = $binding[0] === $alias |
103 | 103 | ? $this->autowire($alias, $parameters, $context) |
104 | 104 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
105 | - } finally { |
|
105 | + }finally{ |
|
106 | 106 | /** @psalm-var class-string $alias */ |
107 | 107 | $this->state->bindings[$alias] = $binding; |
108 | 108 | } |
109 | - } finally { |
|
109 | + }finally{ |
|
110 | 110 | $this->tracer->pop(true); |
111 | 111 | $this->tracer->pop(false); |
112 | 112 | } |
113 | 113 | |
114 | - if ($binding[1]) { |
|
114 | + if ($binding[1]){ |
|
115 | 115 | // Indicates singleton |
116 | 116 | /** @psalm-var class-string $alias */ |
117 | 117 | $this->state->bindings[$alias] = $instance; |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | */ |
131 | 131 | private function autowire(string $class, array $parameters, string $context = null): object |
132 | 132 | { |
133 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
133 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])){ |
|
134 | 134 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
135 | 135 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
136 | 136 | $this->tracer->getRootAlias(), |
@@ -157,18 +157,18 @@ discard block |
||
157 | 157 | array $parameters, |
158 | 158 | string $context = null |
159 | 159 | ): mixed { |
160 | - if (\is_string($target)) { |
|
160 | + if (\is_string($target)){ |
|
161 | 161 | // Reference |
162 | 162 | return $this->make($target, $parameters, $context); |
163 | 163 | } |
164 | 164 | |
165 | - if ($target instanceof Autowire) { |
|
165 | + if ($target instanceof Autowire){ |
|
166 | 166 | return $target->resolve($this, $parameters); |
167 | 167 | } |
168 | 168 | |
169 | - try { |
|
169 | + try{ |
|
170 | 170 | return $this->invoker->invoke($target, $parameters); |
171 | - } catch (NotCallableException $e) { |
|
171 | + }catch (NotCallableException $e){ |
|
172 | 172 | throw new ContainerException( |
173 | 173 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
174 | 174 | $e->getCode(), |
@@ -192,20 +192,20 @@ discard block |
||
192 | 192 | */ |
193 | 193 | private function createInstance(string $class, array $parameters, string $context = null): object |
194 | 194 | { |
195 | - try { |
|
195 | + try{ |
|
196 | 196 | $reflection = new \ReflectionClass($class); |
197 | - } catch (\ReflectionException $e) { |
|
197 | + }catch (\ReflectionException $e){ |
|
198 | 198 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
199 | 199 | } |
200 | 200 | |
201 | 201 | //We have to construct class using external injector when we know exact context |
202 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
202 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
203 | 203 | $injector = $this->state->injectors[$reflection->getName()]; |
204 | 204 | |
205 | - try { |
|
205 | + try{ |
|
206 | 206 | $injectorInstance = $this->container->get($injector); |
207 | 207 | |
208 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
208 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
209 | 209 | throw new InjectionException( |
210 | 210 | \sprintf( |
211 | 211 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | |
218 | 218 | /** @var InjectorInterface<TObject> $injectorInstance */ |
219 | 219 | $instance = $injectorInstance->createInjection($reflection, $context); |
220 | - if (!$reflection->isInstance($instance)) { |
|
220 | + if (!$reflection->isInstance($instance)){ |
|
221 | 221 | throw new InjectionException( |
222 | 222 | \sprintf( |
223 | 223 | "Invalid injection response for '%s'.", |
@@ -227,12 +227,12 @@ discard block |
||
227 | 227 | } |
228 | 228 | |
229 | 229 | return $instance; |
230 | - } finally { |
|
230 | + }finally{ |
|
231 | 231 | $this->state->injectors[$reflection->getName()] = $injector; |
232 | 232 | } |
233 | 233 | } |
234 | 234 | |
235 | - if (!$reflection->isInstantiable()) { |
|
235 | + if (!$reflection->isInstantiable()){ |
|
236 | 236 | $itIs = match (true) { |
237 | 237 | $reflection->isEnum() => 'Enum', |
238 | 238 | $reflection->isAbstract() => 'Abstract class', |
@@ -245,12 +245,12 @@ discard block |
||
245 | 245 | |
246 | 246 | $constructor = $reflection->getConstructor(); |
247 | 247 | |
248 | - if ($constructor !== null) { |
|
249 | - try { |
|
248 | + if ($constructor !== null){ |
|
249 | + try{ |
|
250 | 250 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
251 | 251 | $this->tracer->push(true); |
252 | 252 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
253 | - } catch (ValidationException $e) { |
|
253 | + }catch (ValidationException $e){ |
|
254 | 254 | throw new ContainerException( |
255 | 255 | $this->tracer->combineTraceMessage( |
256 | 256 | \sprintf( |
@@ -260,22 +260,22 @@ discard block |
||
260 | 260 | ) |
261 | 261 | ), |
262 | 262 | ); |
263 | - } finally { |
|
263 | + }finally{ |
|
264 | 264 | $this->tracer->pop(true); |
265 | 265 | $this->tracer->pop(false); |
266 | 266 | } |
267 | - try { |
|
267 | + try{ |
|
268 | 268 | // Using constructor with resolved arguments |
269 | 269 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
270 | 270 | $this->tracer->push(true); |
271 | 271 | $instance = new $class(...$arguments); |
272 | - } catch (\TypeError $e) { |
|
272 | + }catch (\TypeError $e){ |
|
273 | 273 | throw new WrongTypeException($constructor, $e); |
274 | - } finally { |
|
274 | + }finally{ |
|
275 | 275 | $this->tracer->pop(true); |
276 | 276 | $this->tracer->pop(false); |
277 | 277 | } |
278 | - } else { |
|
278 | + }else{ |
|
279 | 279 | // No constructor specified |
280 | 280 | $instance = $reflection->newInstance(); |
281 | 281 | } |
@@ -293,9 +293,9 @@ discard block |
||
293 | 293 | private function registerInstance(object $instance, array $parameters): object |
294 | 294 | { |
295 | 295 | //Declarative singletons (only when class received via direct get) |
296 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
296 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
297 | 297 | $alias = $instance::class; |
298 | - if (!isset($this->state->bindings[$alias])) { |
|
298 | + if (!isset($this->state->bindings[$alias])){ |
|
299 | 299 | $this->state->bindings[$alias] = $instance; |
300 | 300 | } |
301 | 301 | } |
@@ -54,35 +54,48 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
56 | 56 | { |
57 | - if (!isset($this->state->bindings[$alias])) { |
|
57 | + if (!isset($this->state->bindings[$alias])) |
|
58 | + { |
|
58 | 59 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
59 | - try { |
|
60 | + try |
|
61 | + { |
|
60 | 62 | //No direct instructions how to construct class, make is automatically |
61 | 63 | return $this->autowire($alias, $parameters, $context); |
62 | - } finally { |
|
64 | + } |
|
65 | + finally |
|
66 | + { |
|
63 | 67 | $this->tracer->pop(false); |
64 | 68 | } |
65 | 69 | } |
66 | 70 | |
67 | 71 | $binding = $this->state->bindings[$alias]; |
68 | - try { |
|
72 | + try |
|
73 | + { |
|
69 | 74 | $this->tracer->push(false, action: 'resolve from binding', alias: $alias, context: $context, binding: $binding); |
70 | 75 | $this->tracer->push(true); |
71 | 76 | |
72 | - if (\is_object($binding)) { |
|
73 | - if ($binding::class === WeakReference::class) { |
|
74 | - if ($binding->get() === null && \class_exists($alias)) { |
|
75 | - try { |
|
77 | + if (\is_object($binding)) |
|
78 | + { |
|
79 | + if ($binding::class === WeakReference::class) |
|
80 | + { |
|
81 | + if ($binding->get() === null && \class_exists($alias)) |
|
82 | + { |
|
83 | + try |
|
84 | + { |
|
76 | 85 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
77 | 86 | $object = $this->createInstance($alias, $parameters, $context); |
78 | 87 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
79 | - } catch (\Throwable) { |
|
88 | + } |
|
89 | + catch (\Throwable) |
|
90 | + { |
|
80 | 91 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
81 | 92 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
82 | 93 | $this->tracer->getRootAlias(), |
83 | 94 | $alias, |
84 | 95 | ))); |
85 | - } finally { |
|
96 | + } |
|
97 | + finally |
|
98 | + { |
|
86 | 99 | $this->tracer->pop(); |
87 | 100 | } |
88 | 101 | } |
@@ -92,26 +105,33 @@ discard block |
||
92 | 105 | return $binding; |
93 | 106 | } |
94 | 107 | |
95 | - if (\is_string($binding)) { |
|
108 | + if (\is_string($binding)) |
|
109 | + { |
|
96 | 110 | //Binding is pointing to something else |
97 | 111 | return $this->make($binding, $parameters, $context); |
98 | 112 | } |
99 | 113 | |
100 | 114 | unset($this->state->bindings[$alias]); |
101 | - try { |
|
115 | + try |
|
116 | + { |
|
102 | 117 | $instance = $binding[0] === $alias |
103 | 118 | ? $this->autowire($alias, $parameters, $context) |
104 | 119 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
105 | - } finally { |
|
120 | + } |
|
121 | + finally |
|
122 | + { |
|
106 | 123 | /** @psalm-var class-string $alias */ |
107 | 124 | $this->state->bindings[$alias] = $binding; |
108 | 125 | } |
109 | - } finally { |
|
126 | + } |
|
127 | + finally |
|
128 | + { |
|
110 | 129 | $this->tracer->pop(true); |
111 | 130 | $this->tracer->pop(false); |
112 | 131 | } |
113 | 132 | |
114 | - if ($binding[1]) { |
|
133 | + if ($binding[1]) |
|
134 | + { |
|
115 | 135 | // Indicates singleton |
116 | 136 | /** @psalm-var class-string $alias */ |
117 | 137 | $this->state->bindings[$alias] = $instance; |
@@ -130,7 +150,8 @@ discard block |
||
130 | 150 | */ |
131 | 151 | private function autowire(string $class, array $parameters, string $context = null): object |
132 | 152 | { |
133 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
153 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])) |
|
154 | + { |
|
134 | 155 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
135 | 156 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
136 | 157 | $this->tracer->getRootAlias(), |
@@ -157,18 +178,23 @@ discard block |
||
157 | 178 | array $parameters, |
158 | 179 | string $context = null |
159 | 180 | ): mixed { |
160 | - if (\is_string($target)) { |
|
181 | + if (\is_string($target)) |
|
182 | + { |
|
161 | 183 | // Reference |
162 | 184 | return $this->make($target, $parameters, $context); |
163 | 185 | } |
164 | 186 | |
165 | - if ($target instanceof Autowire) { |
|
187 | + if ($target instanceof Autowire) |
|
188 | + { |
|
166 | 189 | return $target->resolve($this, $parameters); |
167 | 190 | } |
168 | 191 | |
169 | - try { |
|
192 | + try |
|
193 | + { |
|
170 | 194 | return $this->invoker->invoke($target, $parameters); |
171 | - } catch (NotCallableException $e) { |
|
195 | + } |
|
196 | + catch (NotCallableException $e) |
|
197 | + { |
|
172 | 198 | throw new ContainerException( |
173 | 199 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
174 | 200 | $e->getCode(), |
@@ -192,20 +218,26 @@ discard block |
||
192 | 218 | */ |
193 | 219 | private function createInstance(string $class, array $parameters, string $context = null): object |
194 | 220 | { |
195 | - try { |
|
221 | + try |
|
222 | + { |
|
196 | 223 | $reflection = new \ReflectionClass($class); |
197 | - } catch (\ReflectionException $e) { |
|
224 | + } |
|
225 | + catch (\ReflectionException $e) |
|
226 | + { |
|
198 | 227 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
199 | 228 | } |
200 | 229 | |
201 | 230 | //We have to construct class using external injector when we know exact context |
202 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
231 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
232 | + { |
|
203 | 233 | $injector = $this->state->injectors[$reflection->getName()]; |
204 | 234 | |
205 | - try { |
|
235 | + try |
|
236 | + { |
|
206 | 237 | $injectorInstance = $this->container->get($injector); |
207 | 238 | |
208 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
239 | + if (!$injectorInstance instanceof InjectorInterface) |
|
240 | + { |
|
209 | 241 | throw new InjectionException( |
210 | 242 | \sprintf( |
211 | 243 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -217,7 +249,8 @@ discard block |
||
217 | 249 | |
218 | 250 | /** @var InjectorInterface<TObject> $injectorInstance */ |
219 | 251 | $instance = $injectorInstance->createInjection($reflection, $context); |
220 | - if (!$reflection->isInstance($instance)) { |
|
252 | + if (!$reflection->isInstance($instance)) |
|
253 | + { |
|
221 | 254 | throw new InjectionException( |
222 | 255 | \sprintf( |
223 | 256 | "Invalid injection response for '%s'.", |
@@ -227,12 +260,15 @@ discard block |
||
227 | 260 | } |
228 | 261 | |
229 | 262 | return $instance; |
230 | - } finally { |
|
263 | + } |
|
264 | + finally |
|
265 | + { |
|
231 | 266 | $this->state->injectors[$reflection->getName()] = $injector; |
232 | 267 | } |
233 | 268 | } |
234 | 269 | |
235 | - if (!$reflection->isInstantiable()) { |
|
270 | + if (!$reflection->isInstantiable()) |
|
271 | + { |
|
236 | 272 | $itIs = match (true) { |
237 | 273 | $reflection->isEnum() => 'Enum', |
238 | 274 | $reflection->isAbstract() => 'Abstract class', |
@@ -245,12 +281,16 @@ discard block |
||
245 | 281 | |
246 | 282 | $constructor = $reflection->getConstructor(); |
247 | 283 | |
248 | - if ($constructor !== null) { |
|
249 | - try { |
|
284 | + if ($constructor !== null) |
|
285 | + { |
|
286 | + try |
|
287 | + { |
|
250 | 288 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
251 | 289 | $this->tracer->push(true); |
252 | 290 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
253 | - } catch (ValidationException $e) { |
|
291 | + } |
|
292 | + catch (ValidationException $e) |
|
293 | + { |
|
254 | 294 | throw new ContainerException( |
255 | 295 | $this->tracer->combineTraceMessage( |
256 | 296 | \sprintf( |
@@ -260,22 +300,31 @@ discard block |
||
260 | 300 | ) |
261 | 301 | ), |
262 | 302 | ); |
263 | - } finally { |
|
303 | + } |
|
304 | + finally |
|
305 | + { |
|
264 | 306 | $this->tracer->pop(true); |
265 | 307 | $this->tracer->pop(false); |
266 | 308 | } |
267 | - try { |
|
309 | + try |
|
310 | + { |
|
268 | 311 | // Using constructor with resolved arguments |
269 | 312 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
270 | 313 | $this->tracer->push(true); |
271 | 314 | $instance = new $class(...$arguments); |
272 | - } catch (\TypeError $e) { |
|
315 | + } |
|
316 | + catch (\TypeError $e) |
|
317 | + { |
|
273 | 318 | throw new WrongTypeException($constructor, $e); |
274 | - } finally { |
|
319 | + } |
|
320 | + finally |
|
321 | + { |
|
275 | 322 | $this->tracer->pop(true); |
276 | 323 | $this->tracer->pop(false); |
277 | 324 | } |
278 | - } else { |
|
325 | + } |
|
326 | + else |
|
327 | + { |
|
279 | 328 | // No constructor specified |
280 | 329 | $instance = $reflection->newInstance(); |
281 | 330 | } |
@@ -293,9 +342,11 @@ discard block |
||
293 | 342 | private function registerInstance(object $instance, array $parameters): object |
294 | 343 | { |
295 | 344 | //Declarative singletons (only when class received via direct get) |
296 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
345 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
346 | + { |
|
297 | 347 | $alias = $instance::class; |
298 | - if (!isset($this->state->bindings[$alias])) { |
|
348 | + if (!isset($this->state->bindings[$alias])) |
|
349 | + { |
|
299 | 350 | $this->state->bindings[$alias] = $instance; |
300 | 351 | } |
301 | 352 | } |