@@ -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 | } |
@@ -18,13 +18,13 @@ discard block |
||
18 | 18 | public function __construct( |
19 | 19 | public readonly string $alias, |
20 | 20 | public array $info, |
21 | - ) { |
|
21 | + ){ |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | public function __toString(): string |
25 | 25 | { |
26 | 26 | $info = [$this->alias]; |
27 | - foreach ($this->info as $key => $item) { |
|
27 | + foreach ($this->info as $key => $item){ |
|
28 | 28 | $info[] = "$key: {$this->stringifyValue($item)}"; |
29 | 29 | } |
30 | 30 | return \implode("\n", $info); |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | \is_string($item) => "'$item'", |
37 | 37 | \is_scalar($item) => (string)$item, |
38 | 38 | $item instanceof Closure => $this->renderClosureSignature(new ReflectionFunction($item)), |
39 | - \is_object($item) => 'instance of ' . $item::class, |
|
39 | + \is_object($item) => 'instance of '.$item::class, |
|
40 | 40 | default => \gettype($item), |
41 | 41 | }; |
42 | 42 | } |
@@ -24,7 +24,8 @@ |
||
24 | 24 | public function __toString(): string |
25 | 25 | { |
26 | 26 | $info = [$this->alias]; |
27 | - foreach ($this->info as $key => $item) { |
|
27 | + foreach ($this->info as $key => $item) |
|
28 | + { |
|
28 | 29 | $info[] = "$key: {$this->stringifyValue($item)}"; |
29 | 30 | } |
30 | 31 | return \implode("\n", $info); |
@@ -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,26 +34,26 @@ discard block |
||
34 | 34 | public function push(string $alias, bool $nextLevel = false, mixed ...$details): void |
35 | 35 | { |
36 | 36 | $trace = new Trace($alias, $details); |
37 | - if ($nextLevel || $this->traces === []) { |
|
37 | + if ($nextLevel || $this->traces === []){ |
|
38 | 38 | $this->traces[] = [$trace]; |
39 | - } else { |
|
39 | + }else{ |
|
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 | } |
53 | 53 | $key = \array_key_last($this->traces); |
54 | 54 | $list = &$this->traces[$key]; |
55 | 55 | \array_pop($list); |
56 | - if ($list === []) { |
|
56 | + if ($list === []){ |
|
57 | 57 | unset($this->traces[$key]); |
58 | 58 | } |
59 | 59 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | { |
71 | 71 | $result = []; |
72 | 72 | $i = 0; |
73 | - foreach ($blocks as $block) { |
|
73 | + foreach ($blocks as $block){ |
|
74 | 74 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
75 | 75 | } |
76 | 76 | return \implode("\n", $result); |
@@ -90,8 +90,8 @@ discard block |
||
90 | 90 | // Separator |
91 | 91 | $s = "\n"; |
92 | 92 | $nexPrefix = "$s$padding "; |
93 | - foreach ($items as $item) { |
|
94 | - $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
|
93 | + foreach ($items as $item){ |
|
94 | + $result[] = $firstPrefix.\str_replace($s, $nexPrefix, (string)$item); |
|
95 | 95 | } |
96 | 96 | return $result; |
97 | 97 | } |
@@ -34,26 +34,32 @@ discard block |
||
34 | 34 | public function push(string $alias, bool $nextLevel = false, mixed ...$details): void |
35 | 35 | { |
36 | 36 | $trace = new Trace($alias, $details); |
37 | - if ($nextLevel || $this->traces === []) { |
|
37 | + if ($nextLevel || $this->traces === []) |
|
38 | + { |
|
38 | 39 | $this->traces[] = [$trace]; |
39 | - } else { |
|
40 | + } |
|
41 | + else |
|
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 | } |
53 | 58 | $key = \array_key_last($this->traces); |
54 | 59 | $list = &$this->traces[$key]; |
55 | 60 | \array_pop($list); |
56 | - if ($list === []) { |
|
61 | + if ($list === []) |
|
62 | + { |
|
57 | 63 | unset($this->traces[$key]); |
58 | 64 | } |
59 | 65 | } |
@@ -70,7 +76,8 @@ discard block |
||
70 | 76 | { |
71 | 77 | $result = []; |
72 | 78 | $i = 0; |
73 | - foreach ($blocks as $block) { |
|
79 | + foreach ($blocks as $block) |
|
80 | + { |
|
74 | 81 | \array_push($result, ...$this->blockToStringList($block, $i++)); |
75 | 82 | } |
76 | 83 | return \implode("\n", $result); |
@@ -90,7 +97,8 @@ discard block |
||
90 | 97 | // Separator |
91 | 98 | $s = "\n"; |
92 | 99 | $nexPrefix = "$s$padding "; |
93 | - foreach ($items as $item) { |
|
100 | + foreach ($items as $item) |
|
101 | + { |
|
94 | 102 | $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item); |
95 | 103 | } |
96 | 104 | return $result; |
@@ -143,9 +143,9 @@ |
||
143 | 143 | { |
144 | 144 | $container = new Container(); |
145 | 145 | |
146 | - try { |
|
146 | + try{ |
|
147 | 147 | $container->get('invalid'); |
148 | - } catch (ContainerException $e) { |
|
148 | + }catch (ContainerException $e){ |
|
149 | 149 | $this->assertSame( |
150 | 150 | $e->getMessage(), |
151 | 151 | <<<MARKDOWN |
@@ -143,9 +143,12 @@ |
||
143 | 143 | { |
144 | 144 | $container = new Container(); |
145 | 145 | |
146 | - try { |
|
146 | + try |
|
147 | + { |
|
147 | 148 | $container->get('invalid'); |
148 | - } catch (ContainerException $e) { |
|
149 | + } |
|
150 | + catch (ContainerException $e) |
|
151 | + { |
|
149 | 152 | $this->assertSame( |
150 | 153 | $e->getMessage(), |
151 | 154 | <<<MARKDOWN |
@@ -54,34 +54,34 @@ 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($alias, false, source: 'autowiring', 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($alias, false, source: 'binding', binding: $binding, context: $context); |
70 | 70 | |
71 | - if (\is_object($binding)) { |
|
72 | - if ($binding::class === WeakReference::class) { |
|
73 | - if ($binding->get() === null && \class_exists($alias)) { |
|
74 | - try { |
|
71 | + if (\is_object($binding)){ |
|
72 | + if ($binding::class === WeakReference::class){ |
|
73 | + if ($binding->get() === null && \class_exists($alias)){ |
|
74 | + try{ |
|
75 | 75 | $this->tracer->push($alias, false, source: WeakReference::class, context: $context); |
76 | 76 | $object = $this->createInstance($alias, $parameters, $context); |
77 | 77 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
78 | - } catch (\Throwable) { |
|
78 | + }catch (\Throwable){ |
|
79 | 79 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
80 | 80 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
81 | 81 | $this->tracer->getRootAlias(), |
82 | 82 | $alias, |
83 | 83 | ))); |
84 | - } finally { |
|
84 | + }finally{ |
|
85 | 85 | $this->tracer->pop(); |
86 | 86 | } |
87 | 87 | } |
@@ -91,25 +91,25 @@ discard block |
||
91 | 91 | return $binding; |
92 | 92 | } |
93 | 93 | |
94 | - if (\is_string($binding)) { |
|
94 | + if (\is_string($binding)){ |
|
95 | 95 | //Binding is pointing to something else |
96 | 96 | return $this->make($binding, $parameters, $context); |
97 | 97 | } |
98 | 98 | |
99 | 99 | unset($this->state->bindings[$alias]); |
100 | - try { |
|
100 | + try{ |
|
101 | 101 | $instance = $binding[0] === $alias |
102 | 102 | ? $this->autowire($alias, $parameters, $context) |
103 | 103 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
104 | - } finally { |
|
104 | + }finally{ |
|
105 | 105 | /** @psalm-var class-string $alias */ |
106 | 106 | $this->state->bindings[$alias] = $binding; |
107 | 107 | } |
108 | - } finally { |
|
108 | + }finally{ |
|
109 | 109 | $this->tracer->pop(false); |
110 | 110 | } |
111 | 111 | |
112 | - if ($binding[1]) { |
|
112 | + if ($binding[1]){ |
|
113 | 113 | // Indicates singleton |
114 | 114 | /** @psalm-var class-string $alias */ |
115 | 115 | $this->state->bindings[$alias] = $instance; |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | */ |
129 | 129 | private function autowire(string $class, array $parameters, string $context = null): object |
130 | 130 | { |
131 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
131 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])){ |
|
132 | 132 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
133 | 133 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
134 | 134 | $this->tracer->getRootAlias(), |
@@ -155,18 +155,18 @@ discard block |
||
155 | 155 | array $parameters, |
156 | 156 | string $context = null |
157 | 157 | ): mixed { |
158 | - if (\is_string($target)) { |
|
158 | + if (\is_string($target)){ |
|
159 | 159 | // Reference |
160 | 160 | return $this->make($target, $parameters, $context); |
161 | 161 | } |
162 | 162 | |
163 | - if ($target instanceof Autowire) { |
|
163 | + if ($target instanceof Autowire){ |
|
164 | 164 | return $target->resolve($this, $parameters); |
165 | 165 | } |
166 | 166 | |
167 | - try { |
|
167 | + try{ |
|
168 | 168 | return $this->invoker->invoke($target, $parameters); |
169 | - } catch (NotCallableException $e) { |
|
169 | + }catch (NotCallableException $e){ |
|
170 | 170 | throw new ContainerException( |
171 | 171 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
172 | 172 | $e->getCode(), |
@@ -190,20 +190,20 @@ discard block |
||
190 | 190 | */ |
191 | 191 | private function createInstance(string $class, array $parameters, string $context = null): object |
192 | 192 | { |
193 | - try { |
|
193 | + try{ |
|
194 | 194 | $reflection = new \ReflectionClass($class); |
195 | - } catch (\ReflectionException $e) { |
|
195 | + }catch (\ReflectionException $e){ |
|
196 | 196 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
197 | 197 | } |
198 | 198 | |
199 | 199 | //We have to construct class using external injector when we know exact context |
200 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
200 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
201 | 201 | $injector = $this->state->injectors[$reflection->getName()]; |
202 | 202 | |
203 | - try { |
|
203 | + try{ |
|
204 | 204 | $injectorInstance = $this->container->get($injector); |
205 | 205 | |
206 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
206 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
207 | 207 | throw new InjectionException( |
208 | 208 | \sprintf( |
209 | 209 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | |
216 | 216 | /** @var InjectorInterface<TObject> $injectorInstance */ |
217 | 217 | $instance = $injectorInstance->createInjection($reflection, $context); |
218 | - if (!$reflection->isInstance($instance)) { |
|
218 | + if (!$reflection->isInstance($instance)){ |
|
219 | 219 | throw new InjectionException( |
220 | 220 | \sprintf( |
221 | 221 | "Invalid injection response for '%s'.", |
@@ -225,12 +225,12 @@ discard block |
||
225 | 225 | } |
226 | 226 | |
227 | 227 | return $instance; |
228 | - } finally { |
|
228 | + }finally{ |
|
229 | 229 | $this->state->injectors[$reflection->getName()] = $injector; |
230 | 230 | } |
231 | 231 | } |
232 | 232 | |
233 | - if (!$reflection->isInstantiable()) { |
|
233 | + if (!$reflection->isInstantiable()){ |
|
234 | 234 | $itIs = match (true) { |
235 | 235 | $reflection->isEnum() => 'Enum', |
236 | 236 | $reflection->isAbstract() => 'Abstract class', |
@@ -243,10 +243,10 @@ discard block |
||
243 | 243 | |
244 | 244 | $constructor = $reflection->getConstructor(); |
245 | 245 | |
246 | - if ($constructor !== null) { |
|
247 | - try { |
|
246 | + if ($constructor !== null){ |
|
247 | + try{ |
|
248 | 248 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
249 | - } catch (ValidationException $e) { |
|
249 | + }catch (ValidationException $e){ |
|
250 | 250 | throw new ContainerException( |
251 | 251 | $this->tracer->combineTraceMessage( |
252 | 252 | \sprintf( |
@@ -257,16 +257,16 @@ discard block |
||
257 | 257 | ), |
258 | 258 | ); |
259 | 259 | } |
260 | - try { |
|
260 | + try{ |
|
261 | 261 | // Using constructor with resolved arguments |
262 | 262 | $this->tracer->push($class, true, ...['constructor', ...$arguments]); |
263 | 263 | $instance = new $class(...$arguments); |
264 | - } catch (\TypeError $e) { |
|
264 | + }catch (\TypeError $e){ |
|
265 | 265 | throw new WrongTypeException($constructor, $e); |
266 | - } finally { |
|
266 | + }finally{ |
|
267 | 267 | $this->tracer->pop(true); |
268 | 268 | } |
269 | - } else { |
|
269 | + }else{ |
|
270 | 270 | // No constructor specified |
271 | 271 | $instance = $reflection->newInstance(); |
272 | 272 | } |
@@ -284,9 +284,9 @@ discard block |
||
284 | 284 | private function registerInstance(object $instance, array $parameters): object |
285 | 285 | { |
286 | 286 | //Declarative singletons (only when class received via direct get) |
287 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
287 | + if ($parameters === [] && $instance instanceof SingletonInterface){ |
|
288 | 288 | $alias = $instance::class; |
289 | - if (!isset($this->state->bindings[$alias])) { |
|
289 | + if (!isset($this->state->bindings[$alias])){ |
|
290 | 290 | $this->state->bindings[$alias] = $instance; |
291 | 291 | } |
292 | 292 | } |
@@ -54,34 +54,47 @@ discard block |
||
54 | 54 | */ |
55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
56 | 56 | { |
57 | - if (!isset($this->state->bindings[$alias])) { |
|
57 | + if (!isset($this->state->bindings[$alias])) |
|
58 | + { |
|
58 | 59 | $this->tracer->push($alias, false, source: 'autowiring', 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($alias, false, source: 'binding', binding: $binding, context: $context); |
70 | 75 | |
71 | - if (\is_object($binding)) { |
|
72 | - if ($binding::class === WeakReference::class) { |
|
73 | - if ($binding->get() === null && \class_exists($alias)) { |
|
74 | - try { |
|
76 | + if (\is_object($binding)) |
|
77 | + { |
|
78 | + if ($binding::class === WeakReference::class) |
|
79 | + { |
|
80 | + if ($binding->get() === null && \class_exists($alias)) |
|
81 | + { |
|
82 | + try |
|
83 | + { |
|
75 | 84 | $this->tracer->push($alias, false, source: WeakReference::class, context: $context); |
76 | 85 | $object = $this->createInstance($alias, $parameters, $context); |
77 | 86 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
78 | - } catch (\Throwable) { |
|
87 | + } |
|
88 | + catch (\Throwable) |
|
89 | + { |
|
79 | 90 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
80 | 91 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
81 | 92 | $this->tracer->getRootAlias(), |
82 | 93 | $alias, |
83 | 94 | ))); |
84 | - } finally { |
|
95 | + } |
|
96 | + finally |
|
97 | + { |
|
85 | 98 | $this->tracer->pop(); |
86 | 99 | } |
87 | 100 | } |
@@ -91,25 +104,32 @@ discard block |
||
91 | 104 | return $binding; |
92 | 105 | } |
93 | 106 | |
94 | - if (\is_string($binding)) { |
|
107 | + if (\is_string($binding)) |
|
108 | + { |
|
95 | 109 | //Binding is pointing to something else |
96 | 110 | return $this->make($binding, $parameters, $context); |
97 | 111 | } |
98 | 112 | |
99 | 113 | unset($this->state->bindings[$alias]); |
100 | - try { |
|
114 | + try |
|
115 | + { |
|
101 | 116 | $instance = $binding[0] === $alias |
102 | 117 | ? $this->autowire($alias, $parameters, $context) |
103 | 118 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
104 | - } finally { |
|
119 | + } |
|
120 | + finally |
|
121 | + { |
|
105 | 122 | /** @psalm-var class-string $alias */ |
106 | 123 | $this->state->bindings[$alias] = $binding; |
107 | 124 | } |
108 | - } finally { |
|
125 | + } |
|
126 | + finally |
|
127 | + { |
|
109 | 128 | $this->tracer->pop(false); |
110 | 129 | } |
111 | 130 | |
112 | - if ($binding[1]) { |
|
131 | + if ($binding[1]) |
|
132 | + { |
|
113 | 133 | // Indicates singleton |
114 | 134 | /** @psalm-var class-string $alias */ |
115 | 135 | $this->state->bindings[$alias] = $instance; |
@@ -128,7 +148,8 @@ discard block |
||
128 | 148 | */ |
129 | 149 | private function autowire(string $class, array $parameters, string $context = null): object |
130 | 150 | { |
131 | - if (!\class_exists($class) && !isset($this->state->injectors[$class])) { |
|
151 | + if (!\class_exists($class) && !isset($this->state->injectors[$class])) |
|
152 | + { |
|
132 | 153 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
133 | 154 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
134 | 155 | $this->tracer->getRootAlias(), |
@@ -155,18 +176,23 @@ discard block |
||
155 | 176 | array $parameters, |
156 | 177 | string $context = null |
157 | 178 | ): mixed { |
158 | - if (\is_string($target)) { |
|
179 | + if (\is_string($target)) |
|
180 | + { |
|
159 | 181 | // Reference |
160 | 182 | return $this->make($target, $parameters, $context); |
161 | 183 | } |
162 | 184 | |
163 | - if ($target instanceof Autowire) { |
|
185 | + if ($target instanceof Autowire) |
|
186 | + { |
|
164 | 187 | return $target->resolve($this, $parameters); |
165 | 188 | } |
166 | 189 | |
167 | - try { |
|
190 | + try |
|
191 | + { |
|
168 | 192 | return $this->invoker->invoke($target, $parameters); |
169 | - } catch (NotCallableException $e) { |
|
193 | + } |
|
194 | + catch (NotCallableException $e) |
|
195 | + { |
|
170 | 196 | throw new ContainerException( |
171 | 197 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
172 | 198 | $e->getCode(), |
@@ -190,20 +216,26 @@ discard block |
||
190 | 216 | */ |
191 | 217 | private function createInstance(string $class, array $parameters, string $context = null): object |
192 | 218 | { |
193 | - try { |
|
219 | + try |
|
220 | + { |
|
194 | 221 | $reflection = new \ReflectionClass($class); |
195 | - } catch (\ReflectionException $e) { |
|
222 | + } |
|
223 | + catch (\ReflectionException $e) |
|
224 | + { |
|
196 | 225 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
197 | 226 | } |
198 | 227 | |
199 | 228 | //We have to construct class using external injector when we know exact context |
200 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
229 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
230 | + { |
|
201 | 231 | $injector = $this->state->injectors[$reflection->getName()]; |
202 | 232 | |
203 | - try { |
|
233 | + try |
|
234 | + { |
|
204 | 235 | $injectorInstance = $this->container->get($injector); |
205 | 236 | |
206 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
237 | + if (!$injectorInstance instanceof InjectorInterface) |
|
238 | + { |
|
207 | 239 | throw new InjectionException( |
208 | 240 | \sprintf( |
209 | 241 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -215,7 +247,8 @@ discard block |
||
215 | 247 | |
216 | 248 | /** @var InjectorInterface<TObject> $injectorInstance */ |
217 | 249 | $instance = $injectorInstance->createInjection($reflection, $context); |
218 | - if (!$reflection->isInstance($instance)) { |
|
250 | + if (!$reflection->isInstance($instance)) |
|
251 | + { |
|
219 | 252 | throw new InjectionException( |
220 | 253 | \sprintf( |
221 | 254 | "Invalid injection response for '%s'.", |
@@ -225,12 +258,15 @@ discard block |
||
225 | 258 | } |
226 | 259 | |
227 | 260 | return $instance; |
228 | - } finally { |
|
261 | + } |
|
262 | + finally |
|
263 | + { |
|
229 | 264 | $this->state->injectors[$reflection->getName()] = $injector; |
230 | 265 | } |
231 | 266 | } |
232 | 267 | |
233 | - if (!$reflection->isInstantiable()) { |
|
268 | + if (!$reflection->isInstantiable()) |
|
269 | + { |
|
234 | 270 | $itIs = match (true) { |
235 | 271 | $reflection->isEnum() => 'Enum', |
236 | 272 | $reflection->isAbstract() => 'Abstract class', |
@@ -243,10 +279,14 @@ discard block |
||
243 | 279 | |
244 | 280 | $constructor = $reflection->getConstructor(); |
245 | 281 | |
246 | - if ($constructor !== null) { |
|
247 | - try { |
|
282 | + if ($constructor !== null) |
|
283 | + { |
|
284 | + try |
|
285 | + { |
|
248 | 286 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
249 | - } catch (ValidationException $e) { |
|
287 | + } |
|
288 | + catch (ValidationException $e) |
|
289 | + { |
|
250 | 290 | throw new ContainerException( |
251 | 291 | $this->tracer->combineTraceMessage( |
252 | 292 | \sprintf( |
@@ -257,16 +297,23 @@ discard block |
||
257 | 297 | ), |
258 | 298 | ); |
259 | 299 | } |
260 | - try { |
|
300 | + try |
|
301 | + { |
|
261 | 302 | // Using constructor with resolved arguments |
262 | 303 | $this->tracer->push($class, true, ...['constructor', ...$arguments]); |
263 | 304 | $instance = new $class(...$arguments); |
264 | - } catch (\TypeError $e) { |
|
305 | + } |
|
306 | + catch (\TypeError $e) |
|
307 | + { |
|
265 | 308 | throw new WrongTypeException($constructor, $e); |
266 | - } finally { |
|
309 | + } |
|
310 | + finally |
|
311 | + { |
|
267 | 312 | $this->tracer->pop(true); |
268 | 313 | } |
269 | - } else { |
|
314 | + } |
|
315 | + else |
|
316 | + { |
|
270 | 317 | // No constructor specified |
271 | 318 | $instance = $reflection->newInstance(); |
272 | 319 | } |
@@ -284,9 +331,11 @@ discard block |
||
284 | 331 | private function registerInstance(object $instance, array $parameters): object |
285 | 332 | { |
286 | 333 | //Declarative singletons (only when class received via direct get) |
287 | - if ($parameters === [] && $instance instanceof SingletonInterface) { |
|
334 | + if ($parameters === [] && $instance instanceof SingletonInterface) |
|
335 | + { |
|
288 | 336 | $alias = $instance::class; |
289 | - if (!isset($this->state->bindings[$alias])) { |
|
337 | + if (!isset($this->state->bindings[$alias])) |
|
338 | + { |
|
290 | 339 | $this->state->bindings[$alias] = $instance; |
291 | 340 | } |
292 | 341 | } |