@@ -22,11 +22,11 @@ discard block |
||
22 | 22 | { |
23 | 23 | $fiber = new Fiber($callable); |
24 | 24 | $value = $fiber->start(); |
25 | - while (!$fiber->isTerminated()) { |
|
26 | - if ($check !== null) { |
|
27 | - try { |
|
25 | + while (!$fiber->isTerminated()){ |
|
26 | + if ($check !== null){ |
|
27 | + try{ |
|
28 | 28 | $value = $check($value); |
29 | - } catch (\Throwable $e) { |
|
29 | + }catch (\Throwable $e){ |
|
30 | 30 | $value = $fiber->throw($e); |
31 | 31 | continue; |
32 | 32 | } |
@@ -50,14 +50,14 @@ discard block |
||
50 | 50 | /** Values that were suspended by the fiber. */ |
51 | 51 | $suspends = []; |
52 | 52 | $results = []; |
53 | - foreach ($callables as $key => $callable) { |
|
53 | + foreach ($callables as $key => $callable){ |
|
54 | 54 | $fiberGenerators[$key] = (static function () use ($callable) { |
55 | 55 | $fiber = new Fiber($callable); |
56 | 56 | // Get ready |
57 | 57 | yield null; |
58 | 58 | |
59 | 59 | $value = yield $fiber->start(); |
60 | - while (!$fiber->isTerminated()) { |
|
60 | + while (!$fiber->isTerminated()){ |
|
61 | 61 | $value = yield $fiber->resume($value); |
62 | 62 | } |
63 | 63 | return $fiber->getReturn(); |
@@ -67,15 +67,15 @@ discard block |
||
67 | 67 | } |
68 | 68 | |
69 | 69 | |
70 | - while ($fiberGenerators !== []) { |
|
71 | - foreach ($fiberGenerators as $key => $generator) { |
|
72 | - try { |
|
70 | + while ($fiberGenerators !== []){ |
|
71 | + foreach ($fiberGenerators as $key => $generator){ |
|
72 | + try{ |
|
73 | 73 | $suspends[$key] = $generator->send($suspends[$key]); |
74 | - if (!$generator->valid()) { |
|
74 | + if (!$generator->valid()){ |
|
75 | 75 | $results[$key] = $generator->getReturn(); |
76 | 76 | unset($fiberGenerators[$key]); |
77 | 77 | } |
78 | - } catch (\Throwable $e) { |
|
78 | + }catch (\Throwable $e){ |
|
79 | 79 | unset($fiberGenerators[$key]); |
80 | 80 | $results[$key] = $e; |
81 | 81 | } |
@@ -22,11 +22,16 @@ discard block |
||
22 | 22 | { |
23 | 23 | $fiber = new Fiber($callable); |
24 | 24 | $value = $fiber->start(); |
25 | - while (!$fiber->isTerminated()) { |
|
26 | - if ($check !== null) { |
|
27 | - try { |
|
25 | + while (!$fiber->isTerminated()) |
|
26 | + { |
|
27 | + if ($check !== null) |
|
28 | + { |
|
29 | + try |
|
30 | + { |
|
28 | 31 | $value = $check($value); |
29 | - } catch (\Throwable $e) { |
|
32 | + } |
|
33 | + catch (\Throwable $e) |
|
34 | + { |
|
30 | 35 | $value = $fiber->throw($e); |
31 | 36 | continue; |
32 | 37 | } |
@@ -50,14 +55,17 @@ discard block |
||
50 | 55 | /** Values that were suspended by the fiber. */ |
51 | 56 | $suspends = []; |
52 | 57 | $results = []; |
53 | - foreach ($callables as $key => $callable) { |
|
54 | - $fiberGenerators[$key] = (static function () use ($callable) { |
|
58 | + foreach ($callables as $key => $callable) |
|
59 | + { |
|
60 | + $fiberGenerators[$key] = (static function () use ($callable) |
|
61 | + { |
|
55 | 62 | $fiber = new Fiber($callable); |
56 | 63 | // Get ready |
57 | 64 | yield null; |
58 | 65 | |
59 | 66 | $value = yield $fiber->start(); |
60 | - while (!$fiber->isTerminated()) { |
|
67 | + while (!$fiber->isTerminated()) |
|
68 | + { |
|
61 | 69 | $value = yield $fiber->resume($value); |
62 | 70 | } |
63 | 71 | return $fiber->getReturn(); |
@@ -67,15 +75,21 @@ discard block |
||
67 | 75 | } |
68 | 76 | |
69 | 77 | |
70 | - while ($fiberGenerators !== []) { |
|
71 | - foreach ($fiberGenerators as $key => $generator) { |
|
72 | - try { |
|
78 | + while ($fiberGenerators !== []) |
|
79 | + { |
|
80 | + foreach ($fiberGenerators as $key => $generator) |
|
81 | + { |
|
82 | + try |
|
83 | + { |
|
73 | 84 | $suspends[$key] = $generator->send($suspends[$key]); |
74 | - if (!$generator->valid()) { |
|
85 | + if (!$generator->valid()) |
|
86 | + { |
|
75 | 87 | $results[$key] = $generator->getReturn(); |
76 | 88 | unset($fiberGenerators[$key]); |
77 | 89 | } |
78 | - } catch (\Throwable $e) { |
|
90 | + } |
|
91 | + catch (\Throwable $e) |
|
92 | + { |
|
79 | 93 | unset($fiberGenerators[$key]); |
80 | 94 | $results[$key] = $e; |
81 | 95 | } |
@@ -28,7 +28,8 @@ discard block |
||
28 | 28 | $root = new Container(); |
29 | 29 | $root->bind('foo', SampleClass::class); |
30 | 30 | |
31 | - $root->runScoped(function (ContainerInterface $c1) { |
|
31 | + $root->runScoped(function (ContainerInterface $c1) |
|
32 | + { |
|
32 | 33 | $c1->get('foo'); |
33 | 34 | }, bindings: ['foo' => SampleClass::class]); |
34 | 35 | |
@@ -61,7 +62,8 @@ discard block |
||
61 | 62 | { |
62 | 63 | $root = new Container(); |
63 | 64 | |
64 | - $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) { |
|
65 | + $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) |
|
66 | + { |
|
65 | 67 | $obj1 = $c1->get($alias); |
66 | 68 | $obj2 = $c1->get($alias); |
67 | 69 | |
@@ -86,14 +88,16 @@ discard block |
||
86 | 88 | { |
87 | 89 | $root = new Container(); |
88 | 90 | |
89 | - $root->runScoped(function (ContainerInterface $c1) use ($root) { |
|
91 | + $root->runScoped(function (ContainerInterface $c1) use ($root) |
|
92 | + { |
|
90 | 93 | $obj1 = $c1->get('foo'); |
91 | 94 | $this->weakMap->offsetSet($obj1, true); |
92 | 95 | |
93 | 96 | self::assertNotSame($root, $c1); |
94 | 97 | self::assertInstanceOf(stdClass::class, $obj1); |
95 | 98 | |
96 | - $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) { |
|
99 | + $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) |
|
100 | + { |
|
97 | 101 | $obj2 = $c2->get('foo'); |
98 | 102 | $this->weakMap->offsetSet($obj2, true); |
99 | 103 | |
@@ -122,14 +126,16 @@ discard block |
||
122 | 126 | $root->bindSingleton('bar', [Factory::class, 'makeStdClass']); |
123 | 127 | $root->bind(stdClass::class, new stdClass()); |
124 | 128 | |
125 | - $root->runScoped(function (ContainerInterface $c1) use ($root) { |
|
129 | + $root->runScoped(function (ContainerInterface $c1) use ($root) |
|
130 | + { |
|
126 | 131 | $obj1 = $c1->get('foo'); |
127 | 132 | $this->weakMap->offsetSet($obj1, true); |
128 | 133 | |
129 | 134 | self::assertInstanceOf(stdClass::class, $obj1); |
130 | 135 | // Singleton must be the same |
131 | 136 | self::assertSame($c1->get('bar'), $root->get('bar')); |
132 | - $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) { |
|
137 | + $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) |
|
138 | + { |
|
133 | 139 | $obj2 = $c2->get('foo'); |
134 | 140 | |
135 | 141 | self::assertInstanceOf(stdClass::class, $obj2); |
@@ -262,7 +268,8 @@ discard block |
||
262 | 268 | { |
263 | 269 | $root = new Container(); |
264 | 270 | |
265 | - $root->invoke(static function () use ($root) { |
|
271 | + $root->invoke(static function () use ($root) |
|
272 | + { |
|
266 | 273 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
267 | 274 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
268 | 275 | }); |
@@ -271,7 +278,8 @@ discard block |
||
271 | 278 | public function testRegisterContainerOnGet(): void |
272 | 279 | { |
273 | 280 | $root = new Container(); |
274 | - $root->bind('foo', function () use ($root) { |
|
281 | + $root->bind('foo', function () use ($root) |
|
282 | + { |
|
275 | 283 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
276 | 284 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
277 | 285 | }); |
@@ -282,7 +290,8 @@ discard block |
||
282 | 290 | public function testRegisterContainerOnMake(): void |
283 | 291 | { |
284 | 292 | $root = new Container(); |
285 | - $root->bind('foo', function () use ($root) { |
|
293 | + $root->bind('foo', function () use ($root) |
|
294 | + { |
|
286 | 295 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
287 | 296 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
288 | 297 | }); |
@@ -23,6 +23,6 @@ |
||
23 | 23 | public readonly ?string $name = null, |
24 | 24 | public readonly array $bindings = [], |
25 | 25 | public readonly bool $autowire = true, |
26 | - ) { |
|
26 | + ){ |
|
27 | 27 | } |
28 | 28 | } |
@@ -170,23 +170,29 @@ discard block |
||
170 | 170 | */ |
171 | 171 | public function runScope(Scope|array $bindings, callable $scope): mixed |
172 | 172 | { |
173 | - if (!\is_array($bindings)) { |
|
173 | + if (!\is_array($bindings)) |
|
174 | + { |
|
174 | 175 | return $this->runIsolatedScope($bindings, $scope); |
175 | 176 | } |
176 | 177 | |
177 | 178 | $binds = &$this->state->bindings; |
178 | 179 | $singletons = &$this->state->singletons; |
179 | 180 | $cleanup = $previous = $prevSin = []; |
180 | - foreach ($bindings as $alias => $resolver) { |
|
181 | + foreach ($bindings as $alias => $resolver) |
|
182 | + { |
|
181 | 183 | // Store previous bindings |
182 | - if (isset($binds[$alias])) { |
|
184 | + if (isset($binds[$alias])) |
|
185 | + { |
|
183 | 186 | $previous[$alias] = $binds[$alias]; |
184 | - } else { |
|
187 | + } |
|
188 | + else |
|
189 | + { |
|
185 | 190 | // Store bindings to be removed |
186 | 191 | $cleanup[] = $alias; |
187 | 192 | } |
188 | 193 | // Store previous singletons |
189 | - if (isset($singletons[$alias])) { |
|
194 | + if (isset($singletons[$alias])) |
|
195 | + { |
|
190 | 196 | $prevSin[$alias] = $singletons[$alias]; |
191 | 197 | unset($singletons[$alias]); |
192 | 198 | } |
@@ -194,21 +200,27 @@ discard block |
||
194 | 200 | $this->binder->bind($alias, $resolver); |
195 | 201 | } |
196 | 202 | |
197 | - try { |
|
203 | + try |
|
204 | + { |
|
198 | 205 | return ContainerScope::getContainer() !== $this |
199 | 206 | ? ContainerScope::runScope($this, $scope) |
200 | 207 | : $scope($this); |
201 | - } finally { |
|
208 | + } |
|
209 | + finally |
|
210 | + { |
|
202 | 211 | // Remove new bindings |
203 | - foreach ($cleanup as $alias) { |
|
212 | + foreach ($cleanup as $alias) |
|
213 | + { |
|
204 | 214 | unset($binds[$alias], $singletons[$alias]); |
205 | 215 | } |
206 | 216 | // Restore previous bindings |
207 | - foreach ($previous as $alias => $resolver) { |
|
217 | + foreach ($previous as $alias => $resolver) |
|
218 | + { |
|
208 | 219 | $binds[$alias] = $resolver; |
209 | 220 | } |
210 | 221 | // Restore singletons |
211 | - foreach ($prevSin as $alias => $instance) { |
|
222 | + foreach ($prevSin as $alias => $instance) |
|
223 | + { |
|
212 | 224 | $singletons[$alias] = $instance; |
213 | 225 | } |
214 | 226 | } |
@@ -265,7 +277,8 @@ discard block |
||
265 | 277 | */ |
266 | 278 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
267 | 279 | { |
268 | - if ($force) { |
|
280 | + if ($force) |
|
281 | + { |
|
269 | 282 | $this->binder->removeBinding($alias); |
270 | 283 | } |
271 | 284 | |
@@ -336,8 +349,10 @@ discard block |
||
336 | 349 | ]); |
337 | 350 | |
338 | 351 | // Create container services |
339 | - foreach ($container->config as $property => $class) { |
|
340 | - if (\property_exists($container, $property)) { |
|
352 | + foreach ($container->config as $property => $class) |
|
353 | + { |
|
354 | + if (\property_exists($container, $property)) |
|
355 | + { |
|
341 | 356 | $container->$property = $constructor->get($property, $class); |
342 | 357 | } |
343 | 358 | } |
@@ -351,7 +366,8 @@ discard block |
||
351 | 366 | private function closeScope(): void |
352 | 367 | { |
353 | 368 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
354 | - if (!isset($this->scope)) { |
|
369 | + if (!isset($this->scope)) |
|
370 | + { |
|
355 | 371 | $this->destruct(); |
356 | 372 | return; |
357 | 373 | } |
@@ -360,10 +376,14 @@ discard block |
||
360 | 376 | |
361 | 377 | // Run finalizers |
362 | 378 | $errors = []; |
363 | - foreach ($this->state->finalizers as $finalizer) { |
|
364 | - try { |
|
379 | + foreach ($this->state->finalizers as $finalizer) |
|
380 | + { |
|
381 | + try |
|
382 | + { |
|
365 | 383 | $this->invoker->invoke($finalizer); |
366 | - } catch (\Throwable $e) { |
|
384 | + } |
|
385 | + catch (\Throwable $e) |
|
386 | + { |
|
367 | 387 | $errors[] = $e; |
368 | 388 | } |
369 | 389 | } |
@@ -372,7 +392,8 @@ discard block |
||
372 | 392 | $this->destruct(); |
373 | 393 | |
374 | 394 | // Throw collected errors |
375 | - if ($errors !== []) { |
|
395 | + if ($errors !== []) |
|
396 | + { |
|
376 | 397 | throw new FinalizersException($scopeName, $errors); |
377 | 398 | } |
378 | 399 | } |
@@ -394,18 +415,22 @@ discard block |
||
394 | 415 | $container->scope->setParent($this, $this->scope); |
395 | 416 | |
396 | 417 | // Add specific bindings |
397 | - foreach ($config->bindings as $alias => $resolver) { |
|
418 | + foreach ($config->bindings as $alias => $resolver) |
|
419 | + { |
|
398 | 420 | $container->binder->bind($alias, $resolver); |
399 | 421 | } |
400 | 422 | |
401 | 423 | return ContainerScope::runScope( |
402 | 424 | $container, |
403 | 425 | static function (self $container) use ($config, $closure): mixed { |
404 | - try { |
|
426 | + try |
|
427 | + { |
|
405 | 428 | return $config->autowire |
406 | 429 | ? $container->invoke($closure) |
407 | 430 | : $closure($container); |
408 | - } finally { |
|
431 | + } |
|
432 | + finally |
|
433 | + { |
|
409 | 434 | $container->closeScope(); |
410 | 435 | } |
411 | 436 | } |
@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
48 | 48 | |
49 | 49 | private Internal\State $state; |
50 | - private ResolverInterface|Internal\Resolver $resolver; |
|
51 | - private FactoryInterface|Internal\Factory $factory; |
|
52 | - private ContainerInterface|Internal\Container $container; |
|
53 | - private BinderInterface|Internal\Binder $binder; |
|
54 | - private InvokerInterface|Internal\Invoker $invoker; |
|
50 | + private ResolverInterface | Internal\Resolver $resolver; |
|
51 | + private FactoryInterface | Internal\Factory $factory; |
|
52 | + private ContainerInterface | Internal\Container $container; |
|
53 | + private BinderInterface | Internal\Binder $binder; |
|
54 | + private InvokerInterface | Internal\Invoker $invoker; |
|
55 | 55 | private Internal\Scope $scope; |
56 | 56 | |
57 | 57 | /** |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | public function __construct( |
61 | 61 | private Config $config = new Config(), |
62 | 62 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
63 | - ) { |
|
63 | + ){ |
|
64 | 64 | $this->initServices($this, $scopeName); |
65 | 65 | |
66 | 66 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | * @throws \Throwable |
113 | 113 | * @psalm-suppress TooManyArguments |
114 | 114 | */ |
115 | - public function make(string $alias, array $parameters = [], \Stringable|string|null $context = null): mixed |
|
115 | + public function make(string $alias, array $parameters = [], \Stringable | string | null $context = null): mixed |
|
116 | 116 | { |
117 | 117 | return ContainerScope::getContainer() === $this |
118 | 118 | ? $this->factory->make($alias, $parameters, $context) |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @throws \Throwable |
139 | 139 | * @psalm-suppress TooManyArguments |
140 | 140 | */ |
141 | - public function get(string|Autowire $id, \Stringable|string|null $context = null): mixed |
|
141 | + public function get(string | Autowire $id, \Stringable | string | null $context = null): mixed |
|
142 | 142 | { |
143 | 143 | return ContainerScope::getContainer() === $this |
144 | 144 | ? $this->container->get($id, $context) |
@@ -168,25 +168,25 @@ discard block |
||
168 | 168 | /** |
169 | 169 | * @throws \Throwable |
170 | 170 | */ |
171 | - public function runScope(Scope|array $bindings, callable $scope): mixed |
|
171 | + public function runScope(Scope | array $bindings, callable $scope): mixed |
|
172 | 172 | { |
173 | - if (!\is_array($bindings)) { |
|
173 | + if (!\is_array($bindings)){ |
|
174 | 174 | return $this->runIsolatedScope($bindings, $scope); |
175 | 175 | } |
176 | 176 | |
177 | 177 | $binds = &$this->state->bindings; |
178 | 178 | $singletons = &$this->state->singletons; |
179 | 179 | $cleanup = $previous = $prevSin = []; |
180 | - foreach ($bindings as $alias => $resolver) { |
|
180 | + foreach ($bindings as $alias => $resolver){ |
|
181 | 181 | // Store previous bindings |
182 | - if (isset($binds[$alias])) { |
|
182 | + if (isset($binds[$alias])){ |
|
183 | 183 | $previous[$alias] = $binds[$alias]; |
184 | - } else { |
|
184 | + }else{ |
|
185 | 185 | // Store bindings to be removed |
186 | 186 | $cleanup[] = $alias; |
187 | 187 | } |
188 | 188 | // Store previous singletons |
189 | - if (isset($singletons[$alias])) { |
|
189 | + if (isset($singletons[$alias])){ |
|
190 | 190 | $prevSin[$alias] = $singletons[$alias]; |
191 | 191 | unset($singletons[$alias]); |
192 | 192 | } |
@@ -194,21 +194,21 @@ discard block |
||
194 | 194 | $this->binder->bind($alias, $resolver); |
195 | 195 | } |
196 | 196 | |
197 | - try { |
|
197 | + try{ |
|
198 | 198 | return ContainerScope::getContainer() !== $this |
199 | 199 | ? ContainerScope::runScope($this, $scope) |
200 | 200 | : $scope($this); |
201 | - } finally { |
|
201 | + }finally{ |
|
202 | 202 | // Remove new bindings |
203 | - foreach ($cleanup as $alias) { |
|
203 | + foreach ($cleanup as $alias){ |
|
204 | 204 | unset($binds[$alias], $singletons[$alias]); |
205 | 205 | } |
206 | 206 | // Restore previous bindings |
207 | - foreach ($previous as $alias => $resolver) { |
|
207 | + foreach ($previous as $alias => $resolver){ |
|
208 | 208 | $binds[$alias] = $resolver; |
209 | 209 | } |
210 | 210 | // Restore singletons |
211 | - foreach ($prevSin as $alias => $instance) { |
|
211 | + foreach ($prevSin as $alias => $instance){ |
|
212 | 212 | $singletons[$alias] = $instance; |
213 | 213 | } |
214 | 214 | } |
@@ -254,9 +254,9 @@ discard block |
||
254 | 254 | * @param bool $force If the value is false, an exception will be thrown when attempting |
255 | 255 | * to bind an already constructed singleton. |
256 | 256 | */ |
257 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
|
257 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, bool $force = true): void |
|
258 | 258 | { |
259 | - if ($force) { |
|
259 | + if ($force){ |
|
260 | 260 | $this->binder->removeBinding($alias); |
261 | 261 | } |
262 | 262 | |
@@ -327,8 +327,8 @@ discard block |
||
327 | 327 | ]); |
328 | 328 | |
329 | 329 | // Create container services |
330 | - foreach ($container->config as $property => $class) { |
|
331 | - if (\property_exists($container, $property)) { |
|
330 | + foreach ($container->config as $property => $class){ |
|
331 | + if (\property_exists($container, $property)){ |
|
332 | 332 | $container->$property = $constructor->get($property, $class); |
333 | 333 | } |
334 | 334 | } |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | private function closeScope(): void |
343 | 343 | { |
344 | 344 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
345 | - if (!isset($this->scope)) { |
|
345 | + if (!isset($this->scope)){ |
|
346 | 346 | $this->destruct(); |
347 | 347 | return; |
348 | 348 | } |
@@ -351,10 +351,10 @@ discard block |
||
351 | 351 | |
352 | 352 | // Run finalizers |
353 | 353 | $errors = []; |
354 | - foreach ($this->state->finalizers as $finalizer) { |
|
355 | - try { |
|
354 | + foreach ($this->state->finalizers as $finalizer){ |
|
355 | + try{ |
|
356 | 356 | $this->invoker->invoke($finalizer); |
357 | - } catch (\Throwable $e) { |
|
357 | + }catch (\Throwable $e){ |
|
358 | 358 | $errors[] = $e; |
359 | 359 | } |
360 | 360 | } |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | $this->destruct(); |
364 | 364 | |
365 | 365 | // Throw collected errors |
366 | - if ($errors !== []) { |
|
366 | + if ($errors !== []){ |
|
367 | 367 | throw new FinalizersException($scopeName, $errors); |
368 | 368 | } |
369 | 369 | } |
@@ -385,18 +385,18 @@ discard block |
||
385 | 385 | $container->scope->setParent($this, $this->scope); |
386 | 386 | |
387 | 387 | // Add specific bindings |
388 | - foreach ($config->bindings as $alias => $resolver) { |
|
388 | + foreach ($config->bindings as $alias => $resolver){ |
|
389 | 389 | $container->binder->bind($alias, $resolver); |
390 | 390 | } |
391 | 391 | |
392 | 392 | return ContainerScope::runScope( |
393 | 393 | $container, |
394 | 394 | static function (self $container) use ($config, $closure): mixed { |
395 | - try { |
|
395 | + try{ |
|
396 | 396 | return $config->autowire |
397 | 397 | ? $container->invoke($closure) |
398 | 398 | : $closure($container); |
399 | - } finally { |
|
399 | + }finally{ |
|
400 | 400 | $container->closeScope(); |
401 | 401 | } |
402 | 402 | } |
@@ -10,7 +10,7 @@ |
||
10 | 10 | { |
11 | 11 | public function __construct( |
12 | 12 | #[Proxy] public ContextInterface $context, |
13 | - ) { |
|
13 | + ){ |
|
14 | 14 | } |
15 | 15 | |
16 | 16 | public function getContext(): ContextInterface |
@@ -7,11 +7,11 @@ |
||
7 | 7 | final class Context implements ContextInterface |
8 | 8 | { |
9 | 9 | public function __construct( |
10 | - public \Stringable|string|null $value, |
|
11 | - ) { |
|
10 | + public \Stringable | string | null $value, |
|
11 | + ){ |
|
12 | 12 | } |
13 | 13 | |
14 | - public function getValue(): \Stringable|string|null |
|
14 | + public function getValue(): \Stringable | string | null |
|
15 | 15 | { |
16 | 16 | return $this->value; |
17 | 17 | } |
@@ -6,5 +6,5 @@ |
||
6 | 6 | |
7 | 7 | interface ContextInterface |
8 | 8 | { |
9 | - public function getValue(): \Stringable|string|null; |
|
9 | + public function getValue(): \Stringable | string | null; |
|
10 | 10 | } |
@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | final class Resolver |
14 | 14 | { |
15 | - public static function resolve(string $alias, \Stringable|string|null $context = null): object |
|
15 | + public static function resolve(string $alias, \Stringable | string | null $context = null): object |
|
16 | 16 | { |
17 | 17 | $c = ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
18 | 18 | |
19 | - try { |
|
19 | + try{ |
|
20 | 20 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
21 | 21 | 'Resolved `null` from the container.', |
22 | 22 | ); |
23 | - } catch (ContainerException $e) { |
|
23 | + }catch (ContainerException $e){ |
|
24 | 24 | throw new ContainerException( |
25 | 25 | // todo : find required scope |
26 | 26 | \sprintf('Unable to resolve `%s` in a Proxy.', $alias), |
@@ -16,11 +16,14 @@ |
||
16 | 16 | { |
17 | 17 | $c = ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
18 | 18 | |
19 | - try { |
|
19 | + try |
|
20 | + { |
|
20 | 21 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
21 | 22 | 'Resolved `null` from the container.', |
22 | 23 | ); |
23 | - } catch (ContainerException $e) { |
|
24 | + } |
|
25 | + catch (ContainerException $e) |
|
26 | + { |
|
24 | 27 | throw new ContainerException( |
25 | 28 | // todo : find required scope |
26 | 29 | \sprintf('Unable to resolve `%s` in a Proxy.', $alias), |
@@ -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,22 +284,22 @@ 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->getParent(); |
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 | return $parent->make($alias, $parameters, $context); |
298 | - } catch (BadScopeException $e) { |
|
299 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
298 | + }catch (BadScopeException $e){ |
|
299 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
300 | 300 | throw $e; |
301 | 301 | } |
302 | - } catch (ContainerExceptionInterface $e) { |
|
302 | + }catch (ContainerExceptionInterface $e){ |
|
303 | 303 | $className = match (true) { |
304 | 304 | $e instanceof NotFoundException => NotFoundException::class, |
305 | 305 | default => ContainerException::class, |
@@ -308,19 +308,19 @@ discard block |
||
308 | 308 | 'Can\'t resolve `%s`.', |
309 | 309 | $alias, |
310 | 310 | )), previous: $e); |
311 | - } finally { |
|
311 | + }finally{ |
|
312 | 312 | $this->tracer->pop(false); |
313 | 313 | } |
314 | 314 | } |
315 | 315 | |
316 | 316 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
317 | - try { |
|
317 | + try{ |
|
318 | 318 | //No direct instructions how to construct class, make is automatically |
319 | 319 | return $this->autowire( |
320 | 320 | new Ctx(alias: $alias, class: $alias, context: $context), |
321 | 321 | $parameters, |
322 | 322 | ); |
323 | - } finally { |
|
323 | + }finally{ |
|
324 | 324 | $this->tracer->pop(false); |
325 | 325 | } |
326 | 326 | } |
@@ -341,7 +341,7 @@ discard block |
||
341 | 341 | && |
342 | 342 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
343 | 343 | )) |
344 | - ) { |
|
344 | + ){ |
|
345 | 345 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
346 | 346 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
347 | 347 | $this->tracer->getRootAlias(), |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | // Check scope name |
371 | 371 | $ctx->reflection = new \ReflectionClass($instance); |
372 | 372 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
373 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
373 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()){ |
|
374 | 374 | throw new BadScopeException($scopeName, $instance::class); |
375 | 375 | } |
376 | 376 | |
@@ -397,24 +397,24 @@ discard block |
||
397 | 397 | array $arguments, |
398 | 398 | ): object { |
399 | 399 | $class = $ctx->class; |
400 | - try { |
|
400 | + try{ |
|
401 | 401 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
402 | - } catch (\ReflectionException $e) { |
|
402 | + }catch (\ReflectionException $e){ |
|
403 | 403 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
404 | 404 | } |
405 | 405 | |
406 | 406 | // Check scope name |
407 | 407 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
408 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
408 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
409 | 409 | throw new BadScopeException($scope, $class); |
410 | 410 | } |
411 | 411 | |
412 | 412 | // We have to construct class using external injector when we know the exact context |
413 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
413 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
414 | 414 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
415 | 415 | } |
416 | 416 | |
417 | - if (!$reflection->isInstantiable()) { |
|
417 | + if (!$reflection->isInstantiable()){ |
|
418 | 418 | $itIs = match (true) { |
419 | 419 | $reflection->isEnum() => 'Enum', |
420 | 420 | $reflection->isAbstract() => 'Abstract class', |
@@ -427,12 +427,12 @@ discard block |
||
427 | 427 | |
428 | 428 | $constructor = $reflection->getConstructor(); |
429 | 429 | |
430 | - if ($constructor !== null) { |
|
431 | - try { |
|
430 | + if ($constructor !== null){ |
|
431 | + try{ |
|
432 | 432 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
433 | 433 | $this->tracer->push(true); |
434 | 434 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
435 | - } catch (ValidationException $e) { |
|
435 | + }catch (ValidationException $e){ |
|
436 | 436 | throw new ContainerException( |
437 | 437 | $this->tracer->combineTraceMessage( |
438 | 438 | \sprintf( |
@@ -442,22 +442,22 @@ discard block |
||
442 | 442 | ) |
443 | 443 | ), |
444 | 444 | ); |
445 | - } finally { |
|
445 | + }finally{ |
|
446 | 446 | $this->tracer->pop(true); |
447 | 447 | $this->tracer->pop(false); |
448 | 448 | } |
449 | - try { |
|
449 | + try{ |
|
450 | 450 | // Using constructor with resolved arguments |
451 | 451 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
452 | 452 | $this->tracer->push(true); |
453 | 453 | $instance = new $class(...$args); |
454 | - } catch (\TypeError $e) { |
|
454 | + }catch (\TypeError $e){ |
|
455 | 455 | throw new WrongTypeException($constructor, $e); |
456 | - } finally { |
|
456 | + }finally{ |
|
457 | 457 | $this->tracer->pop(true); |
458 | 458 | $this->tracer->pop(false); |
459 | 459 | } |
460 | - } else { |
|
460 | + }else{ |
|
461 | 461 | // No constructor specified |
462 | 462 | $instance = $reflection->newInstance(); |
463 | 463 | } |
@@ -475,13 +475,13 @@ discard block |
||
475 | 475 | $instance = $this->runInflector($instance); |
476 | 476 | |
477 | 477 | //Declarative singletons |
478 | - if ($this->isSingleton($ctx)) { |
|
478 | + if ($this->isSingleton($ctx)){ |
|
479 | 479 | $this->state->singletons[$ctx->alias] = $instance; |
480 | 480 | } |
481 | 481 | |
482 | 482 | // Register finalizer |
483 | 483 | $finalizer = $this->getFinalizer($ctx, $instance); |
484 | - if ($finalizer !== null) { |
|
484 | + if ($finalizer !== null){ |
|
485 | 485 | $this->state->finalizers[] = $finalizer; |
486 | 486 | } |
487 | 487 | |
@@ -493,12 +493,12 @@ discard block |
||
493 | 493 | */ |
494 | 494 | private function isSingleton(Ctx $ctx): bool |
495 | 495 | { |
496 | - if ($ctx->singleton === true) { |
|
496 | + if ($ctx->singleton === true){ |
|
497 | 497 | return true; |
498 | 498 | } |
499 | 499 | |
500 | 500 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
501 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
501 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
502 | 502 | return true; |
503 | 503 | } |
504 | 504 | |
@@ -512,7 +512,7 @@ discard block |
||
512 | 512 | * @var Finalize|null $attribute |
513 | 513 | */ |
514 | 514 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
515 | - if ($attribute === null) { |
|
515 | + if ($attribute === null){ |
|
516 | 516 | return null; |
517 | 517 | } |
518 | 518 | |
@@ -526,10 +526,10 @@ discard block |
||
526 | 526 | { |
527 | 527 | $scope = $this->scope; |
528 | 528 | |
529 | - while ($scope !== null) { |
|
530 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
531 | - if ($instance instanceof $class) { |
|
532 | - foreach ($inflectors as $inflector) { |
|
529 | + while ($scope !== null){ |
|
530 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
531 | + if ($instance instanceof $class){ |
|
532 | + foreach ($inflectors as $inflector){ |
|
533 | 533 | $instance = $inflector->getParametersCount() > 1 |
534 | 534 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
535 | 535 | : ($inflector->inflector)($instance); |
@@ -545,9 +545,9 @@ discard block |
||
545 | 545 | |
546 | 546 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
547 | 547 | { |
548 | - try { |
|
548 | + try{ |
|
549 | 549 | $this->resolver->validateArguments($reflection, $arguments); |
550 | - } catch (\Throwable) { |
|
550 | + }catch (\Throwable){ |
|
551 | 551 | return false; |
552 | 552 | } |
553 | 553 |
@@ -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,18 +312,25 @@ discard block |
||
288 | 312 | ): mixed { |
289 | 313 | $parent = $this->scope->getParent(); |
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 | return $parent->make($alias, $parameters, $context); |
298 | - } catch (BadScopeException $e) { |
|
299 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
324 | + } |
|
325 | + catch (BadScopeException $e) |
|
326 | + { |
|
327 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
328 | + { |
|
300 | 329 | throw $e; |
301 | 330 | } |
302 | - } catch (ContainerExceptionInterface $e) { |
|
331 | + } |
|
332 | + catch (ContainerExceptionInterface $e) |
|
333 | + { |
|
303 | 334 | $className = match (true) { |
304 | 335 | $e instanceof NotFoundException => NotFoundException::class, |
305 | 336 | default => ContainerException::class, |
@@ -308,19 +339,24 @@ discard block |
||
308 | 339 | 'Can\'t resolve `%s`.', |
309 | 340 | $alias, |
310 | 341 | )), previous: $e); |
311 | - } finally { |
|
342 | + } |
|
343 | + finally |
|
344 | + { |
|
312 | 345 | $this->tracer->pop(false); |
313 | 346 | } |
314 | 347 | } |
315 | 348 | |
316 | 349 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
317 | - try { |
|
350 | + try |
|
351 | + { |
|
318 | 352 | //No direct instructions how to construct class, make is automatically |
319 | 353 | return $this->autowire( |
320 | 354 | new Ctx(alias: $alias, class: $alias, context: $context), |
321 | 355 | $parameters, |
322 | 356 | ); |
323 | - } finally { |
|
357 | + } |
|
358 | + finally |
|
359 | + { |
|
324 | 360 | $this->tracer->pop(false); |
325 | 361 | } |
326 | 362 | } |
@@ -370,7 +406,8 @@ discard block |
||
370 | 406 | // Check scope name |
371 | 407 | $ctx->reflection = new \ReflectionClass($instance); |
372 | 408 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
373 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
409 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
410 | + { |
|
374 | 411 | throw new BadScopeException($scopeName, $instance::class); |
375 | 412 | } |
376 | 413 | |
@@ -397,24 +434,30 @@ discard block |
||
397 | 434 | array $arguments, |
398 | 435 | ): object { |
399 | 436 | $class = $ctx->class; |
400 | - try { |
|
437 | + try |
|
438 | + { |
|
401 | 439 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
402 | - } catch (\ReflectionException $e) { |
|
440 | + } |
|
441 | + catch (\ReflectionException $e) |
|
442 | + { |
|
403 | 443 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
404 | 444 | } |
405 | 445 | |
406 | 446 | // Check scope name |
407 | 447 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
408 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
448 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
449 | + { |
|
409 | 450 | throw new BadScopeException($scope, $class); |
410 | 451 | } |
411 | 452 | |
412 | 453 | // We have to construct class using external injector when we know the exact context |
413 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
454 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
455 | + { |
|
414 | 456 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
415 | 457 | } |
416 | 458 | |
417 | - if (!$reflection->isInstantiable()) { |
|
459 | + if (!$reflection->isInstantiable()) |
|
460 | + { |
|
418 | 461 | $itIs = match (true) { |
419 | 462 | $reflection->isEnum() => 'Enum', |
420 | 463 | $reflection->isAbstract() => 'Abstract class', |
@@ -427,12 +470,16 @@ discard block |
||
427 | 470 | |
428 | 471 | $constructor = $reflection->getConstructor(); |
429 | 472 | |
430 | - if ($constructor !== null) { |
|
431 | - try { |
|
473 | + if ($constructor !== null) |
|
474 | + { |
|
475 | + try |
|
476 | + { |
|
432 | 477 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
433 | 478 | $this->tracer->push(true); |
434 | 479 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
435 | - } catch (ValidationException $e) { |
|
480 | + } |
|
481 | + catch (ValidationException $e) |
|
482 | + { |
|
436 | 483 | throw new ContainerException( |
437 | 484 | $this->tracer->combineTraceMessage( |
438 | 485 | \sprintf( |
@@ -442,22 +489,31 @@ discard block |
||
442 | 489 | ) |
443 | 490 | ), |
444 | 491 | ); |
445 | - } finally { |
|
492 | + } |
|
493 | + finally |
|
494 | + { |
|
446 | 495 | $this->tracer->pop(true); |
447 | 496 | $this->tracer->pop(false); |
448 | 497 | } |
449 | - try { |
|
498 | + try |
|
499 | + { |
|
450 | 500 | // Using constructor with resolved arguments |
451 | 501 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
452 | 502 | $this->tracer->push(true); |
453 | 503 | $instance = new $class(...$args); |
454 | - } catch (\TypeError $e) { |
|
504 | + } |
|
505 | + catch (\TypeError $e) |
|
506 | + { |
|
455 | 507 | throw new WrongTypeException($constructor, $e); |
456 | - } finally { |
|
508 | + } |
|
509 | + finally |
|
510 | + { |
|
457 | 511 | $this->tracer->pop(true); |
458 | 512 | $this->tracer->pop(false); |
459 | 513 | } |
460 | - } else { |
|
514 | + } |
|
515 | + else |
|
516 | + { |
|
461 | 517 | // No constructor specified |
462 | 518 | $instance = $reflection->newInstance(); |
463 | 519 | } |
@@ -475,13 +531,15 @@ discard block |
||
475 | 531 | $instance = $this->runInflector($instance); |
476 | 532 | |
477 | 533 | //Declarative singletons |
478 | - if ($this->isSingleton($ctx)) { |
|
534 | + if ($this->isSingleton($ctx)) |
|
535 | + { |
|
479 | 536 | $this->state->singletons[$ctx->alias] = $instance; |
480 | 537 | } |
481 | 538 | |
482 | 539 | // Register finalizer |
483 | 540 | $finalizer = $this->getFinalizer($ctx, $instance); |
484 | - if ($finalizer !== null) { |
|
541 | + if ($finalizer !== null) |
|
542 | + { |
|
485 | 543 | $this->state->finalizers[] = $finalizer; |
486 | 544 | } |
487 | 545 | |
@@ -493,12 +551,14 @@ discard block |
||
493 | 551 | */ |
494 | 552 | private function isSingleton(Ctx $ctx): bool |
495 | 553 | { |
496 | - if ($ctx->singleton === true) { |
|
554 | + if ($ctx->singleton === true) |
|
555 | + { |
|
497 | 556 | return true; |
498 | 557 | } |
499 | 558 | |
500 | 559 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
501 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
560 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
561 | + { |
|
502 | 562 | return true; |
503 | 563 | } |
504 | 564 | |
@@ -512,7 +572,8 @@ discard block |
||
512 | 572 | * @var Finalize|null $attribute |
513 | 573 | */ |
514 | 574 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
515 | - if ($attribute === null) { |
|
575 | + if ($attribute === null) |
|
576 | + { |
|
516 | 577 | return null; |
517 | 578 | } |
518 | 579 | |
@@ -526,10 +587,14 @@ discard block |
||
526 | 587 | { |
527 | 588 | $scope = $this->scope; |
528 | 589 | |
529 | - while ($scope !== null) { |
|
530 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
531 | - if ($instance instanceof $class) { |
|
532 | - foreach ($inflectors as $inflector) { |
|
590 | + while ($scope !== null) |
|
591 | + { |
|
592 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
593 | + { |
|
594 | + if ($instance instanceof $class) |
|
595 | + { |
|
596 | + foreach ($inflectors as $inflector) |
|
597 | + { |
|
533 | 598 | $instance = $inflector->getParametersCount() > 1 |
534 | 599 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
535 | 600 | : ($inflector->inflector)($instance); |
@@ -545,9 +610,12 @@ discard block |
||
545 | 610 | |
546 | 611 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
547 | 612 | { |
548 | - try { |
|
613 | + try |
|
614 | + { |
|
549 | 615 | $this->resolver->validateArguments($reflection, $arguments); |
550 | - } catch (\Throwable) { |
|
616 | + } |
|
617 | + catch (\Throwable) |
|
618 | + { |
|
551 | 619 | return false; |
552 | 620 | } |
553 | 621 |