@@ -16,7 +16,7 @@ discard block |
||
16 | 16 | |
17 | 17 | public function __construct( |
18 | 18 | private readonly ?string $scopeName = null, |
19 | - ) { |
|
19 | + ){ |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | public function getScopeName(): ?string |
@@ -35,9 +35,9 @@ discard block |
||
35 | 35 | $this->parentScope = $parentScope; |
36 | 36 | |
37 | 37 | // Check a scope with the same name is not already registered |
38 | - if ($this->scopeName !== null) { |
|
38 | + if ($this->scopeName !== null){ |
|
39 | 39 | $tmp = $this; |
40 | - while ($tmp->parentScope !== null) { |
|
40 | + while ($tmp->parentScope !== null){ |
|
41 | 41 | $tmp = $tmp->parentScope; |
42 | 42 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
43 | 43 | } |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | $result = [$this->scopeName]; |
61 | 61 | |
62 | 62 | $parent = $this; |
63 | - while ($parent->parentScope !== null) { |
|
63 | + while ($parent->parentScope !== null){ |
|
64 | 64 | $parent = $parent->parentScope; |
65 | 65 | $result[] = $parent->scopeName; |
66 | 66 | } |
@@ -35,9 +35,11 @@ discard block |
||
35 | 35 | $this->parentScope = $parentScope; |
36 | 36 | |
37 | 37 | // Check a scope with the same name is not already registered |
38 | - if ($this->scopeName !== null) { |
|
38 | + if ($this->scopeName !== null) |
|
39 | + { |
|
39 | 40 | $tmp = $this; |
40 | - while ($tmp->parentScope !== null) { |
|
41 | + while ($tmp->parentScope !== null) |
|
42 | + { |
|
41 | 43 | $tmp = $tmp->parentScope; |
42 | 44 | $tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
43 | 45 | } |
@@ -60,7 +62,8 @@ discard block |
||
60 | 62 | $result = [$this->scopeName]; |
61 | 63 | |
62 | 64 | $parent = $this; |
63 | - while ($parent->parentScope !== null) { |
|
65 | + while ($parent->parentScope !== null) |
|
66 | + { |
|
64 | 67 | $parent = $parent->parentScope; |
65 | 68 | $result[] = $parent->scopeName; |
66 | 69 | } |
@@ -11,7 +11,7 @@ |
||
11 | 11 | { |
12 | 12 | public function __construct( |
13 | 13 | string $scope, |
14 | - ) { |
|
14 | + ){ |
|
15 | 15 | parent::__construct( |
16 | 16 | $scope, |
17 | 17 | "Error on a scope allocation with the name `{$scope}`. A scope with the same name already exists.", |
@@ -12,7 +12,7 @@ |
||
12 | 12 | public function __construct( |
13 | 13 | string $scope, |
14 | 14 | protected string $className, |
15 | - ) { |
|
15 | + ){ |
|
16 | 16 | parent::__construct( |
17 | 17 | $scope, |
18 | 18 | \sprintf('Class `%s` can be resolved only in `%s` scope.', $className, $scope), |
@@ -16,7 +16,7 @@ |
||
16 | 16 | string $message = '', |
17 | 17 | int $code = 0, |
18 | 18 | ?\Throwable $previous = null, |
19 | - ) { |
|
19 | + ){ |
|
20 | 20 | parent::__construct( |
21 | 21 | $message, |
22 | 22 | $code, |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | public function __construct( |
16 | 16 | ?string $scope, |
17 | 17 | protected array $exceptions, |
18 | - ) { |
|
18 | + ){ |
|
19 | 19 | $count = \count($exceptions); |
20 | 20 | parent::__construct( |
21 | 21 | $scope, |
@@ -24,7 +24,7 @@ discard block |
||
24 | 24 | $count === 1 ? 'An exception has been' : "$count exceptions have been", |
25 | 25 | $scope === null ? 'an unnamed scope' : "the scope `$scope`", |
26 | 26 | \implode("\n\n", \array_map( |
27 | - static fn (Exception $e): string => \sprintf( |
|
27 | + static fn (Exception $e) : string => \sprintf( |
|
28 | 28 | "# %s\n%s", |
29 | 29 | $e::class, |
30 | 30 | $e->getMessage(), |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | public readonly string $binder = Binder::class, |
51 | 51 | public readonly string $invoker = Invoker::class, |
52 | 52 | public readonly string $tracer = Tracer::class, |
53 | - ) { |
|
53 | + ){ |
|
54 | 54 | $this->scope = Scope::class; |
55 | 55 | $this->scopedBindings = new Internal\Config\StateStorage(); |
56 | 56 | } |
@@ -74,9 +74,9 @@ discard block |
||
74 | 74 | */ |
75 | 75 | public function lockRoot(): bool |
76 | 76 | { |
77 | - try { |
|
77 | + try{ |
|
78 | 78 | return $this->rootLocked; |
79 | - } finally { |
|
79 | + }finally{ |
|
80 | 80 | $this->rootLocked = false; |
81 | 81 | } |
82 | 82 | } |
@@ -74,9 +74,12 @@ |
||
74 | 74 | */ |
75 | 75 | public function lockRoot(): bool |
76 | 76 | { |
77 | - try { |
|
77 | + try |
|
78 | + { |
|
78 | 79 | return $this->rootLocked; |
79 | - } finally { |
|
80 | + } |
|
81 | + finally |
|
82 | + { |
|
80 | 83 | $this->rootLocked = false; |
81 | 84 | } |
82 | 85 | } |
@@ -13,9 +13,9 @@ |
||
13 | 13 | public function __construct( |
14 | 14 | ?string $scope, |
15 | 15 | array $parents, |
16 | - ) { |
|
16 | + ){ |
|
17 | 17 | $scopes = \implode('->', \array_map( |
18 | - static fn (?string $scope): string => $scope === null ? 'null' : "\"$scope\"", |
|
18 | + static fn (?string $scope) : string => $scope === null ? 'null' : "\"$scope\"", |
|
19 | 19 | [...\array_reverse($parents), $scope], |
20 | 20 | )); |
21 | 21 | parent::__construct( |
@@ -20,7 +20,7 @@ |
||
20 | 20 | { |
21 | 21 | $this->logger = $logger; |
22 | 22 | $this->finalized = true; |
23 | - if ($this->throwException) { |
|
23 | + if ($this->throwException){ |
|
24 | 24 | throw new RuntimeException('Test exception from finalize method.'); |
25 | 25 | } |
26 | 26 | } |
@@ -20,7 +20,8 @@ |
||
20 | 20 | { |
21 | 21 | $this->logger = $logger; |
22 | 22 | $this->finalized = true; |
23 | - if ($this->throwException) { |
|
23 | + if ($this->throwException) |
|
24 | + { |
|
24 | 25 | throw new RuntimeException('Test exception from finalize method.'); |
25 | 26 | } |
26 | 27 | } |
@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | public const DEFAULT_ROOT_SCOPE_NAME = 'root'; |
48 | 48 | |
49 | 49 | private Internal\State $state; |
50 | - private ResolverInterface|Internal\Resolver $resolver; |
|
51 | - private FactoryInterface|Internal\Factory $factory; |
|
52 | - private ContainerInterface|Internal\Container $container; |
|
53 | - private BinderInterface|Internal\Binder $binder; |
|
54 | - private InvokerInterface|Internal\Invoker $invoker; |
|
50 | + private ResolverInterface | Internal\Resolver $resolver; |
|
51 | + private FactoryInterface | Internal\Factory $factory; |
|
52 | + private ContainerInterface | Internal\Container $container; |
|
53 | + private BinderInterface | Internal\Binder $binder; |
|
54 | + private InvokerInterface | Internal\Invoker $invoker; |
|
55 | 55 | private Internal\Scope $scope; |
56 | 56 | |
57 | 57 | /** |
@@ -60,7 +60,7 @@ discard block |
||
60 | 60 | public function __construct( |
61 | 61 | private Config $config = new Config(), |
62 | 62 | ?string $scopeName = self::DEFAULT_ROOT_SCOPE_NAME, |
63 | - ) { |
|
63 | + ){ |
|
64 | 64 | $this->initServices($this, $scopeName); |
65 | 65 | |
66 | 66 | // Bind himself |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * |
131 | 131 | * @psalm-suppress InvalidArgument, InvalidCast |
132 | 132 | */ |
133 | - public function get(string|Autowire $id, string $context = null): mixed |
|
133 | + public function get(string | Autowire $id, string $context = null): mixed |
|
134 | 134 | { |
135 | 135 | return $this->container->get($id, $context); |
136 | 136 | } |
@@ -158,26 +158,26 @@ discard block |
||
158 | 158 | { |
159 | 159 | $binds = &$this->state->bindings; |
160 | 160 | $cleanup = $previous = []; |
161 | - foreach ($bindings as $alias => $resolver) { |
|
162 | - if (isset($binds[$alias])) { |
|
161 | + foreach ($bindings as $alias => $resolver){ |
|
162 | + if (isset($binds[$alias])){ |
|
163 | 163 | $previous[$alias] = $binds[$alias]; |
164 | - } else { |
|
164 | + }else{ |
|
165 | 165 | $cleanup[] = $alias; |
166 | 166 | } |
167 | 167 | |
168 | 168 | $this->binder->bind($alias, $resolver); |
169 | 169 | } |
170 | 170 | |
171 | - try { |
|
171 | + try{ |
|
172 | 172 | return ContainerScope::getContainer() !== $this |
173 | 173 | ? ContainerScope::runScope($this, $scope) |
174 | 174 | : $scope($this); |
175 | - } finally { |
|
176 | - foreach ($previous as $alias => $resolver) { |
|
175 | + }finally{ |
|
176 | + foreach ($previous as $alias => $resolver){ |
|
177 | 177 | $binds[$alias] = $resolver; |
178 | 178 | } |
179 | 179 | |
180 | - foreach ($cleanup as $alias) { |
|
180 | + foreach ($cleanup as $alias){ |
|
181 | 181 | unset($binds[$alias]); |
182 | 182 | } |
183 | 183 | } |
@@ -202,32 +202,32 @@ discard block |
||
202 | 202 | // Open scope |
203 | 203 | $container = new self($this->config, $name); |
204 | 204 | |
205 | - try { |
|
205 | + try{ |
|
206 | 206 | // Configure scope |
207 | 207 | $container->scope->setParent($this, $this->scope); |
208 | 208 | |
209 | 209 | // Add specific bindings |
210 | - foreach ($bindings as $alias => $resolver) { |
|
210 | + foreach ($bindings as $alias => $resolver){ |
|
211 | 211 | $container->binder->bind($alias, $resolver); |
212 | 212 | } |
213 | 213 | |
214 | 214 | return ContainerScope::runScope( |
215 | 215 | $container, |
216 | 216 | static function (self $container) use ($autowire, $closure): mixed { |
217 | - try { |
|
217 | + try{ |
|
218 | 218 | return $autowire |
219 | 219 | ? $container->invoke($closure) |
220 | 220 | : $closure($container); |
221 | - } finally { |
|
221 | + }finally{ |
|
222 | 222 | $container->closeScope(); |
223 | 223 | } |
224 | 224 | } |
225 | 225 | ); |
226 | - } finally { |
|
226 | + }finally{ |
|
227 | 227 | // Check the container has not been leaked |
228 | 228 | $link = \WeakReference::create($container); |
229 | 229 | unset($container); |
230 | - if ($link->get() !== null) { |
|
230 | + if ($link->get() !== null){ |
|
231 | 231 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
232 | 232 | } |
233 | 233 | } |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | * for each method call), function array or Closure (executed every call). Only object resolvers |
239 | 239 | * supported by this method. |
240 | 240 | */ |
241 | - public function bind(string $alias, string|array|callable|object $resolver): void |
|
241 | + public function bind(string $alias, string | array | callable | object $resolver): void |
|
242 | 242 | { |
243 | 243 | $this->binder->bind($alias, $resolver); |
244 | 244 | } |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | * |
250 | 250 | * @psalm-param TResolver $resolver |
251 | 251 | */ |
252 | - public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
|
252 | + public function bindSingleton(string $alias, string | array | callable | object $resolver): void |
|
253 | 253 | { |
254 | 254 | $this->binder->bindSingleton($alias, $resolver); |
255 | 255 | } |
@@ -319,8 +319,8 @@ discard block |
||
319 | 319 | ]); |
320 | 320 | |
321 | 321 | // Create container services |
322 | - foreach ($container->config as $property => $class) { |
|
323 | - if (\property_exists($container, $property)) { |
|
322 | + foreach ($container->config as $property => $class){ |
|
323 | + if (\property_exists($container, $property)){ |
|
324 | 324 | $container->$property = $constructor->get($property, $class); |
325 | 325 | } |
326 | 326 | } |
@@ -334,7 +334,7 @@ discard block |
||
334 | 334 | private function closeScope(): void |
335 | 335 | { |
336 | 336 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
337 | - if (!isset($this->scope)) { |
|
337 | + if (!isset($this->scope)){ |
|
338 | 338 | $this->destruct(); |
339 | 339 | return; |
340 | 340 | } |
@@ -343,10 +343,10 @@ discard block |
||
343 | 343 | |
344 | 344 | // Run finalizers |
345 | 345 | $errors = []; |
346 | - foreach ($this->state->finalizers as $finalizer) { |
|
347 | - try { |
|
346 | + foreach ($this->state->finalizers as $finalizer){ |
|
347 | + try{ |
|
348 | 348 | $this->invoker->invoke($finalizer); |
349 | - } catch (\Throwable $e) { |
|
349 | + }catch (\Throwable $e){ |
|
350 | 350 | $errors[] = $e; |
351 | 351 | } |
352 | 352 | } |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | $this->destruct(); |
356 | 356 | |
357 | 357 | // Throw collected errors |
358 | - if ($errors !== []) { |
|
358 | + if ($errors !== []){ |
|
359 | 359 | throw new FinalizersException($scopeName, $errors); |
360 | 360 | } |
361 | 361 | } |
@@ -158,26 +158,35 @@ discard block |
||
158 | 158 | { |
159 | 159 | $binds = &$this->state->bindings; |
160 | 160 | $cleanup = $previous = []; |
161 | - foreach ($bindings as $alias => $resolver) { |
|
162 | - if (isset($binds[$alias])) { |
|
161 | + foreach ($bindings as $alias => $resolver) |
|
162 | + { |
|
163 | + if (isset($binds[$alias])) |
|
164 | + { |
|
163 | 165 | $previous[$alias] = $binds[$alias]; |
164 | - } else { |
|
166 | + } |
|
167 | + else |
|
168 | + { |
|
165 | 169 | $cleanup[] = $alias; |
166 | 170 | } |
167 | 171 | |
168 | 172 | $this->binder->bind($alias, $resolver); |
169 | 173 | } |
170 | 174 | |
171 | - try { |
|
175 | + try |
|
176 | + { |
|
172 | 177 | return ContainerScope::getContainer() !== $this |
173 | 178 | ? ContainerScope::runScope($this, $scope) |
174 | 179 | : $scope($this); |
175 | - } finally { |
|
176 | - foreach ($previous as $alias => $resolver) { |
|
180 | + } |
|
181 | + finally |
|
182 | + { |
|
183 | + foreach ($previous as $alias => $resolver) |
|
184 | + { |
|
177 | 185 | $binds[$alias] = $resolver; |
178 | 186 | } |
179 | 187 | |
180 | - foreach ($cleanup as $alias) { |
|
188 | + foreach ($cleanup as $alias) |
|
189 | + { |
|
181 | 190 | unset($binds[$alias]); |
182 | 191 | } |
183 | 192 | } |
@@ -202,32 +211,40 @@ discard block |
||
202 | 211 | // Open scope |
203 | 212 | $container = new self($this->config, $name); |
204 | 213 | |
205 | - try { |
|
214 | + try |
|
215 | + { |
|
206 | 216 | // Configure scope |
207 | 217 | $container->scope->setParent($this, $this->scope); |
208 | 218 | |
209 | 219 | // Add specific bindings |
210 | - foreach ($bindings as $alias => $resolver) { |
|
220 | + foreach ($bindings as $alias => $resolver) |
|
221 | + { |
|
211 | 222 | $container->binder->bind($alias, $resolver); |
212 | 223 | } |
213 | 224 | |
214 | 225 | return ContainerScope::runScope( |
215 | 226 | $container, |
216 | 227 | static function (self $container) use ($autowire, $closure): mixed { |
217 | - try { |
|
228 | + try |
|
229 | + { |
|
218 | 230 | return $autowire |
219 | 231 | ? $container->invoke($closure) |
220 | 232 | : $closure($container); |
221 | - } finally { |
|
233 | + } |
|
234 | + finally |
|
235 | + { |
|
222 | 236 | $container->closeScope(); |
223 | 237 | } |
224 | 238 | } |
225 | 239 | ); |
226 | - } finally { |
|
240 | + } |
|
241 | + finally |
|
242 | + { |
|
227 | 243 | // Check the container has not been leaked |
228 | 244 | $link = \WeakReference::create($container); |
229 | 245 | unset($container); |
230 | - if ($link->get() !== null) { |
|
246 | + if ($link->get() !== null) |
|
247 | + { |
|
231 | 248 | throw new ScopeContainerLeakedException($name, $this->scope->getParentScopeNames()); |
232 | 249 | } |
233 | 250 | } |
@@ -319,8 +336,10 @@ discard block |
||
319 | 336 | ]); |
320 | 337 | |
321 | 338 | // Create container services |
322 | - foreach ($container->config as $property => $class) { |
|
323 | - if (\property_exists($container, $property)) { |
|
339 | + foreach ($container->config as $property => $class) |
|
340 | + { |
|
341 | + if (\property_exists($container, $property)) |
|
342 | + { |
|
324 | 343 | $container->$property = $constructor->get($property, $class); |
325 | 344 | } |
326 | 345 | } |
@@ -334,7 +353,8 @@ discard block |
||
334 | 353 | private function closeScope(): void |
335 | 354 | { |
336 | 355 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
337 | - if (!isset($this->scope)) { |
|
356 | + if (!isset($this->scope)) |
|
357 | + { |
|
338 | 358 | $this->destruct(); |
339 | 359 | return; |
340 | 360 | } |
@@ -343,10 +363,14 @@ discard block |
||
343 | 363 | |
344 | 364 | // Run finalizers |
345 | 365 | $errors = []; |
346 | - foreach ($this->state->finalizers as $finalizer) { |
|
347 | - try { |
|
366 | + foreach ($this->state->finalizers as $finalizer) |
|
367 | + { |
|
368 | + try |
|
369 | + { |
|
348 | 370 | $this->invoker->invoke($finalizer); |
349 | - } catch (\Throwable $e) { |
|
371 | + } |
|
372 | + catch (\Throwable $e) |
|
373 | + { |
|
350 | 374 | $errors[] = $e; |
351 | 375 | } |
352 | 376 | } |
@@ -355,7 +379,8 @@ discard block |
||
355 | 379 | $this->destruct(); |
356 | 380 | |
357 | 381 | // Throw collected errors |
358 | - if ($errors !== []) { |
|
382 | + if ($errors !== []) |
|
383 | + { |
|
359 | 384 | throw new FinalizersException($scopeName, $errors); |
360 | 385 | } |
361 | 386 | } |