@@ -31,7 +31,8 @@ |
||
31 | 31 | $option->checkScope = true; |
32 | 32 | yield [$option]; |
33 | 33 | |
34 | - $option = new class extends Options { |
|
34 | + $option = new class extends Options |
|
35 | + { |
|
35 | 36 | public int $customOption = 3; |
36 | 37 | }; |
37 | 38 | $option->customOption = 5; |
@@ -171,23 +171,29 @@ discard block |
||
171 | 171 | */ |
172 | 172 | public function runScope(Scope|array $bindings, callable $scope): mixed |
173 | 173 | { |
174 | - if (!\is_array($bindings)) { |
|
174 | + if (!\is_array($bindings)) |
|
175 | + { |
|
175 | 176 | return $this->runIsolatedScope($bindings, $scope); |
176 | 177 | } |
177 | 178 | |
178 | 179 | $binds = &$this->state->bindings; |
179 | 180 | $singletons = &$this->state->singletons; |
180 | 181 | $cleanup = $previous = $prevSin = []; |
181 | - foreach ($bindings as $alias => $resolver) { |
|
182 | + foreach ($bindings as $alias => $resolver) |
|
183 | + { |
|
182 | 184 | // Store previous bindings |
183 | - if (isset($binds[$alias])) { |
|
185 | + if (isset($binds[$alias])) |
|
186 | + { |
|
184 | 187 | $previous[$alias] = $binds[$alias]; |
185 | - } else { |
|
188 | + } |
|
189 | + else |
|
190 | + { |
|
186 | 191 | // Store bindings to be removed |
187 | 192 | $cleanup[] = $alias; |
188 | 193 | } |
189 | 194 | // Store previous singletons |
190 | - if (isset($singletons[$alias])) { |
|
195 | + if (isset($singletons[$alias])) |
|
196 | + { |
|
191 | 197 | $prevSin[$alias] = $singletons[$alias]; |
192 | 198 | unset($singletons[$alias]); |
193 | 199 | } |
@@ -195,21 +201,27 @@ discard block |
||
195 | 201 | $this->binder->bind($alias, $resolver); |
196 | 202 | } |
197 | 203 | |
198 | - try { |
|
204 | + try |
|
205 | + { |
|
199 | 206 | return ContainerScope::getContainer() !== $this |
200 | 207 | ? ContainerScope::runScope($this, $scope) |
201 | 208 | : $scope($this); |
202 | - } finally { |
|
209 | + } |
|
210 | + finally |
|
211 | + { |
|
203 | 212 | // Remove new bindings |
204 | - foreach ($cleanup as $alias) { |
|
213 | + foreach ($cleanup as $alias) |
|
214 | + { |
|
205 | 215 | unset($binds[$alias], $singletons[$alias]); |
206 | 216 | } |
207 | 217 | // Restore previous bindings |
208 | - foreach ($previous as $alias => $resolver) { |
|
218 | + foreach ($previous as $alias => $resolver) |
|
219 | + { |
|
209 | 220 | $binds[$alias] = $resolver; |
210 | 221 | } |
211 | 222 | // Restore singletons |
212 | - foreach ($prevSin as $alias => $instance) { |
|
223 | + foreach ($prevSin as $alias => $instance) |
|
224 | + { |
|
213 | 225 | $singletons[$alias] = $instance; |
214 | 226 | } |
215 | 227 | } |
@@ -257,7 +269,8 @@ discard block |
||
257 | 269 | */ |
258 | 270 | public function bindSingleton(string $alias, string|array|callable|object $resolver, bool $force = true): void |
259 | 271 | { |
260 | - if ($force) { |
|
272 | + if ($force) |
|
273 | + { |
|
261 | 274 | $this->binder->removeBinding($alias); |
262 | 275 | } |
263 | 276 | |
@@ -328,8 +341,10 @@ discard block |
||
328 | 341 | ], $this->options); |
329 | 342 | |
330 | 343 | // Create container services |
331 | - foreach ($container->config as $property => $class) { |
|
332 | - if (\property_exists($container, $property)) { |
|
344 | + foreach ($container->config as $property => $class) |
|
345 | + { |
|
346 | + if (\property_exists($container, $property)) |
|
347 | + { |
|
333 | 348 | $container->$property = $constructor->get($property, $class); |
334 | 349 | } |
335 | 350 | } |
@@ -343,7 +358,8 @@ discard block |
||
343 | 358 | private function closeScope(): void |
344 | 359 | { |
345 | 360 | /** @psalm-suppress RedundantPropertyInitializationCheck */ |
346 | - if (!isset($this->scope)) { |
|
361 | + if (!isset($this->scope)) |
|
362 | + { |
|
347 | 363 | $this->destruct(); |
348 | 364 | return; |
349 | 365 | } |
@@ -352,10 +368,14 @@ discard block |
||
352 | 368 | |
353 | 369 | // Run finalizers |
354 | 370 | $errors = []; |
355 | - foreach ($this->state->finalizers as $finalizer) { |
|
356 | - try { |
|
371 | + foreach ($this->state->finalizers as $finalizer) |
|
372 | + { |
|
373 | + try |
|
374 | + { |
|
357 | 375 | $this->invoker->invoke($finalizer); |
358 | - } catch (\Throwable $e) { |
|
376 | + } |
|
377 | + catch (\Throwable $e) |
|
378 | + { |
|
359 | 379 | $errors[] = $e; |
360 | 380 | } |
361 | 381 | } |
@@ -364,7 +384,8 @@ discard block |
||
364 | 384 | $this->destruct(); |
365 | 385 | |
366 | 386 | // Throw collected errors |
367 | - if ($errors !== []) { |
|
387 | + if ($errors !== []) |
|
388 | + { |
|
368 | 389 | throw new FinalizersException($scopeName, $errors); |
369 | 390 | } |
370 | 391 | } |
@@ -386,18 +407,22 @@ discard block |
||
386 | 407 | $container->scope->setParent($this, $this->scope, $this->factory); |
387 | 408 | |
388 | 409 | // Add specific bindings |
389 | - foreach ($config->bindings as $alias => $resolver) { |
|
410 | + foreach ($config->bindings as $alias => $resolver) |
|
411 | + { |
|
390 | 412 | $container->binder->bind($alias, $resolver); |
391 | 413 | } |
392 | 414 | |
393 | 415 | return ContainerScope::runScope( |
394 | 416 | $container, |
395 | 417 | static function (self $container) use ($config, $closure): mixed { |
396 | - try { |
|
418 | + try |
|
419 | + { |
|
397 | 420 | return $config->autowire |
398 | 421 | ? $container->invoke($closure) |
399 | 422 | : $closure($container); |
400 | - } finally { |
|
423 | + } |
|
424 | + finally |
|
425 | + { |
|
401 | 426 | $container->closeScope(); |
402 | 427 | } |
403 | 428 | } |
@@ -21,8 +21,10 @@ discard block |
||
21 | 21 | { |
22 | 22 | $root = self::makeContainer(); |
23 | 23 | |
24 | - $obj = $root->runScoped(static function (Container $c1) { |
|
25 | - $obj = $c1->runScoped(static function (Container $c2) { |
|
24 | + $obj = $root->runScoped(static function (Container $c1) |
|
25 | + { |
|
26 | + $obj = $c1->runScoped(static function (Container $c2) |
|
27 | + { |
|
26 | 28 | $obj = $c2->get(AttrScopeFooFinalize::class); |
27 | 29 | |
28 | 30 | self::assertFalse($obj->finalized); |
@@ -46,8 +48,10 @@ discard block |
||
46 | 48 | $root->bindSingleton(LoggerInterface::class, FileLogger::class); |
47 | 49 | |
48 | 50 | $obj2 = null; |
49 | - $obj = $root->runScoped(static function (Container $c1) use (&$obj2) { |
|
50 | - $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) { |
|
51 | + $obj = $root->runScoped(static function (Container $c1) use (&$obj2) |
|
52 | + { |
|
53 | + $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) |
|
54 | + { |
|
51 | 55 | $obj = $c2->get(AttrScopeFooFinalize::class); |
52 | 56 | $obj2 = $c2->get(AttrScopeFooFinalize::class); |
53 | 57 | |
@@ -77,8 +81,10 @@ discard block |
||
77 | 81 | $root->bindSingleton(LoggerInterface::class, FileLogger::class); |
78 | 82 | |
79 | 83 | $obj2 = null; |
80 | - $obj = $root->runScoped(static function (Container $c1) use (&$obj2) { |
|
81 | - $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) { |
|
84 | + $obj = $root->runScoped(static function (Container $c1) use (&$obj2) |
|
85 | + { |
|
86 | + $obj = $c1->runScoped(static function (Container $c2) use (&$obj2) |
|
87 | + { |
|
82 | 88 | $obj = $c2->get(AttrFinalize::class); |
83 | 89 | $obj2 = $c2->get(AttrFinalize::class); |
84 | 90 | |
@@ -124,12 +130,16 @@ discard block |
||
124 | 130 | self::expectException(FinalizersException::class); |
125 | 131 | self::expectExceptionMessage('An exception has been thrown during finalization of the scope `foo`'); |
126 | 132 | |
127 | - try { |
|
128 | - $root->runScoped(static function (Container $c1) { |
|
133 | + try |
|
134 | + { |
|
135 | + $root->runScoped(static function (Container $c1) |
|
136 | + { |
|
129 | 137 | $obj = $c1->get(AttrScopeFooFinalize::class); |
130 | 138 | $obj->throwException = true; |
131 | 139 | }, name: 'foo'); |
132 | - } catch (FinalizersException $e) { |
|
140 | + } |
|
141 | + catch (FinalizersException $e) |
|
142 | + { |
|
133 | 143 | self::assertSame('foo', $e->getScope()); |
134 | 144 | self::assertCount(1, $e->getExceptions()); |
135 | 145 | // Contains the message from the inner exception. |
@@ -153,13 +163,17 @@ discard block |
||
153 | 163 | self::expectException(FinalizersException::class); |
154 | 164 | self::expectExceptionMessage('3 exceptions have been thrown during finalization of the scope `foo`'); |
155 | 165 | |
156 | - try { |
|
157 | - $root->runScoped(static function (Container $c1) { |
|
166 | + try |
|
167 | + { |
|
168 | + $root->runScoped(static function (Container $c1) |
|
169 | + { |
|
158 | 170 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
159 | 171 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
160 | 172 | $c1->get(AttrScopeFooFinalize::class)->throwException = true; |
161 | 173 | }, name: 'foo'); |
162 | - } catch (FinalizersException $e) { |
|
174 | + } |
|
175 | + catch (FinalizersException $e) |
|
176 | + { |
|
163 | 177 | self::assertSame('foo', $e->getScope()); |
164 | 178 | self::assertCount(3, $e->getExceptions()); |
165 | 179 | // Contains the message from the inner exception. |
@@ -39,8 +39,10 @@ discard block |
||
39 | 39 | { |
40 | 40 | $root = self::makeContainer(); |
41 | 41 | |
42 | - $root->runScoped(static function (Container $c1) { |
|
43 | - $c1->runScoped(static function (Container $c2) use ($c1) { |
|
42 | + $root->runScoped(static function (Container $c1) |
|
43 | + { |
|
44 | + $c1->runScoped(static function (Container $c2) use ($c1) |
|
45 | + { |
|
44 | 46 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
45 | 47 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
46 | 48 | |
@@ -54,8 +56,10 @@ discard block |
||
54 | 56 | $root = self::makeContainer(); |
55 | 57 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
56 | 58 | |
57 | - $root->runScoped(static function (Container $fooScope) { |
|
58 | - $fooScope->runScoped(static function (Container $container) { |
|
59 | + $root->runScoped(static function (Container $fooScope) |
|
60 | + { |
|
61 | + $fooScope->runScoped(static function (Container $container) |
|
62 | + { |
|
59 | 63 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
60 | 64 | }, name: 'bar'); |
61 | 65 | }, name: 'foo'); |
@@ -69,8 +73,10 @@ discard block |
||
69 | 73 | $root = self::makeContainer(); |
70 | 74 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
71 | 75 | |
72 | - $root->runScoped(static function (Container $fooScope) { |
|
73 | - $fooScope->runScoped(static function (Container $container) { |
|
76 | + $root->runScoped(static function (Container $fooScope) |
|
77 | + { |
|
78 | + $fooScope->runScoped(static function (Container $container) |
|
79 | + { |
|
74 | 80 | $container->get('binding'); |
75 | 81 | }, name: 'bar'); |
76 | 82 | }, name: 'baz'); |
@@ -81,8 +87,10 @@ discard block |
||
81 | 87 | $root = self::makeContainer(checkScope: false); |
82 | 88 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
83 | 89 | |
84 | - $root->runScoped(static function (Container $fooScope) { |
|
85 | - $fooScope->runScoped(static function (Container $container) { |
|
90 | + $root->runScoped(static function (Container $fooScope) |
|
91 | + { |
|
92 | + $fooScope->runScoped(static function (Container $container) |
|
93 | + { |
|
86 | 94 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
87 | 95 | }, name: 'bar'); |
88 | 96 | }, name: 'baz'); |
@@ -101,8 +109,10 @@ discard block |
||
101 | 109 | $root = self::makeContainer(); |
102 | 110 | $root->bind('foo', self::makeFooScopeObject(...)); |
103 | 111 | |
104 | - $root->runScoped(static function (Container $c1) { |
|
105 | - $c1->runScoped(static function (Container $c2) { |
|
112 | + $root->runScoped(static function (Container $c1) |
|
113 | + { |
|
114 | + $c1->runScoped(static function (Container $c2) |
|
115 | + { |
|
106 | 116 | $c2->get('foo'); |
107 | 117 | }, name: 'foo'); |
108 | 118 | }); |
@@ -114,8 +124,10 @@ discard block |
||
114 | 124 | $root = self::makeContainer(checkScope: false); |
115 | 125 | $root->bind('foo', self::makeFooScopeObject(...)); |
116 | 126 | |
117 | - $root->runScoped(static function (Container $c1) { |
|
118 | - $c1->runScoped(static function (Container $c2) { |
|
127 | + $root->runScoped(static function (Container $c1) |
|
128 | + { |
|
129 | + $c1->runScoped(static function (Container $c2) |
|
130 | + { |
|
119 | 131 | self::assertInstanceOf(AttrScopeFoo::class, $c2->get('foo')); |
120 | 132 | }, name: 'foo'); |
121 | 133 | }); |
@@ -133,8 +145,10 @@ discard block |
||
133 | 145 | $root = self::makeContainer(); |
134 | 146 | $root->bind('foo', self::makeFooScopeObject(...)); |
135 | 147 | |
136 | - $root->runScoped(static function (Container $c1) { |
|
137 | - $c1->runScoped(static function (Container $c2) { |
|
148 | + $root->runScoped(static function (Container $c1) |
|
149 | + { |
|
150 | + $c1->runScoped(static function (Container $c2) |
|
151 | + { |
|
138 | 152 | $c2->get('foo'); |
139 | 153 | }); |
140 | 154 | }); |
@@ -146,8 +160,10 @@ discard block |
||
146 | 160 | $root = self::makeContainer(checkScope: false); |
147 | 161 | $root->bind('foo', self::makeFooScopeObject(...)); |
148 | 162 | |
149 | - $root->runScoped(static function (Container $c1) { |
|
150 | - $c1->runScoped(static function (Container $c2) { |
|
163 | + $root->runScoped(static function (Container $c1) |
|
164 | + { |
|
165 | + $c1->runScoped(static function (Container $c2) |
|
166 | + { |
|
151 | 167 | self::assertInstanceOf(AttrScopeFoo::class, $c2->get('foo')); |
152 | 168 | }); |
153 | 169 | }); |
@@ -165,15 +181,21 @@ discard block |
||
165 | 181 | |
166 | 182 | $root = self::makeContainer(); |
167 | 183 | |
168 | - try { |
|
169 | - $root->runScoped(static function (Container $c1) { |
|
170 | - $c1->runScoped(static function (Container $c2) { |
|
171 | - $c2->runScoped(static function (Container $c3) { |
|
184 | + try |
|
185 | + { |
|
186 | + $root->runScoped(static function (Container $c1) |
|
187 | + { |
|
188 | + $c1->runScoped(static function (Container $c2) |
|
189 | + { |
|
190 | + $c2->runScoped(static function (Container $c3) |
|
191 | + { |
|
172 | 192 | // do nothing |
173 | 193 | }, name: 'root'); |
174 | 194 | }); |
175 | 195 | }); |
176 | - } catch (NamedScopeDuplicationException $e) { |
|
196 | + } |
|
197 | + catch (NamedScopeDuplicationException $e) |
|
198 | + { |
|
177 | 199 | self::assertSame('root', $e->getScope()); |
178 | 200 | throw $e; |
179 | 201 | } |
@@ -188,14 +210,19 @@ discard block |
||
188 | 210 | { |
189 | 211 | self::expectException(BadScopeException::class); |
190 | 212 | |
191 | - try { |
|
213 | + try |
|
214 | + { |
|
192 | 215 | $root = self::makeContainer(); |
193 | - $root->runScoped(static function (Container $c1) { |
|
194 | - $c1->runScoped(static function (Container $c2) { |
|
216 | + $root->runScoped(static function (Container $c1) |
|
217 | + { |
|
218 | + $c1->runScoped(static function (Container $c2) |
|
219 | + { |
|
195 | 220 | $c2->get(AttrScopeFoo::class); |
196 | 221 | }); |
197 | 222 | }, name: 'bar'); |
198 | - } catch (BadScopeException $e) { |
|
223 | + } |
|
224 | + catch (BadScopeException $e) |
|
225 | + { |
|
199 | 226 | self::assertSame('foo', $e->getScope()); |
200 | 227 | throw $e; |
201 | 228 | } |
@@ -68,17 +68,20 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
70 | 70 | { |
71 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
71 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
72 | + { |
|
72 | 73 | return $this->state->singletons[$alias]; |
73 | 74 | } |
74 | 75 | |
75 | 76 | $binding = $this->state->bindings[$alias] ?? null; |
76 | 77 | |
77 | - if ($binding === null) { |
|
78 | + if ($binding === null) |
|
79 | + { |
|
78 | 80 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
79 | 81 | } |
80 | 82 | |
81 | - try { |
|
83 | + try |
|
84 | + { |
|
82 | 85 | $this->tracer->push( |
83 | 86 | false, |
84 | 87 | action: 'resolve from binding', |
@@ -108,7 +111,9 @@ discard block |
||
108 | 111 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
109 | 112 | default => $binding, |
110 | 113 | }; |
111 | - } finally { |
|
114 | + } |
|
115 | + finally |
|
116 | + { |
|
112 | 117 | $this->state->bindings[$alias] ??= $binding; |
113 | 118 | $this->tracer->pop(true); |
114 | 119 | $this->tracer->pop(false); |
@@ -122,18 +127,23 @@ discard block |
||
122 | 127 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
123 | 128 | { |
124 | 129 | $context = $ctx->context; |
125 | - try { |
|
130 | + try |
|
131 | + { |
|
126 | 132 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
127 | - } catch (\ReflectionException $e) { |
|
133 | + } |
|
134 | + catch (\ReflectionException $e) |
|
135 | + { |
|
128 | 136 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
129 | 137 | } |
130 | 138 | |
131 | 139 | $injector = $binding->injector; |
132 | 140 | |
133 | - try { |
|
141 | + try |
|
142 | + { |
|
134 | 143 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
135 | 144 | |
136 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
145 | + if (!$injectorInstance instanceof InjectorInterface) |
|
146 | + { |
|
137 | 147 | throw new InjectionException( |
138 | 148 | \sprintf( |
139 | 149 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -160,7 +170,8 @@ discard block |
||
160 | 170 | default => (string)$context, |
161 | 171 | }); |
162 | 172 | |
163 | - if (!$reflection->isInstance($instance)) { |
|
173 | + if (!$reflection->isInstance($instance)) |
|
174 | + { |
|
164 | 175 | throw new InjectionException( |
165 | 176 | \sprintf( |
166 | 177 | "Invalid injection response for '%s'.", |
@@ -170,7 +181,9 @@ discard block |
||
170 | 181 | } |
171 | 182 | |
172 | 183 | return $instance; |
173 | - } finally { |
|
184 | + } |
|
185 | + finally |
|
186 | + { |
|
174 | 187 | $this->state->bindings[$ctx->class] ??= $binding; |
175 | 188 | } |
176 | 189 | } |
@@ -189,7 +202,8 @@ discard block |
||
189 | 202 | //Binding is pointing to something else |
190 | 203 | : $this->make($binding->alias, $arguments, $context); |
191 | 204 | |
192 | - if ($binding->singleton && $arguments === []) { |
|
205 | + if ($binding->singleton && $arguments === []) |
|
206 | + { |
|
193 | 207 | $this->state->singletons[$alias] = $result; |
194 | 208 | } |
195 | 209 | |
@@ -200,7 +214,8 @@ discard block |
||
200 | 214 | { |
201 | 215 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
202 | 216 | |
203 | - if ($binding->singleton) { |
|
217 | + if ($binding->singleton) |
|
218 | + { |
|
204 | 219 | $this->state->singletons[$alias] = $result; |
205 | 220 | } |
206 | 221 | |
@@ -241,11 +256,14 @@ discard block |
||
241 | 256 | array $arguments, |
242 | 257 | ): mixed { |
243 | 258 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
244 | - try { |
|
259 | + try |
|
260 | + { |
|
245 | 261 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
246 | 262 | ? ($binding->factory)() |
247 | 263 | : $this->invoker->invoke($binding->factory, $arguments); |
248 | - } catch (NotCallableException $e) { |
|
264 | + } |
|
265 | + catch (NotCallableException $e) |
|
266 | + { |
|
249 | 267 | throw new ContainerException( |
250 | 268 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
251 | 269 | $e->getCode(), |
@@ -264,19 +282,24 @@ discard block |
||
264 | 282 | ): ?object { |
265 | 283 | $avoidCache = $arguments !== []; |
266 | 284 | |
267 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
268 | - try { |
|
285 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
286 | + { |
|
287 | + try |
|
288 | + { |
|
269 | 289 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
270 | 290 | |
271 | 291 | $object = $this->createInstance( |
272 | 292 | new Ctx(alias: $alias, class: $alias, context: $context), |
273 | 293 | $arguments, |
274 | 294 | ); |
275 | - if ($avoidCache) { |
|
295 | + if ($avoidCache) |
|
296 | + { |
|
276 | 297 | return $object; |
277 | 298 | } |
278 | 299 | $binding->reference = WeakReference::create($object); |
279 | - } catch (\Throwable) { |
|
300 | + } |
|
301 | + catch (\Throwable) |
|
302 | + { |
|
280 | 303 | throw new ContainerException( |
281 | 304 | $this->tracer->combineTraceMessage( |
282 | 305 | \sprintf( |
@@ -286,7 +309,9 @@ discard block |
||
286 | 309 | ) |
287 | 310 | ) |
288 | 311 | ); |
289 | - } finally { |
|
312 | + } |
|
313 | + finally |
|
314 | + { |
|
290 | 315 | $this->tracer->pop(); |
291 | 316 | } |
292 | 317 | } |
@@ -301,19 +326,26 @@ discard block |
||
301 | 326 | ): mixed { |
302 | 327 | $parent = $this->scope->getParentFactory(); |
303 | 328 | |
304 | - if ($parent !== null) { |
|
305 | - try { |
|
329 | + if ($parent !== null) |
|
330 | + { |
|
331 | + try |
|
332 | + { |
|
306 | 333 | $this->tracer->push(false, ...[ |
307 | 334 | 'current scope' => $this->scope->getScopeName(), |
308 | 335 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
309 | 336 | ]); |
310 | 337 | /** @psalm-suppress TooManyArguments */ |
311 | 338 | return $parent->make($alias, $parameters, $context); |
312 | - } catch (BadScopeException $e) { |
|
313 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
339 | + } |
|
340 | + catch (BadScopeException $e) |
|
341 | + { |
|
342 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
343 | + { |
|
314 | 344 | throw $e; |
315 | 345 | } |
316 | - } catch (ContainerExceptionInterface $e) { |
|
346 | + } |
|
347 | + catch (ContainerExceptionInterface $e) |
|
348 | + { |
|
317 | 349 | $className = match (true) { |
318 | 350 | $e instanceof NotFoundException => NotFoundException::class, |
319 | 351 | default => ContainerException::class, |
@@ -322,19 +354,24 @@ discard block |
||
322 | 354 | 'Can\'t resolve `%s`.', |
323 | 355 | $alias, |
324 | 356 | )), previous: $e); |
325 | - } finally { |
|
357 | + } |
|
358 | + finally |
|
359 | + { |
|
326 | 360 | $this->tracer->pop(false); |
327 | 361 | } |
328 | 362 | } |
329 | 363 | |
330 | 364 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
331 | - try { |
|
365 | + try |
|
366 | + { |
|
332 | 367 | //No direct instructions how to construct class, make is automatically |
333 | 368 | return $this->autowire( |
334 | 369 | new Ctx(alias: $alias, class: $alias, context: $context), |
335 | 370 | $parameters, |
336 | 371 | ); |
337 | - } finally { |
|
372 | + } |
|
373 | + finally |
|
374 | + { |
|
338 | 375 | $this->tracer->pop(false); |
339 | 376 | } |
340 | 377 | } |
@@ -381,13 +418,16 @@ discard block |
||
381 | 418 | Ctx $ctx, |
382 | 419 | array $arguments, |
383 | 420 | ): object { |
384 | - if ($this->options->checkScope) { |
|
421 | + if ($this->options->checkScope) |
|
422 | + { |
|
385 | 423 | // Check scope name |
386 | 424 | $ctx->reflection = new \ReflectionClass($instance); |
387 | 425 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
388 | - if ($scopeName !== null) { |
|
426 | + if ($scopeName !== null) |
|
427 | + { |
|
389 | 428 | $scope = $this->scope; |
390 | - while ($scope->getScopeName() !== $scopeName) { |
|
429 | + while ($scope->getScopeName() !== $scopeName) |
|
430 | + { |
|
391 | 431 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
392 | 432 | } |
393 | 433 | } |
@@ -416,26 +456,33 @@ discard block |
||
416 | 456 | array $arguments, |
417 | 457 | ): object { |
418 | 458 | $class = $ctx->class; |
419 | - try { |
|
459 | + try |
|
460 | + { |
|
420 | 461 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
421 | - } catch (\ReflectionException $e) { |
|
462 | + } |
|
463 | + catch (\ReflectionException $e) |
|
464 | + { |
|
422 | 465 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
423 | 466 | } |
424 | 467 | |
425 | 468 | // Check scope name |
426 | - if ($this->options->checkScope) { |
|
469 | + if ($this->options->checkScope) |
|
470 | + { |
|
427 | 471 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
428 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
472 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
473 | + { |
|
429 | 474 | throw new BadScopeException($scope, $class); |
430 | 475 | } |
431 | 476 | } |
432 | 477 | |
433 | 478 | // We have to construct class using external injector when we know the exact context |
434 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
479 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
480 | + { |
|
435 | 481 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
436 | 482 | } |
437 | 483 | |
438 | - if (!$reflection->isInstantiable()) { |
|
484 | + if (!$reflection->isInstantiable()) |
|
485 | + { |
|
439 | 486 | $itIs = match (true) { |
440 | 487 | $reflection->isEnum() => 'Enum', |
441 | 488 | $reflection->isAbstract() => 'Abstract class', |
@@ -448,12 +495,16 @@ discard block |
||
448 | 495 | |
449 | 496 | $constructor = $reflection->getConstructor(); |
450 | 497 | |
451 | - if ($constructor !== null) { |
|
452 | - try { |
|
498 | + if ($constructor !== null) |
|
499 | + { |
|
500 | + try |
|
501 | + { |
|
453 | 502 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
454 | 503 | $this->tracer->push(true); |
455 | 504 | $args = $this->resolver->resolveArguments($constructor, $arguments, $this->options->validateArguments); |
456 | - } catch (ValidationException $e) { |
|
505 | + } |
|
506 | + catch (ValidationException $e) |
|
507 | + { |
|
457 | 508 | throw new ContainerException( |
458 | 509 | $this->tracer->combineTraceMessage( |
459 | 510 | \sprintf( |
@@ -463,22 +514,31 @@ discard block |
||
463 | 514 | ) |
464 | 515 | ), |
465 | 516 | ); |
466 | - } finally { |
|
517 | + } |
|
518 | + finally |
|
519 | + { |
|
467 | 520 | $this->tracer->pop(true); |
468 | 521 | $this->tracer->pop(false); |
469 | 522 | } |
470 | - try { |
|
523 | + try |
|
524 | + { |
|
471 | 525 | // Using constructor with resolved arguments |
472 | 526 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
473 | 527 | $this->tracer->push(true); |
474 | 528 | $instance = new $class(...$args); |
475 | - } catch (\TypeError $e) { |
|
529 | + } |
|
530 | + catch (\TypeError $e) |
|
531 | + { |
|
476 | 532 | throw new WrongTypeException($constructor, $e); |
477 | - } finally { |
|
533 | + } |
|
534 | + finally |
|
535 | + { |
|
478 | 536 | $this->tracer->pop(true); |
479 | 537 | $this->tracer->pop(false); |
480 | 538 | } |
481 | - } else { |
|
539 | + } |
|
540 | + else |
|
541 | + { |
|
482 | 542 | // No constructor specified |
483 | 543 | $instance = $reflection->newInstance(); |
484 | 544 | } |
@@ -496,13 +556,15 @@ discard block |
||
496 | 556 | $instance = $this->runInflector($instance); |
497 | 557 | |
498 | 558 | //Declarative singletons |
499 | - if ($this->isSingleton($ctx)) { |
|
559 | + if ($this->isSingleton($ctx)) |
|
560 | + { |
|
500 | 561 | $this->state->singletons[$ctx->alias] = $instance; |
501 | 562 | } |
502 | 563 | |
503 | 564 | // Register finalizer |
504 | 565 | $finalizer = $this->getFinalizer($ctx, $instance); |
505 | - if ($finalizer !== null) { |
|
566 | + if ($finalizer !== null) |
|
567 | + { |
|
506 | 568 | $this->state->finalizers[] = $finalizer; |
507 | 569 | } |
508 | 570 | |
@@ -514,12 +576,14 @@ discard block |
||
514 | 576 | */ |
515 | 577 | private function isSingleton(Ctx $ctx): bool |
516 | 578 | { |
517 | - if ($ctx->singleton === true) { |
|
579 | + if ($ctx->singleton === true) |
|
580 | + { |
|
518 | 581 | return true; |
519 | 582 | } |
520 | 583 | |
521 | 584 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
522 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
585 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
586 | + { |
|
523 | 587 | return true; |
524 | 588 | } |
525 | 589 | |
@@ -533,7 +597,8 @@ discard block |
||
533 | 597 | * @var Attribute\Finalize|null $attribute |
534 | 598 | */ |
535 | 599 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
536 | - if ($attribute === null) { |
|
600 | + if ($attribute === null) |
|
601 | + { |
|
537 | 602 | return null; |
538 | 603 | } |
539 | 604 | |
@@ -547,10 +612,14 @@ discard block |
||
547 | 612 | { |
548 | 613 | $scope = $this->scope; |
549 | 614 | |
550 | - while ($scope !== null) { |
|
551 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
552 | - if ($instance instanceof $class) { |
|
553 | - foreach ($inflectors as $inflector) { |
|
615 | + while ($scope !== null) |
|
616 | + { |
|
617 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
618 | + { |
|
619 | + if ($instance instanceof $class) |
|
620 | + { |
|
621 | + foreach ($inflectors as $inflector) |
|
622 | + { |
|
554 | 623 | $instance = $inflector->getParametersCount() > 1 |
555 | 624 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
556 | 625 | : ($inflector->inflector)($instance); |
@@ -566,9 +635,12 @@ discard block |
||
566 | 635 | |
567 | 636 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
568 | 637 | { |
569 | - try { |
|
638 | + try |
|
639 | + { |
|
570 | 640 | $this->resolver->validateArguments($reflection, $arguments); |
571 | - } catch (\Throwable) { |
|
641 | + } |
|
642 | + catch (\Throwable) |
|
643 | + { |
|
572 | 644 | return false; |
573 | 645 | } |
574 | 646 |