@@ -53,7 +53,8 @@ discard block |
||
53 | 53 | public function testScope(): void |
54 | 54 | { |
55 | 55 | $core = $this->httpCore([CookiesMiddleware::class]); |
56 | - $core->setHandler(function ($r) { |
|
56 | + $core->setHandler(function ($r) |
|
57 | + { |
|
57 | 58 | $this->assertInstanceOf( |
58 | 59 | CookieQueue::class, |
59 | 60 | ContainerScope::getContainer()->get(ServerRequestInterface::class) |
@@ -77,7 +78,8 @@ discard block |
||
77 | 78 | public function testSetEncryptedCookie(): void |
78 | 79 | { |
79 | 80 | $core = $this->httpCore([CookiesMiddleware::class]); |
80 | - $core->setHandler(function ($r) { |
|
81 | + $core->setHandler(function ($r) |
|
82 | + { |
|
81 | 83 | ContainerScope::getContainer()->get(ServerRequestInterface::class) |
82 | 84 | ->getAttribute(CookieQueue::ATTRIBUTE)->set('name', 'value'); |
83 | 85 | |
@@ -99,7 +101,8 @@ discard block |
||
99 | 101 | public function testSetNotProtectedCookie(): void |
100 | 102 | { |
101 | 103 | $core = $this->httpCore([CookiesMiddleware::class]); |
102 | - $core->setHandler(function ($r) { |
|
104 | + $core->setHandler(function ($r) |
|
105 | + { |
|
103 | 106 | ContainerScope::getContainer()->get(ServerRequestInterface::class) |
104 | 107 | ->getAttribute(CookieQueue::ATTRIBUTE)->set('PHPSESSID', 'value'); |
105 | 108 | |
@@ -118,7 +121,8 @@ discard block |
||
118 | 121 | public function testDecrypt(): void |
119 | 122 | { |
120 | 123 | $core = $this->httpCore([CookiesMiddleware::class]); |
121 | - $core->setHandler(function ($r) { |
|
124 | + $core->setHandler(function ($r) |
|
125 | + { |
|
122 | 126 | |
123 | 127 | /** |
124 | 128 | * @var ServerRequest $r |
@@ -136,7 +140,8 @@ discard block |
||
136 | 140 | public function testDecryptArray(): void |
137 | 141 | { |
138 | 142 | $core = $this->httpCore([CookiesMiddleware::class]); |
139 | - $core->setHandler(function ($r) { |
|
143 | + $core->setHandler(function ($r) |
|
144 | + { |
|
140 | 145 | |
141 | 146 | /** |
142 | 147 | * @var ServerRequest $r |
@@ -154,7 +159,8 @@ discard block |
||
154 | 159 | public function testDecryptBroken(): void |
155 | 160 | { |
156 | 161 | $core = $this->httpCore([CookiesMiddleware::class]); |
157 | - $core->setHandler(function ($r) { |
|
162 | + $core->setHandler(function ($r) |
|
163 | + { |
|
158 | 164 | |
159 | 165 | /** |
160 | 166 | * @var ServerRequest $r |
@@ -172,7 +178,8 @@ discard block |
||
172 | 178 | public function testDelete(): void |
173 | 179 | { |
174 | 180 | $core = $this->httpCore([CookiesMiddleware::class]); |
175 | - $core->setHandler(function ($r) { |
|
181 | + $core->setHandler(function ($r) |
|
182 | + { |
|
176 | 183 | ContainerScope::getContainer()->get(ServerRequestInterface::class) |
177 | 184 | ->getAttribute(CookieQueue::ATTRIBUTE)->set('name', 'value'); |
178 | 185 | |
@@ -200,7 +207,8 @@ discard block |
||
200 | 207 | ])); |
201 | 208 | |
202 | 209 | $core = $this->httpCore([CookiesMiddleware::class]); |
203 | - $core->setHandler(function ($r) { |
|
210 | + $core->setHandler(function ($r) |
|
211 | + { |
|
204 | 212 | ContainerScope::getContainer()->get(ServerRequestInterface::class) |
205 | 213 | ->getAttribute(CookieQueue::ATTRIBUTE)->set('name', 'value'); |
206 | 214 | |
@@ -225,7 +233,8 @@ discard block |
||
225 | 233 | ])); |
226 | 234 | |
227 | 235 | $core = $this->httpCore([CookiesMiddleware::class]); |
228 | - $core->setHandler(function ($r) { |
|
236 | + $core->setHandler(function ($r) |
|
237 | + { |
|
229 | 238 | |
230 | 239 | /** |
231 | 240 | * @var ServerRequest $r |
@@ -249,7 +258,8 @@ discard block |
||
249 | 258 | ])); |
250 | 259 | |
251 | 260 | $core = $this->httpCore([CookiesMiddleware::class]); |
252 | - $core->setHandler(function ($r) { |
|
261 | + $core->setHandler(function ($r) |
|
262 | + { |
|
253 | 263 | ContainerScope::getContainer()->get(ServerRequestInterface::class) |
254 | 264 | ->getAttribute(CookieQueue::ATTRIBUTE)->set('name', 'value'); |
255 | 265 | |
@@ -263,7 +273,8 @@ discard block |
||
263 | 273 | $cookies = $this->fetchCookies($response); |
264 | 274 | $this->assertArrayHasKey('name', $cookies); |
265 | 275 | |
266 | - $core->setHandler(function ($r) { |
|
276 | + $core->setHandler(function ($r) |
|
277 | + { |
|
267 | 278 | return $r->getCookieParams()['name']; |
268 | 279 | }); |
269 | 280 | |
@@ -318,7 +329,8 @@ discard block |
||
318 | 329 | { |
319 | 330 | $result = []; |
320 | 331 | |
321 | - foreach ($response->getHeaders() as $line) { |
|
332 | + foreach ($response->getHeaders() as $line) |
|
333 | + { |
|
322 | 334 | $cookie = explode('=', implode('', $line)); |
323 | 335 | $result[$cookie[0]] = rawurldecode(substr( |
324 | 336 | (string)$cookie[1], |
@@ -45,7 +45,8 @@ discard block |
||
45 | 45 | { |
46 | 46 | $core = $this->getCore(); |
47 | 47 | |
48 | - $core->setHandler(function () { |
|
48 | + $core->setHandler(function () |
|
49 | + { |
|
49 | 50 | return 'hello world'; |
50 | 51 | }); |
51 | 52 | |
@@ -67,7 +68,8 @@ discard block |
||
67 | 68 | { |
68 | 69 | $core = $this->getCore(); |
69 | 70 | $core->setHandler( |
70 | - new CallableHandler(function () { |
|
71 | + new CallableHandler(function () |
|
72 | + { |
|
71 | 73 | return 'hello world'; |
72 | 74 | }, new ResponseFactory(new HttpConfig(['headers' => []]))) |
73 | 75 | ); |
@@ -80,7 +82,8 @@ discard block |
||
80 | 82 | { |
81 | 83 | $core = $this->getCore(); |
82 | 84 | |
83 | - $core->setHandler(function ($req, $resp) { |
|
85 | + $core->setHandler(function ($req, $resp) |
|
86 | + { |
|
84 | 87 | return $resp->withAddedHeader('hello', 'value'); |
85 | 88 | }); |
86 | 89 | |
@@ -93,7 +96,8 @@ discard block |
||
93 | 96 | { |
94 | 97 | $core = $this->getCore(); |
95 | 98 | |
96 | - $core->setHandler(function ($req, $resp) { |
|
99 | + $core->setHandler(function ($req, $resp) |
|
100 | + { |
|
97 | 101 | echo 'hello!'; |
98 | 102 | |
99 | 103 | return $resp->withAddedHeader('hello', 'value'); |
@@ -109,7 +113,8 @@ discard block |
||
109 | 113 | { |
110 | 114 | $core = $this->getCore(); |
111 | 115 | |
112 | - $core->setHandler(function ($req, $resp) { |
|
116 | + $core->setHandler(function ($req, $resp) |
|
117 | + { |
|
113 | 118 | echo 'hello!'; |
114 | 119 | $resp->getBody()->write('world '); |
115 | 120 | |
@@ -126,7 +131,8 @@ discard block |
||
126 | 131 | { |
127 | 132 | $core = $this->getCore(); |
128 | 133 | |
129 | - $core->setHandler(function () { |
|
134 | + $core->setHandler(function () |
|
135 | + { |
|
130 | 136 | ob_start(); |
131 | 137 | ob_start(); |
132 | 138 | echo 'hello!'; |
@@ -147,7 +153,8 @@ discard block |
||
147 | 153 | { |
148 | 154 | $core = $this->getCore(); |
149 | 155 | |
150 | - $core->setHandler(function () { |
|
156 | + $core->setHandler(function () |
|
157 | + { |
|
151 | 158 | return [ |
152 | 159 | 'status' => 404, |
153 | 160 | 'message' => 'not found', |
@@ -163,7 +170,8 @@ discard block |
||
163 | 170 | { |
164 | 171 | $core = $this->getCore(); |
165 | 172 | |
166 | - $core->setHandler(function () { |
|
173 | + $core->setHandler(function () |
|
174 | + { |
|
167 | 175 | return new Json([ |
168 | 176 | 'status' => 404, |
169 | 177 | 'message' => 'not found', |
@@ -179,7 +187,8 @@ discard block |
||
179 | 187 | { |
180 | 188 | $core = $this->getCore([HeaderMiddleware::class]); |
181 | 189 | |
182 | - $core->setHandler(function () { |
|
190 | + $core->setHandler(function () |
|
191 | + { |
|
183 | 192 | return 'hello?'; |
184 | 193 | }); |
185 | 194 | |
@@ -196,7 +205,8 @@ discard block |
||
196 | 205 | $core->getPipeline()->pushMiddleware(new Header2Middleware()); |
197 | 206 | $core->getPipeline()->riseMiddleware(new HeaderMiddleware()); |
198 | 207 | |
199 | - $core->setHandler(function () { |
|
208 | + $core->setHandler(function () |
|
209 | + { |
|
200 | 210 | return 'hello?'; |
201 | 211 | }); |
202 | 212 | |
@@ -213,7 +223,8 @@ discard block |
||
213 | 223 | $core->getPipeline()->pushMiddleware(new HeaderMiddleware()); |
214 | 224 | $core->getPipeline()->riseMiddleware(new Header2Middleware()); |
215 | 225 | |
216 | - $core->setHandler(function () { |
|
226 | + $core->setHandler(function () |
|
227 | + { |
|
217 | 228 | return 'hello?'; |
218 | 229 | }); |
219 | 230 | |
@@ -227,7 +238,8 @@ discard block |
||
227 | 238 | { |
228 | 239 | $core = $this->getCore(); |
229 | 240 | |
230 | - $core->setHandler(function () { |
|
241 | + $core->setHandler(function () |
|
242 | + { |
|
231 | 243 | $this->assertTrue(ContainerScope::getContainer()->has(ServerRequestInterface::class)); |
232 | 244 | |
233 | 245 | return 'OK'; |
@@ -265,7 +277,8 @@ discard block |
||
265 | 277 | |
266 | 278 | $core = $this->getCore(); |
267 | 279 | |
268 | - $core->setHandler(function () { |
|
280 | + $core->setHandler(function () |
|
281 | + { |
|
269 | 282 | return 'hello world'; |
270 | 283 | }); |
271 | 284 | |
@@ -286,7 +299,8 @@ discard block |
||
286 | 299 | $tracerFactory = m::mock(TracerFactoryInterface::class), |
287 | 300 | ); |
288 | 301 | |
289 | - $http->setHandler(function () { |
|
302 | + $http->setHandler(function () |
|
303 | + { |
|
290 | 304 | return 'hello world'; |
291 | 305 | }); |
292 | 306 |
@@ -28,7 +28,8 @@ |
||
28 | 28 | |
29 | 29 | public function hasInstance(string $alias): bool |
30 | 30 | { |
31 | - if (!$this->container->has($alias)) { |
|
31 | + if (!$this->container->has($alias)) |
|
32 | + { |
|
32 | 33 | return false; |
33 | 34 | } |
34 | 35 | return parent::hasInstance($alias); |
@@ -26,7 +26,8 @@ discard block |
||
26 | 26 | */ |
27 | 27 | public function bind(string $alias, string|array|callable|object $resolver): void |
28 | 28 | { |
29 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
29 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) |
|
30 | + { |
|
30 | 31 | // array means = execute me, false = not singleton |
31 | 32 | $this->state->bindings[$alias] = [$resolver, false]; |
32 | 33 | |
@@ -41,7 +42,8 @@ discard block |
||
41 | 42 | */ |
42 | 43 | public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
43 | 44 | { |
44 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
45 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) |
|
46 | + { |
|
45 | 47 | // direct binding to an instance |
46 | 48 | $this->state->bindings[$alias] = $resolver; |
47 | 49 | |
@@ -55,7 +57,8 @@ discard block |
||
55 | 57 | { |
56 | 58 | $bindings = &$this->state->bindings; |
57 | 59 | |
58 | - while (\is_string($bindings[$alias] ?? null)) { |
|
60 | + while (\is_string($bindings[$alias] ?? null)) |
|
61 | + { |
|
59 | 62 | //Checking alias tree |
60 | 63 | $alias = $bindings[$alias]; |
61 | 64 | } |
@@ -80,13 +83,17 @@ discard block |
||
80 | 83 | |
81 | 84 | public function hasInjector(string $class): bool |
82 | 85 | { |
83 | - try { |
|
86 | + try |
|
87 | + { |
|
84 | 88 | $reflection = new \ReflectionClass($class); |
85 | - } catch (\ReflectionException $e) { |
|
89 | + } |
|
90 | + catch (\ReflectionException $e) |
|
91 | + { |
|
86 | 92 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
87 | 93 | } |
88 | 94 | |
89 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
95 | + if (\array_key_exists($class, $this->state->injectors)) |
|
96 | + { |
|
90 | 97 | return $this->state->injectors[$class] !== null; |
91 | 98 | } |
92 | 99 | |
@@ -100,7 +107,8 @@ discard block |
||
100 | 107 | } |
101 | 108 | |
102 | 109 | // check interfaces |
103 | - foreach ($this->state->injectors as $target => $injector) { |
|
110 | + foreach ($this->state->injectors as $target => $injector) |
|
111 | + { |
|
104 | 112 | if ( |
105 | 113 | \class_exists($target, true) |
106 | 114 | && $reflection->isSubclassOf($target) |
@@ -64,13 +64,15 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
66 | 66 | { |
67 | - if (!isset($this->state->bindings[$alias])) { |
|
67 | + if (!isset($this->state->bindings[$alias])) |
|
68 | + { |
|
68 | 69 | return $this->resolveWithoutBinding($alias, $parameters, $context); |
69 | 70 | } |
70 | 71 | |
71 | 72 | $avoidCache = $parameters !== []; |
72 | 73 | $binding = $this->state->bindings[$alias]; |
73 | - try { |
|
74 | + try |
|
75 | + { |
|
74 | 76 | $this->tracer->push( |
75 | 77 | false, |
76 | 78 | action: 'resolve from binding', |
@@ -81,8 +83,10 @@ discard block |
||
81 | 83 | ); |
82 | 84 | $this->tracer->push(true); |
83 | 85 | |
84 | - if (\is_object($binding)) { |
|
85 | - if ($binding::class === WeakReference::class) { |
|
86 | + if (\is_object($binding)) |
|
87 | + { |
|
88 | + if ($binding::class === WeakReference::class) |
|
89 | + { |
|
86 | 90 | return $this->resolveWeakReference($binding, $alias, $context, $parameters); |
87 | 91 | } |
88 | 92 | |
@@ -96,7 +100,8 @@ discard block |
||
96 | 100 | } |
97 | 101 | |
98 | 102 | $ctx = new Ctx(alias: $alias, class: $alias, parameter: $context); |
99 | - if (\is_string($binding)) { |
|
103 | + if (\is_string($binding)) |
|
104 | + { |
|
100 | 105 | $ctx->class = $binding; |
101 | 106 | return $binding === $alias |
102 | 107 | ? $this->autowire($ctx, $parameters) |
@@ -104,19 +109,25 @@ discard block |
||
104 | 109 | : $this->make($binding, $parameters, $context); |
105 | 110 | } |
106 | 111 | |
107 | - if ($binding[1] === true) { |
|
112 | + if ($binding[1] === true) |
|
113 | + { |
|
108 | 114 | $ctx->singleton = true; |
109 | 115 | } |
110 | 116 | unset($this->state->bindings[$alias]); |
111 | - try { |
|
117 | + try |
|
118 | + { |
|
112 | 119 | return $binding[0] === $alias |
113 | 120 | ? $this->autowire($ctx, $parameters) |
114 | 121 | : $this->evaluateBinding($ctx, $binding[0], $parameters); |
115 | - } finally { |
|
122 | + } |
|
123 | + finally |
|
124 | + { |
|
116 | 125 | /** @psalm-var class-string $alias */ |
117 | 126 | $this->state->bindings[$alias] ??= $binding; |
118 | 127 | } |
119 | - } finally { |
|
128 | + } |
|
129 | + finally |
|
130 | + { |
|
120 | 131 | $this->tracer->pop(true); |
121 | 132 | $this->tracer->pop(false); |
122 | 133 | } |
@@ -130,18 +141,23 @@ discard block |
||
130 | 141 | ): ?object { |
131 | 142 | $avoidCache = $parameters !== []; |
132 | 143 | |
133 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
134 | - try { |
|
144 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)) |
|
145 | + { |
|
146 | + try |
|
147 | + { |
|
135 | 148 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
136 | 149 | $object = $this->createInstance( |
137 | 150 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
138 | 151 | $parameters, |
139 | 152 | ); |
140 | - if ($avoidCache) { |
|
153 | + if ($avoidCache) |
|
154 | + { |
|
141 | 155 | return $object; |
142 | 156 | } |
143 | 157 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
144 | - } catch (\Throwable) { |
|
158 | + } |
|
159 | + catch (\Throwable) |
|
160 | + { |
|
145 | 161 | throw new ContainerException( |
146 | 162 | $this->tracer->combineTraceMessage( |
147 | 163 | \sprintf( |
@@ -151,7 +167,9 @@ discard block |
||
151 | 167 | ) |
152 | 168 | ) |
153 | 169 | ); |
154 | - } finally { |
|
170 | + } |
|
171 | + finally |
|
172 | + { |
|
155 | 173 | $this->tracer->pop(); |
156 | 174 | } |
157 | 175 | } |
@@ -163,18 +181,25 @@ discard block |
||
163 | 181 | { |
164 | 182 | $parent = $this->scope->getParent(); |
165 | 183 | |
166 | - if ($parent !== null) { |
|
167 | - try { |
|
184 | + if ($parent !== null) |
|
185 | + { |
|
186 | + try |
|
187 | + { |
|
168 | 188 | $this->tracer->push(false, ...[ |
169 | 189 | 'current scope' => $this->scope->getScopeName(), |
170 | 190 | 'jump to parent scope' => $this->scope->getParentScope()->getScopeName(), |
171 | 191 | ]); |
172 | 192 | return $parent->make($alias, $parameters, $context); |
173 | - } catch (BadScopeException $e) { |
|
174 | - if ($this->scope->getScopeName() !== $e->getScope()) { |
|
193 | + } |
|
194 | + catch (BadScopeException $e) |
|
195 | + { |
|
196 | + if ($this->scope->getScopeName() !== $e->getScope()) |
|
197 | + { |
|
175 | 198 | throw $e; |
176 | 199 | } |
177 | - } catch (ContainerExceptionInterface $e) { |
|
200 | + } |
|
201 | + catch (ContainerExceptionInterface $e) |
|
202 | + { |
|
178 | 203 | $className = match (true) { |
179 | 204 | $e instanceof NotFoundException => NotFoundException::class, |
180 | 205 | default => ContainerException::class, |
@@ -183,19 +208,24 @@ discard block |
||
183 | 208 | 'Can\'t resolve `%s`.', |
184 | 209 | $alias, |
185 | 210 | )), previous: $e); |
186 | - } finally { |
|
211 | + } |
|
212 | + finally |
|
213 | + { |
|
187 | 214 | $this->tracer->pop(false); |
188 | 215 | } |
189 | 216 | } |
190 | 217 | |
191 | 218 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
192 | - try { |
|
219 | + try |
|
220 | + { |
|
193 | 221 | //No direct instructions how to construct class, make is automatically |
194 | 222 | return $this->autowire( |
195 | 223 | new Ctx(alias: $alias, class: $alias, parameter: $context), |
196 | 224 | $parameters, |
197 | 225 | ); |
198 | - } finally { |
|
226 | + } |
|
227 | + finally |
|
228 | + { |
|
199 | 229 | $this->tracer->pop(false); |
200 | 230 | } |
201 | 231 | } |
@@ -244,16 +274,25 @@ discard block |
||
244 | 274 | mixed $target, |
245 | 275 | array $arguments, |
246 | 276 | ): mixed { |
247 | - if (\is_string($target)) { |
|
277 | + if (\is_string($target)) |
|
278 | + { |
|
248 | 279 | // Reference |
249 | 280 | $instance = $this->make($target, $arguments, $ctx->parameter); |
250 | - } else { |
|
251 | - if ($target instanceof Autowire) { |
|
281 | + } |
|
282 | + else |
|
283 | + { |
|
284 | + if ($target instanceof Autowire) |
|
285 | + { |
|
252 | 286 | $instance = $target->resolve($this, $arguments); |
253 | - } else { |
|
254 | - try { |
|
287 | + } |
|
288 | + else |
|
289 | + { |
|
290 | + try |
|
291 | + { |
|
255 | 292 | $instance = $this->invoker->invoke($target, $arguments); |
256 | - } catch (NotCallableException $e) { |
|
293 | + } |
|
294 | + catch (NotCallableException $e) |
|
295 | + { |
|
257 | 296 | throw new ContainerException( |
258 | 297 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $ctx->alias)), |
259 | 298 | $e->getCode(), |
@@ -263,10 +302,12 @@ discard block |
||
263 | 302 | } |
264 | 303 | |
265 | 304 | // Check scope name |
266 | - if (\is_object($instance)) { |
|
305 | + if (\is_object($instance)) |
|
306 | + { |
|
267 | 307 | $ctx->reflection = new \ReflectionClass($instance); |
268 | 308 | $scopeName = ($ctx->reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
269 | - if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) { |
|
309 | + if ($scopeName !== null && $scopeName !== $this->scope->getScopeName()) |
|
310 | + { |
|
270 | 311 | throw new BadScopeException($scopeName, $instance::class); |
271 | 312 | } |
272 | 313 | } |
@@ -295,26 +336,33 @@ discard block |
||
295 | 336 | array $parameters, |
296 | 337 | ): object { |
297 | 338 | $class = $ctx->class; |
298 | - try { |
|
339 | + try |
|
340 | + { |
|
299 | 341 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
300 | - } catch (\ReflectionException $e) { |
|
342 | + } |
|
343 | + catch (\ReflectionException $e) |
|
344 | + { |
|
301 | 345 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
302 | 346 | } |
303 | 347 | |
304 | 348 | // Check scope name |
305 | 349 | $scope = ($reflection->getAttributes(ScopeAttribute::class)[0] ?? null)?->newInstance()->name; |
306 | - if ($scope !== null && $scope !== $this->scope->getScopeName()) { |
|
350 | + if ($scope !== null && $scope !== $this->scope->getScopeName()) |
|
351 | + { |
|
307 | 352 | throw new BadScopeException($scope, $class); |
308 | 353 | } |
309 | 354 | |
310 | 355 | //We have to construct class using external injector when we know exact context |
311 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
356 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
357 | + { |
|
312 | 358 | $injector = $this->state->injectors[$reflection->getName()]; |
313 | 359 | |
314 | - try { |
|
360 | + try |
|
361 | + { |
|
315 | 362 | $injectorInstance = $this->container->get($injector); |
316 | 363 | |
317 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
364 | + if (!$injectorInstance instanceof InjectorInterface) |
|
365 | + { |
|
318 | 366 | throw new InjectionException( |
319 | 367 | \sprintf( |
320 | 368 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -329,7 +377,8 @@ discard block |
||
329 | 377 | * @psalm-suppress RedundantCondition |
330 | 378 | */ |
331 | 379 | $instance = $injectorInstance->createInjection($reflection, $ctx->parameter); |
332 | - if (!$reflection->isInstance($instance)) { |
|
380 | + if (!$reflection->isInstance($instance)) |
|
381 | + { |
|
333 | 382 | throw new InjectionException( |
334 | 383 | \sprintf( |
335 | 384 | "Invalid injection response for '%s'.", |
@@ -339,12 +388,15 @@ discard block |
||
339 | 388 | } |
340 | 389 | |
341 | 390 | return $instance; |
342 | - } finally { |
|
391 | + } |
|
392 | + finally |
|
393 | + { |
|
343 | 394 | $this->state->injectors[$reflection->getName()] = $injector; |
344 | 395 | } |
345 | 396 | } |
346 | 397 | |
347 | - if (!$reflection->isInstantiable()) { |
|
398 | + if (!$reflection->isInstantiable()) |
|
399 | + { |
|
348 | 400 | $itIs = match (true) { |
349 | 401 | $reflection->isEnum() => 'Enum', |
350 | 402 | $reflection->isAbstract() => 'Abstract class', |
@@ -357,12 +409,16 @@ discard block |
||
357 | 409 | |
358 | 410 | $constructor = $reflection->getConstructor(); |
359 | 411 | |
360 | - if ($constructor !== null) { |
|
361 | - try { |
|
412 | + if ($constructor !== null) |
|
413 | + { |
|
414 | + try |
|
415 | + { |
|
362 | 416 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
363 | 417 | $this->tracer->push(true); |
364 | 418 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
365 | - } catch (ValidationException $e) { |
|
419 | + } |
|
420 | + catch (ValidationException $e) |
|
421 | + { |
|
366 | 422 | throw new ContainerException( |
367 | 423 | $this->tracer->combineTraceMessage( |
368 | 424 | \sprintf( |
@@ -372,22 +428,31 @@ discard block |
||
372 | 428 | ) |
373 | 429 | ), |
374 | 430 | ); |
375 | - } finally { |
|
431 | + } |
|
432 | + finally |
|
433 | + { |
|
376 | 434 | $this->tracer->pop(true); |
377 | 435 | $this->tracer->pop(false); |
378 | 436 | } |
379 | - try { |
|
437 | + try |
|
438 | + { |
|
380 | 439 | // Using constructor with resolved arguments |
381 | 440 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
382 | 441 | $this->tracer->push(true); |
383 | 442 | $instance = new $class(...$arguments); |
384 | - } catch (\TypeError $e) { |
|
443 | + } |
|
444 | + catch (\TypeError $e) |
|
445 | + { |
|
385 | 446 | throw new WrongTypeException($constructor, $e); |
386 | - } finally { |
|
447 | + } |
|
448 | + finally |
|
449 | + { |
|
387 | 450 | $this->tracer->pop(true); |
388 | 451 | $this->tracer->pop(false); |
389 | 452 | } |
390 | - } else { |
|
453 | + } |
|
454 | + else |
|
455 | + { |
|
391 | 456 | // No constructor specified |
392 | 457 | $instance = $reflection->newInstance(); |
393 | 458 | } |
@@ -411,13 +476,15 @@ discard block |
||
411 | 476 | $ctx->reflection ??= new \ReflectionClass($instance); |
412 | 477 | |
413 | 478 | //Declarative singletons |
414 | - if ($this->isSingleton($ctx)) { |
|
479 | + if ($this->isSingleton($ctx)) |
|
480 | + { |
|
415 | 481 | $this->state->bindings[$ctx->alias] = $instance; |
416 | 482 | } |
417 | 483 | |
418 | 484 | // Register finalizer |
419 | 485 | $finalizer = $this->getFinalizer($ctx, $instance); |
420 | - if ($finalizer !== null) { |
|
486 | + if ($finalizer !== null) |
|
487 | + { |
|
421 | 488 | $this->state->finalizers[] = $finalizer; |
422 | 489 | } |
423 | 490 | |
@@ -429,11 +496,13 @@ discard block |
||
429 | 496 | */ |
430 | 497 | private function isSingleton(Ctx $ctx): bool |
431 | 498 | { |
432 | - if ($ctx->singleton === true) { |
|
499 | + if ($ctx->singleton === true) |
|
500 | + { |
|
433 | 501 | return true; |
434 | 502 | } |
435 | 503 | |
436 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
504 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
505 | + { |
|
437 | 506 | return true; |
438 | 507 | } |
439 | 508 | |
@@ -444,7 +513,8 @@ discard block |
||
444 | 513 | { |
445 | 514 | /** @var Finalize|null $attribute */ |
446 | 515 | $attribute = ($ctx->reflection->getAttributes(Finalize::class)[0] ?? null)?->newInstance(); |
447 | - if ($attribute === null) { |
|
516 | + if ($attribute === null) |
|
517 | + { |
|
448 | 518 | return null; |
449 | 519 | } |
450 | 520 |
@@ -34,22 +34,27 @@ |
||
34 | 34 | { |
35 | 35 | [$previous, self::$container] = [self::$container, $container]; |
36 | 36 | |
37 | - try { |
|
38 | - if (Fiber::getCurrent() === null) { |
|
37 | + try |
|
38 | + { |
|
39 | + if (Fiber::getCurrent() === null) |
|
40 | + { |
|
39 | 41 | return $scope(self::$container); |
40 | 42 | } |
41 | 43 | |
42 | 44 | // Wrap scope into fiber |
43 | 45 | $fiber = new Fiber(static fn () => $scope(self::$container)); |
44 | 46 | $value = $fiber->start(); |
45 | - while (!$fiber->isTerminated()) { |
|
47 | + while (!$fiber->isTerminated()) |
|
48 | + { |
|
46 | 49 | self::$container = $previous; |
47 | 50 | $resume = Fiber::suspend($value); |
48 | 51 | self::$container = $container; |
49 | 52 | $value = $fiber->resume($resume); |
50 | 53 | } |
51 | 54 | return $fiber->getReturn(); |
52 | - } finally { |
|
55 | + } |
|
56 | + finally |
|
57 | + { |
|
53 | 58 | self::$container = $previous; |
54 | 59 | } |
55 | 60 | } |
@@ -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 | } |
@@ -18,11 +18,15 @@ discard block |
||
18 | 18 | |
19 | 19 | $container = new Container(); |
20 | 20 | |
21 | - $container->scope(static function (Container $c1) { |
|
22 | - try { |
|
21 | + $container->scope(static function (Container $c1) |
|
22 | + { |
|
23 | + try |
|
24 | + { |
|
23 | 25 | $c1->get(ExceptionConstructor::class); |
24 | 26 | self::fail('Exception should be thrown'); |
25 | - } catch (\Throwable $e) { |
|
27 | + } |
|
28 | + catch (\Throwable $e) |
|
29 | + { |
|
26 | 30 | // self::assertInstanceOf(\InvalidArgumentException::class, $e); |
27 | 31 | throw $e; |
28 | 32 | } |
@@ -36,11 +40,15 @@ discard block |
||
36 | 40 | |
37 | 41 | $container = new Container(); |
38 | 42 | |
39 | - $container->scope(static function (Container $c1) { |
|
40 | - try { |
|
43 | + $container->scope(static function (Container $c1) |
|
44 | + { |
|
45 | + try |
|
46 | + { |
|
41 | 47 | $c1->get(ExceptionConstructor::class); |
42 | 48 | self::fail('Exception should be thrown'); |
43 | - } catch (\Throwable $e) { |
|
49 | + } |
|
50 | + catch (\Throwable $e) |
|
51 | + { |
|
44 | 52 | // self::assertInstanceOf(\InvalidArgumentException::class, $e); |
45 | 53 | throw $e; |
46 | 54 | } |
@@ -54,11 +62,15 @@ discard block |
||
54 | 62 | |
55 | 63 | $container = new Container(); |
56 | 64 | |
57 | - $container->scope(static function (Container $c1) { |
|
58 | - try { |
|
65 | + $container->scope(static function (Container $c1) |
|
66 | + { |
|
67 | + try |
|
68 | + { |
|
59 | 69 | $c1->get(DatetimeCarrier::class); |
60 | 70 | self::fail('Exception should be thrown'); |
61 | - } catch (\Throwable $e) { |
|
71 | + } |
|
72 | + catch (\Throwable $e) |
|
73 | + { |
|
62 | 74 | self::assertInstanceOf(NotFoundException::class, $e); |
63 | 75 | self::assertInstanceOf(NotFoundException::class, $e->getPrevious()); |
64 | 76 | self::assertStringContainsString( |
@@ -22,10 +22,12 @@ discard block |
||
22 | 22 | $root = new Container(); |
23 | 23 | $root->bind(LoggerInterface::class, KVLogger::class); |
24 | 24 | |
25 | - $root->scope(static function (Container $c1) { |
|
25 | + $root->scope(static function (Container $c1) |
|
26 | + { |
|
26 | 27 | $c1->bind(LoggerInterface::class, FileLogger::class); |
27 | 28 | |
28 | - $c1->scope(static function (LoggerCarrier $carrier, LoggerInterface $logger) { |
|
29 | + $c1->scope(static function (LoggerCarrier $carrier, LoggerInterface $logger) |
|
30 | + { |
|
29 | 31 | // from the $root container |
30 | 32 | self::assertInstanceOf(KVLogger::class, $carrier->logger); |
31 | 33 | // from the $c1 container |
@@ -39,7 +41,8 @@ discard block |
||
39 | 41 | $root = new Container(); |
40 | 42 | $root->bind(LoggerInterface::class, KVLogger::class); |
41 | 43 | |
42 | - $root->scope(static function (Container $c1) { |
|
44 | + $root->scope(static function (Container $c1) |
|
45 | + { |
|
43 | 46 | $c1->bind(LoggerInterface::class, FileLogger::class); |
44 | 47 | |
45 | 48 | self::assertInstanceOf( |