@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | { |
34 | 34 | $root = new Container(); |
35 | 35 | |
36 | - $root->runScoped(static function (Container $c1) { |
|
36 | + $root->runScoped(static function (Container $c1){ |
|
37 | 37 | $c1->runScoped(static function (Container $c2) use ($c1) { |
38 | 38 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
39 | 39 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
@@ -48,8 +48,8 @@ discard block |
||
48 | 48 | $root = new Container(); |
49 | 49 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
50 | 50 | |
51 | - $root->runScoped(static function (Container $fooScope) { |
|
52 | - $fooScope->runScoped(static function (Container $container) { |
|
51 | + $root->runScoped(static function (Container $fooScope){ |
|
52 | + $fooScope->runScoped(static function (Container $container){ |
|
53 | 53 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
54 | 54 | }, name: 'bar'); |
55 | 55 | }, name: 'foo'); |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | $root = new Container(); |
64 | 64 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
65 | 65 | |
66 | - $root->runScoped(static function (Container $fooScope) { |
|
67 | - $fooScope->runScoped(static function (Container $container) { |
|
66 | + $root->runScoped(static function (Container $fooScope){ |
|
67 | + $fooScope->runScoped(static function (Container $container){ |
|
68 | 68 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
69 | 69 | }, name: 'bar'); |
70 | 70 | }, name: 'baz'); |
@@ -83,8 +83,8 @@ discard block |
||
83 | 83 | $root = new Container(); |
84 | 84 | $root->bind('foo', self::makeFooScopeObject(...)); |
85 | 85 | |
86 | - $root->runScoped(static function (Container $c1) { |
|
87 | - $c1->runScoped(static function (Container $c2) { |
|
86 | + $root->runScoped(static function (Container $c1){ |
|
87 | + $c1->runScoped(static function (Container $c2){ |
|
88 | 88 | $c2->get('foo'); |
89 | 89 | }, name: 'foo'); |
90 | 90 | }); |
@@ -102,8 +102,8 @@ discard block |
||
102 | 102 | $root = new Container(); |
103 | 103 | $root->bind('foo', self::makeFooScopeObject(...)); |
104 | 104 | |
105 | - $root->runScoped(static function (Container $c1) { |
|
106 | - $c1->runScoped(static function (Container $c2) { |
|
105 | + $root->runScoped(static function (Container $c1){ |
|
106 | + $c1->runScoped(static function (Container $c2){ |
|
107 | 107 | $c2->get('foo'); |
108 | 108 | }); |
109 | 109 | }); |
@@ -121,15 +121,15 @@ discard block |
||
121 | 121 | |
122 | 122 | $root = new Container(); |
123 | 123 | |
124 | - try { |
|
125 | - $root->runScoped(static function (Container $c1) { |
|
126 | - $c1->runScoped(static function (Container $c2) { |
|
127 | - $c2->runScoped(static function (Container $c3) { |
|
124 | + try{ |
|
125 | + $root->runScoped(static function (Container $c1){ |
|
126 | + $c1->runScoped(static function (Container $c2){ |
|
127 | + $c2->runScoped(static function (Container $c3){ |
|
128 | 128 | // do nothing |
129 | 129 | }, name: 'root'); |
130 | 130 | }); |
131 | 131 | }); |
132 | - } catch (NamedScopeDuplicationException $e) { |
|
132 | + }catch (NamedScopeDuplicationException $e){ |
|
133 | 133 | self::assertSame('root', $e->getScope()); |
134 | 134 | throw $e; |
135 | 135 | } |
@@ -144,14 +144,14 @@ discard block |
||
144 | 144 | { |
145 | 145 | self::expectException(BadScopeException::class); |
146 | 146 | |
147 | - try { |
|
147 | + try{ |
|
148 | 148 | $root = new Container(); |
149 | - $root->runScoped(static function (Container $c1) { |
|
150 | - $c1->runScoped(static function (Container $c2) { |
|
149 | + $root->runScoped(static function (Container $c1){ |
|
150 | + $c1->runScoped(static function (Container $c2){ |
|
151 | 151 | $c2->get(AttrScopeFoo::class); |
152 | 152 | }); |
153 | 153 | }, name: 'bar'); |
154 | - } catch (BadScopeException $e) { |
|
154 | + }catch (BadScopeException $e){ |
|
155 | 155 | self::assertSame('foo', $e->getScope()); |
156 | 156 | throw $e; |
157 | 157 | } |
@@ -33,8 +33,10 @@ discard block |
||
33 | 33 | { |
34 | 34 | $root = new Container(); |
35 | 35 | |
36 | - $root->runScoped(static function (Container $c1) { |
|
37 | - $c1->runScoped(static function (Container $c2) use ($c1) { |
|
36 | + $root->runScoped(static function (Container $c1) |
|
37 | + { |
|
38 | + $c1->runScoped(static function (Container $c2) use ($c1) |
|
39 | + { |
|
38 | 40 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
39 | 41 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
40 | 42 | |
@@ -48,8 +50,10 @@ discard block |
||
48 | 50 | $root = new Container(); |
49 | 51 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
50 | 52 | |
51 | - $root->runScoped(static function (Container $fooScope) { |
|
52 | - $fooScope->runScoped(static function (Container $container) { |
|
53 | + $root->runScoped(static function (Container $fooScope) |
|
54 | + { |
|
55 | + $fooScope->runScoped(static function (Container $container) |
|
56 | + { |
|
53 | 57 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
54 | 58 | }, name: 'bar'); |
55 | 59 | }, name: 'foo'); |
@@ -63,8 +67,10 @@ discard block |
||
63 | 67 | $root = new Container(); |
64 | 68 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
65 | 69 | |
66 | - $root->runScoped(static function (Container $fooScope) { |
|
67 | - $fooScope->runScoped(static function (Container $container) { |
|
70 | + $root->runScoped(static function (Container $fooScope) |
|
71 | + { |
|
72 | + $fooScope->runScoped(static function (Container $container) |
|
73 | + { |
|
68 | 74 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
69 | 75 | }, name: 'bar'); |
70 | 76 | }, name: 'baz'); |
@@ -83,8 +89,10 @@ discard block |
||
83 | 89 | $root = new Container(); |
84 | 90 | $root->bind('foo', self::makeFooScopeObject(...)); |
85 | 91 | |
86 | - $root->runScoped(static function (Container $c1) { |
|
87 | - $c1->runScoped(static function (Container $c2) { |
|
92 | + $root->runScoped(static function (Container $c1) |
|
93 | + { |
|
94 | + $c1->runScoped(static function (Container $c2) |
|
95 | + { |
|
88 | 96 | $c2->get('foo'); |
89 | 97 | }, name: 'foo'); |
90 | 98 | }); |
@@ -102,8 +110,10 @@ discard block |
||
102 | 110 | $root = new Container(); |
103 | 111 | $root->bind('foo', self::makeFooScopeObject(...)); |
104 | 112 | |
105 | - $root->runScoped(static function (Container $c1) { |
|
106 | - $c1->runScoped(static function (Container $c2) { |
|
113 | + $root->runScoped(static function (Container $c1) |
|
114 | + { |
|
115 | + $c1->runScoped(static function (Container $c2) |
|
116 | + { |
|
107 | 117 | $c2->get('foo'); |
108 | 118 | }); |
109 | 119 | }); |
@@ -121,15 +131,21 @@ discard block |
||
121 | 131 | |
122 | 132 | $root = new Container(); |
123 | 133 | |
124 | - try { |
|
125 | - $root->runScoped(static function (Container $c1) { |
|
126 | - $c1->runScoped(static function (Container $c2) { |
|
127 | - $c2->runScoped(static function (Container $c3) { |
|
134 | + try |
|
135 | + { |
|
136 | + $root->runScoped(static function (Container $c1) |
|
137 | + { |
|
138 | + $c1->runScoped(static function (Container $c2) |
|
139 | + { |
|
140 | + $c2->runScoped(static function (Container $c3) |
|
141 | + { |
|
128 | 142 | // do nothing |
129 | 143 | }, name: 'root'); |
130 | 144 | }); |
131 | 145 | }); |
132 | - } catch (NamedScopeDuplicationException $e) { |
|
146 | + } |
|
147 | + catch (NamedScopeDuplicationException $e) |
|
148 | + { |
|
133 | 149 | self::assertSame('root', $e->getScope()); |
134 | 150 | throw $e; |
135 | 151 | } |
@@ -144,14 +160,19 @@ discard block |
||
144 | 160 | { |
145 | 161 | self::expectException(BadScopeException::class); |
146 | 162 | |
147 | - try { |
|
163 | + try |
|
164 | + { |
|
148 | 165 | $root = new Container(); |
149 | - $root->runScoped(static function (Container $c1) { |
|
150 | - $c1->runScoped(static function (Container $c2) { |
|
166 | + $root->runScoped(static function (Container $c1) |
|
167 | + { |
|
168 | + $c1->runScoped(static function (Container $c2) |
|
169 | + { |
|
151 | 170 | $c2->get(AttrScopeFoo::class); |
152 | 171 | }); |
153 | 172 | }, name: 'bar'); |
154 | - } catch (BadScopeException $e) { |
|
173 | + } |
|
174 | + catch (BadScopeException $e) |
|
175 | + { |
|
155 | 176 | self::assertSame('foo', $e->getScope()); |
156 | 177 | throw $e; |
157 | 178 | } |
@@ -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); |
@@ -120,18 +120,18 @@ discard block |
||
120 | 120 | private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments) |
121 | 121 | { |
122 | 122 | $context = $ctx->context; |
123 | - try { |
|
123 | + try{ |
|
124 | 124 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
125 | - } catch (\ReflectionException $e) { |
|
125 | + }catch (\ReflectionException $e){ |
|
126 | 126 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
127 | 127 | } |
128 | 128 | |
129 | 129 | $injector = $binding->injector; |
130 | 130 | |
131 | - try { |
|
131 | + try{ |
|
132 | 132 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
133 | 133 | |
134 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
134 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
135 | 135 | throw new InjectionException( |
136 | 136 | \sprintf( |
137 | 137 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -158,7 +158,7 @@ discard block |
||
158 | 158 | default => (string)$context, |
159 | 159 | }); |
160 | 160 | |
161 | - if (!$reflection->isInstance($instance)) { |
|
161 | + if (!$reflection->isInstance($instance)){ |
|
162 | 162 | throw new InjectionException( |
163 | 163 | \sprintf( |
164 | 164 | "Invalid injection response for '%s'.", |
@@ -168,7 +168,7 @@ discard block |
||
168 | 168 | } |
169 | 169 | |
170 | 170 | return $instance; |
171 | - } finally { |
|
171 | + }finally{ |
|
172 | 172 | $this->state->bindings[$ctx->class] ??= $binding; |
173 | 173 | } |
174 | 174 | } |
@@ -176,18 +176,18 @@ discard block |
||
176 | 176 | private function resolveAlias( |
177 | 177 | \Spiral\Core\Config\Alias $binding, |
178 | 178 | string $alias, |
179 | - Stringable|string|null $context, |
|
179 | + Stringable | string | null $context, |
|
180 | 180 | array $arguments, |
181 | 181 | ): mixed { |
182 | 182 | $result = $binding->alias === $alias |
183 | 183 | ? $this->autowire( |
184 | - new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton), |
|
184 | + new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton), |
|
185 | 185 | $arguments, |
186 | 186 | ) |
187 | 187 | //Binding is pointing to something else |
188 | 188 | : $this->make($binding->alias, $arguments, $context); |
189 | 189 | |
190 | - if ($binding->singleton && $arguments === []) { |
|
190 | + if ($binding->singleton && $arguments === []){ |
|
191 | 191 | $this->state->singletons[$alias] = $result; |
192 | 192 | } |
193 | 193 | |
@@ -197,13 +197,13 @@ discard block |
||
197 | 197 | private function resolveShared( |
198 | 198 | \Spiral\Core\Config\Shared $binding, |
199 | 199 | string $alias, |
200 | - Stringable|string|null $context, |
|
200 | + Stringable | string | null $context, |
|
201 | 201 | array $arguments, |
202 | 202 | ): object { |
203 | 203 | $avoidCache = $arguments !== []; |
204 | 204 | return $avoidCache |
205 | 205 | ? $this->createInstance( |
206 | - new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
|
206 | + new Ctx(alias : $alias, class : $binding->value::class, context : $context), |
|
207 | 207 | $arguments, |
208 | 208 | ) |
209 | 209 | : $binding->value; |
@@ -212,7 +212,7 @@ discard block |
||
212 | 212 | private function resolveAutowire( |
213 | 213 | \Spiral\Core\Config\Autowire $binding, |
214 | 214 | string $alias, |
215 | - Stringable|string|null $context, |
|
215 | + Stringable | string | null $context, |
|
216 | 216 | array $arguments, |
217 | 217 | ): mixed { |
218 | 218 | $instance = $binding->autowire->resolve($this, $arguments); |
@@ -222,17 +222,17 @@ discard block |
||
222 | 222 | } |
223 | 223 | |
224 | 224 | private function resolveFactory( |
225 | - \Spiral\Core\Config\Factory|DeferredFactory $binding, |
|
225 | + \Spiral\Core\Config\Factory | DeferredFactory $binding, |
|
226 | 226 | string $alias, |
227 | - Stringable|string|null $context, |
|
227 | + Stringable | string | null $context, |
|
228 | 228 | array $arguments, |
229 | 229 | ): mixed { |
230 | 230 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
231 | - try { |
|
231 | + try{ |
|
232 | 232 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
233 | 233 | ? ($binding->factory)() |
234 | 234 | : $this->invoker->invoke($binding->factory, $arguments); |
235 | - } catch (NotCallableException $e) { |
|
235 | + }catch (NotCallableException $e){ |
|
236 | 236 | throw new ContainerException( |
237 | 237 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
238 | 238 | $e->getCode(), |
@@ -246,24 +246,24 @@ discard block |
||
246 | 246 | private function resolveWeakReference( |
247 | 247 | \Spiral\Core\Config\WeakReference $binding, |
248 | 248 | string $alias, |
249 | - Stringable|string|null $context, |
|
249 | + Stringable | string | null $context, |
|
250 | 250 | array $arguments, |
251 | 251 | ): ?object { |
252 | 252 | $avoidCache = $arguments !== []; |
253 | 253 | |
254 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
255 | - try { |
|
254 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
255 | + try{ |
|
256 | 256 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
257 | 257 | |
258 | 258 | $object = $this->createInstance( |
259 | 259 | new Ctx(alias: $alias, class: $alias, context: $context), |
260 | 260 | $arguments, |
261 | 261 | ); |
262 | - if ($avoidCache) { |
|
262 | + if ($avoidCache){ |
|
263 | 263 | return $object; |
264 | 264 | } |
265 | 265 | $binding->reference = WeakReference::create($object); |
266 | - } catch (\Throwable) { |
|
266 | + }catch (\Throwable){ |
|
267 | 267 | throw new ContainerException( |
268 | 268 | $this->tracer->combineTraceMessage( |
269 | 269 | \sprintf( |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | ) |
274 | 274 | ) |
275 | 275 | ); |
276 | - } finally { |
|
276 | + }finally{ |
|
277 | 277 | $this->tracer->pop(); |
278 | 278 | } |
279 | 279 | } |
@@ -284,23 +284,23 @@ discard block |
||
284 | 284 | private function resolveWithoutBinding( |
285 | 285 | string $alias, |
286 | 286 | array $parameters = [], |
287 | - Stringable|string|null $context = null |
|
287 | + Stringable | string | null $context = null |
|
288 | 288 | ): mixed { |
289 | 289 | $parent = $this->scope->getParentFactory(); |
290 | 290 | |
291 | - if ($parent !== null) { |
|
292 | - try { |
|
291 | + if ($parent !== null){ |
|
292 | + try{ |
|
293 | 293 | $this->tracer->push(false, ...[ |
294 | 294 | 'current scope' => $this->scope->getScopeName(), |
295 | 295 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
296 | 296 | ]); |
297 | 297 | /** @psalm-suppress TooManyArguments */ |
298 | 298 | return $parent->make($alias, $parameters, $context); |
299 | - } catch (BadScopeException $e) { |
|
300 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
299 | + }catch (BadScopeException $e){ |
|
300 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
301 | 301 | throw $e; |
302 | 302 | } |
303 | - } catch (ContainerExceptionInterface $e) { |
|
303 | + }catch (ContainerExceptionInterface $e){ |
|
304 | 304 | $className = match (true) { |
305 | 305 | $e instanceof NotFoundException => NotFoundException::class, |
306 | 306 | default => ContainerException::class, |
@@ -309,19 +309,19 @@ discard block |
||
309 | 309 | 'Can\'t resolve `%s`.', |
310 | 310 | $alias, |
311 | 311 | )), previous: $e); |
312 | - } finally { |
|
312 | + }finally{ |
|
313 | 313 | $this->tracer->pop(false); |
314 | 314 | } |
315 | 315 | } |
316 | 316 | |
317 | 317 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
318 | - try { |
|
318 | + try{ |
|
319 | 319 | //No direct instructions how to construct class, make is automatically |
320 | 320 | return $this->autowire( |
321 | 321 | new Ctx(alias: $alias, class: $alias, context: $context), |
322 | 322 | $parameters, |
323 | 323 | ); |
324 | - } finally { |
|
324 | + }finally{ |
|
325 | 325 | $this->tracer->pop(false); |
326 | 326 | } |
327 | 327 | } |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | && |
343 | 343 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
344 | 344 | )) |
345 | - ) { |
|
345 | + ){ |
|
346 | 346 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
347 | 347 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
348 | 348 | $this->tracer->getRootAlias(), |
@@ -371,10 +371,10 @@ discard block |
||
371 | 371 | // Check scope name |
372 | 372 | $ctx->reflection = new \ReflectionClass($instance); |
373 | 373 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
374 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
374 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()){ |
|
375 | 375 | $parent = $this->scope->getParentScope(); |
376 | - while ($parent !== null) { |
|
377 | - if ($correctScope = $parent->getScopeName() === $scopeName) { |
|
376 | + while ($parent !== null){ |
|
377 | + if ($correctScope = $parent->getScopeName() === $scopeName){ |
|
378 | 378 | break; |
379 | 379 | } |
380 | 380 | $parent = $parent->getParentScope(); |
@@ -405,24 +405,24 @@ discard block |
||
405 | 405 | array $arguments, |
406 | 406 | ): object { |
407 | 407 | $class = $ctx->class; |
408 | - try { |
|
408 | + try{ |
|
409 | 409 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
410 | - } catch (\ReflectionException $e) { |
|
410 | + }catch (\ReflectionException $e){ |
|
411 | 411 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
412 | 412 | } |
413 | 413 | |
414 | 414 | // Check scope name |
415 | 415 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
416 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
416 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
417 | 417 | throw new BadScopeException($scope, $class); |
418 | 418 | } |
419 | 419 | |
420 | 420 | // We have to construct class using external injector when we know the exact context |
421 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
421 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
422 | 422 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
423 | 423 | } |
424 | 424 | |
425 | - if (!$reflection->isInstantiable()) { |
|
425 | + if (!$reflection->isInstantiable()){ |
|
426 | 426 | $itIs = match (true) { |
427 | 427 | $reflection->isEnum() => 'Enum', |
428 | 428 | $reflection->isAbstract() => 'Abstract class', |
@@ -435,12 +435,12 @@ discard block |
||
435 | 435 | |
436 | 436 | $constructor = $reflection->getConstructor(); |
437 | 437 | |
438 | - if ($constructor !== null) { |
|
439 | - try { |
|
438 | + if ($constructor !== null){ |
|
439 | + try{ |
|
440 | 440 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
441 | 441 | $this->tracer->push(true); |
442 | 442 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
443 | - } catch (ValidationException $e) { |
|
443 | + }catch (ValidationException $e){ |
|
444 | 444 | throw new ContainerException( |
445 | 445 | $this->tracer->combineTraceMessage( |
446 | 446 | \sprintf( |
@@ -450,22 +450,22 @@ discard block |
||
450 | 450 | ) |
451 | 451 | ), |
452 | 452 | ); |
453 | - } finally { |
|
453 | + }finally{ |
|
454 | 454 | $this->tracer->pop(true); |
455 | 455 | $this->tracer->pop(false); |
456 | 456 | } |
457 | - try { |
|
457 | + try{ |
|
458 | 458 | // Using constructor with resolved arguments |
459 | 459 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
460 | 460 | $this->tracer->push(true); |
461 | 461 | $instance = new $class(...$args); |
462 | - } catch (\TypeError $e) { |
|
462 | + }catch (\TypeError $e){ |
|
463 | 463 | throw new WrongTypeException($constructor, $e); |
464 | - } finally { |
|
464 | + }finally{ |
|
465 | 465 | $this->tracer->pop(true); |
466 | 466 | $this->tracer->pop(false); |
467 | 467 | } |
468 | - } else { |
|
468 | + }else{ |
|
469 | 469 | // No constructor specified |
470 | 470 | $instance = $reflection->newInstance(); |
471 | 471 | } |
@@ -483,13 +483,13 @@ discard block |
||
483 | 483 | $instance = $this->runInflector($instance); |
484 | 484 | |
485 | 485 | //Declarative singletons |
486 | - if ($this->isSingleton($ctx)) { |
|
486 | + if ($this->isSingleton($ctx)){ |
|
487 | 487 | $this->state->singletons[$ctx->alias] = $instance; |
488 | 488 | } |
489 | 489 | |
490 | 490 | // Register finalizer |
491 | 491 | $finalizer = $this->getFinalizer($ctx, $instance); |
492 | - if ($finalizer !== null) { |
|
492 | + if ($finalizer !== null){ |
|
493 | 493 | $this->state->finalizers[] = $finalizer; |
494 | 494 | } |
495 | 495 | |
@@ -501,12 +501,12 @@ discard block |
||
501 | 501 | */ |
502 | 502 | private function isSingleton(Ctx $ctx): bool |
503 | 503 | { |
504 | - if ($ctx->singleton === true) { |
|
504 | + if ($ctx->singleton === true){ |
|
505 | 505 | return true; |
506 | 506 | } |
507 | 507 | |
508 | 508 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
509 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
509 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
510 | 510 | return true; |
511 | 511 | } |
512 | 512 | |
@@ -520,7 +520,7 @@ discard block |
||
520 | 520 | * @var Finalize|null $attribute |
521 | 521 | */ |
522 | 522 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
523 | - if ($attribute === null) { |
|
523 | + if ($attribute === null){ |
|
524 | 524 | return null; |
525 | 525 | } |
526 | 526 | |
@@ -534,10 +534,10 @@ discard block |
||
534 | 534 | { |
535 | 535 | $scope = $this->scope; |
536 | 536 | |
537 | - while ($scope !== null) { |
|
538 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
539 | - if ($instance instanceof $class) { |
|
540 | - foreach ($inflectors as $inflector) { |
|
537 | + while ($scope !== null){ |
|
538 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
539 | + if ($instance instanceof $class){ |
|
540 | + foreach ($inflectors as $inflector){ |
|
541 | 541 | $instance = $inflector->getParametersCount() > 1 |
542 | 542 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
543 | 543 | : ($inflector->inflector)($instance); |
@@ -553,9 +553,9 @@ discard block |
||
553 | 553 | |
554 | 554 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
555 | 555 | { |
556 | - try { |
|
556 | + try{ |
|
557 | 557 | $this->resolver->validateArguments($reflection, $arguments); |
558 | - } catch (\Throwable) { |
|
558 | + }catch (\Throwable){ |
|
559 | 559 | return false; |
560 | 560 | } |
561 | 561 |
@@ -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); |
@@ -120,18 +125,23 @@ discard block |
||
120 | 125 | private function resolveInjector(Injectable $binding, Ctx $ctx, array $arguments) |
121 | 126 | { |
122 | 127 | $context = $ctx->context; |
123 | - try { |
|
128 | + try |
|
129 | + { |
|
124 | 130 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
125 | - } catch (\ReflectionException $e) { |
|
131 | + } |
|
132 | + catch (\ReflectionException $e) |
|
133 | + { |
|
126 | 134 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
127 | 135 | } |
128 | 136 | |
129 | 137 | $injector = $binding->injector; |
130 | 138 | |
131 | - try { |
|
139 | + try |
|
140 | + { |
|
132 | 141 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
133 | 142 | |
134 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
143 | + if (!$injectorInstance instanceof InjectorInterface) |
|
144 | + { |
|
135 | 145 | throw new InjectionException( |
136 | 146 | \sprintf( |
137 | 147 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -158,7 +168,8 @@ discard block |
||
158 | 168 | default => (string)$context, |
159 | 169 | }); |
160 | 170 | |
161 | - if (!$reflection->isInstance($instance)) { |
|
171 | + if (!$reflection->isInstance($instance)) |
|
172 | + { |
|
162 | 173 | throw new InjectionException( |
163 | 174 | \sprintf( |
164 | 175 | "Invalid injection response for '%s'.", |
@@ -168,7 +179,9 @@ discard block |
||
168 | 179 | } |
169 | 180 | |
170 | 181 | return $instance; |
171 | - } finally { |
|
182 | + } |
|
183 | + finally |
|
184 | + { |
|
172 | 185 | $this->state->bindings[$ctx->class] ??= $binding; |
173 | 186 | } |
174 | 187 | } |
@@ -187,7 +200,8 @@ discard block |
||
187 | 200 | //Binding is pointing to something else |
188 | 201 | : $this->make($binding->alias, $arguments, $context); |
189 | 202 | |
190 | - if ($binding->singleton && $arguments === []) { |
|
203 | + if ($binding->singleton && $arguments === []) |
|
204 | + { |
|
191 | 205 | $this->state->singletons[$alias] = $result; |
192 | 206 | } |
193 | 207 | |
@@ -228,11 +242,14 @@ discard block |
||
228 | 242 | array $arguments, |
229 | 243 | ): mixed { |
230 | 244 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
231 | - try { |
|
245 | + try |
|
246 | + { |
|
232 | 247 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
233 | 248 | ? ($binding->factory)() |
234 | 249 | : $this->invoker->invoke($binding->factory, $arguments); |
235 | - } catch (NotCallableException $e) { |
|
250 | + } |
|
251 | + catch (NotCallableException $e) |
|
252 | + { |
|
236 | 253 | throw new ContainerException( |
237 | 254 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
238 | 255 | $e->getCode(), |
@@ -251,19 +268,24 @@ discard block |
||
251 | 268 | ): ?object { |
252 | 269 | $avoidCache = $arguments !== []; |
253 | 270 | |
254 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
255 | - try { |
|
271 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
272 | + { |
|
273 | + try |
|
274 | + { |
|
256 | 275 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
257 | 276 | |
258 | 277 | $object = $this->createInstance( |
259 | 278 | new Ctx(alias: $alias, class: $alias, context: $context), |
260 | 279 | $arguments, |
261 | 280 | ); |
262 | - if ($avoidCache) { |
|
281 | + if ($avoidCache) |
|
282 | + { |
|
263 | 283 | return $object; |
264 | 284 | } |
265 | 285 | $binding->reference = WeakReference::create($object); |
266 | - } catch (\Throwable) { |
|
286 | + } |
|
287 | + catch (\Throwable) |
|
288 | + { |
|
267 | 289 | throw new ContainerException( |
268 | 290 | $this->tracer->combineTraceMessage( |
269 | 291 | \sprintf( |
@@ -273,7 +295,9 @@ discard block |
||
273 | 295 | ) |
274 | 296 | ) |
275 | 297 | ); |
276 | - } finally { |
|
298 | + } |
|
299 | + finally |
|
300 | + { |
|
277 | 301 | $this->tracer->pop(); |
278 | 302 | } |
279 | 303 | } |
@@ -288,19 +312,26 @@ discard block |
||
288 | 312 | ): mixed { |
289 | 313 | $parent = $this->scope->getParentFactory(); |
290 | 314 | |
291 | - if ($parent !== null) { |
|
292 | - try { |
|
315 | + if ($parent !== null) |
|
316 | + { |
|
317 | + try |
|
318 | + { |
|
293 | 319 | $this->tracer->push(false, ...[ |
294 | 320 | 'current scope' => $this->scope->getScopeName(), |
295 | 321 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
296 | 322 | ]); |
297 | 323 | /** @psalm-suppress TooManyArguments */ |
298 | 324 | return $parent->make($alias, $parameters, $context); |
299 | - } catch (BadScopeException $e) { |
|
300 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
325 | + } |
|
326 | + catch (BadScopeException $e) |
|
327 | + { |
|
328 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
329 | + { |
|
301 | 330 | throw $e; |
302 | 331 | } |
303 | - } catch (ContainerExceptionInterface $e) { |
|
332 | + } |
|
333 | + catch (ContainerExceptionInterface $e) |
|
334 | + { |
|
304 | 335 | $className = match (true) { |
305 | 336 | $e instanceof NotFoundException => NotFoundException::class, |
306 | 337 | default => ContainerException::class, |
@@ -309,19 +340,24 @@ discard block |
||
309 | 340 | 'Can\'t resolve `%s`.', |
310 | 341 | $alias, |
311 | 342 | )), previous: $e); |
312 | - } finally { |
|
343 | + } |
|
344 | + finally |
|
345 | + { |
|
313 | 346 | $this->tracer->pop(false); |
314 | 347 | } |
315 | 348 | } |
316 | 349 | |
317 | 350 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
318 | - try { |
|
351 | + try |
|
352 | + { |
|
319 | 353 | //No direct instructions how to construct class, make is automatically |
320 | 354 | return $this->autowire( |
321 | 355 | new Ctx(alias: $alias, class: $alias, context: $context), |
322 | 356 | $parameters, |
323 | 357 | ); |
324 | - } finally { |
|
358 | + } |
|
359 | + finally |
|
360 | + { |
|
325 | 361 | $this->tracer->pop(false); |
326 | 362 | } |
327 | 363 | } |
@@ -371,10 +407,13 @@ discard block |
||
371 | 407 | // Check scope name |
372 | 408 | $ctx->reflection = new \ReflectionClass($instance); |
373 | 409 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
374 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
410 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
411 | + { |
|
375 | 412 | $parent = $this->scope->getParentScope(); |
376 | - while ($parent !== null) { |
|
377 | - if ($correctScope = $parent->getScopeName() === $scopeName) { |
|
413 | + while ($parent !== null) |
|
414 | + { |
|
415 | + if ($correctScope = $parent->getScopeName() === $scopeName) |
|
416 | + { |
|
378 | 417 | break; |
379 | 418 | } |
380 | 419 | $parent = $parent->getParentScope(); |
@@ -405,24 +444,30 @@ discard block |
||
405 | 444 | array $arguments, |
406 | 445 | ): object { |
407 | 446 | $class = $ctx->class; |
408 | - try { |
|
447 | + try |
|
448 | + { |
|
409 | 449 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
410 | - } catch (\ReflectionException $e) { |
|
450 | + } |
|
451 | + catch (\ReflectionException $e) |
|
452 | + { |
|
411 | 453 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
412 | 454 | } |
413 | 455 | |
414 | 456 | // Check scope name |
415 | 457 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
416 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
458 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
459 | + { |
|
417 | 460 | throw new BadScopeException($scope, $class); |
418 | 461 | } |
419 | 462 | |
420 | 463 | // We have to construct class using external injector when we know the exact context |
421 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
464 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
465 | + { |
|
422 | 466 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
423 | 467 | } |
424 | 468 | |
425 | - if (!$reflection->isInstantiable()) { |
|
469 | + if (!$reflection->isInstantiable()) |
|
470 | + { |
|
426 | 471 | $itIs = match (true) { |
427 | 472 | $reflection->isEnum() => 'Enum', |
428 | 473 | $reflection->isAbstract() => 'Abstract class', |
@@ -435,12 +480,16 @@ discard block |
||
435 | 480 | |
436 | 481 | $constructor = $reflection->getConstructor(); |
437 | 482 | |
438 | - if ($constructor !== null) { |
|
439 | - try { |
|
483 | + if ($constructor !== null) |
|
484 | + { |
|
485 | + try |
|
486 | + { |
|
440 | 487 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
441 | 488 | $this->tracer->push(true); |
442 | 489 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
443 | - } catch (ValidationException $e) { |
|
490 | + } |
|
491 | + catch (ValidationException $e) |
|
492 | + { |
|
444 | 493 | throw new ContainerException( |
445 | 494 | $this->tracer->combineTraceMessage( |
446 | 495 | \sprintf( |
@@ -450,22 +499,31 @@ discard block |
||
450 | 499 | ) |
451 | 500 | ), |
452 | 501 | ); |
453 | - } finally { |
|
502 | + } |
|
503 | + finally |
|
504 | + { |
|
454 | 505 | $this->tracer->pop(true); |
455 | 506 | $this->tracer->pop(false); |
456 | 507 | } |
457 | - try { |
|
508 | + try |
|
509 | + { |
|
458 | 510 | // Using constructor with resolved arguments |
459 | 511 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
460 | 512 | $this->tracer->push(true); |
461 | 513 | $instance = new $class(...$args); |
462 | - } catch (\TypeError $e) { |
|
514 | + } |
|
515 | + catch (\TypeError $e) |
|
516 | + { |
|
463 | 517 | throw new WrongTypeException($constructor, $e); |
464 | - } finally { |
|
518 | + } |
|
519 | + finally |
|
520 | + { |
|
465 | 521 | $this->tracer->pop(true); |
466 | 522 | $this->tracer->pop(false); |
467 | 523 | } |
468 | - } else { |
|
524 | + } |
|
525 | + else |
|
526 | + { |
|
469 | 527 | // No constructor specified |
470 | 528 | $instance = $reflection->newInstance(); |
471 | 529 | } |
@@ -483,13 +541,15 @@ discard block |
||
483 | 541 | $instance = $this->runInflector($instance); |
484 | 542 | |
485 | 543 | //Declarative singletons |
486 | - if ($this->isSingleton($ctx)) { |
|
544 | + if ($this->isSingleton($ctx)) |
|
545 | + { |
|
487 | 546 | $this->state->singletons[$ctx->alias] = $instance; |
488 | 547 | } |
489 | 548 | |
490 | 549 | // Register finalizer |
491 | 550 | $finalizer = $this->getFinalizer($ctx, $instance); |
492 | - if ($finalizer !== null) { |
|
551 | + if ($finalizer !== null) |
|
552 | + { |
|
493 | 553 | $this->state->finalizers[] = $finalizer; |
494 | 554 | } |
495 | 555 | |
@@ -501,12 +561,14 @@ discard block |
||
501 | 561 | */ |
502 | 562 | private function isSingleton(Ctx $ctx): bool |
503 | 563 | { |
504 | - if ($ctx->singleton === true) { |
|
564 | + if ($ctx->singleton === true) |
|
565 | + { |
|
505 | 566 | return true; |
506 | 567 | } |
507 | 568 | |
508 | 569 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
509 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
570 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
571 | + { |
|
510 | 572 | return true; |
511 | 573 | } |
512 | 574 | |
@@ -520,7 +582,8 @@ discard block |
||
520 | 582 | * @var Finalize|null $attribute |
521 | 583 | */ |
522 | 584 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
523 | - if ($attribute === null) { |
|
585 | + if ($attribute === null) |
|
586 | + { |
|
524 | 587 | return null; |
525 | 588 | } |
526 | 589 | |
@@ -534,10 +597,14 @@ discard block |
||
534 | 597 | { |
535 | 598 | $scope = $this->scope; |
536 | 599 | |
537 | - while ($scope !== null) { |
|
538 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
539 | - if ($instance instanceof $class) { |
|
540 | - foreach ($inflectors as $inflector) { |
|
600 | + while ($scope !== null) |
|
601 | + { |
|
602 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
603 | + { |
|
604 | + if ($instance instanceof $class) |
|
605 | + { |
|
606 | + foreach ($inflectors as $inflector) |
|
607 | + { |
|
541 | 608 | $instance = $inflector->getParametersCount() > 1 |
542 | 609 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
543 | 610 | : ($inflector->inflector)($instance); |
@@ -553,9 +620,12 @@ discard block |
||
553 | 620 | |
554 | 621 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
555 | 622 | { |
556 | - try { |
|
623 | + try |
|
624 | + { |
|
557 | 625 | $this->resolver->validateArguments($reflection, $arguments); |
558 | - } catch (\Throwable) { |
|
626 | + } |
|
627 | + catch (\Throwable) |
|
628 | + { |
|
559 | 629 | return false; |
560 | 630 | } |
561 | 631 |