@@ -164,16 +164,21 @@ discard block |
||
| 164 | 164 | $binds = &$this->state->bindings; |
| 165 | 165 | $singletons = &$this->state->singletons; |
| 166 | 166 | $cleanup = $previous = $prevSin = []; |
| 167 | - foreach ($bindings as $alias => $resolver) { |
|
| 167 | + foreach ($bindings as $alias => $resolver) |
|
| 168 | + { |
|
| 168 | 169 | // Store previous bindings |
| 169 | - if (isset($binds[$alias])) { |
|
| 170 | + if (isset($binds[$alias])) |
|
| 171 | + { |
|
| 170 | 172 | $previous[$alias] = $binds[$alias]; |
| 171 | - } else { |
|
| 173 | + } |
|
| 174 | + else |
|
| 175 | + { |
|
| 172 | 176 | // Store bindings to be removed |
| 173 | 177 | $cleanup[] = $alias; |
| 174 | 178 | } |
| 175 | 179 | // Store previous singletons |
| 176 | - if (isset($singletons[$alias])) { |
|
| 180 | + if (isset($singletons[$alias])) |
|
| 181 | + { |
|
| 177 | 182 | $prevSin[$alias] = $singletons[$alias]; |
| 178 | 183 | unset($singletons[$alias]); |
| 179 | 184 | } |
@@ -181,21 +186,27 @@ discard block |
||
| 181 | 186 | $this->binder->bind($alias, $resolver); |
| 182 | 187 | } |
| 183 | 188 | |
| 184 | - try { |
|
| 189 | + try |
|
| 190 | + { |
|
| 185 | 191 | return ContainerScope::getContainer() !== $this |
| 186 | 192 | ? ContainerScope::runScope($this, $scope) |
| 187 | 193 | : $scope($this); |
| 188 | - } finally { |
|
| 194 | + } |
|
| 195 | + finally |
|
| 196 | + { |
|
| 189 | 197 | // Remove new bindings |
| 190 | - foreach ($cleanup as $alias) { |
|
| 198 | + foreach ($cleanup as $alias) |
|
| 199 | + { |
|
| 191 | 200 | unset($binds[$alias], $singletons[$alias]); |
| 192 | 201 | } |
| 193 | 202 | // Restore previous bindings |
| 194 | - foreach ($previous as $alias => $resolver) { |
|
| 203 | + foreach ($previous as $alias => $resolver) |
|
| 204 | + { |
|
| 195 | 205 | $binds[$alias] = $resolver; |
| 196 | 206 | } |
| 197 | 207 | // Restore singletons |
| 198 | - foreach ($prevSin as $alias => $instance) { |
|
| 208 | + foreach ($prevSin as $alias => $instance) |
|
| 209 | + { |
|
| 199 | 210 | $singletons[$alias] = $instance; |
| 200 | 211 | } |
| 201 | 212 | } |
@@ -213,18 +224,22 @@ discard block |
||
| 213 | 224 | $container->scope->setParent($this, $this->scope); |
| 214 | 225 | |
| 215 | 226 | // Add specific bindings |
| 216 | - foreach ($bindings as $alias => $resolver) { |
|
| 227 | + foreach ($bindings as $alias => $resolver) |
|
| 228 | + { |
|
| 217 | 229 | $container->binder->bind($alias, $resolver); |
| 218 | 230 | } |
| 219 | 231 | |
| 220 | 232 | return ContainerScope::runScope( |
| 221 | 233 | $container, |
| 222 | 234 | static function (self $container) use ($autowire, $closure): mixed { |
| 223 | - try { |
|
| 235 | + try |
|
| 236 | + { |
|
| 224 | 237 | return $autowire |
| 225 | 238 | ? $container->invoke($closure) |
| 226 | 239 | : $closure($container); |
| 227 | - } finally { |
|
| 240 | + } |
|
| 241 | + finally |
|
| 242 | + { |
|
| 228 | 243 | $container->closeScope(); |
| 229 | 244 | } |
| 230 | 245 | } |
@@ -261,7 +276,8 @@ discard block |
||
| 261 | 276 | */ |
| 262 | 277 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
| 263 | 278 | { |
| 264 | - if ($force) { |
|
| 279 | + if ($force) |
|
| 280 | + { |
|
| 265 | 281 | $this->binder->removeBinding($alias); |
| 266 | 282 | } |
| 267 | 283 | |
@@ -330,8 +346,10 @@ discard block |
||
| 330 | 346 | ]); |
| 331 | 347 | |
| 332 | 348 | // Create container services |
| 333 | - foreach ($container->config as $property => $class) { |
|
| 334 | - if (\property_exists($container, $property)) { |
|
| 349 | + foreach ($container->config as $property => $class) |
|
| 350 | + { |
|
| 351 | + if (\property_exists($container, $property)) |
|
| 352 | + { |
|
| 335 | 353 | $container->$property = $constructor->get($property, $class); |
| 336 | 354 | } |
| 337 | 355 | } |
@@ -345,7 +363,8 @@ discard block |
||
| 345 | 363 | private function closeScope(): void |
| 346 | 364 | { |
| 347 | 365 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 348 | - if (!isset($this->scope)) { |
|
| 366 | + if (!isset($this->scope)) |
|
| 367 | + { |
|
| 349 | 368 | $this->destruct(); |
| 350 | 369 | return; |
| 351 | 370 | } |
@@ -354,10 +373,14 @@ discard block |
||
| 354 | 373 | |
| 355 | 374 | // Run finalizers |
| 356 | 375 | $errors = []; |
| 357 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 358 | - try { |
|
| 376 | + foreach ($this->state->finalizers as $finalizer) |
|
| 377 | + { |
|
| 378 | + try |
|
| 379 | + { |
|
| 359 | 380 | $this->invoker->invoke($finalizer); |
| 360 | - } catch (\Throwable $e) { |
|
| 381 | + } |
|
| 382 | + catch (\Throwable $e) |
|
| 383 | + { |
|
| 361 | 384 | $errors[] = $e; |
| 362 | 385 | } |
| 363 | 386 | } |
@@ -366,7 +389,8 @@ discard block |
||
| 366 | 389 | $this->destruct(); |
| 367 | 390 | |
| 368 | 391 | // Throw collected errors |
| 369 | - if ($errors !== []) { |
|
| 392 | + if ($errors !== []) |
|
| 393 | + { |
|
| 370 | 394 | throw new FinalizersException($scopeName, $errors); |
| 371 | 395 | } |
| 372 | 396 | } |