@@ -99,7 +99,8 @@ discard block |
||
99 | 99 | $container->bind(ConfigsInterface::class, $configurator); |
100 | 100 | |
101 | 101 | $configurator->shouldReceive('createInjection') |
102 | - ->with(m::on(static function (ReflectionClass $r) { |
|
102 | + ->with(m::on(static function (ReflectionClass $r) |
|
103 | + { |
|
103 | 104 | return $r->getName() === TestConfig::class; |
104 | 105 | }), null) |
105 | 106 | ->andReturn($expected); |
@@ -116,7 +117,8 @@ discard block |
||
116 | 117 | $container->bind(ConfigsInterface::class, $configurator); |
117 | 118 | |
118 | 119 | $configurator->shouldReceive('createInjection') |
119 | - ->with(m::on(static function (ReflectionClass $r) { |
|
120 | + ->with(m::on(static function (ReflectionClass $r) |
|
121 | + { |
|
120 | 122 | return $r->getName() === TestConfig::class; |
121 | 123 | }), 'context') |
122 | 124 | ->andReturn($expected); |
@@ -135,9 +135,12 @@ discard block |
||
135 | 135 | { |
136 | 136 | $container = new Container(); |
137 | 137 | |
138 | - try { |
|
138 | + try |
|
139 | + { |
|
139 | 140 | $container->get('invalid'); |
140 | - } catch (ContainerException $e) { |
|
141 | + } |
|
142 | + catch (ContainerException $e) |
|
143 | + { |
|
141 | 144 | $this->assertSame( |
142 | 145 | <<<MARKDOWN |
143 | 146 | Can't resolve `invalid`: undefined class or binding `invalid`. |
@@ -169,9 +172,12 @@ discard block |
||
169 | 172 | { |
170 | 173 | $this->expectException(ContainerException::class); |
171 | 174 | |
172 | - try { |
|
175 | + try |
|
176 | + { |
|
173 | 177 | $container->get(ClassWithUndefinedDependency::class); |
174 | - } catch (ContainerException $e) { |
|
178 | + } |
|
179 | + catch (ContainerException $e) |
|
180 | + { |
|
175 | 181 | self::assertSame($message, $e->getMessage()); |
176 | 182 | |
177 | 183 | throw $e; |
@@ -28,7 +28,8 @@ |
||
28 | 28 | |
29 | 29 | public function hasInstance(string $alias): bool |
30 | 30 | { |
31 | - if (!$this->container->has($alias)) { |
|
31 | + if (!$this->container->has($alias)) |
|
32 | + { |
|
32 | 33 | return false; |
33 | 34 | } |
34 | 35 |
@@ -66,17 +66,20 @@ discard block |
||
66 | 66 | */ |
67 | 67 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
68 | 68 | { |
69 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
69 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
70 | + { |
|
70 | 71 | return $this->state->singletons[$alias]; |
71 | 72 | } |
72 | 73 | |
73 | 74 | $binding = $this->state->bindings[$alias] ?? null; |
74 | 75 | |
75 | - if ($binding === null) { |
|
76 | + if ($binding === null) |
|
77 | + { |
|
76 | 78 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
77 | 79 | } |
78 | 80 | |
79 | - try { |
|
81 | + try |
|
82 | + { |
|
80 | 83 | $this->tracer->push( |
81 | 84 | false, |
82 | 85 | action: 'resolve from binding', |
@@ -100,7 +103,9 @@ discard block |
||
100 | 103 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
101 | 104 | default => $binding, |
102 | 105 | }; |
103 | - } finally { |
|
106 | + } |
|
107 | + finally |
|
108 | + { |
|
104 | 109 | $this->state->bindings[$alias] ??= $binding; |
105 | 110 | $this->tracer->pop(true); |
106 | 111 | $this->tracer->pop(false); |
@@ -116,23 +121,29 @@ discard block |
||
116 | 121 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
117 | 122 | |
118 | 123 | // We have to construct class using external injector when we know exact context |
119 | - if ($arguments !== []) { |
|
124 | + if ($arguments !== []) |
|
125 | + { |
|
120 | 126 | // todo factory? |
121 | 127 | } |
122 | 128 | |
123 | 129 | $class = $ctx->class; |
124 | - try { |
|
130 | + try |
|
131 | + { |
|
125 | 132 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
126 | - } catch (\ReflectionException $e) { |
|
133 | + } |
|
134 | + catch (\ReflectionException $e) |
|
135 | + { |
|
127 | 136 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
128 | 137 | } |
129 | 138 | |
130 | 139 | $injector = $binding->injector; |
131 | 140 | |
132 | - try { |
|
141 | + try |
|
142 | + { |
|
133 | 143 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
134 | 144 | |
135 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
145 | + if (!$injectorInstance instanceof InjectorInterface) |
|
146 | + { |
|
136 | 147 | throw new InjectionException( |
137 | 148 | \sprintf( |
138 | 149 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -147,7 +158,8 @@ discard block |
||
147 | 158 | * @psalm-suppress RedundantCondition |
148 | 159 | */ |
149 | 160 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
150 | - if (!$reflection->isInstance($instance)) { |
|
161 | + if (!$reflection->isInstance($instance)) |
|
162 | + { |
|
151 | 163 | throw new InjectionException( |
152 | 164 | \sprintf( |
153 | 165 | "Invalid injection response for '%s'.", |
@@ -157,7 +169,9 @@ discard block |
||
157 | 169 | } |
158 | 170 | |
159 | 171 | return $instance; |
160 | - } finally { |
|
172 | + } |
|
173 | + finally |
|
174 | + { |
|
161 | 175 | $this->state->bindings[$reflection->getName()] ??= $binding; //new Injector($injector); |
162 | 176 | } |
163 | 177 | } |
@@ -176,7 +190,8 @@ discard block |
||
176 | 190 | //Binding is pointing to something else |
177 | 191 | : $this->make($binding->alias, $arguments, $context); |
178 | 192 | |
179 | - if ($binding->singleton && $arguments === []) { |
|
193 | + if ($binding->singleton && $arguments === []) |
|
194 | + { |
|
180 | 195 | $this->state->singletons[$alias] = $result; |
181 | 196 | } |
182 | 197 | |
@@ -217,11 +232,14 @@ discard block |
||
217 | 232 | array $arguments, |
218 | 233 | ): mixed { |
219 | 234 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context, singleton: $binding->singleton); |
220 | - try { |
|
235 | + try |
|
236 | + { |
|
221 | 237 | $instance = $binding::class === \Spiral\Core\Config\Factory::class && $binding->getParametersCount() === 0 |
222 | 238 | ? ($binding->factory)() |
223 | 239 | : $this->invoker->invoke($binding->factory, $arguments); |
224 | - } catch (NotCallableException $e) { |
|
240 | + } |
|
241 | + catch (NotCallableException $e) |
|
242 | + { |
|
225 | 243 | throw new ContainerException( |
226 | 244 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
227 | 245 | $e->getCode(), |
@@ -240,19 +258,24 @@ discard block |
||
240 | 258 | ): ?object { |
241 | 259 | $avoidCache = $arguments !== []; |
242 | 260 | |
243 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
244 | - try { |
|
261 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
262 | + { |
|
263 | + try |
|
264 | + { |
|
245 | 265 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
246 | 266 | |
247 | 267 | $object = $this->createInstance( |
248 | 268 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
249 | 269 | $arguments, |
250 | 270 | ); |
251 | - if ($avoidCache) { |
|
271 | + if ($avoidCache) |
|
272 | + { |
|
252 | 273 | return $object; |
253 | 274 | } |
254 | 275 | $binding->reference = WeakReference::create($object); |
255 | - } catch (\Throwable) { |
|
276 | + } |
|
277 | + catch (\Throwable) |
|
278 | + { |
|
256 | 279 | throw new ContainerException( |
257 | 280 | $this->tracer->combineTraceMessage( |
258 | 281 | \sprintf( |
@@ -262,7 +285,9 @@ discard block |
||
262 | 285 | ) |
263 | 286 | ) |
264 | 287 | ); |
265 | - } finally { |
|
288 | + } |
|
289 | + finally |
|
290 | + { |
|
266 | 291 | $this->tracer->pop(); |
267 | 292 | } |
268 | 293 | } |
@@ -274,18 +299,25 @@ discard block |
||
274 | 299 | { |
275 | 300 | $parent = $this->scope->getParent(); |
276 | 301 | |
277 | - if ($parent !== null) { |
|
278 | - try { |
|
302 | + if ($parent !== null) |
|
303 | + { |
|
304 | + try |
|
305 | + { |
|
279 | 306 | $this->tracer->push(false, ...[ |
280 | 307 | 'current scope' => $this->scope->getScopeName(), |
281 | 308 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
282 | 309 | ]); |
283 | 310 | return $parent->make($alias, $parameters, $context); |
284 | - } catch (BadScopeException $e) { |
|
285 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
311 | + } |
|
312 | + catch (BadScopeException $e) |
|
313 | + { |
|
314 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
315 | + { |
|
286 | 316 | throw $e; |
287 | 317 | } |
288 | - } catch (ContainerExceptionInterface $e) { |
|
318 | + } |
|
319 | + catch (ContainerExceptionInterface $e) |
|
320 | + { |
|
289 | 321 | $className = match (true) { |
290 | 322 | $e instanceof NotFoundException => NotFoundException::class, |
291 | 323 | default => ContainerException::class, |
@@ -294,19 +326,24 @@ discard block |
||
294 | 326 | 'Can\'t resolve `%s`.', |
295 | 327 | $alias, |
296 | 328 | )), previous: $e); |
297 | - } finally { |
|
329 | + } |
|
330 | + finally |
|
331 | + { |
|
298 | 332 | $this->tracer->pop(false); |
299 | 333 | } |
300 | 334 | } |
301 | 335 | |
302 | 336 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
303 | - try { |
|
337 | + try |
|
338 | + { |
|
304 | 339 | //No direct instructions how to construct class, make is automatically |
305 | 340 | return $this->autowire( |
306 | 341 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
307 | 342 | $parameters, |
308 | 343 | ); |
309 | - } finally { |
|
344 | + } |
|
345 | + finally |
|
346 | + { |
|
310 | 347 | $this->tracer->pop(false); |
311 | 348 | } |
312 | 349 | } |
@@ -357,7 +394,8 @@ discard block |
||
357 | 394 | // Check scope name |
358 | 395 | $ctx->reflection = new \ReflectionClass($instance); |
359 | 396 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
360 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
397 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
398 | + { |
|
361 | 399 | throw new BadScopeException($scopeName, $instance::class); |
362 | 400 | } |
363 | 401 | |
@@ -384,24 +422,30 @@ discard block |
||
384 | 422 | array $parameters, |
385 | 423 | ): object { |
386 | 424 | $class = $ctx->class; |
387 | - try { |
|
425 | + try |
|
426 | + { |
|
388 | 427 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
389 | - } catch (\ReflectionException $e) { |
|
428 | + } |
|
429 | + catch (\ReflectionException $e) |
|
430 | + { |
|
390 | 431 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
391 | 432 | } |
392 | 433 | |
393 | 434 | // Check scope name |
394 | 435 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
395 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
436 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
437 | + { |
|
396 | 438 | throw new BadScopeException($scope, $class); |
397 | 439 | } |
398 | 440 | |
399 | 441 | //We have to construct class using external injector when we know exact context |
400 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
442 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
443 | + { |
|
401 | 444 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx->class, $ctx->parameter, $parameters); |
402 | 445 | } |
403 | 446 | |
404 | - if (!$reflection->isInstantiable()) { |
|
447 | + if (!$reflection->isInstantiable()) |
|
448 | + { |
|
405 | 449 | $itIs = match (true) { |
406 | 450 | $reflection->isEnum() => 'Enum', |
407 | 451 | $reflection->isAbstract() => 'Abstract class', |
@@ -414,12 +458,16 @@ discard block |
||
414 | 458 | |
415 | 459 | $constructor = $reflection->getConstructor(); |
416 | 460 | |
417 | - if ($constructor !== null) { |
|
418 | - try { |
|
461 | + if ($constructor !== null) |
|
462 | + { |
|
463 | + try |
|
464 | + { |
|
419 | 465 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
420 | 466 | $this->tracer->push(true); |
421 | 467 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
422 | - } catch (ValidationException $e) { |
|
468 | + } |
|
469 | + catch (ValidationException $e) |
|
470 | + { |
|
423 | 471 | throw new ContainerException( |
424 | 472 | $this->tracer->combineTraceMessage( |
425 | 473 | \sprintf( |
@@ -429,22 +477,31 @@ discard block |
||
429 | 477 | ) |
430 | 478 | ), |
431 | 479 | ); |
432 | - } finally { |
|
480 | + } |
|
481 | + finally |
|
482 | + { |
|
433 | 483 | $this->tracer->pop(true); |
434 | 484 | $this->tracer->pop(false); |
435 | 485 | } |
436 | - try { |
|
486 | + try |
|
487 | + { |
|
437 | 488 | // Using constructor with resolved arguments |
438 | 489 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
439 | 490 | $this->tracer->push(true); |
440 | 491 | $instance = new $class(...$arguments); |
441 | - } catch (\TypeError $e) { |
|
492 | + } |
|
493 | + catch (\TypeError $e) |
|
494 | + { |
|
442 | 495 | throw new WrongTypeException($constructor, $e); |
443 | - } finally { |
|
496 | + } |
|
497 | + finally |
|
498 | + { |
|
444 | 499 | $this->tracer->pop(true); |
445 | 500 | $this->tracer->pop(false); |
446 | 501 | } |
447 | - } else { |
|
502 | + } |
|
503 | + else |
|
504 | + { |
|
448 | 505 | // No constructor specified |
449 | 506 | $instance = $reflection->newInstance(); |
450 | 507 | } |
@@ -470,13 +527,15 @@ discard block |
||
470 | 527 | $instance = $this->runInflector($instance); |
471 | 528 | |
472 | 529 | //Declarative singletons |
473 | - if ($this->isSingleton($ctx)) { |
|
530 | + if ($this->isSingleton($ctx)) |
|
531 | + { |
|
474 | 532 | $this->state->singletons[$ctx->alias] = $instance; |
475 | 533 | } |
476 | 534 | |
477 | 535 | // Register finalizer |
478 | 536 | $finalizer = $this->getFinalizer($ctx, $instance); |
479 | - if ($finalizer !== null) { |
|
537 | + if ($finalizer !== null) |
|
538 | + { |
|
480 | 539 | $this->state->finalizers[] = $finalizer; |
481 | 540 | } |
482 | 541 | |
@@ -488,12 +547,14 @@ discard block |
||
488 | 547 | */ |
489 | 548 | private function isSingleton(Ctx $ctx): bool |
490 | 549 | { |
491 | - if ($ctx->singleton === true) { |
|
550 | + if ($ctx->singleton === true) |
|
551 | + { |
|
492 | 552 | return true; |
493 | 553 | } |
494 | 554 | |
495 | 555 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
496 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
556 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
557 | + { |
|
497 | 558 | return true; |
498 | 559 | } |
499 | 560 | |
@@ -507,7 +568,8 @@ discard block |
||
507 | 568 | * @var Finalize|null $attribute |
508 | 569 | */ |
509 | 570 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
510 | - if ($attribute === null) { |
|
571 | + if ($attribute === null) |
|
572 | + { |
|
511 | 573 | return null; |
512 | 574 | } |
513 | 575 | |
@@ -521,10 +583,14 @@ discard block |
||
521 | 583 | { |
522 | 584 | $scope = $this->scope; |
523 | 585 | |
524 | - while ($scope !== null) { |
|
525 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
526 | - if ($instance instanceof $class) { |
|
527 | - foreach ($inflectors as $inflector) { |
|
586 | + while ($scope !== null) |
|
587 | + { |
|
588 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
589 | + { |
|
590 | + if ($instance instanceof $class) |
|
591 | + { |
|
592 | + foreach ($inflectors as $inflector) |
|
593 | + { |
|
528 | 594 | $instance = $inflector->getParametersCount() > 1 |
529 | 595 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
530 | 596 | : ($inflector->inflector)($instance); |
@@ -168,16 +168,21 @@ discard block |
||
168 | 168 | $binds = &$this->state->bindings; |
169 | 169 | $singletons = &$this->state->singletons; |
170 | 170 | $cleanup = $previous = $prevSin = []; |
171 | - foreach ($bindings as $alias => $resolver) { |
|
171 | + foreach ($bindings as $alias => $resolver) |
|
172 | + { |
|
172 | 173 | // Store previous bindings |
173 | - if (isset($binds[$alias])) { |
|
174 | + if (isset($binds[$alias])) |
|
175 | + { |
|
174 | 176 | $previous[$alias] = $binds[$alias]; |
175 | - } else { |
|
177 | + } |
|
178 | + else |
|
179 | + { |
|
176 | 180 | // Store bindings to be removed |
177 | 181 | $cleanup[] = $alias; |
178 | 182 | } |
179 | 183 | // Store previous singletons |
180 | - if (isset($singletons[$alias])) { |
|
184 | + if (isset($singletons[$alias])) |
|
185 | + { |
|
181 | 186 | $prevSin[$alias] = $singletons[$alias]; |
182 | 187 | unset($singletons[$alias]); |
183 | 188 | } |
@@ -185,21 +190,27 @@ discard block |
||
185 | 190 | $this->binder->bind($alias, $resolver); |
186 | 191 | } |
187 | 192 | |
188 | - try { |
|
193 | + try |
|
194 | + { |
|
189 | 195 | return ContainerScope::getContainer() !== $this |
190 | 196 | ? ContainerScope::runScope($this, $scope) |
191 | 197 | : $scope($this); |
192 | - } finally { |
|
198 | + } |
|
199 | + finally |
|
200 | + { |
|
193 | 201 | // Remove new bindings |
194 | - foreach ($cleanup as $alias) { |
|
202 | + foreach ($cleanup as $alias) |
|
203 | + { |
|
195 | 204 | unset($binds[$alias], $singletons[$alias]); |
196 | 205 | } |
197 | 206 | // Restore previous bindings |
198 | - foreach ($previous as $alias => $resolver) { |
|
207 | + foreach ($previous as $alias => $resolver) |
|
208 | + { |
|
199 | 209 | $binds[$alias] = $resolver; |
200 | 210 | } |
201 | 211 | // Restore singletons |
202 | - foreach ($prevSin as $alias => $instance) { |
|
212 | + foreach ($prevSin as $alias => $instance) |
|
213 | + { |
|
203 | 214 | $singletons[$alias] = $instance; |
204 | 215 | } |
205 | 216 | } |
@@ -213,32 +224,40 @@ discard block |
||
213 | 224 | // Open scope |
214 | 225 | $container = new self($this->config, $name); |
215 | 226 | |
216 | - try { |
|
227 | + try |
|
228 | + { |
|
217 | 229 | // Configure scope |
218 | 230 | $container->scope->setParent($this, $this->scope); |
219 | 231 | |
220 | 232 | // Add specific bindings |
221 | - foreach ($bindings as $alias => $resolver) { |
|
233 | + foreach ($bindings as $alias => $resolver) |
|
234 | + { |
|
222 | 235 | $container->binder->bind($alias, $resolver); |
223 | 236 | } |
224 | 237 | |
225 | 238 | return ContainerScope::runScope( |
226 | 239 | $container, |
227 | 240 | static function (self $container) use ($autowire, $closure): mixed { |
228 | - try { |
|
241 | + try |
|
242 | + { |
|
229 | 243 | return $autowire |
230 | 244 | ? $container->invoke($closure) |
231 | 245 | : $closure($container); |
232 | - } finally { |
|
246 | + } |
|
247 | + finally |
|
248 | + { |
|
233 | 249 | $container->closeScope(); |
234 | 250 | } |
235 | 251 | } |
236 | 252 | ); |
237 | - } finally { |
|
253 | + } |
|
254 | + finally |
|
255 | + { |
|
238 | 256 | // Check the container has not been leaked |
239 | 257 | $link = \WeakReference::create($container); |
240 | 258 | unset($container); |
241 | - if ($link->get() !== null) { |
|
259 | + if ($link->get() !== null) |
|
260 | + { |
|
242 | 261 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
243 | 262 | } |
244 | 263 | } |
@@ -340,8 +359,10 @@ discard block |
||
340 | 359 | ]); |
341 | 360 | |
342 | 361 | // Create container services |
343 | - foreach ($container->config as $property => $class) { |
|
344 | - if (\property_exists($container, $property)) { |
|
362 | + foreach ($container->config as $property => $class) |
|
363 | + { |
|
364 | + if (\property_exists($container, $property)) |
|
365 | + { |
|
345 | 366 | $container->$property = $constructor->get($property, $class); |
346 | 367 | } |
347 | 368 | } |
@@ -355,7 +376,8 @@ discard block |
||
355 | 376 | private function closeScope(): void |
356 | 377 | { |
357 | 378 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
358 | - if (!isset($this->scope)) { |
|
379 | + if (!isset($this->scope)) |
|
380 | + { |
|
359 | 381 | $this->destruct(); |
360 | 382 | return; |
361 | 383 | } |
@@ -364,10 +386,14 @@ discard block |
||
364 | 386 | |
365 | 387 | // Run finalizers |
366 | 388 | $errors = []; |
367 | - foreach ($this->state->finalizers as $finalizer) { |
|
368 | - try { |
|
389 | + foreach ($this->state->finalizers as $finalizer) |
|
390 | + { |
|
391 | + try |
|
392 | + { |
|
369 | 393 | $this->invoker->invoke($finalizer); |
370 | - } catch (\Throwable $e) { |
|
394 | + } |
|
395 | + catch (\Throwable $e) |
|
396 | + { |
|
371 | 397 | $errors[] = $e; |
372 | 398 | } |
373 | 399 | } |
@@ -376,7 +402,8 @@ discard block |
||
376 | 402 | $this->destruct(); |
377 | 403 | |
378 | 404 | // Throw collected errors |
379 | - if ($errors !== []) { |
|
405 | + if ($errors !== []) |
|
406 | + { |
|
380 | 407 | throw new FinalizersException($scopeName, $errors); |
381 | 408 | } |
382 | 409 | } |
@@ -39,14 +39,18 @@ discard block |
||
39 | 39 | */ |
40 | 40 | public function bind(string $alias, mixed $resolver): void |
41 | 41 | { |
42 | - if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) { |
|
42 | + if ($resolver instanceof Inflector && (\interface_exists($alias) || \class_exists($alias))) |
|
43 | + { |
|
43 | 44 | $this->state->inflectors[$alias][] = $resolver; |
44 | 45 | return; |
45 | 46 | } |
46 | 47 | |
47 | - try { |
|
48 | + try |
|
49 | + { |
|
48 | 50 | $config = $this->makeConfig($resolver, false); |
49 | - } catch (\Throwable $e) { |
|
51 | + } |
|
52 | + catch (\Throwable $e) |
|
53 | + { |
|
50 | 54 | throw $this->invalidBindingException($alias, $e); |
51 | 55 | } |
52 | 56 | |
@@ -58,9 +62,12 @@ discard block |
||
58 | 62 | */ |
59 | 63 | public function bindSingleton(string $alias, mixed $resolver): void |
60 | 64 | { |
61 | - try { |
|
65 | + try |
|
66 | + { |
|
62 | 67 | $config = $this->makeConfig($resolver, true); |
63 | - } catch (\Throwable $e) { |
|
68 | + } |
|
69 | + catch (\Throwable $e) |
|
70 | + { |
|
64 | 71 | throw $this->invalidBindingException($alias, $e); |
65 | 72 | } |
66 | 73 | |
@@ -72,9 +79,11 @@ discard block |
||
72 | 79 | $bindings = &$this->state->bindings; |
73 | 80 | |
74 | 81 | $flags = []; |
75 | - while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) { |
|
82 | + while ($binding = $bindings[$alias] ?? null and $binding::class === Alias::class) |
|
83 | + { |
|
76 | 84 | //Checking alias tree |
77 | - if ($flags[$binding->alias] ?? false) { |
|
85 | + if ($flags[$binding->alias] ?? false) |
|
86 | + { |
|
78 | 87 | return $binding->alias === $alias ?: throw new Exception('Circular alias detected'); |
79 | 88 | } |
80 | 89 | |
@@ -99,7 +108,8 @@ discard block |
||
99 | 108 | public function removeInjector(string $class): void |
100 | 109 | { |
101 | 110 | unset($this->state->injectors[$class]); |
102 | - if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) { |
|
111 | + if (!isset($this->state->bindings[$class]) || $this->state->bindings[$class]::class !== Injectable::class) |
|
112 | + { |
|
103 | 113 | return; |
104 | 114 | } |
105 | 115 | unset($this->state->bindings[$class]); |
@@ -107,13 +117,17 @@ discard block |
||
107 | 117 | |
108 | 118 | public function hasInjector(string $class): bool |
109 | 119 | { |
110 | - try { |
|
120 | + try |
|
121 | + { |
|
111 | 122 | $reflection = new \ReflectionClass($class); |
112 | - } catch (\ReflectionException $e) { |
|
123 | + } |
|
124 | + catch (\ReflectionException $e) |
|
125 | + { |
|
113 | 126 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
114 | 127 | } |
115 | 128 | |
116 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
129 | + if (\array_key_exists($class, $this->state->injectors)) |
|
130 | + { |
|
117 | 131 | return true; |
118 | 132 | } |
119 | 133 | |
@@ -128,7 +142,8 @@ discard block |
||
128 | 142 | } |
129 | 143 | |
130 | 144 | // check interfaces |
131 | - foreach ($this->state->injectors as $target => $injector) { |
|
145 | + foreach ($this->state->injectors as $target => $injector) |
|
146 | + { |
|
132 | 147 | if ( |
133 | 148 | (\class_exists($target, true) && $reflection->isSubclassOf($target)) |
134 | 149 | || |
@@ -160,15 +175,18 @@ discard block |
||
160 | 175 | |
161 | 176 | private function makeConfigFromArray(array $resolver, bool $singleton): Binding |
162 | 177 | { |
163 | - if (\is_callable($resolver)) { |
|
178 | + if (\is_callable($resolver)) |
|
179 | + { |
|
164 | 180 | return new Factory($resolver, $singleton); |
165 | 181 | } |
166 | 182 | |
167 | 183 | // Validate lazy invokable array |
168 | - if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') { |
|
184 | + if (!isset($resolver[0]) || !isset($resolver[1]) || !\is_string($resolver[1]) || $resolver[1] === '') |
|
185 | + { |
|
169 | 186 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
170 | 187 | } |
171 | - if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') { |
|
188 | + if ((!\is_string($resolver[0]) && !\is_object($resolver[0])) || $resolver[0] === '') |
|
189 | + { |
|
172 | 190 | throw new InvalidArgumentException('Incompatible array declaration for resolver.'); |
173 | 191 | } |
174 | 192 |