@@ -44,11 +44,13 @@ discard block |
||
44 | 44 | LoggerInterface::class => KVLogger::class, |
45 | 45 | ], |
46 | 46 | ), |
47 | - static function (ScopedProxyLoggerCarrier $carrier, LoggerInterface $logger) use ($lc) { |
|
47 | + static function (ScopedProxyLoggerCarrier $carrier, LoggerInterface $logger) use ($lc) |
|
48 | + { |
|
48 | 49 | // from the current `foo` scope |
49 | 50 | self::assertInstanceOf(KVLogger::class, $logger); |
50 | 51 | |
51 | - for ($i = 0; $i < 10; $i++) { |
|
52 | + for ($i = 0; $i < 10; $i++) |
|
53 | + { |
|
52 | 54 | // because of proxy |
53 | 55 | self::assertNotInstanceOf(KVLogger::class, $carrier->getLogger()); |
54 | 56 | self::assertSame('kv', $carrier->logger->getName()); |
@@ -64,11 +66,13 @@ discard block |
||
64 | 66 | LoggerInterface::class => FileLogger::class, |
65 | 67 | ], |
66 | 68 | ), |
67 | - static function (ScopedProxyLoggerCarrier $carrier, LoggerInterface $logger) use ($lc) { |
|
69 | + static function (ScopedProxyLoggerCarrier $carrier, LoggerInterface $logger) use ($lc) |
|
70 | + { |
|
68 | 71 | // from the current `foo` scope |
69 | 72 | self::assertInstanceOf(FileLogger::class, $logger); |
70 | 73 | |
71 | - for ($i = 0; $i < 10; $i++) { |
|
74 | + for ($i = 0; $i < 10; $i++) |
|
75 | + { |
|
72 | 76 | // because of proxy |
73 | 77 | self::assertNotInstanceOf(FileLogger::class, $carrier->getLogger()); |
74 | 78 | self::assertSame('file', $carrier->logger->getName()); |
@@ -85,7 +89,8 @@ discard block |
||
85 | 89 | $root = new Container(); |
86 | 90 | $root->getBinder('http')->bindSingleton(LoggerInterface::class, KVLogger::class); |
87 | 91 | |
88 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
92 | + $root->runScope(new Scope(), static function (Container $c1) |
|
93 | + { |
|
89 | 94 | $c1->runScope( |
90 | 95 | new Scope(name: 'http'), |
91 | 96 | static function ( |
@@ -112,10 +117,12 @@ discard block |
||
112 | 117 | $root = new Container(); |
113 | 118 | $root->getBinder('foo')->bind(LoggerInterface::class, KVLogger::class); |
114 | 119 | |
115 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
120 | + $root->runScope(new Scope(), static function (Container $c1) |
|
121 | + { |
|
116 | 122 | $c1->runScope( |
117 | 123 | new Scope(name: 'foo'), |
118 | - static function (ScopedProxyLoggerCarrier $carrier, LoggerInterface $logger) { |
|
124 | + static function (ScopedProxyLoggerCarrier $carrier, LoggerInterface $logger) |
|
125 | + { |
|
119 | 126 | // from the current `foo` scope |
120 | 127 | self::assertInstanceOf(KVLogger::class, $logger); |
121 | 128 | |
@@ -136,7 +143,8 @@ discard block |
||
136 | 143 | ->bind( |
137 | 144 | ContextInterface::class, |
138 | 145 | new \Spiral\Core\Config\Injectable( |
139 | - new class implements InjectorInterface { |
|
146 | + new class implements InjectorInterface |
|
147 | + { |
|
140 | 148 | public function createInjection(\ReflectionClass $class, mixed $context = null): Context |
141 | 149 | { |
142 | 150 | return new Context($context); |
@@ -145,8 +153,10 @@ discard block |
||
145 | 153 | ), |
146 | 154 | ); |
147 | 155 | |
148 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
149 | - $c1->runScope(new Scope(name: 'foo'), static function (Container $c, ContextInterface $param) { |
|
156 | + $root->runScope(new Scope(), static function (Container $c1) |
|
157 | + { |
|
158 | + $c1->runScope(new Scope(name: 'foo'), static function (Container $c, ContextInterface $param) |
|
159 | + { |
|
150 | 160 | self::assertInstanceOf(ReflectionParameter::class, $param->value); |
151 | 161 | self::assertSame('param', $param->value->getName()); |
152 | 162 | |
@@ -177,7 +187,8 @@ discard block |
||
177 | 187 | ->bind( |
178 | 188 | ContextInterface::class, |
179 | 189 | new \Spiral\Core\Config\Injectable( |
180 | - new class implements InjectorInterface { |
|
190 | + new class implements InjectorInterface |
|
191 | + { |
|
181 | 192 | public function createInjection(\ReflectionClass $class, mixed $context = null): Context |
182 | 193 | { |
183 | 194 | return new Context($context); |
@@ -187,15 +198,19 @@ discard block |
||
187 | 198 | ); |
188 | 199 | |
189 | 200 | FiberHelper::runFiberSequence( |
190 | - static fn() => $root->runScope(new Scope(name: 'foo'), static function (ContextInterface $ctx) { |
|
191 | - for ($i = 0; $i < 10; $i++) { |
|
201 | + static fn() => $root->runScope(new Scope(name: 'foo'), static function (ContextInterface $ctx) |
|
202 | + { |
|
203 | + for ($i = 0; $i < 10; $i++) |
|
204 | + { |
|
192 | 205 | self::assertInstanceOf(ReflectionParameter::class, $ctx->getValue(), 'Context injected'); |
193 | 206 | self::assertSame('ctx', $ctx->getValue()->getName()); |
194 | 207 | \Fiber::suspend(); |
195 | 208 | } |
196 | 209 | }), |
197 | - static fn() => $root->runScope(new Scope(name: 'foo'), static function (ContextInterface $context) { |
|
198 | - for ($i = 0; $i < 10; $i++) { |
|
210 | + static fn() => $root->runScope(new Scope(name: 'foo'), static function (ContextInterface $context) |
|
211 | + { |
|
212 | + for ($i = 0; $i < 10; $i++) |
|
213 | + { |
|
199 | 214 | self::assertInstanceOf(ReflectionParameter::class, $context->getValue(), 'Context injected'); |
200 | 215 | self::assertSame('context', $context->getValue()->getName()); |
201 | 216 | \Fiber::suspend(); |
@@ -211,8 +226,10 @@ discard block |
||
211 | 226 | |
212 | 227 | $root->runScope( |
213 | 228 | new Scope(), |
214 | - static function (#[Proxy] ContainerInterface $cp) use ($root) { |
|
215 | - $root->runScope(new Scope(name: 'http'), static function (ContainerInterface $c) use ($cp) { |
|
229 | + static function (#[Proxy] ContainerInterface $cp) use ($root) |
|
230 | + { |
|
231 | + $root->runScope(new Scope(name: 'http'), static function (ContainerInterface $c) use ($cp) |
|
232 | + { |
|
216 | 233 | self::assertNotSame($c, $cp); |
217 | 234 | self::assertSame($c, $cp->get(ContainerInterface::class)); |
218 | 235 | self::assertInstanceOf(KVLogger::class, $cp->get(LoggerInterface::class)); |
@@ -256,7 +273,8 @@ discard block |
||
256 | 273 | { |
257 | 274 | $root = new Container(); |
258 | 275 | $context = (object)['destroyed' => false]; |
259 | - $class = new class($context) implements DestroyableInterface { |
|
276 | + $class = new class($context) implements DestroyableInterface |
|
277 | + { |
|
260 | 278 | public function __construct( |
261 | 279 | private readonly \stdClass $context, |
262 | 280 | ) { |
@@ -285,7 +303,8 @@ discard block |
||
285 | 303 | |
286 | 304 | $root->runScope( |
287 | 305 | new Scope('http'), |
288 | - static function () use ($root, $proxy) { |
|
306 | + static function () use ($root, $proxy) |
|
307 | + { |
|
289 | 308 | self::assertSame('Foo', $proxy->getName()); |
290 | 309 | $proxy->setName(new class implements \Stringable { |
291 | 310 | public function __toString(): string |
@@ -326,9 +345,11 @@ discard block |
||
326 | 345 | |
327 | 346 | $root->runScope( |
328 | 347 | new Scope(), |
329 | - static function (#[Proxy] ContainerInterface $proxy, ContainerInterface $scoped) { |
|
348 | + static function (#[Proxy] ContainerInterface $proxy, ContainerInterface $scoped) |
|
349 | + { |
|
330 | 350 | self::assertNotSame($scoped, $proxy); |
331 | - ContainerScope::runScope($proxy, static function (ContainerInterface $passed) use ($proxy, $scoped) { |
|
351 | + ContainerScope::runScope($proxy, static function (ContainerInterface $passed) use ($proxy, $scoped) |
|
352 | + { |
|
332 | 353 | self::assertNotSame($passed, $proxy); |
333 | 354 | self::assertSame($scoped, ContainerScope::getContainer()); |
334 | 355 | }); |
@@ -24,12 +24,15 @@ discard block |
||
24 | 24 | ): object { |
25 | 25 | $c ??= ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
26 | 26 | |
27 | - try { |
|
27 | + try |
|
28 | + { |
|
28 | 29 | /** @psalm-suppress TooManyArguments */ |
29 | 30 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
30 | 31 | 'Resolved `null` from the container.', |
31 | 32 | ); |
32 | - } catch (\Throwable $e) { |
|
33 | + } |
|
34 | + catch (\Throwable $e) |
|
35 | + { |
|
33 | 36 | $scope = self::getScope($c); |
34 | 37 | throw new ContainerException( |
35 | 38 | $scope === null |
@@ -39,7 +42,8 @@ discard block |
||
39 | 42 | ); |
40 | 43 | } |
41 | 44 | |
42 | - if (Proxy::isProxy($result)) { |
|
45 | + if (Proxy::isProxy($result)) |
|
46 | + { |
|
43 | 47 | $scope = self::getScope($c); |
44 | 48 | throw new RecursiveProxyException( |
45 | 49 | $scope === null |
@@ -56,8 +60,10 @@ discard block |
||
56 | 60 | */ |
57 | 61 | private static function getScope(ContainerInterface $c): ?string |
58 | 62 | { |
59 | - if (!$c instanceof Container) { |
|
60 | - if (!Proxy::isProxy($c)) { |
|
63 | + if (!$c instanceof Container) |
|
64 | + { |
|
65 | + if (!Proxy::isProxy($c)) |
|
66 | + { |
|
61 | 67 | return null; |
62 | 68 | } |
63 | 69 |
@@ -34,26 +34,35 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public static function runScope(ContainerInterface $container, callable $scope): mixed |
36 | 36 | { |
37 | - if (Proxy::isProxy($container)) { |
|
37 | + if (Proxy::isProxy($container)) |
|
38 | + { |
|
38 | 39 | // Ignore Proxy to avoid recursion |
39 | 40 | $container = $previous = self::$container ?? throw new ContainerException('Proxy is out of scope.'); |
40 | - } else { |
|
41 | + } |
|
42 | + else |
|
43 | + { |
|
41 | 44 | [$previous, self::$container] = [self::$container, $container]; |
42 | 45 | } |
43 | 46 | |
44 | - try { |
|
45 | - if (Fiber::getCurrent() === null) { |
|
47 | + try |
|
48 | + { |
|
49 | + if (Fiber::getCurrent() === null) |
|
50 | + { |
|
46 | 51 | return $scope(self::$container); |
47 | 52 | } |
48 | 53 | |
49 | 54 | // Wrap scope into fiber |
50 | 55 | $fiber = new Fiber(static fn () => $scope(self::$container)); |
51 | 56 | $value = $fiber->start(); |
52 | - while (!$fiber->isTerminated()) { |
|
57 | + while (!$fiber->isTerminated()) |
|
58 | + { |
|
53 | 59 | self::$container = $previous; |
54 | - try { |
|
60 | + try |
|
61 | + { |
|
55 | 62 | $resume = Fiber::suspend($value); |
56 | - } catch (Throwable $e) { |
|
63 | + } |
|
64 | + catch (Throwable $e) |
|
65 | + { |
|
57 | 66 | self::$container = $container; |
58 | 67 | $value = $fiber->throw($e); |
59 | 68 | continue; |
@@ -63,7 +72,9 @@ discard block |
||
63 | 72 | $value = $fiber->resume($resume); |
64 | 73 | } |
65 | 74 | return $fiber->getReturn(); |
66 | - } finally { |
|
75 | + } |
|
76 | + finally |
|
77 | + { |
|
67 | 78 | self::$container = $previous; |
68 | 79 | } |
69 | 80 | } |