@@ -166,16 +166,21 @@ discard block |
||
| 166 | 166 | $binds = &$this->state->bindings; |
| 167 | 167 | $singletons = &$this->state->singletons; |
| 168 | 168 | $cleanup = $previous = $prevSin = []; |
| 169 | - foreach ($bindings as $alias => $resolver) { |
|
| 169 | + foreach ($bindings as $alias => $resolver) |
|
| 170 | + { |
|
| 170 | 171 | // Store previous bindings |
| 171 | - if (isset($binds[$alias])) { |
|
| 172 | + if (isset($binds[$alias])) |
|
| 173 | + { |
|
| 172 | 174 | $previous[$alias] = $binds[$alias]; |
| 173 | - } else { |
|
| 175 | + } |
|
| 176 | + else |
|
| 177 | + { |
|
| 174 | 178 | // Store bindings to be removed |
| 175 | 179 | $cleanup[] = $alias; |
| 176 | 180 | } |
| 177 | 181 | // Store previous singletons |
| 178 | - if (isset($singletons[$alias])) { |
|
| 182 | + if (isset($singletons[$alias])) |
|
| 183 | + { |
|
| 179 | 184 | $prevSin[$alias] = $singletons[$alias]; |
| 180 | 185 | unset($singletons[$alias]); |
| 181 | 186 | } |
@@ -183,21 +188,27 @@ discard block |
||
| 183 | 188 | $this->binder->bind($alias, $resolver); |
| 184 | 189 | } |
| 185 | 190 | |
| 186 | - try { |
|
| 191 | + try |
|
| 192 | + { |
|
| 187 | 193 | return ContainerScope::getContainer() !== $this |
| 188 | 194 | ? ContainerScope::runScope($this, $scope) |
| 189 | 195 | : $scope($this); |
| 190 | - } finally { |
|
| 196 | + } |
|
| 197 | + finally |
|
| 198 | + { |
|
| 191 | 199 | // Remove new bindings |
| 192 | - foreach ($cleanup as $alias) { |
|
| 200 | + foreach ($cleanup as $alias) |
|
| 201 | + { |
|
| 193 | 202 | unset($binds[$alias], $singletons[$alias]); |
| 194 | 203 | } |
| 195 | 204 | // Restore previous bindings |
| 196 | - foreach ($previous as $alias => $resolver) { |
|
| 205 | + foreach ($previous as $alias => $resolver) |
|
| 206 | + { |
|
| 197 | 207 | $binds[$alias] = $resolver; |
| 198 | 208 | } |
| 199 | 209 | // Restore singletons |
| 200 | - foreach ($prevSin as $alias => $instance) { |
|
| 210 | + foreach ($prevSin as $alias => $instance) |
|
| 211 | + { |
|
| 201 | 212 | $singletons[$alias] = $instance; |
| 202 | 213 | } |
| 203 | 214 | } |
@@ -211,32 +222,40 @@ discard block |
||
| 211 | 222 | // Open scope |
| 212 | 223 | $container = new self($this->config, $name); |
| 213 | 224 | |
| 214 | - try { |
|
| 225 | + try |
|
| 226 | + { |
|
| 215 | 227 | // Configure scope |
| 216 | 228 | $container->scope->setParent($this, $this->scope); |
| 217 | 229 | |
| 218 | 230 | // Add specific bindings |
| 219 | - foreach ($bindings as $alias => $resolver) { |
|
| 231 | + foreach ($bindings as $alias => $resolver) |
|
| 232 | + { |
|
| 220 | 233 | $container->binder->bind($alias, $resolver); |
| 221 | 234 | } |
| 222 | 235 | |
| 223 | 236 | return ContainerScope::runScope( |
| 224 | 237 | $container, |
| 225 | 238 | static function (self $container) use ($autowire, $closure): mixed { |
| 226 | - try { |
|
| 239 | + try |
|
| 240 | + { |
|
| 227 | 241 | return $autowire |
| 228 | 242 | ? $container->invoke($closure) |
| 229 | 243 | : $closure($container); |
| 230 | - } finally { |
|
| 244 | + } |
|
| 245 | + finally |
|
| 246 | + { |
|
| 231 | 247 | $container->closeScope(); |
| 232 | 248 | } |
| 233 | 249 | } |
| 234 | 250 | ); |
| 235 | - } finally { |
|
| 251 | + } |
|
| 252 | + finally |
|
| 253 | + { |
|
| 236 | 254 | // Check the container has not been leaked |
| 237 | 255 | $link = \WeakReference::create($container); |
| 238 | 256 | unset($container); |
| 239 | - if ($link->get() !== null) { |
|
| 257 | + if ($link->get() !== null) |
|
| 258 | + { |
|
| 240 | 259 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
| 241 | 260 | } |
| 242 | 261 | } |
@@ -272,7 +291,8 @@ discard block |
||
| 272 | 291 | */ |
| 273 | 292 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
| 274 | 293 | { |
| 275 | - if ($force) { |
|
| 294 | + if ($force) |
|
| 295 | + { |
|
| 276 | 296 | $this->binder->removeBinding($alias); |
| 277 | 297 | } |
| 278 | 298 | |
@@ -341,8 +361,10 @@ discard block |
||
| 341 | 361 | ]); |
| 342 | 362 | |
| 343 | 363 | // Create container services |
| 344 | - foreach ($container->config as $property => $class) { |
|
| 345 | - if (\property_exists($container, $property)) { |
|
| 364 | + foreach ($container->config as $property => $class) |
|
| 365 | + { |
|
| 366 | + if (\property_exists($container, $property)) |
|
| 367 | + { |
|
| 346 | 368 | $container->$property = $constructor->get($property, $class); |
| 347 | 369 | } |
| 348 | 370 | } |
@@ -356,7 +378,8 @@ discard block |
||
| 356 | 378 | private function closeScope(): void |
| 357 | 379 | { |
| 358 | 380 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 359 | - if (!isset($this->scope)) { |
|
| 381 | + if (!isset($this->scope)) |
|
| 382 | + { |
|
| 360 | 383 | $this->destruct(); |
| 361 | 384 | return; |
| 362 | 385 | } |
@@ -365,10 +388,14 @@ discard block |
||
| 365 | 388 | |
| 366 | 389 | // Run finalizers |
| 367 | 390 | $errors = []; |
| 368 | - foreach ($this->state->finalizers as $finalizer) { |
|
| 369 | - try { |
|
| 391 | + foreach ($this->state->finalizers as $finalizer) |
|
| 392 | + { |
|
| 393 | + try |
|
| 394 | + { |
|
| 370 | 395 | $this->invoker->invoke($finalizer); |
| 371 | - } catch (\Throwable $e) { |
|
| 396 | + } |
|
| 397 | + catch (\Throwable $e) |
|
| 398 | + { |
|
| 372 | 399 | $errors[] = $e; |
| 373 | 400 | } |
| 374 | 401 | } |
@@ -377,7 +404,8 @@ discard block |
||
| 377 | 404 | $this->destruct(); |
| 378 | 405 | |
| 379 | 406 | // Throw collected errors |
| 380 | - if ($errors !== []) { |
|
| 407 | + if ($errors !== []) |
|
| 408 | + { |
|
| 381 | 409 | throw new FinalizersException($scopeName, $errors); |
| 382 | 410 | } |
| 383 | 411 | } |