@@ -63,7 +63,8 @@ discard block |
||
| 63 | 63 | string|\BackedEnum|null $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
| 64 | 64 | private Options $options = new Options(), |
| 65 | 65 | ) { |
| 66 | - if (\is_object($scopeName)) { |
|
| 66 | + if (\is_object($scopeName)) |
|
| 67 | + { |
|
| 67 | 68 | $scopeName = (string) $scopeName->value; |
| 68 | 69 | } |
| 69 | 70 | |
@@ -178,23 +179,29 @@ discard block |
||
| 178 | 179 | */ |
| 179 | 180 | public function runScope(Scope|array $bindings, callable $scope): mixed |
| 180 | 181 | { |
| 181 | - if (!\is_array($bindings)) { |
|
| 182 | + if (!\is_array($bindings)) |
|
| 183 | + { |
|
| 182 | 184 | return $this->runIsolatedScope($bindings, $scope); |
| 183 | 185 | } |
| 184 | 186 | |
| 185 | 187 | $binds = &$this->state->bindings; |
| 186 | 188 | $singletons = &$this->state->singletons; |
| 187 | 189 | $cleanup = $previous = $prevSin = []; |
| 188 | - foreach ($bindings as $alias => $resolver) { |
|
| 190 | + foreach ($bindings as $alias => $resolver) |
|
| 191 | + { |
|
| 189 | 192 | // Store previous bindings |
| 190 | - if (isset($binds[$alias])) { |
|
| 193 | + if (isset($binds[$alias])) |
|
| 194 | + { |
|
| 191 | 195 | $previous[$alias] = $binds[$alias]; |
| 192 | - } else { |
|
| 196 | + } |
|
| 197 | + else |
|
| 198 | + { |
|
| 193 | 199 | // Store bindings to be removed |
| 194 | 200 | $cleanup[] = $alias; |
| 195 | 201 | } |
| 196 | 202 | // Store previous singletons |
| 197 | - if (isset($singletons[$alias])) { |
|
| 203 | + if (isset($singletons[$alias])) |
|
| 204 | + { |
|
| 198 | 205 | $prevSin[$alias] = $singletons[$alias]; |
| 199 | 206 | unset($singletons[$alias]); |
| 200 | 207 | } |
@@ -202,21 +209,27 @@ discard block |
||
| 202 | 209 | $this->binder->bind($alias, $resolver); |
| 203 | 210 | } |
| 204 | 211 | |
| 205 | - try { |
|
| 212 | + try |
|
| 213 | + { |
|
| 206 | 214 | return ContainerScope::getContainer() !== $this |
| 207 | 215 | ? ContainerScope::runScope($this, $scope) |
| 208 | 216 | : $scope($this); |
| 209 | - } finally { |
|
| 217 | + } |
|
| 218 | + finally |
|
| 219 | + { |
|
| 210 | 220 | // Remove new bindings |
| 211 | - foreach ($cleanup as $alias) { |
|
| 221 | + foreach ($cleanup as $alias) |
|
| 222 | + { |
|
| 212 | 223 | unset($binds[$alias], $singletons[$alias]); |
| 213 | 224 | } |
| 214 | 225 | // Restore previous bindings |
| 215 | - foreach ($previous as $alias => $resolver) { |
|
| 226 | + foreach ($previous as $alias => $resolver) |
|
| 227 | + { |
|
| 216 | 228 | $binds[$alias] = $resolver; |
| 217 | 229 | } |
| 218 | 230 | // Restore singletons |
| 219 | - foreach ($prevSin as $alias => $instance) { |
|
| 231 | + foreach ($prevSin as $alias => $instance) |
|
| 232 | + { |
|
| 220 | 233 | $singletons[$alias] = $instance; |
| 221 | 234 | } |
| 222 | 235 | } |
@@ -266,7 +279,8 @@ discard block |
||
| 266 | 279 | */ |
| 267 | 280 | public function bindSingleton(string $alias, string|array|callable|object $resolver, ?bool $force = null): void |
| 268 | 281 | { |
| 269 | - if ($force ?? $this->options->allowSingletonsRebind) { |
|
| 282 | + if ($force ?? $this->options->allowSingletonsRebind) |
|
| 283 | + { |
|
| 270 | 284 | $this->binder->removeBinding($alias); |
| 271 | 285 | } |
| 272 | 286 | |
@@ -337,8 +351,10 @@ discard block |
||
| 337 | 351 | ], $this->options); |
| 338 | 352 | |
| 339 | 353 | // Create container services |
| 340 | - foreach ($container->config as $property => $class) { |
|
| 341 | - if (\property_exists($container, $property)) { |
|
| 354 | + foreach ($container->config as $property => $class) |
|
| 355 | + { |
|
| 356 | + if (\property_exists($container, $property)) |
|
| 357 | + { |
|
| 342 | 358 | $container->$property = $constructor->get($property, $class); |
| 343 | 359 | } |
| 344 | 360 | } |
@@ -352,7 +368,8 @@ discard block |
||
| 352 | 368 | private function closeScope(): void |
| 353 | 369 | { |
| 354 | 370 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 355 | - if (!isset($this->scope)) { |
|
| 371 | + if (!isset($this->scope)) |
|
| 372 | + { |
|
| 356 | 373 | $this->destruct(); |
| 357 | 374 | return; |
| 358 | 375 | } |
@@ -361,10 +378,14 @@ discard block |
||
| 361 | 378 | |
| 362 | 379 | // Run finalizers |
| 363 | 380 | $errors = []; |
| 364 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 365 | - try { |
|
| 381 | + foreach ($this->state->finalizers as $finalizer) |
|
| 382 | + { |
|
| 383 | + try |
|
| 384 | + { |
|
| 366 | 385 | $this->invoker->invoke($finalizer); |
| 367 | - } catch (\Throwable $e) { |
|
| 386 | + } |
|
| 387 | + catch (\Throwable $e) |
|
| 388 | + { |
|
| 368 | 389 | $errors[] = $e; |
| 369 | 390 | } |
| 370 | 391 | } |
@@ -373,7 +394,8 @@ discard block |
||
| 373 | 394 | $this->destruct(); |
| 374 | 395 | |
| 375 | 396 | // Throw collected errors |
| 376 | - if ($errors !== []) { |
|
| 397 | + if ($errors !== []) |
|
| 398 | + { |
|
| 377 | 399 | throw new FinalizersException($scopeName, $errors); |
| 378 | 400 | } |
| 379 | 401 | } |
@@ -395,18 +417,22 @@ discard block |
||
| 395 | 417 | $container->scope->setParent($this, $this->scope, $this->factory); |
| 396 | 418 | |
| 397 | 419 | // Add specific bindings |
| 398 | - foreach ($config->bindings as $alias => $resolver) { |
|
| 420 | + foreach ($config->bindings as $alias => $resolver) |
|
| 421 | + { |
|
| 399 | 422 | $container->binder->bind($alias, $resolver); |
| 400 | 423 | } |
| 401 | 424 | |
| 402 | 425 | return ContainerScope::runScope( |
| 403 | 426 | $container, |
| 404 | 427 | static function (self $container) use ($config, $closure): mixed { |
| 405 | - try { |
|
| 428 | + try |
|
| 429 | + { |
|
| 406 | 430 | return $config->autowire |
| 407 | 431 | ? $container->invoke($closure) |
| 408 | 432 | : $closure($container); |
| 409 | - } finally { |
|
| 433 | + } |
|
| 434 | + finally |
|
| 435 | + { |
|
| 410 | 436 | $container->closeScope(); |
| 411 | 437 | } |
| 412 | 438 | } |