@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | $root = new Container(); |
33 | 33 | $root->bind('foo', SampleClass::class); |
34 | 34 | |
35 | - $root->runScoped(function (ContainerInterface $c1) { |
|
35 | + $root->runScoped(function (ContainerInterface $c1){ |
|
36 | 36 | $c1->get('foo'); |
37 | 37 | }, bindings: ['foo' => SampleClass::class]); |
38 | 38 | |
@@ -319,7 +319,7 @@ discard block |
||
319 | 319 | |
320 | 320 | $root->runScope( |
321 | 321 | new Scope('foo'), |
322 | - function (ContainerInterface $c) { |
|
322 | + function (ContainerInterface $c){ |
|
323 | 323 | self::assertTrue($c->get('isFoo')); |
324 | 324 | $c->get('foo'); |
325 | 325 | } |
@@ -331,20 +331,20 @@ discard block |
||
331 | 331 | $root = new Container(); |
332 | 332 | $root->bindSingleton('sampleClass', SampleClass::class); |
333 | 333 | |
334 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
334 | + $root->runScope(new Scope('foo'), function (Container $container){ |
|
335 | 335 | $this->assertTrue($container->has('sampleClass')); |
336 | 336 | }); |
337 | 337 | |
338 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
339 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
338 | + $root->runScope(new Scope('foo'), function (Container $container){ |
|
339 | + $container->runScope(new Scope('bar'), function (Container $container){ |
|
340 | 340 | $this->assertTrue($container->has('sampleClass')); |
341 | 341 | }); |
342 | 342 | }); |
343 | 343 | |
344 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
344 | + $root->runScope(new Scope('foo'), function (Container $container){ |
|
345 | 345 | $container->bindSingleton('otherClass', SampleClass::class); |
346 | 346 | |
347 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
347 | + $container->runScope(new Scope('bar'), function (Container $container){ |
|
348 | 348 | $this->assertTrue($container->has('sampleClass')); |
349 | 349 | $this->assertTrue($container->has('otherClass')); |
350 | 350 | }); |
@@ -356,33 +356,33 @@ discard block |
||
356 | 356 | $root = new Container(); |
357 | 357 | $root->getBinder('foo')->bindSingleton('sampleClass', AttrScopeFoo::class); |
358 | 358 | |
359 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
359 | + $root->runScope(new Scope('foo'), function (Container $container){ |
|
360 | 360 | $this->assertTrue($container->has('sampleClass')); |
361 | 361 | }); |
362 | 362 | |
363 | - $root->runScope(new Scope('bar'), function (Container $container) { |
|
363 | + $root->runScope(new Scope('bar'), function (Container $container){ |
|
364 | 364 | $this->assertFalse($container->has('sampleClass')); |
365 | 365 | }); |
366 | 366 | |
367 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
368 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
367 | + $root->runScope(new Scope('foo'), function (Container $container){ |
|
368 | + $container->runScope(new Scope('bar'), function (Container $container){ |
|
369 | 369 | $this->assertTrue($container->has('sampleClass')); |
370 | 370 | }); |
371 | 371 | }); |
372 | 372 | |
373 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
373 | + $root->runScope(new Scope('foo'), function (Container $container){ |
|
374 | 374 | $container->bindSingleton('otherClass', AttrScopeFoo::class); |
375 | 375 | |
376 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
376 | + $container->runScope(new Scope('bar'), function (Container $container){ |
|
377 | 377 | $this->assertTrue($container->has('sampleClass')); |
378 | 378 | $this->assertTrue($container->has('otherClass')); |
379 | 379 | }); |
380 | 380 | }); |
381 | 381 | |
382 | - $root->runScope(new Scope('baz'), function (Container $container) { |
|
382 | + $root->runScope(new Scope('baz'), function (Container $container){ |
|
383 | 383 | $container->getBinder('foo')->bindSingleton('otherClass', AttrScopeFoo::class); |
384 | 384 | |
385 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
385 | + $container->runScope(new Scope('bar'), function (Container $container){ |
|
386 | 386 | $this->assertFalse($container->has('sampleClass')); |
387 | 387 | $this->assertFalse($container->has('otherClass')); |
388 | 388 | }); |
@@ -32,7 +32,8 @@ discard block |
||
32 | 32 | $root = new Container(); |
33 | 33 | $root->bind('foo', SampleClass::class); |
34 | 34 | |
35 | - $root->runScoped(function (ContainerInterface $c1) { |
|
35 | + $root->runScoped(function (ContainerInterface $c1) |
|
36 | + { |
|
36 | 37 | $c1->get('foo'); |
37 | 38 | }, bindings: ['foo' => SampleClass::class]); |
38 | 39 | |
@@ -65,7 +66,8 @@ discard block |
||
65 | 66 | { |
66 | 67 | $root = new Container(); |
67 | 68 | |
68 | - $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) { |
|
69 | + $root->runScoped(function (ContainerInterface $c1) use ($theSame, $alias) |
|
70 | + { |
|
69 | 71 | $obj1 = $c1->get($alias); |
70 | 72 | $obj2 = $c1->get($alias); |
71 | 73 | |
@@ -90,14 +92,16 @@ discard block |
||
90 | 92 | { |
91 | 93 | $root = new Container(); |
92 | 94 | |
93 | - $root->runScoped(function (ContainerInterface $c1) use ($root) { |
|
95 | + $root->runScoped(function (ContainerInterface $c1) use ($root) |
|
96 | + { |
|
94 | 97 | $obj1 = $c1->get('foo'); |
95 | 98 | $this->weakMap->offsetSet($obj1, true); |
96 | 99 | |
97 | 100 | self::assertNotSame($root, $c1); |
98 | 101 | self::assertInstanceOf(stdClass::class, $obj1); |
99 | 102 | |
100 | - $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) { |
|
103 | + $c1->runScoped(function (ContainerInterface $c2) use ($root, $c1, $obj1) |
|
104 | + { |
|
101 | 105 | $obj2 = $c2->get('foo'); |
102 | 106 | $this->weakMap->offsetSet($obj2, true); |
103 | 107 | |
@@ -126,14 +130,16 @@ discard block |
||
126 | 130 | $root->bindSingleton('bar', [Factory::class, 'makeStdClass']); |
127 | 131 | $root->bind(stdClass::class, new stdClass()); |
128 | 132 | |
129 | - $root->runScoped(function (ContainerInterface $c1) use ($root) { |
|
133 | + $root->runScoped(function (ContainerInterface $c1) use ($root) |
|
134 | + { |
|
130 | 135 | $obj1 = $c1->get('foo'); |
131 | 136 | $this->weakMap->offsetSet($obj1, true); |
132 | 137 | |
133 | 138 | self::assertInstanceOf(stdClass::class, $obj1); |
134 | 139 | // Singleton must be the same |
135 | 140 | self::assertSame($c1->get('bar'), $root->get('bar')); |
136 | - $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) { |
|
141 | + $c1->runScoped(function (ContainerInterface $c2) use ($root, $obj1) |
|
142 | + { |
|
137 | 143 | $obj2 = $c2->get('foo'); |
138 | 144 | |
139 | 145 | self::assertInstanceOf(stdClass::class, $obj2); |
@@ -266,7 +272,8 @@ discard block |
||
266 | 272 | { |
267 | 273 | $root = new Container(); |
268 | 274 | |
269 | - $root->invoke(static function () use ($root) { |
|
275 | + $root->invoke(static function () use ($root) |
|
276 | + { |
|
270 | 277 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
271 | 278 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
272 | 279 | }); |
@@ -275,7 +282,8 @@ discard block |
||
275 | 282 | public function testRegisterContainerOnGet(): void |
276 | 283 | { |
277 | 284 | $root = new Container(); |
278 | - $root->bind('foo', function () use ($root) { |
|
285 | + $root->bind('foo', function () use ($root) |
|
286 | + { |
|
279 | 287 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
280 | 288 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
281 | 289 | }); |
@@ -286,7 +294,8 @@ discard block |
||
286 | 294 | public function testRegisterContainerOnMake(): void |
287 | 295 | { |
288 | 296 | $root = new Container(); |
289 | - $root->bind('foo', function () use ($root) { |
|
297 | + $root->bind('foo', function () use ($root) |
|
298 | + { |
|
290 | 299 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
291 | 300 | self::assertSame($root, \Spiral\Core\ContainerScope::getContainer()); |
292 | 301 | }); |
@@ -304,7 +313,8 @@ discard block |
||
304 | 313 | $root->bind('isFoo', new Scalar(false)); |
305 | 314 | $root->getBinder('foo')->bind('isFoo', new Scalar(true)); |
306 | 315 | |
307 | - $root->bind('foo', function (#[Proxy] ContainerInterface $c, ContainerInterface $r) use ($root) { |
|
316 | + $root->bind('foo', function (#[Proxy] ContainerInterface $c, ContainerInterface $r) use ($root) |
|
317 | + { |
|
308 | 318 | // Direct |
309 | 319 | self::assertNotNull(\Spiral\Core\ContainerScope::getContainer()); |
310 | 320 | self::assertNotSame($root, \Spiral\Core\ContainerScope::getContainer()); |
@@ -319,7 +329,8 @@ discard block |
||
319 | 329 | |
320 | 330 | $root->runScope( |
321 | 331 | new Scope('foo'), |
322 | - function (ContainerInterface $c) { |
|
332 | + function (ContainerInterface $c) |
|
333 | + { |
|
323 | 334 | self::assertTrue($c->get('isFoo')); |
324 | 335 | $c->get('foo'); |
325 | 336 | } |
@@ -331,20 +342,25 @@ discard block |
||
331 | 342 | $root = new Container(); |
332 | 343 | $root->bindSingleton('sampleClass', SampleClass::class); |
333 | 344 | |
334 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
345 | + $root->runScope(new Scope('foo'), function (Container $container) |
|
346 | + { |
|
335 | 347 | $this->assertTrue($container->has('sampleClass')); |
336 | 348 | }); |
337 | 349 | |
338 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
339 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
350 | + $root->runScope(new Scope('foo'), function (Container $container) |
|
351 | + { |
|
352 | + $container->runScope(new Scope('bar'), function (Container $container) |
|
353 | + { |
|
340 | 354 | $this->assertTrue($container->has('sampleClass')); |
341 | 355 | }); |
342 | 356 | }); |
343 | 357 | |
344 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
358 | + $root->runScope(new Scope('foo'), function (Container $container) |
|
359 | + { |
|
345 | 360 | $container->bindSingleton('otherClass', SampleClass::class); |
346 | 361 | |
347 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
362 | + $container->runScope(new Scope('bar'), function (Container $container) |
|
363 | + { |
|
348 | 364 | $this->assertTrue($container->has('sampleClass')); |
349 | 365 | $this->assertTrue($container->has('otherClass')); |
350 | 366 | }); |
@@ -356,33 +372,41 @@ discard block |
||
356 | 372 | $root = new Container(); |
357 | 373 | $root->getBinder('foo')->bindSingleton('sampleClass', AttrScopeFoo::class); |
358 | 374 | |
359 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
375 | + $root->runScope(new Scope('foo'), function (Container $container) |
|
376 | + { |
|
360 | 377 | $this->assertTrue($container->has('sampleClass')); |
361 | 378 | }); |
362 | 379 | |
363 | - $root->runScope(new Scope('bar'), function (Container $container) { |
|
380 | + $root->runScope(new Scope('bar'), function (Container $container) |
|
381 | + { |
|
364 | 382 | $this->assertFalse($container->has('sampleClass')); |
365 | 383 | }); |
366 | 384 | |
367 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
368 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
385 | + $root->runScope(new Scope('foo'), function (Container $container) |
|
386 | + { |
|
387 | + $container->runScope(new Scope('bar'), function (Container $container) |
|
388 | + { |
|
369 | 389 | $this->assertTrue($container->has('sampleClass')); |
370 | 390 | }); |
371 | 391 | }); |
372 | 392 | |
373 | - $root->runScope(new Scope('foo'), function (Container $container) { |
|
393 | + $root->runScope(new Scope('foo'), function (Container $container) |
|
394 | + { |
|
374 | 395 | $container->bindSingleton('otherClass', AttrScopeFoo::class); |
375 | 396 | |
376 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
397 | + $container->runScope(new Scope('bar'), function (Container $container) |
|
398 | + { |
|
377 | 399 | $this->assertTrue($container->has('sampleClass')); |
378 | 400 | $this->assertTrue($container->has('otherClass')); |
379 | 401 | }); |
380 | 402 | }); |
381 | 403 | |
382 | - $root->runScope(new Scope('baz'), function (Container $container) { |
|
404 | + $root->runScope(new Scope('baz'), function (Container $container) |
|
405 | + { |
|
383 | 406 | $container->getBinder('foo')->bindSingleton('otherClass', AttrScopeFoo::class); |
384 | 407 | |
385 | - $container->runScope(new Scope('bar'), function (Container $container) { |
|
408 | + $container->runScope(new Scope('bar'), function (Container $container) |
|
409 | + { |
|
386 | 410 | $this->assertFalse($container->has('sampleClass')); |
387 | 411 | $this->assertFalse($container->has('otherClass')); |
388 | 412 | }); |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | |
19 | 19 | public function __construct( |
20 | 20 | private readonly ?string $scopeName = null, |
21 | - ) { |
|
21 | + ){ |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | public function getScopeName(): ?string |
@@ -38,9 +38,9 @@ discard block |
||
38 | 38 | $this->parentFactory = $factory; |
39 | 39 | |
40 | 40 | // Check a scope with the same name is not already registered |
41 | - if ($this->scopeName !== null) { |
|
41 | + if ($this->scopeName !== null){ |
|
42 | 42 | $tmp = $this; |
43 | - while ($tmp->parentScope !== null) { |
|
43 | + while ($tmp->parentScope !== null){ |
|
44 | 44 | $tmp = $tmp->parentScope; |
45 | 45 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
46 | 46 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | $result = [$this->scopeName]; |
69 | 69 | |
70 | 70 | $parent = $this; |
71 | - while ($parent->parentScope !== null) { |
|
71 | + while ($parent->parentScope !== null){ |
|
72 | 72 | $parent = $parent->parentScope; |
73 | 73 | $result[] = $parent->scopeName; |
74 | 74 | } |
@@ -38,9 +38,11 @@ discard block |
||
38 | 38 | $this->parentFactory = $factory; |
39 | 39 | |
40 | 40 | // Check a scope with the same name is not already registered |
41 | - if ($this->scopeName !== null) { |
|
41 | + if ($this->scopeName !== null) |
|
42 | + { |
|
42 | 43 | $tmp = $this; |
43 | - while ($tmp->parentScope !== null) { |
|
44 | + while ($tmp->parentScope !== null) |
|
45 | + { |
|
44 | 46 | $tmp = $tmp->parentScope; |
45 | 47 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
46 | 48 | } |
@@ -68,7 +70,8 @@ discard block |
||
68 | 70 | $result = [$this->scopeName]; |
69 | 71 | |
70 | 72 | $parent = $this; |
71 | - while ($parent->parentScope !== null) { |
|
73 | + while ($parent->parentScope !== null) |
|
74 | + { |
|
72 | 75 | $parent = $parent->parentScope; |
73 | 76 | $result[] = $parent->scopeName; |
74 | 77 | } |
@@ -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, $this->factory); |
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 | } |
@@ -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 | } |
@@ -256,7 +268,8 @@ discard block |
||
256 | 268 | */ |
257 | 269 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
258 | 270 | { |
259 | - if ($force) { |
|
271 | + if ($force) |
|
272 | + { |
|
260 | 273 | $this->binder->removeBinding($alias); |
261 | 274 | } |
262 | 275 | |
@@ -327,8 +340,10 @@ discard block |
||
327 | 340 | ]); |
328 | 341 | |
329 | 342 | // Create container services |
330 | - foreach ($container->config as $property => $class) { |
|
331 | - if (\property_exists($container, $property)) { |
|
343 | + foreach ($container->config as $property => $class) |
|
344 | + { |
|
345 | + if (\property_exists($container, $property)) |
|
346 | + { |
|
332 | 347 | $container->$property = $constructor->get($property, $class); |
333 | 348 | } |
334 | 349 | } |
@@ -342,7 +357,8 @@ discard block |
||
342 | 357 | private function closeScope(): void |
343 | 358 | { |
344 | 359 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
345 | - if (!isset($this->scope)) { |
|
360 | + if (!isset($this->scope)) |
|
361 | + { |
|
346 | 362 | $this->destruct(); |
347 | 363 | return; |
348 | 364 | } |
@@ -351,10 +367,14 @@ discard block |
||
351 | 367 | |
352 | 368 | // Run finalizers |
353 | 369 | $errors = []; |
354 | - foreach ($this->state->finalizers as $finalizer) { |
|
355 | - try { |
|
370 | + foreach ($this->state->finalizers as $finalizer) |
|
371 | + { |
|
372 | + try |
|
373 | + { |
|
356 | 374 | $this->invoker->invoke($finalizer); |
357 | - } catch (\Throwable $e) { |
|
375 | + } |
|
376 | + catch (\Throwable $e) |
|
377 | + { |
|
358 | 378 | $errors[] = $e; |
359 | 379 | } |
360 | 380 | } |
@@ -363,7 +383,8 @@ discard block |
||
363 | 383 | $this->destruct(); |
364 | 384 | |
365 | 385 | // Throw collected errors |
366 | - if ($errors !== []) { |
|
386 | + if ($errors !== []) |
|
387 | + { |
|
367 | 388 | throw new FinalizersException($scopeName, $errors); |
368 | 389 | } |
369 | 390 | } |
@@ -385,18 +406,22 @@ discard block |
||
385 | 406 | $container->scope->setParent($this, $this->scope, $this->factory); |
386 | 407 | |
387 | 408 | // Add specific bindings |
388 | - foreach ($config->bindings as $alias => $resolver) { |
|
409 | + foreach ($config->bindings as $alias => $resolver) |
|
410 | + { |
|
389 | 411 | $container->binder->bind($alias, $resolver); |
390 | 412 | } |
391 | 413 | |
392 | 414 | return ContainerScope::runScope( |
393 | 415 | $container, |
394 | 416 | static function (self $container) use ($config, $closure): mixed { |
395 | - try { |
|
417 | + try |
|
418 | + { |
|
396 | 419 | return $config->autowire |
397 | 420 | ? $container->invoke($closure) |
398 | 421 | : $closure($container); |
399 | - } finally { |
|
422 | + } |
|
423 | + finally |
|
424 | + { |
|
400 | 425 | $container->closeScope(); |
401 | 426 | } |
402 | 427 | } |
@@ -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,7 +371,7 @@ 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 | throw new BadScopeException($scopeName, $instance::class); |
376 | 376 | } |
377 | 377 | |
@@ -398,24 +398,24 @@ discard block |
||
398 | 398 | array $arguments, |
399 | 399 | ): object { |
400 | 400 | $class = $ctx->class; |
401 | - try { |
|
401 | + try{ |
|
402 | 402 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
403 | - } catch (\ReflectionException $e) { |
|
403 | + }catch (\ReflectionException $e){ |
|
404 | 404 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
405 | 405 | } |
406 | 406 | |
407 | 407 | // Check scope name |
408 | 408 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
409 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
409 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
410 | 410 | throw new BadScopeException($scope, $class); |
411 | 411 | } |
412 | 412 | |
413 | 413 | // We have to construct class using external injector when we know the exact context |
414 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
414 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
415 | 415 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
416 | 416 | } |
417 | 417 | |
418 | - if (!$reflection->isInstantiable()) { |
|
418 | + if (!$reflection->isInstantiable()){ |
|
419 | 419 | $itIs = match (true) { |
420 | 420 | $reflection->isEnum() => 'Enum', |
421 | 421 | $reflection->isAbstract() => 'Abstract class', |
@@ -428,12 +428,12 @@ discard block |
||
428 | 428 | |
429 | 429 | $constructor = $reflection->getConstructor(); |
430 | 430 | |
431 | - if ($constructor !== null) { |
|
432 | - try { |
|
431 | + if ($constructor !== null){ |
|
432 | + try{ |
|
433 | 433 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
434 | 434 | $this->tracer->push(true); |
435 | 435 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
436 | - } catch (ValidationException $e) { |
|
436 | + }catch (ValidationException $e){ |
|
437 | 437 | throw new ContainerException( |
438 | 438 | $this->tracer->combineTraceMessage( |
439 | 439 | \sprintf( |
@@ -443,22 +443,22 @@ discard block |
||
443 | 443 | ) |
444 | 444 | ), |
445 | 445 | ); |
446 | - } finally { |
|
446 | + }finally{ |
|
447 | 447 | $this->tracer->pop(true); |
448 | 448 | $this->tracer->pop(false); |
449 | 449 | } |
450 | - try { |
|
450 | + try{ |
|
451 | 451 | // Using constructor with resolved arguments |
452 | 452 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
453 | 453 | $this->tracer->push(true); |
454 | 454 | $instance = new $class(...$args); |
455 | - } catch (\TypeError $e) { |
|
455 | + }catch (\TypeError $e){ |
|
456 | 456 | throw new WrongTypeException($constructor, $e); |
457 | - } finally { |
|
457 | + }finally{ |
|
458 | 458 | $this->tracer->pop(true); |
459 | 459 | $this->tracer->pop(false); |
460 | 460 | } |
461 | - } else { |
|
461 | + }else{ |
|
462 | 462 | // No constructor specified |
463 | 463 | $instance = $reflection->newInstance(); |
464 | 464 | } |
@@ -476,13 +476,13 @@ discard block |
||
476 | 476 | $instance = $this->runInflector($instance); |
477 | 477 | |
478 | 478 | //Declarative singletons |
479 | - if ($this->isSingleton($ctx)) { |
|
479 | + if ($this->isSingleton($ctx)){ |
|
480 | 480 | $this->state->singletons[$ctx->alias] = $instance; |
481 | 481 | } |
482 | 482 | |
483 | 483 | // Register finalizer |
484 | 484 | $finalizer = $this->getFinalizer($ctx, $instance); |
485 | - if ($finalizer !== null) { |
|
485 | + if ($finalizer !== null){ |
|
486 | 486 | $this->state->finalizers[] = $finalizer; |
487 | 487 | } |
488 | 488 | |
@@ -494,12 +494,12 @@ discard block |
||
494 | 494 | */ |
495 | 495 | private function isSingleton(Ctx $ctx): bool |
496 | 496 | { |
497 | - if ($ctx->singleton === true) { |
|
497 | + if ($ctx->singleton === true){ |
|
498 | 498 | return true; |
499 | 499 | } |
500 | 500 | |
501 | 501 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
502 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
502 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
503 | 503 | return true; |
504 | 504 | } |
505 | 505 | |
@@ -513,7 +513,7 @@ discard block |
||
513 | 513 | * @var Finalize|null $attribute |
514 | 514 | */ |
515 | 515 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
516 | - if ($attribute === null) { |
|
516 | + if ($attribute === null){ |
|
517 | 517 | return null; |
518 | 518 | } |
519 | 519 | |
@@ -527,10 +527,10 @@ discard block |
||
527 | 527 | { |
528 | 528 | $scope = $this->scope; |
529 | 529 | |
530 | - while ($scope !== null) { |
|
531 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
532 | - if ($instance instanceof $class) { |
|
533 | - foreach ($inflectors as $inflector) { |
|
530 | + while ($scope !== null){ |
|
531 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
532 | + if ($instance instanceof $class){ |
|
533 | + foreach ($inflectors as $inflector){ |
|
534 | 534 | $instance = $inflector->getParametersCount() > 1 |
535 | 535 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
536 | 536 | : ($inflector->inflector)($instance); |
@@ -546,9 +546,9 @@ discard block |
||
546 | 546 | |
547 | 547 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
548 | 548 | { |
549 | - try { |
|
549 | + try{ |
|
550 | 550 | $this->resolver->validateArguments($reflection, $arguments); |
551 | - } catch (\Throwable) { |
|
551 | + }catch (\Throwable){ |
|
552 | 552 | return false; |
553 | 553 | } |
554 | 554 |
@@ -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,7 +407,8 @@ 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 | throw new BadScopeException($scopeName, $instance::class); |
376 | 413 | } |
377 | 414 | |
@@ -398,24 +435,30 @@ discard block |
||
398 | 435 | array $arguments, |
399 | 436 | ): object { |
400 | 437 | $class = $ctx->class; |
401 | - try { |
|
438 | + try |
|
439 | + { |
|
402 | 440 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
403 | - } catch (\ReflectionException $e) { |
|
441 | + } |
|
442 | + catch (\ReflectionException $e) |
|
443 | + { |
|
404 | 444 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
405 | 445 | } |
406 | 446 | |
407 | 447 | // Check scope name |
408 | 448 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
409 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
449 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
450 | + { |
|
410 | 451 | throw new BadScopeException($scope, $class); |
411 | 452 | } |
412 | 453 | |
413 | 454 | // We have to construct class using external injector when we know the exact context |
414 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
455 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
456 | + { |
|
415 | 457 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
416 | 458 | } |
417 | 459 | |
418 | - if (!$reflection->isInstantiable()) { |
|
460 | + if (!$reflection->isInstantiable()) |
|
461 | + { |
|
419 | 462 | $itIs = match (true) { |
420 | 463 | $reflection->isEnum() => 'Enum', |
421 | 464 | $reflection->isAbstract() => 'Abstract class', |
@@ -428,12 +471,16 @@ discard block |
||
428 | 471 | |
429 | 472 | $constructor = $reflection->getConstructor(); |
430 | 473 | |
431 | - if ($constructor !== null) { |
|
432 | - try { |
|
474 | + if ($constructor !== null) |
|
475 | + { |
|
476 | + try |
|
477 | + { |
|
433 | 478 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
434 | 479 | $this->tracer->push(true); |
435 | 480 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
436 | - } catch (ValidationException $e) { |
|
481 | + } |
|
482 | + catch (ValidationException $e) |
|
483 | + { |
|
437 | 484 | throw new ContainerException( |
438 | 485 | $this->tracer->combineTraceMessage( |
439 | 486 | \sprintf( |
@@ -443,22 +490,31 @@ discard block |
||
443 | 490 | ) |
444 | 491 | ), |
445 | 492 | ); |
446 | - } finally { |
|
493 | + } |
|
494 | + finally |
|
495 | + { |
|
447 | 496 | $this->tracer->pop(true); |
448 | 497 | $this->tracer->pop(false); |
449 | 498 | } |
450 | - try { |
|
499 | + try |
|
500 | + { |
|
451 | 501 | // Using constructor with resolved arguments |
452 | 502 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
453 | 503 | $this->tracer->push(true); |
454 | 504 | $instance = new $class(...$args); |
455 | - } catch (\TypeError $e) { |
|
505 | + } |
|
506 | + catch (\TypeError $e) |
|
507 | + { |
|
456 | 508 | throw new WrongTypeException($constructor, $e); |
457 | - } finally { |
|
509 | + } |
|
510 | + finally |
|
511 | + { |
|
458 | 512 | $this->tracer->pop(true); |
459 | 513 | $this->tracer->pop(false); |
460 | 514 | } |
461 | - } else { |
|
515 | + } |
|
516 | + else |
|
517 | + { |
|
462 | 518 | // No constructor specified |
463 | 519 | $instance = $reflection->newInstance(); |
464 | 520 | } |
@@ -476,13 +532,15 @@ discard block |
||
476 | 532 | $instance = $this->runInflector($instance); |
477 | 533 | |
478 | 534 | //Declarative singletons |
479 | - if ($this->isSingleton($ctx)) { |
|
535 | + if ($this->isSingleton($ctx)) |
|
536 | + { |
|
480 | 537 | $this->state->singletons[$ctx->alias] = $instance; |
481 | 538 | } |
482 | 539 | |
483 | 540 | // Register finalizer |
484 | 541 | $finalizer = $this->getFinalizer($ctx, $instance); |
485 | - if ($finalizer !== null) { |
|
542 | + if ($finalizer !== null) |
|
543 | + { |
|
486 | 544 | $this->state->finalizers[] = $finalizer; |
487 | 545 | } |
488 | 546 | |
@@ -494,12 +552,14 @@ discard block |
||
494 | 552 | */ |
495 | 553 | private function isSingleton(Ctx $ctx): bool |
496 | 554 | { |
497 | - if ($ctx->singleton === true) { |
|
555 | + if ($ctx->singleton === true) |
|
556 | + { |
|
498 | 557 | return true; |
499 | 558 | } |
500 | 559 | |
501 | 560 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
502 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
561 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
562 | + { |
|
503 | 563 | return true; |
504 | 564 | } |
505 | 565 | |
@@ -513,7 +573,8 @@ discard block |
||
513 | 573 | * @var Finalize|null $attribute |
514 | 574 | */ |
515 | 575 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
516 | - if ($attribute === null) { |
|
576 | + if ($attribute === null) |
|
577 | + { |
|
517 | 578 | return null; |
518 | 579 | } |
519 | 580 | |
@@ -527,10 +588,14 @@ discard block |
||
527 | 588 | { |
528 | 589 | $scope = $this->scope; |
529 | 590 | |
530 | - while ($scope !== null) { |
|
531 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
532 | - if ($instance instanceof $class) { |
|
533 | - foreach ($inflectors as $inflector) { |
|
591 | + while ($scope !== null) |
|
592 | + { |
|
593 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
594 | + { |
|
595 | + if ($instance instanceof $class) |
|
596 | + { |
|
597 | + foreach ($inflectors as $inflector) |
|
598 | + { |
|
534 | 599 | $instance = $inflector->getParametersCount() > 1 |
535 | 600 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
536 | 601 | : ($inflector->inflector)($instance); |
@@ -546,9 +611,12 @@ discard block |
||
546 | 611 | |
547 | 612 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
548 | 613 | { |
549 | - try { |
|
614 | + try |
|
615 | + { |
|
550 | 616 | $this->resolver->validateArguments($reflection, $arguments); |
551 | - } catch (\Throwable) { |
|
617 | + } |
|
618 | + catch (\Throwable) |
|
619 | + { |
|
552 | 620 | return false; |
553 | 621 | } |
554 | 622 |