@@ -49,11 +49,11 @@ discard block |
||
49 | 49 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
50 | 50 | |
51 | 51 | private Internal\State $state; |
52 | - private ResolverInterface|Internal\Resolver $resolver; |
|
53 | - private FactoryInterface|Internal\Factory $factory; |
|
54 | - private ContainerInterface|Internal\Container $container; |
|
55 | - private BinderInterface|Internal\Binder $binder; |
|
56 | - private InvokerInterface|Internal\Invoker $invoker; |
|
52 | + private ResolverInterface | Internal\Resolver $resolver; |
|
53 | + private FactoryInterface | Internal\Factory $factory; |
|
54 | + private ContainerInterface | Internal\Container $container; |
|
55 | + private BinderInterface | Internal\Binder $binder; |
|
56 | + private InvokerInterface | Internal\Invoker $invoker; |
|
57 | 57 | private Internal\Scope $scope; |
58 | 58 | |
59 | 59 | /** |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | public function __construct( |
63 | 63 | private Config $config = new Config(), |
64 | 64 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
65 | - ) { |
|
65 | + ){ |
|
66 | 66 | $this->initServices($this, $scopeName); |
67 | 67 | |
68 | 68 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
@@ -138,7 +138,7 @@ discard block |
||
138 | 138 | * @throws ContainerException |
139 | 139 | * @throws \Throwable |
140 | 140 | */ |
141 | - public function get(string|Autowire $id, string $context = null): mixed |
|
141 | + public function get(string | Autowire $id, string $context = null): mixed |
|
142 | 142 | { |
143 | 143 | /** @psalm-suppress TooManyArguments */ |
144 | 144 | return $this->container->get($id, $context); |
@@ -166,16 +166,16 @@ 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 | // Store previous bindings |
171 | - if (isset($binds[$alias])) { |
|
171 | + if (isset($binds[$alias])){ |
|
172 | 172 | $previous[$alias] = $binds[$alias]; |
173 | - } else { |
|
173 | + }else{ |
|
174 | 174 | // Store bindings to be removed |
175 | 175 | $cleanup[] = $alias; |
176 | 176 | } |
177 | 177 | // Store previous singletons |
178 | - if (isset($singletons[$alias])) { |
|
178 | + if (isset($singletons[$alias])){ |
|
179 | 179 | $prevSin[$alias] = $singletons[$alias]; |
180 | 180 | unset($singletons[$alias]); |
181 | 181 | } |
@@ -183,21 +183,21 @@ discard block |
||
183 | 183 | $this->binder->bind($alias, $resolver); |
184 | 184 | } |
185 | 185 | |
186 | - try { |
|
186 | + try{ |
|
187 | 187 | return ContainerScope::getContainer() !== $this |
188 | 188 | ? ContainerScope::runScope($this, $scope) |
189 | 189 | : $scope($this); |
190 | - } finally { |
|
190 | + }finally{ |
|
191 | 191 | // Remove new bindings |
192 | - foreach ($cleanup as $alias) { |
|
192 | + foreach ($cleanup as $alias){ |
|
193 | 193 | unset($binds[$alias], $singletons[$alias]); |
194 | 194 | } |
195 | 195 | // Restore previous bindings |
196 | - foreach ($previous as $alias => $resolver) { |
|
196 | + foreach ($previous as $alias => $resolver){ |
|
197 | 197 | $binds[$alias] = $resolver; |
198 | 198 | } |
199 | 199 | // Restore singletons |
200 | - foreach ($prevSin as $alias => $instance) { |
|
200 | + foreach ($prevSin as $alias => $instance){ |
|
201 | 201 | $singletons[$alias] = $instance; |
202 | 202 | } |
203 | 203 | } |
@@ -211,32 +211,32 @@ discard block |
||
211 | 211 | // Open scope |
212 | 212 | $container = new self($this->config, $name); |
213 | 213 | |
214 | - try { |
|
214 | + try{ |
|
215 | 215 | // Configure scope |
216 | 216 | $container->scope->setParent($this, $this->scope); |
217 | 217 | |
218 | 218 | // Add specific bindings |
219 | - foreach ($bindings as $alias => $resolver) { |
|
219 | + foreach ($bindings as $alias => $resolver){ |
|
220 | 220 | $container->binder->bind($alias, $resolver); |
221 | 221 | } |
222 | 222 | |
223 | 223 | return ContainerScope::runScope( |
224 | 224 | $container, |
225 | 225 | static function (self $container) use ($autowire, $closure): mixed { |
226 | - try { |
|
226 | + try{ |
|
227 | 227 | return $autowire |
228 | 228 | ? $container->invoke($closure) |
229 | 229 | : $closure($container); |
230 | - } finally { |
|
230 | + }finally{ |
|
231 | 231 | $container->closeScope(); |
232 | 232 | } |
233 | 233 | } |
234 | 234 | ); |
235 | - } finally { |
|
235 | + }finally{ |
|
236 | 236 | // Check the container has not been leaked |
237 | 237 | $link = \WeakReference::create($container); |
238 | 238 | unset($container); |
239 | - if ($link->get() !== null) { |
|
239 | + if ($link->get() !== null){ |
|
240 | 240 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
241 | 241 | } |
242 | 242 | } |
@@ -270,9 +270,9 @@ discard block |
||
270 | 270 | * @param bool $force If the value is false, an exception will be thrown when attempting |
271 | 271 | * to bind an already constructed singleton. |
272 | 272 | */ |
273 | - public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
|
273 | + public function bindSingleton(string $alias, string | array | callable | object $resolver, bool $force = true): void |
|
274 | 274 | { |
275 | - if ($force) { |
|
275 | + if ($force){ |
|
276 | 276 | $this->binder->removeBinding($alias); |
277 | 277 | } |
278 | 278 | |
@@ -341,8 +341,8 @@ discard block |
||
341 | 341 | ]); |
342 | 342 | |
343 | 343 | // Create container services |
344 | - foreach ($container->config as $property => $class) { |
|
345 | - if (\property_exists($container, $property)) { |
|
344 | + foreach ($container->config as $property => $class){ |
|
345 | + if (\property_exists($container, $property)){ |
|
346 | 346 | $container->$property = $constructor->get($property, $class); |
347 | 347 | } |
348 | 348 | } |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | private function closeScope(): void |
357 | 357 | { |
358 | 358 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
359 | - if (!isset($this->scope)) { |
|
359 | + if (!isset($this->scope)){ |
|
360 | 360 | $this->destruct(); |
361 | 361 | return; |
362 | 362 | } |
@@ -365,10 +365,10 @@ discard block |
||
365 | 365 | |
366 | 366 | // Run finalizers |
367 | 367 | $errors = []; |
368 | - foreach ($this->state->finalizers as $finalizer) { |
|
369 | - try { |
|
368 | + foreach ($this->state->finalizers as $finalizer){ |
|
369 | + try{ |
|
370 | 370 | $this->invoker->invoke($finalizer); |
371 | - } catch (\Throwable $e) { |
|
371 | + }catch (\Throwable $e){ |
|
372 | 372 | $errors[] = $e; |
373 | 373 | } |
374 | 374 | } |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | $this->destruct(); |
378 | 378 | |
379 | 379 | // Throw collected errors |
380 | - if ($errors !== []) { |
|
380 | + if ($errors !== []){ |
|
381 | 381 | throw new FinalizersException($scopeName, $errors); |
382 | 382 | } |
383 | 383 | } |
@@ -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 | } |