@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | { |
34 | 34 | $root = new Container(); |
35 | 35 | |
36 | - $root->runScoped(static function (Container $c1) { |
|
36 | + $root->runScoped(static function (Container $c1){ |
|
37 | 37 | $c1->runScoped(static function (Container $c2) use ($c1) { |
38 | 38 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
39 | 39 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
@@ -48,8 +48,8 @@ discard block |
||
48 | 48 | $root = new Container(); |
49 | 49 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
50 | 50 | |
51 | - $root->runScoped(static function (Container $fooScope) { |
|
52 | - $fooScope->runScoped(static function (Container $container) { |
|
51 | + $root->runScoped(static function (Container $fooScope){ |
|
52 | + $fooScope->runScoped(static function (Container $container){ |
|
53 | 53 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
54 | 54 | }, name: 'bar'); |
55 | 55 | }, name: 'foo'); |
@@ -63,8 +63,8 @@ discard block |
||
63 | 63 | $root = new Container(); |
64 | 64 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
65 | 65 | |
66 | - $root->runScoped(static function (Container $fooScope) { |
|
67 | - $fooScope->runScoped(static function (Container $container) { |
|
66 | + $root->runScoped(static function (Container $fooScope){ |
|
67 | + $fooScope->runScoped(static function (Container $container){ |
|
68 | 68 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
69 | 69 | }, name: 'bar'); |
70 | 70 | }, name: 'baz'); |
@@ -83,8 +83,8 @@ discard block |
||
83 | 83 | $root = new Container(); |
84 | 84 | $root->bind('foo', self::makeFooScopeObject(...)); |
85 | 85 | |
86 | - $root->runScoped(static function (Container $c1) { |
|
87 | - $c1->runScoped(static function (Container $c2) { |
|
86 | + $root->runScoped(static function (Container $c1){ |
|
87 | + $c1->runScoped(static function (Container $c2){ |
|
88 | 88 | $c2->get('foo'); |
89 | 89 | }, name: 'foo'); |
90 | 90 | }); |
@@ -102,8 +102,8 @@ discard block |
||
102 | 102 | $root = new Container(); |
103 | 103 | $root->bind('foo', self::makeFooScopeObject(...)); |
104 | 104 | |
105 | - $root->runScoped(static function (Container $c1) { |
|
106 | - $c1->runScoped(static function (Container $c2) { |
|
105 | + $root->runScoped(static function (Container $c1){ |
|
106 | + $c1->runScoped(static function (Container $c2){ |
|
107 | 107 | $c2->get('foo'); |
108 | 108 | }); |
109 | 109 | }); |
@@ -121,15 +121,15 @@ discard block |
||
121 | 121 | |
122 | 122 | $root = new Container(); |
123 | 123 | |
124 | - try { |
|
125 | - $root->runScoped(static function (Container $c1) { |
|
126 | - $c1->runScoped(static function (Container $c2) { |
|
127 | - $c2->runScoped(static function (Container $c3) { |
|
124 | + try{ |
|
125 | + $root->runScoped(static function (Container $c1){ |
|
126 | + $c1->runScoped(static function (Container $c2){ |
|
127 | + $c2->runScoped(static function (Container $c3){ |
|
128 | 128 | // do nothing |
129 | 129 | }, name: 'root'); |
130 | 130 | }); |
131 | 131 | }); |
132 | - } catch (NamedScopeDuplicationException $e) { |
|
132 | + }catch (NamedScopeDuplicationException $e){ |
|
133 | 133 | self::assertSame('root', $e->getScope()); |
134 | 134 | throw $e; |
135 | 135 | } |
@@ -144,14 +144,14 @@ discard block |
||
144 | 144 | { |
145 | 145 | self::expectException(BadScopeException::class); |
146 | 146 | |
147 | - try { |
|
147 | + try{ |
|
148 | 148 | $root = new Container(); |
149 | - $root->runScoped(static function (Container $c1) { |
|
150 | - $c1->runScoped(static function (Container $c2) { |
|
149 | + $root->runScoped(static function (Container $c1){ |
|
150 | + $c1->runScoped(static function (Container $c2){ |
|
151 | 151 | $c2->get(AttrScopeFoo::class); |
152 | 152 | }); |
153 | 153 | }, name: 'bar'); |
154 | - } catch (BadScopeException $e) { |
|
154 | + }catch (BadScopeException $e){ |
|
155 | 155 | self::assertSame('foo', $e->getScope()); |
156 | 156 | throw $e; |
157 | 157 | } |
@@ -33,8 +33,10 @@ discard block |
||
33 | 33 | { |
34 | 34 | $root = new Container(); |
35 | 35 | |
36 | - $root->runScoped(static function (Container $c1) { |
|
37 | - $c1->runScoped(static function (Container $c2) use ($c1) { |
|
36 | + $root->runScoped(static function (Container $c1) |
|
37 | + { |
|
38 | + $c1->runScoped(static function (Container $c2) use ($c1) |
|
39 | + { |
|
38 | 40 | $obj1 = $c1->get(AttrScopeFooSingleton::class); |
39 | 41 | $obj2 = $c2->get(AttrScopeFooSingleton::class); |
40 | 42 | |
@@ -48,8 +50,10 @@ discard block |
||
48 | 50 | $root = new Container(); |
49 | 51 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
50 | 52 | |
51 | - $root->runScoped(static function (Container $fooScope) { |
|
52 | - $fooScope->runScoped(static function (Container $container) { |
|
53 | + $root->runScoped(static function (Container $fooScope) |
|
54 | + { |
|
55 | + $fooScope->runScoped(static function (Container $container) |
|
56 | + { |
|
53 | 57 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
54 | 58 | }, name: 'bar'); |
55 | 59 | }, name: 'foo'); |
@@ -63,8 +67,10 @@ discard block |
||
63 | 67 | $root = new Container(); |
64 | 68 | $root->getBinder('bar')->bindSingleton('binding', static fn () => new AttrScopeFoo()); |
65 | 69 | |
66 | - $root->runScoped(static function (Container $fooScope) { |
|
67 | - $fooScope->runScoped(static function (Container $container) { |
|
70 | + $root->runScoped(static function (Container $fooScope) |
|
71 | + { |
|
72 | + $fooScope->runScoped(static function (Container $container) |
|
73 | + { |
|
68 | 74 | self::assertInstanceOf(AttrScopeFoo::class, $container->get('binding')); |
69 | 75 | }, name: 'bar'); |
70 | 76 | }, name: 'baz'); |
@@ -83,8 +89,10 @@ discard block |
||
83 | 89 | $root = new Container(); |
84 | 90 | $root->bind('foo', self::makeFooScopeObject(...)); |
85 | 91 | |
86 | - $root->runScoped(static function (Container $c1) { |
|
87 | - $c1->runScoped(static function (Container $c2) { |
|
92 | + $root->runScoped(static function (Container $c1) |
|
93 | + { |
|
94 | + $c1->runScoped(static function (Container $c2) |
|
95 | + { |
|
88 | 96 | $c2->get('foo'); |
89 | 97 | }, name: 'foo'); |
90 | 98 | }); |
@@ -102,8 +110,10 @@ discard block |
||
102 | 110 | $root = new Container(); |
103 | 111 | $root->bind('foo', self::makeFooScopeObject(...)); |
104 | 112 | |
105 | - $root->runScoped(static function (Container $c1) { |
|
106 | - $c1->runScoped(static function (Container $c2) { |
|
113 | + $root->runScoped(static function (Container $c1) |
|
114 | + { |
|
115 | + $c1->runScoped(static function (Container $c2) |
|
116 | + { |
|
107 | 117 | $c2->get('foo'); |
108 | 118 | }); |
109 | 119 | }); |
@@ -121,15 +131,21 @@ discard block |
||
121 | 131 | |
122 | 132 | $root = new Container(); |
123 | 133 | |
124 | - try { |
|
125 | - $root->runScoped(static function (Container $c1) { |
|
126 | - $c1->runScoped(static function (Container $c2) { |
|
127 | - $c2->runScoped(static function (Container $c3) { |
|
134 | + try |
|
135 | + { |
|
136 | + $root->runScoped(static function (Container $c1) |
|
137 | + { |
|
138 | + $c1->runScoped(static function (Container $c2) |
|
139 | + { |
|
140 | + $c2->runScoped(static function (Container $c3) |
|
141 | + { |
|
128 | 142 | // do nothing |
129 | 143 | }, name: 'root'); |
130 | 144 | }); |
131 | 145 | }); |
132 | - } catch (NamedScopeDuplicationException $e) { |
|
146 | + } |
|
147 | + catch (NamedScopeDuplicationException $e) |
|
148 | + { |
|
133 | 149 | self::assertSame('root', $e->getScope()); |
134 | 150 | throw $e; |
135 | 151 | } |
@@ -144,14 +160,19 @@ discard block |
||
144 | 160 | { |
145 | 161 | self::expectException(BadScopeException::class); |
146 | 162 | |
147 | - try { |
|
163 | + try |
|
164 | + { |
|
148 | 165 | $root = new Container(); |
149 | - $root->runScoped(static function (Container $c1) { |
|
150 | - $c1->runScoped(static function (Container $c2) { |
|
166 | + $root->runScoped(static function (Container $c1) |
|
167 | + { |
|
168 | + $c1->runScoped(static function (Container $c2) |
|
169 | + { |
|
151 | 170 | $c2->get(AttrScopeFoo::class); |
152 | 171 | }); |
153 | 172 | }, name: 'bar'); |
154 | - } catch (BadScopeException $e) { |
|
173 | + } |
|
174 | + catch (BadScopeException $e) |
|
175 | + { |
|
155 | 176 | self::assertSame('foo', $e->getScope()); |
156 | 177 | throw $e; |
157 | 178 | } |
@@ -63,19 +63,19 @@ discard block |
||
63 | 63 | * |
64 | 64 | * @throws \Throwable |
65 | 65 | */ |
66 | - public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
|
66 | + public function make(string $alias, array $parameters = [], Stringable | string | null $context = null): mixed |
|
67 | 67 | { |
68 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
68 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)){ |
|
69 | 69 | return $this->state->singletons[$alias]; |
70 | 70 | } |
71 | 71 | |
72 | 72 | $binding = $this->state->bindings[$alias] ?? null; |
73 | 73 | |
74 | - if ($binding === null) { |
|
74 | + if ($binding === null){ |
|
75 | 75 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
76 | 76 | } |
77 | 77 | |
78 | - try { |
|
78 | + try{ |
|
79 | 79 | $this->tracer->push( |
80 | 80 | false, |
81 | 81 | action: 'resolve from binding', |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
106 | 106 | default => $binding, |
107 | 107 | }; |
108 | - } finally { |
|
108 | + }finally{ |
|
109 | 109 | $this->state->bindings[$alias] ??= $binding; |
110 | 110 | $this->tracer->pop(true); |
111 | 111 | $this->tracer->pop(false); |
@@ -119,18 +119,18 @@ discard block |
||
119 | 119 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
120 | 120 | { |
121 | 121 | $context = $ctx->context; |
122 | - try { |
|
122 | + try{ |
|
123 | 123 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
124 | - } catch (\ReflectionException $e) { |
|
124 | + }catch (\ReflectionException $e){ |
|
125 | 125 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
126 | 126 | } |
127 | 127 | |
128 | 128 | $injector = $binding->injector; |
129 | 129 | |
130 | - try { |
|
130 | + try{ |
|
131 | 131 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
132 | 132 | |
133 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
133 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
134 | 134 | throw new InjectionException( |
135 | 135 | \sprintf( |
136 | 136 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -157,7 +157,7 @@ discard block |
||
157 | 157 | default => (string)$context, |
158 | 158 | }); |
159 | 159 | |
160 | - if (!$reflection->isInstance($instance)) { |
|
160 | + if (!$reflection->isInstance($instance)){ |
|
161 | 161 | throw new InjectionException( |
162 | 162 | \sprintf( |
163 | 163 | "Invalid injection response for '%s'.", |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | } |
168 | 168 | |
169 | 169 | return $instance; |
170 | - } finally { |
|
170 | + }finally{ |
|
171 | 171 | $this->state->bindings[$ctx->class] ??= $binding; |
172 | 172 | } |
173 | 173 | } |
@@ -175,29 +175,29 @@ discard block |
||
175 | 175 | private function resolveAlias( |
176 | 176 | Config\Alias $binding, |
177 | 177 | string $alias, |
178 | - Stringable|string|null $context, |
|
178 | + Stringable | string | null $context, |
|
179 | 179 | array $arguments, |
180 | 180 | ): mixed { |
181 | 181 | $result = $binding->alias === $alias |
182 | 182 | ? $this->autowire( |
183 | - new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton), |
|
183 | + new Ctx(alias : $alias, class : $binding->alias, context : $context, singleton : $binding->singleton), |
|
184 | 184 | $arguments, |
185 | 185 | ) |
186 | 186 | //Binding is pointing to something else |
187 | 187 | : $this->make($binding->alias, $arguments, $context); |
188 | 188 | |
189 | - if ($binding->singleton && $arguments === []) { |
|
189 | + if ($binding->singleton && $arguments === []){ |
|
190 | 190 | $this->state->singletons[$alias] = $result; |
191 | 191 | } |
192 | 192 | |
193 | 193 | return $result; |
194 | 194 | } |
195 | 195 | |
196 | - private function resolveProxy(Config\Proxy $binding, string $alias, Stringable|string|null $context): mixed |
|
196 | + private function resolveProxy(Config\Proxy $binding, string $alias, Stringable | string | null $context): mixed |
|
197 | 197 | { |
198 | 198 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
199 | 199 | |
200 | - if ($binding->singleton) { |
|
200 | + if ($binding->singleton){ |
|
201 | 201 | $this->state->singletons[$alias] = $result; |
202 | 202 | } |
203 | 203 | |
@@ -207,13 +207,13 @@ discard block |
||
207 | 207 | private function resolveShared( |
208 | 208 | Config\Shared $binding, |
209 | 209 | string $alias, |
210 | - Stringable|string|null $context, |
|
210 | + Stringable | string | null $context, |
|
211 | 211 | array $arguments, |
212 | 212 | ): object { |
213 | 213 | $avoidCache = $arguments !== []; |
214 | 214 | return $avoidCache |
215 | 215 | ? $this->createInstance( |
216 | - new Ctx(alias: $alias, class: $binding->value::class, context: $context), |
|
216 | + new Ctx(alias : $alias, class : $binding->value::class, context : $context), |
|
217 | 217 | $arguments, |
218 | 218 | ) |
219 | 219 | : $binding->value; |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | private function resolveAutowire( |
223 | 223 | Config\Autowire $binding, |
224 | 224 | string $alias, |
225 | - Stringable|string|null $context, |
|
225 | + Stringable | string | null $context, |
|
226 | 226 | array $arguments, |
227 | 227 | ): mixed { |
228 | 228 | $instance = $binding->autowire->resolve($this, $arguments); |
@@ -232,17 +232,17 @@ discard block |
||
232 | 232 | } |
233 | 233 | |
234 | 234 | private function resolveFactory( |
235 | - Config\Factory|Config\DeferredFactory $binding, |
|
235 | + Config\Factory | Config\DeferredFactory $binding, |
|
236 | 236 | string $alias, |
237 | - Stringable|string|null $context, |
|
237 | + Stringable | string | null $context, |
|
238 | 238 | array $arguments, |
239 | 239 | ): mixed { |
240 | 240 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
241 | - try { |
|
241 | + try{ |
|
242 | 242 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
243 | 243 | ? ($binding->factory)() |
244 | 244 | : $this->invoker->invoke($binding->factory, $arguments); |
245 | - } catch (NotCallableException $e) { |
|
245 | + }catch (NotCallableException $e){ |
|
246 | 246 | throw new ContainerException( |
247 | 247 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
248 | 248 | $e->getCode(), |
@@ -256,24 +256,24 @@ discard block |
||
256 | 256 | private function resolveWeakReference( |
257 | 257 | Config\WeakReference $binding, |
258 | 258 | string $alias, |
259 | - Stringable|string|null $context, |
|
259 | + Stringable | string | null $context, |
|
260 | 260 | array $arguments, |
261 | 261 | ): ?object { |
262 | 262 | $avoidCache = $arguments !== []; |
263 | 263 | |
264 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
265 | - try { |
|
264 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
265 | + try{ |
|
266 | 266 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
267 | 267 | |
268 | 268 | $object = $this->createInstance( |
269 | 269 | new Ctx(alias: $alias, class: $alias, context: $context), |
270 | 270 | $arguments, |
271 | 271 | ); |
272 | - if ($avoidCache) { |
|
272 | + if ($avoidCache){ |
|
273 | 273 | return $object; |
274 | 274 | } |
275 | 275 | $binding->reference = WeakReference::create($object); |
276 | - } catch (\Throwable) { |
|
276 | + }catch (\Throwable){ |
|
277 | 277 | throw new ContainerException( |
278 | 278 | $this->tracer->combineTraceMessage( |
279 | 279 | \sprintf( |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | ) |
284 | 284 | ) |
285 | 285 | ); |
286 | - } finally { |
|
286 | + }finally{ |
|
287 | 287 | $this->tracer->pop(); |
288 | 288 | } |
289 | 289 | } |
@@ -294,23 +294,23 @@ discard block |
||
294 | 294 | private function resolveWithoutBinding( |
295 | 295 | string $alias, |
296 | 296 | array $parameters = [], |
297 | - Stringable|string|null $context = null |
|
297 | + Stringable | string | null $context = null |
|
298 | 298 | ): mixed { |
299 | 299 | $parent = $this->scope->getParentFactory(); |
300 | 300 | |
301 | - if ($parent !== null) { |
|
302 | - try { |
|
301 | + if ($parent !== null){ |
|
302 | + try{ |
|
303 | 303 | $this->tracer->push(false, ...[ |
304 | 304 | 'current scope' => $this->scope->getScopeName(), |
305 | 305 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
306 | 306 | ]); |
307 | 307 | /** @psalm-suppress TooManyArguments */ |
308 | 308 | return $parent->make($alias, $parameters, $context); |
309 | - } catch (BadScopeException $e) { |
|
310 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
309 | + }catch (BadScopeException $e){ |
|
310 | + if ($this->scope->getScopeName() !== $e->getScope()){ |
|
311 | 311 | throw $e; |
312 | 312 | } |
313 | - } catch (ContainerExceptionInterface $e) { |
|
313 | + }catch (ContainerExceptionInterface $e){ |
|
314 | 314 | $className = match (true) { |
315 | 315 | $e instanceof NotFoundException => NotFoundException::class, |
316 | 316 | default => ContainerException::class, |
@@ -319,19 +319,19 @@ discard block |
||
319 | 319 | 'Can\'t resolve `%s`.', |
320 | 320 | $alias, |
321 | 321 | )), previous: $e); |
322 | - } finally { |
|
322 | + }finally{ |
|
323 | 323 | $this->tracer->pop(false); |
324 | 324 | } |
325 | 325 | } |
326 | 326 | |
327 | 327 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
328 | - try { |
|
328 | + try{ |
|
329 | 329 | //No direct instructions how to construct class, make is automatically |
330 | 330 | return $this->autowire( |
331 | 331 | new Ctx(alias: $alias, class: $alias, context: $context), |
332 | 332 | $parameters, |
333 | 333 | ); |
334 | - } finally { |
|
334 | + }finally{ |
|
335 | 335 | $this->tracer->pop(false); |
336 | 336 | } |
337 | 337 | } |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | && |
353 | 353 | (isset($this->state->injectors[$ctx->class]) || $this->binder->hasInjector($ctx->class)) |
354 | 354 | )) |
355 | - ) { |
|
355 | + ){ |
|
356 | 356 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
357 | 357 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
358 | 358 | $this->tracer->getRootAlias(), |
@@ -381,9 +381,9 @@ discard block |
||
381 | 381 | // Check scope name |
382 | 382 | $ctx->reflection = new \ReflectionClass($instance); |
383 | 383 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
384 | - if ($scopeName !== null) { |
|
384 | + if ($scopeName !== null){ |
|
385 | 385 | $scope = $this->scope; |
386 | - while ($scope->getScopeName() !== $scopeName) { |
|
386 | + while ($scope->getScopeName() !== $scopeName){ |
|
387 | 387 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
388 | 388 | } |
389 | 389 | } |
@@ -411,24 +411,24 @@ discard block |
||
411 | 411 | array $arguments, |
412 | 412 | ): object { |
413 | 413 | $class = $ctx->class; |
414 | - try { |
|
414 | + try{ |
|
415 | 415 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
416 | - } catch (\ReflectionException $e) { |
|
416 | + }catch (\ReflectionException $e){ |
|
417 | 417 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
418 | 418 | } |
419 | 419 | |
420 | 420 | // Check scope name |
421 | 421 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
422 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
422 | + if ($scope !== null && $scope !== $this->scope->getScopeName()){ |
|
423 | 423 | throw new BadScopeException($scope, $class); |
424 | 424 | } |
425 | 425 | |
426 | 426 | // We have to construct class using external injector when we know the exact context |
427 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
427 | + if ($arguments === [] && $this->binder->hasInjector($class)){ |
|
428 | 428 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
429 | 429 | } |
430 | 430 | |
431 | - if (!$reflection->isInstantiable()) { |
|
431 | + if (!$reflection->isInstantiable()){ |
|
432 | 432 | $itIs = match (true) { |
433 | 433 | $reflection->isEnum() => 'Enum', |
434 | 434 | $reflection->isAbstract() => 'Abstract class', |
@@ -441,12 +441,12 @@ discard block |
||
441 | 441 | |
442 | 442 | $constructor = $reflection->getConstructor(); |
443 | 443 | |
444 | - if ($constructor !== null) { |
|
445 | - try { |
|
444 | + if ($constructor !== null){ |
|
445 | + try{ |
|
446 | 446 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
447 | 447 | $this->tracer->push(true); |
448 | 448 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
449 | - } catch (ValidationException $e) { |
|
449 | + }catch (ValidationException $e){ |
|
450 | 450 | throw new ContainerException( |
451 | 451 | $this->tracer->combineTraceMessage( |
452 | 452 | \sprintf( |
@@ -456,22 +456,22 @@ discard block |
||
456 | 456 | ) |
457 | 457 | ), |
458 | 458 | ); |
459 | - } finally { |
|
459 | + }finally{ |
|
460 | 460 | $this->tracer->pop(true); |
461 | 461 | $this->tracer->pop(false); |
462 | 462 | } |
463 | - try { |
|
463 | + try{ |
|
464 | 464 | // Using constructor with resolved arguments |
465 | 465 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
466 | 466 | $this->tracer->push(true); |
467 | 467 | $instance = new $class(...$args); |
468 | - } catch (\TypeError $e) { |
|
468 | + }catch (\TypeError $e){ |
|
469 | 469 | throw new WrongTypeException($constructor, $e); |
470 | - } finally { |
|
470 | + }finally{ |
|
471 | 471 | $this->tracer->pop(true); |
472 | 472 | $this->tracer->pop(false); |
473 | 473 | } |
474 | - } else { |
|
474 | + }else{ |
|
475 | 475 | // No constructor specified |
476 | 476 | $instance = $reflection->newInstance(); |
477 | 477 | } |
@@ -489,13 +489,13 @@ discard block |
||
489 | 489 | $instance = $this->runInflector($instance); |
490 | 490 | |
491 | 491 | //Declarative singletons |
492 | - if ($this->isSingleton($ctx)) { |
|
492 | + if ($this->isSingleton($ctx)){ |
|
493 | 493 | $this->state->singletons[$ctx->alias] = $instance; |
494 | 494 | } |
495 | 495 | |
496 | 496 | // Register finalizer |
497 | 497 | $finalizer = $this->getFinalizer($ctx, $instance); |
498 | - if ($finalizer !== null) { |
|
498 | + if ($finalizer !== null){ |
|
499 | 499 | $this->state->finalizers[] = $finalizer; |
500 | 500 | } |
501 | 501 | |
@@ -507,12 +507,12 @@ discard block |
||
507 | 507 | */ |
508 | 508 | private function isSingleton(Ctx $ctx): bool |
509 | 509 | { |
510 | - if ($ctx->singleton === true) { |
|
510 | + if ($ctx->singleton === true){ |
|
511 | 511 | return true; |
512 | 512 | } |
513 | 513 | |
514 | 514 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
515 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
515 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
516 | 516 | return true; |
517 | 517 | } |
518 | 518 | |
@@ -526,7 +526,7 @@ discard block |
||
526 | 526 | * @var Attribute\Finalize|null $attribute |
527 | 527 | */ |
528 | 528 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
529 | - if ($attribute === null) { |
|
529 | + if ($attribute === null){ |
|
530 | 530 | return null; |
531 | 531 | } |
532 | 532 | |
@@ -540,10 +540,10 @@ discard block |
||
540 | 540 | { |
541 | 541 | $scope = $this->scope; |
542 | 542 | |
543 | - while ($scope !== null) { |
|
544 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
545 | - if ($instance instanceof $class) { |
|
546 | - foreach ($inflectors as $inflector) { |
|
543 | + while ($scope !== null){ |
|
544 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
545 | + if ($instance instanceof $class){ |
|
546 | + foreach ($inflectors as $inflector){ |
|
547 | 547 | $instance = $inflector->getParametersCount() > 1 |
548 | 548 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
549 | 549 | : ($inflector->inflector)($instance); |
@@ -559,9 +559,9 @@ discard block |
||
559 | 559 | |
560 | 560 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
561 | 561 | { |
562 | - try { |
|
562 | + try{ |
|
563 | 563 | $this->resolver->validateArguments($reflection, $arguments); |
564 | - } catch (\Throwable) { |
|
564 | + }catch (\Throwable){ |
|
565 | 565 | return false; |
566 | 566 | } |
567 | 567 |
@@ -65,17 +65,20 @@ discard block |
||
65 | 65 | */ |
66 | 66 | public function make(string $alias, array $parameters = [], Stringable|string|null $context = null): mixed |
67 | 67 | { |
68 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
68 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
69 | + { |
|
69 | 70 | return $this->state->singletons[$alias]; |
70 | 71 | } |
71 | 72 | |
72 | 73 | $binding = $this->state->bindings[$alias] ?? null; |
73 | 74 | |
74 | - if ($binding === null) { |
|
75 | + if ($binding === null) |
|
76 | + { |
|
75 | 77 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
76 | 78 | } |
77 | 79 | |
78 | - try { |
|
80 | + try |
|
81 | + { |
|
79 | 82 | $this->tracer->push( |
80 | 83 | false, |
81 | 84 | action: 'resolve from binding', |
@@ -105,7 +108,9 @@ discard block |
||
105 | 108 | ->resolveWeakReference($binding, $alias, $context, $parameters), |
106 | 109 | default => $binding, |
107 | 110 | }; |
108 | - } finally { |
|
111 | + } |
|
112 | + finally |
|
113 | + { |
|
109 | 114 | $this->state->bindings[$alias] ??= $binding; |
110 | 115 | $this->tracer->pop(true); |
111 | 116 | $this->tracer->pop(false); |
@@ -119,18 +124,23 @@ discard block |
||
119 | 124 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments) |
120 | 125 | { |
121 | 126 | $context = $ctx->context; |
122 | - try { |
|
127 | + try |
|
128 | + { |
|
123 | 129 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
124 | - } catch (\ReflectionException $e) { |
|
130 | + } |
|
131 | + catch (\ReflectionException $e) |
|
132 | + { |
|
125 | 133 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
126 | 134 | } |
127 | 135 | |
128 | 136 | $injector = $binding->injector; |
129 | 137 | |
130 | - try { |
|
138 | + try |
|
139 | + { |
|
131 | 140 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
132 | 141 | |
133 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
142 | + if (!$injectorInstance instanceof InjectorInterface) |
|
143 | + { |
|
134 | 144 | throw new InjectionException( |
135 | 145 | \sprintf( |
136 | 146 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -157,7 +167,8 @@ discard block |
||
157 | 167 | default => (string)$context, |
158 | 168 | }); |
159 | 169 | |
160 | - if (!$reflection->isInstance($instance)) { |
|
170 | + if (!$reflection->isInstance($instance)) |
|
171 | + { |
|
161 | 172 | throw new InjectionException( |
162 | 173 | \sprintf( |
163 | 174 | "Invalid injection response for '%s'.", |
@@ -167,7 +178,9 @@ discard block |
||
167 | 178 | } |
168 | 179 | |
169 | 180 | return $instance; |
170 | - } finally { |
|
181 | + } |
|
182 | + finally |
|
183 | + { |
|
171 | 184 | $this->state->bindings[$ctx->class] ??= $binding; |
172 | 185 | } |
173 | 186 | } |
@@ -186,7 +199,8 @@ discard block |
||
186 | 199 | //Binding is pointing to something else |
187 | 200 | : $this->make($binding->alias, $arguments, $context); |
188 | 201 | |
189 | - if ($binding->singleton && $arguments === []) { |
|
202 | + if ($binding->singleton && $arguments === []) |
|
203 | + { |
|
190 | 204 | $this->state->singletons[$alias] = $result; |
191 | 205 | } |
192 | 206 | |
@@ -197,7 +211,8 @@ discard block |
||
197 | 211 | { |
198 | 212 | $result = Proxy::create(new \ReflectionClass($binding->getInterface()), $context, new Attribute\Proxy()); |
199 | 213 | |
200 | - if ($binding->singleton) { |
|
214 | + if ($binding->singleton) |
|
215 | + { |
|
201 | 216 | $this->state->singletons[$alias] = $result; |
202 | 217 | } |
203 | 218 | |
@@ -238,11 +253,14 @@ discard block |
||
238 | 253 | array $arguments, |
239 | 254 | ): mixed { |
240 | 255 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton); |
241 | - try { |
|
256 | + try |
|
257 | + { |
|
242 | 258 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
243 | 259 | ? ($binding->factory)() |
244 | 260 | : $this->invoker->invoke($binding->factory, $arguments); |
245 | - } catch (NotCallableException $e) { |
|
261 | + } |
|
262 | + catch (NotCallableException $e) |
|
263 | + { |
|
246 | 264 | throw new ContainerException( |
247 | 265 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
248 | 266 | $e->getCode(), |
@@ -261,19 +279,24 @@ discard block |
||
261 | 279 | ): ?object { |
262 | 280 | $avoidCache = $arguments !== []; |
263 | 281 | |
264 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
265 | - try { |
|
282 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
283 | + { |
|
284 | + try |
|
285 | + { |
|
266 | 286 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
267 | 287 | |
268 | 288 | $object = $this->createInstance( |
269 | 289 | new Ctx(alias: $alias, class: $alias, context: $context), |
270 | 290 | $arguments, |
271 | 291 | ); |
272 | - if ($avoidCache) { |
|
292 | + if ($avoidCache) |
|
293 | + { |
|
273 | 294 | return $object; |
274 | 295 | } |
275 | 296 | $binding->reference = WeakReference::create($object); |
276 | - } catch (\Throwable) { |
|
297 | + } |
|
298 | + catch (\Throwable) |
|
299 | + { |
|
277 | 300 | throw new ContainerException( |
278 | 301 | $this->tracer->combineTraceMessage( |
279 | 302 | \sprintf( |
@@ -283,7 +306,9 @@ discard block |
||
283 | 306 | ) |
284 | 307 | ) |
285 | 308 | ); |
286 | - } finally { |
|
309 | + } |
|
310 | + finally |
|
311 | + { |
|
287 | 312 | $this->tracer->pop(); |
288 | 313 | } |
289 | 314 | } |
@@ -298,19 +323,26 @@ discard block |
||
298 | 323 | ): mixed { |
299 | 324 | $parent = $this->scope->getParentFactory(); |
300 | 325 | |
301 | - if ($parent !== null) { |
|
302 | - try { |
|
326 | + if ($parent !== null) |
|
327 | + { |
|
328 | + try |
|
329 | + { |
|
303 | 330 | $this->tracer->push(false, ...[ |
304 | 331 | 'current scope' => $this->scope->getScopeName(), |
305 | 332 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
306 | 333 | ]); |
307 | 334 | /** @psalm-suppress TooManyArguments */ |
308 | 335 | return $parent->make($alias, $parameters, $context); |
309 | - } catch (BadScopeException $e) { |
|
310 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
336 | + } |
|
337 | + catch (BadScopeException $e) |
|
338 | + { |
|
339 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
340 | + { |
|
311 | 341 | throw $e; |
312 | 342 | } |
313 | - } catch (ContainerExceptionInterface $e) { |
|
343 | + } |
|
344 | + catch (ContainerExceptionInterface $e) |
|
345 | + { |
|
314 | 346 | $className = match (true) { |
315 | 347 | $e instanceof NotFoundException => NotFoundException::class, |
316 | 348 | default => ContainerException::class, |
@@ -319,19 +351,24 @@ discard block |
||
319 | 351 | 'Can\'t resolve `%s`.', |
320 | 352 | $alias, |
321 | 353 | )), previous: $e); |
322 | - } finally { |
|
354 | + } |
|
355 | + finally |
|
356 | + { |
|
323 | 357 | $this->tracer->pop(false); |
324 | 358 | } |
325 | 359 | } |
326 | 360 | |
327 | 361 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
328 | - try { |
|
362 | + try |
|
363 | + { |
|
329 | 364 | //No direct instructions how to construct class, make is automatically |
330 | 365 | return $this->autowire( |
331 | 366 | new Ctx(alias: $alias, class: $alias, context: $context), |
332 | 367 | $parameters, |
333 | 368 | ); |
334 | - } finally { |
|
369 | + } |
|
370 | + finally |
|
371 | + { |
|
335 | 372 | $this->tracer->pop(false); |
336 | 373 | } |
337 | 374 | } |
@@ -381,9 +418,11 @@ discard block |
||
381 | 418 | // Check scope name |
382 | 419 | $ctx->reflection = new \ReflectionClass($instance); |
383 | 420 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
384 | - if ($scopeName !== null) { |
|
421 | + if ($scopeName !== null) |
|
422 | + { |
|
385 | 423 | $scope = $this->scope; |
386 | - while ($scope->getScopeName() !== $scopeName) { |
|
424 | + while ($scope->getScopeName() !== $scopeName) |
|
425 | + { |
|
387 | 426 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
388 | 427 | } |
389 | 428 | } |
@@ -411,24 +450,30 @@ discard block |
||
411 | 450 | array $arguments, |
412 | 451 | ): object { |
413 | 452 | $class = $ctx->class; |
414 | - try { |
|
453 | + try |
|
454 | + { |
|
415 | 455 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
416 | - } catch (\ReflectionException $e) { |
|
456 | + } |
|
457 | + catch (\ReflectionException $e) |
|
458 | + { |
|
417 | 459 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
418 | 460 | } |
419 | 461 | |
420 | 462 | // Check scope name |
421 | 463 | $scope = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
422 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
464 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
465 | + { |
|
423 | 466 | throw new BadScopeException($scope, $class); |
424 | 467 | } |
425 | 468 | |
426 | 469 | // We have to construct class using external injector when we know the exact context |
427 | - if ($arguments === [] && $this->binder->hasInjector($class)) { |
|
470 | + if ($arguments === [] && $this->binder->hasInjector($class)) |
|
471 | + { |
|
428 | 472 | return $this->resolveInjector($this->state->bindings[$ctx->class], $ctx, $arguments); |
429 | 473 | } |
430 | 474 | |
431 | - if (!$reflection->isInstantiable()) { |
|
475 | + if (!$reflection->isInstantiable()) |
|
476 | + { |
|
432 | 477 | $itIs = match (true) { |
433 | 478 | $reflection->isEnum() => 'Enum', |
434 | 479 | $reflection->isAbstract() => 'Abstract class', |
@@ -441,12 +486,16 @@ discard block |
||
441 | 486 | |
442 | 487 | $constructor = $reflection->getConstructor(); |
443 | 488 | |
444 | - if ($constructor !== null) { |
|
445 | - try { |
|
489 | + if ($constructor !== null) |
|
490 | + { |
|
491 | + try |
|
492 | + { |
|
446 | 493 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
447 | 494 | $this->tracer->push(true); |
448 | 495 | $args = $this->resolver->resolveArguments($constructor, $arguments); |
449 | - } catch (ValidationException $e) { |
|
496 | + } |
|
497 | + catch (ValidationException $e) |
|
498 | + { |
|
450 | 499 | throw new ContainerException( |
451 | 500 | $this->tracer->combineTraceMessage( |
452 | 501 | \sprintf( |
@@ -456,22 +505,31 @@ discard block |
||
456 | 505 | ) |
457 | 506 | ), |
458 | 507 | ); |
459 | - } finally { |
|
508 | + } |
|
509 | + finally |
|
510 | + { |
|
460 | 511 | $this->tracer->pop(true); |
461 | 512 | $this->tracer->pop(false); |
462 | 513 | } |
463 | - try { |
|
514 | + try |
|
515 | + { |
|
464 | 516 | // Using constructor with resolved arguments |
465 | 517 | $this->tracer->push(false, call: "$class::__construct", arguments: $args); |
466 | 518 | $this->tracer->push(true); |
467 | 519 | $instance = new $class(...$args); |
468 | - } catch (\TypeError $e) { |
|
520 | + } |
|
521 | + catch (\TypeError $e) |
|
522 | + { |
|
469 | 523 | throw new WrongTypeException($constructor, $e); |
470 | - } finally { |
|
524 | + } |
|
525 | + finally |
|
526 | + { |
|
471 | 527 | $this->tracer->pop(true); |
472 | 528 | $this->tracer->pop(false); |
473 | 529 | } |
474 | - } else { |
|
530 | + } |
|
531 | + else |
|
532 | + { |
|
475 | 533 | // No constructor specified |
476 | 534 | $instance = $reflection->newInstance(); |
477 | 535 | } |
@@ -489,13 +547,15 @@ discard block |
||
489 | 547 | $instance = $this->runInflector($instance); |
490 | 548 | |
491 | 549 | //Declarative singletons |
492 | - if ($this->isSingleton($ctx)) { |
|
550 | + if ($this->isSingleton($ctx)) |
|
551 | + { |
|
493 | 552 | $this->state->singletons[$ctx->alias] = $instance; |
494 | 553 | } |
495 | 554 | |
496 | 555 | // Register finalizer |
497 | 556 | $finalizer = $this->getFinalizer($ctx, $instance); |
498 | - if ($finalizer !== null) { |
|
557 | + if ($finalizer !== null) |
|
558 | + { |
|
499 | 559 | $this->state->finalizers[] = $finalizer; |
500 | 560 | } |
501 | 561 | |
@@ -507,12 +567,14 @@ discard block |
||
507 | 567 | */ |
508 | 568 | private function isSingleton(Ctx $ctx): bool |
509 | 569 | { |
510 | - if ($ctx->singleton === true) { |
|
570 | + if ($ctx->singleton === true) |
|
571 | + { |
|
511 | 572 | return true; |
512 | 573 | } |
513 | 574 | |
514 | 575 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
515 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
576 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
577 | + { |
|
516 | 578 | return true; |
517 | 579 | } |
518 | 580 | |
@@ -526,7 +588,8 @@ discard block |
||
526 | 588 | * @var Attribute\Finalize|null $attribute |
527 | 589 | */ |
528 | 590 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
529 | - if ($attribute === null) { |
|
591 | + if ($attribute === null) |
|
592 | + { |
|
530 | 593 | return null; |
531 | 594 | } |
532 | 595 | |
@@ -540,10 +603,14 @@ discard block |
||
540 | 603 | { |
541 | 604 | $scope = $this->scope; |
542 | 605 | |
543 | - while ($scope !== null) { |
|
544 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
545 | - if ($instance instanceof $class) { |
|
546 | - foreach ($inflectors as $inflector) { |
|
606 | + while ($scope !== null) |
|
607 | + { |
|
608 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
609 | + { |
|
610 | + if ($instance instanceof $class) |
|
611 | + { |
|
612 | + foreach ($inflectors as $inflector) |
|
613 | + { |
|
547 | 614 | $instance = $inflector->getParametersCount() > 1 |
548 | 615 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
549 | 616 | : ($inflector->inflector)($instance); |
@@ -559,9 +626,12 @@ discard block |
||
559 | 626 | |
560 | 627 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
561 | 628 | { |
562 | - try { |
|
629 | + try |
|
630 | + { |
|
563 | 631 | $this->resolver->validateArguments($reflection, $arguments); |
564 | - } catch (\Throwable) { |
|
632 | + } |
|
633 | + catch (\Throwable) |
|
634 | + { |
|
565 | 635 | return false; |
566 | 636 | } |
567 | 637 |