@@ -26,8 +26,8 @@ discard block |
||
| 26 | 26 | ?\Throwable $previous = null, |
| 27 | 27 | ): static { |
| 28 | 28 | $result = new static( |
| 29 | - $message . ($traces === [] ? '' : "\nResolving trace:\n" . Tracer::renderTraceList($traces)), |
|
| 30 | - previous: $previous, |
|
| 29 | + $message.($traces === [] ? '' : "\nResolving trace:\n".Tracer::renderTraceList($traces)), |
|
| 30 | + previous : $previous, |
|
| 31 | 31 | ); |
| 32 | 32 | $result->originalMessage = $message; |
| 33 | 33 | $result->containerTrace = $traces; |
@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | public static function extendTracedException(string $message, array $traces, self $exception): static |
| 38 | 38 | { |
| 39 | 39 | $merge = $exception->containerTrace; |
| 40 | - if ($traces !== [] && $merge !== []) { |
|
| 40 | + if ($traces !== [] && $merge !== []){ |
|
| 41 | 41 | // merge lat element of $traces with first element of $merge |
| 42 | 42 | \array_push($traces[\count($traces) - 1], ...$merge[0]); |
| 43 | 43 | unset($merge[0]); |
@@ -37,7 +37,8 @@ |
||
| 37 | 37 | public static function extendTracedException(string $message, array $traces, self $exception): static |
| 38 | 38 | { |
| 39 | 39 | $merge = $exception->containerTrace; |
| 40 | - if ($traces !== [] && $merge !== []) { |
|
| 40 | + if ($traces !== [] && $merge !== []) |
|
| 41 | + { |
|
| 41 | 42 | // merge lat element of $traces with first element of $merge |
| 42 | 43 | \array_push($traces[\count($traces) - 1], ...$merge[0]); |
| 43 | 44 | unset($merge[0]); |
@@ -7,4 +7,4 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * When class can not be created. |
| 9 | 9 | */ |
| 10 | -class AutowireException extends TracedContainerException {} |
|
| 10 | +class AutowireException extends TracedContainerException{} |
|
@@ -7,4 +7,6 @@ |
||
| 7 | 7 | /** |
| 8 | 8 | * When class can not be created. |
| 9 | 9 | */ |
| 10 | -class AutowireException extends TracedContainerException {} |
|
| 10 | +class AutowireException extends TracedContainerException |
|
| 11 | +{ |
|
| 12 | +} |
|
@@ -82,7 +82,7 @@ discard block |
||
| 82 | 82 | public function testNullableUnionDefaultScalar(): void |
| 83 | 83 | { |
| 84 | 84 | $result = $this->resolveClosure( |
| 85 | - static fn(null|int|string $param = 42) => $param, |
|
| 85 | + static fn(null | int | string $param = 42) => $param, |
|
| 86 | 86 | ); |
| 87 | 87 | |
| 88 | 88 | $this->assertSame([42], $result); |
@@ -103,12 +103,12 @@ discard block |
||
| 103 | 103 | { |
| 104 | 104 | $this->bind(\DateTimeInterface::class, static fn() => throw new \RuntimeException('fail!')); |
| 105 | 105 | |
| 106 | - try { |
|
| 106 | + try{ |
|
| 107 | 107 | $this->resolveClosure( |
| 108 | 108 | static fn(\DateTimeInterface $param) => $param, |
| 109 | 109 | ); |
| 110 | 110 | self::fail('Exception should be thrown'); |
| 111 | - } catch (NotFoundException $e) { |
|
| 111 | + }catch (NotFoundException $e){ |
|
| 112 | 112 | self::assertNotNull($e->getPrevious()); |
| 113 | 113 | self::assertStringContainsString('fail!', $e->getPrevious()->getMessage()); |
| 114 | 114 | } |
@@ -103,12 +103,15 @@ |
||
| 103 | 103 | { |
| 104 | 104 | $this->bind(\DateTimeInterface::class, static fn() => throw new \RuntimeException('fail!')); |
| 105 | 105 | |
| 106 | - try { |
|
| 106 | + try |
|
| 107 | + { |
|
| 107 | 108 | $this->resolveClosure( |
| 108 | 109 | static fn(\DateTimeInterface $param) => $param, |
| 109 | 110 | ); |
| 110 | 111 | self::fail('Exception should be thrown'); |
| 111 | - } catch (NotFoundException $e) { |
|
| 112 | + } |
|
| 113 | + catch (NotFoundException $e) |
|
| 114 | + { |
|
| 112 | 115 | self::assertNotNull($e->getPrevious()); |
| 113 | 116 | self::assertStringContainsString('fail!', $e->getPrevious()->getMessage()); |
| 114 | 117 | } |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | { |
| 74 | 74 | $this->container->bindSingleton(Bucket::class, $bucket = new Bucket('foo')); |
| 75 | 75 | |
| 76 | - $result = $this->container->invoke(Storage::class . '::createBucket', ['name' => 'bar']); |
|
| 76 | + $result = $this->container->invoke(Storage::class.'::createBucket', ['name' => 'bar']); |
|
| 77 | 77 | |
| 78 | 78 | self::assertSame($bucket, $result['bucket']); |
| 79 | 79 | self::assertInstanceOf(SampleClass::class, $result['class']); |
@@ -142,10 +142,10 @@ discard block |
||
| 142 | 142 | // Note: do not add a return type to the closure |
| 143 | 143 | $this->container->bind('foo', fn() => throw new \Exception('Factory called')); |
| 144 | 144 | |
| 145 | - try { |
|
| 145 | + try{ |
|
| 146 | 146 | $this->container->invoke(['foo', 'publicMethod'], [42]); |
| 147 | 147 | self::fail('Exception should be thrown'); |
| 148 | - } catch (\Throwable $e) { |
|
| 148 | + }catch (\Throwable $e){ |
|
| 149 | 149 | self::assertInstanceOf(NotFoundException::class, $e); |
| 150 | 150 | self::assertNotNull($e->getPrevious()); |
| 151 | 151 | self::assertStringContainsString('Factory called', $e->getPrevious()->getMessage()); |
@@ -157,12 +157,12 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | public function testCallStaticMethodWithoutInstantiationWithOvertypedFactory(): void |
| 159 | 159 | { |
| 160 | - $this->container->bind('foo', fn(): PrivateConstructor|SampleClass => throw new \Exception('Factory called')); |
|
| 160 | + $this->container->bind('foo', fn(): PrivateConstructor | SampleClass => throw new \Exception('Factory called')); |
|
| 161 | 161 | |
| 162 | - try { |
|
| 162 | + try{ |
|
| 163 | 163 | $this->container->invoke(['foo', 'publicMethod'], [42]); |
| 164 | 164 | self::fail('Exception should be thrown'); |
| 165 | - } catch (\Throwable $e) { |
|
| 165 | + }catch (\Throwable $e){ |
|
| 166 | 166 | self::assertInstanceOf(NotFoundException::class, $e); |
| 167 | 167 | self::assertNotNull($e->getPrevious()); |
| 168 | 168 | self::assertStringContainsString('Factory called', $e->getPrevious()->getMessage()); |
@@ -175,10 +175,10 @@ discard block |
||
| 175 | 175 | public function testCallStaticMethodWithoutInstantiationWithNullableTypedFactory(): void |
| 176 | 176 | { |
| 177 | 177 | $this->container->bind('foo', fn(): ?PrivateConstructor => throw new \Exception('Factory called')); |
| 178 | - try { |
|
| 178 | + try{ |
|
| 179 | 179 | $this->container->invoke(['foo', 'publicMethod'], [42]); |
| 180 | 180 | self::fail('Exception should be thrown'); |
| 181 | - } catch (\Throwable $e) { |
|
| 181 | + }catch (\Throwable $e){ |
|
| 182 | 182 | self::assertInstanceOf(NotFoundException::class, $e); |
| 183 | 183 | self::assertNotNull($e->getPrevious()); |
| 184 | 184 | self::assertStringContainsString('Factory called', $e->getPrevious()->getMessage()); |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | $this->expectException(ArgumentResolvingException::class); |
| 191 | 191 | $this->expectExceptionMessage('Unable to resolve required argument `name` when resolving'); |
| 192 | 192 | |
| 193 | - $this->container->invoke(Storage::class . '::createBucket', ['name' => 'bar']); |
|
| 193 | + $this->container->invoke(Storage::class.'::createBucket', ['name' => 'bar']); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | public function testCallValidClosure(): void |
@@ -142,10 +142,13 @@ discard block |
||
| 142 | 142 | // Note: do not add a return type to the closure |
| 143 | 143 | $this->container->bind('foo', fn() => throw new \Exception('Factory called')); |
| 144 | 144 | |
| 145 | - try { |
|
| 145 | + try |
|
| 146 | + { |
|
| 146 | 147 | $this->container->invoke(['foo', 'publicMethod'], [42]); |
| 147 | 148 | self::fail('Exception should be thrown'); |
| 148 | - } catch (\Throwable $e) { |
|
| 149 | + } |
|
| 150 | + catch (\Throwable $e) |
|
| 151 | + { |
|
| 149 | 152 | self::assertInstanceOf(NotFoundException::class, $e); |
| 150 | 153 | self::assertNotNull($e->getPrevious()); |
| 151 | 154 | self::assertStringContainsString('Factory called', $e->getPrevious()->getMessage()); |
@@ -159,10 +162,13 @@ discard block |
||
| 159 | 162 | { |
| 160 | 163 | $this->container->bind('foo', fn(): PrivateConstructor|SampleClass => throw new \Exception('Factory called')); |
| 161 | 164 | |
| 162 | - try { |
|
| 165 | + try |
|
| 166 | + { |
|
| 163 | 167 | $this->container->invoke(['foo', 'publicMethod'], [42]); |
| 164 | 168 | self::fail('Exception should be thrown'); |
| 165 | - } catch (\Throwable $e) { |
|
| 169 | + } |
|
| 170 | + catch (\Throwable $e) |
|
| 171 | + { |
|
| 166 | 172 | self::assertInstanceOf(NotFoundException::class, $e); |
| 167 | 173 | self::assertNotNull($e->getPrevious()); |
| 168 | 174 | self::assertStringContainsString('Factory called', $e->getPrevious()->getMessage()); |
@@ -175,10 +181,13 @@ discard block |
||
| 175 | 181 | public function testCallStaticMethodWithoutInstantiationWithNullableTypedFactory(): void |
| 176 | 182 | { |
| 177 | 183 | $this->container->bind('foo', fn(): ?PrivateConstructor => throw new \Exception('Factory called')); |
| 178 | - try { |
|
| 184 | + try |
|
| 185 | + { |
|
| 179 | 186 | $this->container->invoke(['foo', 'publicMethod'], [42]); |
| 180 | 187 | self::fail('Exception should be thrown'); |
| 181 | - } catch (\Throwable $e) { |
|
| 188 | + } |
|
| 189 | + catch (\Throwable $e) |
|
| 190 | + { |
|
| 182 | 191 | self::assertInstanceOf(NotFoundException::class, $e); |
| 183 | 192 | self::assertNotNull($e->getPrevious()); |
| 184 | 193 | self::assertStringContainsString('Factory called', $e->getPrevious()->getMessage()); |
@@ -37,23 +37,23 @@ discard block |
||
| 37 | 37 | public function make( |
| 38 | 38 | string $alias, |
| 39 | 39 | array $parameters = [], |
| 40 | - \Stringable|string|null $context = null, |
|
| 40 | + \Stringable | string | null $context = null, |
|
| 41 | 41 | ?Tracer $tracer = null, |
| 42 | 42 | ): mixed { |
| 43 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 43 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)){ |
|
| 44 | 44 | return $this->state->singletons[$alias]; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | |
| 48 | 48 | $this->actor->resolveType($alias, $binding, $singleton, $injector, $actor, false); |
| 49 | - if ($parameters === [] && $singleton !== null) { |
|
| 49 | + if ($parameters === [] && $singleton !== null){ |
|
| 50 | 50 | return $singleton; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | $tracer ??= new Tracer(); |
| 54 | 54 | |
| 55 | 55 | // Resolve without binding |
| 56 | - if ($binding === null) { |
|
| 56 | + if ($binding === null){ |
|
| 57 | 57 | $tracer->push( |
| 58 | 58 | false, |
| 59 | 59 | action: 'autowire', |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | scope: $this->scope->getScopeName(), |
| 62 | 62 | context: $context, |
| 63 | 63 | ); |
| 64 | - try { |
|
| 64 | + try{ |
|
| 65 | 65 | // No direct instructions how to construct class, make is automatically |
| 66 | 66 | return $this->actor->autowire( |
| 67 | 67 | new Ctx(alias: $alias, class: $alias, context: $context, singleton: $parameters === [] ? null : false), |
@@ -69,12 +69,12 @@ discard block |
||
| 69 | 69 | $actor, |
| 70 | 70 | $tracer, |
| 71 | 71 | ); |
| 72 | - } finally { |
|
| 72 | + }finally{ |
|
| 73 | 73 | $tracer->pop(false); |
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | - try { |
|
| 77 | + try{ |
|
| 78 | 78 | $tracer->push( |
| 79 | 79 | false, |
| 80 | 80 | action: 'resolve from binding', |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | $actor->disableBinding($alias); |
| 89 | 89 | return $actor->resolveBinding($binding, $alias, $context, $parameters, $tracer); |
| 90 | - } finally { |
|
| 90 | + }finally{ |
|
| 91 | 91 | $actor->enableBinding($alias, $binding); |
| 92 | 92 | $tracer->pop(true); |
| 93 | 93 | $tracer->pop(false); |
@@ -40,20 +40,23 @@ discard block |
||
| 40 | 40 | \Stringable|string|null $context = null, |
| 41 | 41 | ?Tracer $tracer = null, |
| 42 | 42 | ): mixed { |
| 43 | - if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
| 43 | + if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) |
|
| 44 | + { |
|
| 44 | 45 | return $this->state->singletons[$alias]; |
| 45 | 46 | } |
| 46 | 47 | |
| 47 | 48 | |
| 48 | 49 | $this->actor->resolveType($alias, $binding, $singleton, $injector, $actor, false); |
| 49 | - if ($parameters === [] && $singleton !== null) { |
|
| 50 | + if ($parameters === [] && $singleton !== null) |
|
| 51 | + { |
|
| 50 | 52 | return $singleton; |
| 51 | 53 | } |
| 52 | 54 | |
| 53 | 55 | $tracer ??= new Tracer(); |
| 54 | 56 | |
| 55 | 57 | // Resolve without binding |
| 56 | - if ($binding === null) { |
|
| 58 | + if ($binding === null) |
|
| 59 | + { |
|
| 57 | 60 | $tracer->push( |
| 58 | 61 | false, |
| 59 | 62 | action: 'autowire', |
@@ -61,7 +64,8 @@ discard block |
||
| 61 | 64 | scope: $this->scope->getScopeName(), |
| 62 | 65 | context: $context, |
| 63 | 66 | ); |
| 64 | - try { |
|
| 67 | + try |
|
| 68 | + { |
|
| 65 | 69 | // No direct instructions how to construct class, make is automatically |
| 66 | 70 | return $this->actor->autowire( |
| 67 | 71 | new Ctx(alias: $alias, class: $alias, context: $context, singleton: $parameters === [] ? null : false), |
@@ -69,12 +73,15 @@ discard block |
||
| 69 | 73 | $actor, |
| 70 | 74 | $tracer, |
| 71 | 75 | ); |
| 72 | - } finally { |
|
| 76 | + } |
|
| 77 | + finally |
|
| 78 | + { |
|
| 73 | 79 | $tracer->pop(false); |
| 74 | 80 | } |
| 75 | 81 | } |
| 76 | 82 | |
| 77 | - try { |
|
| 83 | + try |
|
| 84 | + { |
|
| 78 | 85 | $tracer->push( |
| 79 | 86 | false, |
| 80 | 87 | action: 'resolve from binding', |
@@ -87,7 +94,9 @@ discard block |
||
| 87 | 94 | |
| 88 | 95 | $actor->disableBinding($alias); |
| 89 | 96 | return $actor->resolveBinding($binding, $alias, $context, $parameters, $tracer); |
| 90 | - } finally { |
|
| 97 | + } |
|
| 98 | + finally |
|
| 99 | + { |
|
| 91 | 100 | $actor->enableBinding($alias, $binding); |
| 92 | 101 | $tracer->pop(true); |
| 93 | 102 | $tracer->pop(false); |
@@ -86,7 +86,7 @@ discard block |
||
| 86 | 86 | */ |
| 87 | 87 | public function resolveType( |
| 88 | 88 | string $alias, |
| 89 | - ?Binding &$binding = null, |
|
| 89 | + ?Binding & $binding = null, |
|
| 90 | 90 | ?object &$singleton = null, |
| 91 | 91 | ?object &$injector = null, |
| 92 | 92 | ?self &$actor = null, |
@@ -95,20 +95,20 @@ discard block |
||
| 95 | 95 | // Aliases to prevent circular dependencies |
| 96 | 96 | $as = []; |
| 97 | 97 | $actor = $this; |
| 98 | - do { |
|
| 98 | + do{ |
|
| 99 | 99 | $bindings = &$actor->state->bindings; |
| 100 | 100 | $singletons = &$actor->state->singletons; |
| 101 | 101 | $injectors = &$actor->state->injectors; |
| 102 | 102 | $binding = $bindings[$alias] ?? null; |
| 103 | - if (\array_key_exists($alias, $singletons)) { |
|
| 103 | + if (\array_key_exists($alias, $singletons)){ |
|
| 104 | 104 | $singleton = $singletons[$alias]; |
| 105 | 105 | $injector = $injectors[$alias] ?? null; |
| 106 | 106 | return \is_object($singleton::class) ? $singleton::class : null; |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - if ($binding !== null) { |
|
| 110 | - if ($followAlias && $binding::class === Alias::class) { |
|
| 111 | - if ($binding->alias === $alias) { |
|
| 109 | + if ($binding !== null){ |
|
| 110 | + if ($followAlias && $binding::class === Alias::class){ |
|
| 111 | + if ($binding->alias === $alias){ |
|
| 112 | 112 | break; |
| 113 | 113 | } |
| 114 | 114 | |
@@ -123,7 +123,7 @@ discard block |
||
| 123 | 123 | return $binding->getReturnClass(); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if (\array_key_exists($alias, $injectors)) { |
|
| 126 | + if (\array_key_exists($alias, $injectors)){ |
|
| 127 | 127 | $injector = $injectors[$alias]; |
| 128 | 128 | $binding = $bindings[$alias] ?? null; |
| 129 | 129 | return $alias; |
@@ -131,12 +131,12 @@ discard block |
||
| 131 | 131 | |
| 132 | 132 | // Go to parent scope |
| 133 | 133 | $parent = $actor->scope->getParentActor(); |
| 134 | - if ($parent === null) { |
|
| 134 | + if ($parent === null){ |
|
| 135 | 135 | break; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | 138 | $actor = $parent; |
| 139 | - } while (true); |
|
| 139 | + }while (true); |
|
| 140 | 140 | |
| 141 | 141 | return \class_exists($alias) ? $alias : null; |
| 142 | 142 | } |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | public function resolveBinding( |
| 145 | 145 | object $binding, |
| 146 | 146 | string $alias, |
| 147 | - \Stringable|string|null $context, |
|
| 147 | + \Stringable | string | null $context, |
|
| 148 | 148 | array $arguments, |
| 149 | 149 | Tracer $tracer, |
| 150 | 150 | ): mixed { |
@@ -201,18 +201,18 @@ discard block |
||
| 201 | 201 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments, Tracer $tracer) |
| 202 | 202 | { |
| 203 | 203 | $context = $ctx->context; |
| 204 | - try { |
|
| 204 | + try{ |
|
| 205 | 205 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 206 | - } catch (\ReflectionException $e) { |
|
| 206 | + }catch (\ReflectionException $e){ |
|
| 207 | 207 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | $injector = $binding->injector; |
| 211 | 211 | |
| 212 | - try { |
|
| 212 | + try{ |
|
| 213 | 213 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 214 | 214 | |
| 215 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 215 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 216 | 216 | throw new InjectionException( |
| 217 | 217 | \sprintf( |
| 218 | 218 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | static $cache = []; |
| 227 | 227 | $extended = $cache[$injectorInstance::class] ??= ( |
| 228 | 228 | static fn(\ReflectionType $type): bool => |
| 229 | - $type::class === \ReflectionUnionType::class || (string) $type === 'mixed' |
|
| 229 | + $type::class === \ReflectionUnionType::class || (string)$type === 'mixed' |
|
| 230 | 230 | )( |
| 231 | 231 | ($refMethod = new \ReflectionMethod($injectorInstance, 'createInjection')) |
| 232 | 232 | ->getParameters()[1]->getType() |
@@ -236,10 +236,10 @@ discard block |
||
| 236 | 236 | $instance = $injectorInstance->createInjection($reflection, match (true) { |
| 237 | 237 | $asIs => $context, |
| 238 | 238 | $context instanceof \ReflectionParameter => $context->getName(), |
| 239 | - default => (string) $context, |
|
| 239 | + default => (string)$context, |
|
| 240 | 240 | }); |
| 241 | 241 | |
| 242 | - if (!$reflection->isInstance($instance)) { |
|
| 242 | + if (!$reflection->isInstance($instance)){ |
|
| 243 | 243 | throw new InjectionException( |
| 244 | 244 | \sprintf( |
| 245 | 245 | "Invalid injection response for '%s'.", |
@@ -249,12 +249,12 @@ discard block |
||
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | return $instance; |
| 252 | - } catch (TracedContainerException $e) { |
|
| 252 | + }catch (TracedContainerException $e){ |
|
| 253 | 253 | throw isset($injectorInstance) ? $e : $e::extendTracedException(\sprintf( |
| 254 | 254 | 'Can\'t resolve `%s`.', |
| 255 | 255 | $tracer->getRootAlias(), |
| 256 | 256 | ), $tracer->getTraces(), $e); |
| 257 | - } finally { |
|
| 257 | + }finally{ |
|
| 258 | 258 | $this->state->bindings[$ctx->class] ??= $binding; |
| 259 | 259 | } |
| 260 | 260 | } |
@@ -262,22 +262,22 @@ discard block |
||
| 262 | 262 | private function resolveAlias( |
| 263 | 263 | Config\Alias $binding, |
| 264 | 264 | string $alias, |
| 265 | - \Stringable|string|null $context, |
|
| 265 | + \Stringable | string | null $context, |
|
| 266 | 266 | array $arguments, |
| 267 | 267 | Tracer $tracer, |
| 268 | 268 | ): mixed { |
| 269 | - if ($binding->alias === $alias) { |
|
| 269 | + if ($binding->alias === $alias){ |
|
| 270 | 270 | $instance = $this->autowire( |
| 271 | 271 | new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton && $arguments === []), |
| 272 | 272 | $arguments, |
| 273 | 273 | $this, |
| 274 | 274 | $tracer, |
| 275 | 275 | ); |
| 276 | - } else { |
|
| 277 | - try { |
|
| 276 | + }else{ |
|
| 277 | + try{ |
|
| 278 | 278 | //Binding is pointing to something else |
| 279 | 279 | $instance = $this->factory->make($binding->alias, $arguments, $context); |
| 280 | - } catch (TracedContainerException $e) { |
|
| 280 | + }catch (TracedContainerException $e){ |
|
| 281 | 281 | throw $e::extendTracedException( |
| 282 | 282 | $alias === $tracer->getRootAlias() |
| 283 | 283 | ? "Can't resolve `{$alias}`." |
@@ -294,9 +294,9 @@ discard block |
||
| 294 | 294 | return $instance; |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | - private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
|
| 297 | + private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable | string | null $context): mixed |
|
| 298 | 298 | { |
| 299 | - if ($context instanceof RetryContext) { |
|
| 299 | + if ($context instanceof RetryContext){ |
|
| 300 | 300 | return $binding->fallbackFactory === null |
| 301 | 301 | ? throw new RecursiveProxyException( |
| 302 | 302 | $alias, |
@@ -307,7 +307,7 @@ discard block |
||
| 307 | 307 | |
| 308 | 308 | $result = Proxy::create(new \ReflectionClass($binding->getReturnClass()), $context, new Attribute\Proxy()); |
| 309 | 309 | |
| 310 | - if ($binding->singleton) { |
|
| 310 | + if ($binding->singleton){ |
|
| 311 | 311 | $this->state->singletons[$alias] = $result; |
| 312 | 312 | } |
| 313 | 313 | |
@@ -317,11 +317,11 @@ discard block |
||
| 317 | 317 | private function resolveShared( |
| 318 | 318 | Config\Shared $binding, |
| 319 | 319 | string $alias, |
| 320 | - \Stringable|string|null $context, |
|
| 320 | + \Stringable | string | null $context, |
|
| 321 | 321 | array $arguments, |
| 322 | 322 | Tracer $tracer, |
| 323 | 323 | ): object { |
| 324 | - if ($arguments !== []) { |
|
| 324 | + if ($arguments !== []){ |
|
| 325 | 325 | // Avoid singleton cache |
| 326 | 326 | return $this->createInstance( |
| 327 | 327 | new Ctx(alias: $alias, class: $binding->value::class, context: $context, singleton: false), |
@@ -331,7 +331,7 @@ discard block |
||
| 331 | 331 | ); |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - if ($binding->singleton) { |
|
| 334 | + if ($binding->singleton){ |
|
| 335 | 335 | $this->state->singletons[$alias] = $binding->value; |
| 336 | 336 | } |
| 337 | 337 | |
@@ -341,16 +341,16 @@ discard block |
||
| 341 | 341 | private function resolveAutowire( |
| 342 | 342 | Config\Autowire $binding, |
| 343 | 343 | string $alias, |
| 344 | - \Stringable|string|null $context, |
|
| 344 | + \Stringable | string | null $context, |
|
| 345 | 345 | array $arguments, |
| 346 | 346 | Tracer $tracer, |
| 347 | 347 | ): mixed { |
| 348 | 348 | $target = $binding->autowire->alias; |
| 349 | 349 | $ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 350 | 350 | |
| 351 | - if ($alias === $target) { |
|
| 351 | + if ($alias === $target){ |
|
| 352 | 352 | $instance = $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments), $this, $tracer); |
| 353 | - } else { |
|
| 353 | + }else{ |
|
| 354 | 354 | $instance = $binding->autowire->resolve($this->factory, $arguments); |
| 355 | 355 | $this->validateConstraint($instance, $ctx); |
| 356 | 356 | } |
@@ -359,30 +359,30 @@ discard block |
||
| 359 | 359 | } |
| 360 | 360 | |
| 361 | 361 | private function resolveFactory( |
| 362 | - Config\Factory|Config\DeferredFactory $binding, |
|
| 362 | + Config\Factory | Config\DeferredFactory $binding, |
|
| 363 | 363 | string $alias, |
| 364 | - \Stringable|string|null $context, |
|
| 364 | + \Stringable | string | null $context, |
|
| 365 | 365 | array $arguments, |
| 366 | 366 | Tracer $tracer, |
| 367 | 367 | ): mixed { |
| 368 | 368 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 369 | - try { |
|
| 369 | + try{ |
|
| 370 | 370 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 371 | 371 | ? ($binding->factory)() |
| 372 | 372 | : $this->invoker->invoke($binding->factory, $arguments); |
| 373 | - } catch (NotCallableException $e) { |
|
| 373 | + }catch (NotCallableException $e){ |
|
| 374 | 374 | throw TracedContainerException::createWithTrace( |
| 375 | 375 | \sprintf('Invalid callable binding for `%s`.', $ctx->alias), |
| 376 | 376 | $tracer->getTraces(), |
| 377 | 377 | $e, |
| 378 | 378 | ); |
| 379 | - } catch (TracedContainerException $e) { |
|
| 379 | + }catch (TracedContainerException $e){ |
|
| 380 | 380 | throw $e::extendTracedException( |
| 381 | 381 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
| 382 | 382 | $tracer->getTraces(), |
| 383 | 383 | $e, |
| 384 | 384 | ); |
| 385 | - } catch (\Throwable $e) { |
|
| 385 | + }catch (\Throwable $e){ |
|
| 386 | 386 | throw NotFoundException::createWithTrace( |
| 387 | 387 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
| 388 | 388 | $tracer->getTraces(), |
@@ -390,7 +390,7 @@ discard block |
||
| 390 | 390 | ); |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | - if (\is_object($instance)) { |
|
| 393 | + if (\is_object($instance)){ |
|
| 394 | 394 | $this->validateConstraint($instance, $ctx); |
| 395 | 395 | return $this->registerInstance($ctx, $instance); |
| 396 | 396 | } |
@@ -401,14 +401,14 @@ discard block |
||
| 401 | 401 | private function resolveWeakReference( |
| 402 | 402 | Config\WeakReference $binding, |
| 403 | 403 | string $alias, |
| 404 | - \Stringable|string|null $context, |
|
| 404 | + \Stringable | string | null $context, |
|
| 405 | 405 | array $arguments, |
| 406 | 406 | Tracer $tracer, |
| 407 | 407 | ): ?object { |
| 408 | 408 | $avoidCache = $arguments !== []; |
| 409 | 409 | |
| 410 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 411 | - try { |
|
| 410 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)){ |
|
| 411 | + try{ |
|
| 412 | 412 | $tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
| 413 | 413 | |
| 414 | 414 | $object = $this->createInstance( |
@@ -417,17 +417,17 @@ discard block |
||
| 417 | 417 | $this, |
| 418 | 418 | $tracer, |
| 419 | 419 | ); |
| 420 | - if ($avoidCache) { |
|
| 420 | + if ($avoidCache){ |
|
| 421 | 421 | return $object; |
| 422 | 422 | } |
| 423 | 423 | $binding->reference = \WeakReference::create($object); |
| 424 | - } catch (\Throwable) { |
|
| 424 | + }catch (\Throwable){ |
|
| 425 | 425 | throw ContainerException::createWithTrace(\sprintf( |
| 426 | 426 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 427 | 427 | $tracer->getRootAlias(), |
| 428 | 428 | $alias, |
| 429 | 429 | ), $tracer->getTraces()); |
| 430 | - } finally { |
|
| 430 | + }finally{ |
|
| 431 | 431 | $tracer->pop(); |
| 432 | 432 | } |
| 433 | 433 | } |
@@ -443,13 +443,13 @@ discard block |
||
| 443 | 443 | object $instance, |
| 444 | 444 | Ctx $ctx, |
| 445 | 445 | ): void { |
| 446 | - if ($this->options->checkScope) { |
|
| 446 | + if ($this->options->checkScope){ |
|
| 447 | 447 | // Check scope name |
| 448 | 448 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 449 | 449 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 450 | - if ($scopeName !== null) { |
|
| 450 | + if ($scopeName !== null){ |
|
| 451 | 451 | $scope = $this->scope; |
| 452 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 452 | + while ($scope->getScopeName() !== $scopeName){ |
|
| 453 | 453 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 454 | 454 | } |
| 455 | 455 | } |
@@ -476,26 +476,26 @@ discard block |
||
| 476 | 476 | Tracer $tracer, |
| 477 | 477 | ): object { |
| 478 | 478 | $class = $ctx->class; |
| 479 | - try { |
|
| 479 | + try{ |
|
| 480 | 480 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 481 | - } catch (\ReflectionException $e) { |
|
| 481 | + }catch (\ReflectionException $e){ |
|
| 482 | 482 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | 485 | // Check Scope attribute |
| 486 | 486 | $actor = $fallbackActor ?? $this; |
| 487 | - if ($this->options->checkScope) { # todo |
|
| 487 | + if ($this->options->checkScope){ # todo |
|
| 488 | 488 | $ar = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null); |
| 489 | - if ($ar !== null) { |
|
| 489 | + if ($ar !== null){ |
|
| 490 | 490 | /** @var Attribute\Scope $attr */ |
| 491 | 491 | $attr = $ar->newInstance(); |
| 492 | 492 | $scope = $this->scope; |
| 493 | 493 | $actor = $this; |
| 494 | 494 | // Go through all parent scopes |
| 495 | 495 | $needed = $actor; |
| 496 | - while ($attr->name !== $scope->getScopeName()) { |
|
| 496 | + while ($attr->name !== $scope->getScopeName()){ |
|
| 497 | 497 | $needed = $scope->getParentActor(); |
| 498 | - if ($needed === null) { |
|
| 498 | + if ($needed === null){ |
|
| 499 | 499 | throw new BadScopeException($attr->name, $class); |
| 500 | 500 | } |
| 501 | 501 | |
@@ -508,11 +508,11 @@ discard block |
||
| 508 | 508 | } # todo |
| 509 | 509 | |
| 510 | 510 | // We have to construct class using external injector when we know the exact context |
| 511 | - if ($arguments === [] && $actor->binder->hasInjector($class)) { |
|
| 511 | + if ($arguments === [] && $actor->binder->hasInjector($class)){ |
|
| 512 | 512 | return $actor->resolveInjector($actor->state->bindings[$ctx->class], $ctx, $arguments, $tracer); |
| 513 | 513 | } |
| 514 | 514 | |
| 515 | - if (!$reflection->isInstantiable()) { |
|
| 515 | + if (!$reflection->isInstantiable()){ |
|
| 516 | 516 | $itIs = match (true) { |
| 517 | 517 | $reflection->isEnum() => 'Enum', |
| 518 | 518 | $reflection->isAbstract() => 'Abstract class', |
@@ -526,8 +526,8 @@ discard block |
||
| 526 | 526 | |
| 527 | 527 | $constructor = $reflection->getConstructor(); |
| 528 | 528 | |
| 529 | - if ($constructor !== null) { |
|
| 530 | - try { |
|
| 529 | + if ($constructor !== null){ |
|
| 530 | + try{ |
|
| 531 | 531 | $newScope = $this !== $actor; |
| 532 | 532 | $debug = [ |
| 533 | 533 | 'action' => 'resolve arguments', |
@@ -541,39 +541,39 @@ discard block |
||
| 541 | 541 | $tracer->push($newScope, ...$debug); |
| 542 | 542 | $tracer->push(true); |
| 543 | 543 | $args = $actor->resolver->resolveArguments($constructor, $arguments, $actor->options->validateArguments); |
| 544 | - } catch (ValidationException $e) { |
|
| 544 | + }catch (ValidationException $e){ |
|
| 545 | 545 | throw TracedContainerException::createWithTrace(\sprintf( |
| 546 | 546 | 'Can\'t resolve `%s`. %s', |
| 547 | 547 | $tracer->getRootAlias(), |
| 548 | 548 | $e->getMessage(), |
| 549 | 549 | ), $tracer->getTraces()); |
| 550 | - } catch (TracedContainerException $e) { |
|
| 550 | + }catch (TracedContainerException $e){ |
|
| 551 | 551 | throw $e::extendTracedException(\sprintf( |
| 552 | 552 | 'Can\'t resolve `%s`.', |
| 553 | 553 | $tracer->getRootAlias(), |
| 554 | 554 | ), $tracer->getTraces(), $e); |
| 555 | - } finally { |
|
| 555 | + }finally{ |
|
| 556 | 556 | $tracer->pop($newScope); |
| 557 | 557 | $tracer->pop(false); |
| 558 | 558 | } |
| 559 | - try { |
|
| 559 | + try{ |
|
| 560 | 560 | // Using constructor with resolved arguments |
| 561 | 561 | $tracer->push(false, call: "$class::__construct", arguments: $args); |
| 562 | 562 | $tracer->push(true); |
| 563 | 563 | $instance = new $class(...$args); |
| 564 | - } catch (\TypeError $e) { |
|
| 564 | + }catch (\TypeError $e){ |
|
| 565 | 565 | throw new WrongTypeException($constructor, $e); |
| 566 | - } catch (TracedContainerException $e) { |
|
| 566 | + }catch (TracedContainerException $e){ |
|
| 567 | 567 | throw $e::extendTracedException(\sprintf( |
| 568 | 568 | 'Can\'t resolve `%s`: failed constructing `%s`.', |
| 569 | 569 | $tracer->getRootAlias(), |
| 570 | 570 | $class, |
| 571 | 571 | ), $tracer->getTraces(), $e); |
| 572 | - } finally { |
|
| 572 | + }finally{ |
|
| 573 | 573 | $tracer->pop(true); |
| 574 | 574 | $tracer->pop(false); |
| 575 | 575 | } |
| 576 | - } else { |
|
| 576 | + }else{ |
|
| 577 | 577 | // No constructor specified |
| 578 | 578 | $instance = $reflection->newInstance(); |
| 579 | 579 | } |
@@ -605,12 +605,12 @@ discard block |
||
| 605 | 605 | */ |
| 606 | 606 | private function isSingleton(Ctx $ctx): bool |
| 607 | 607 | { |
| 608 | - if (is_bool($ctx->singleton)) { |
|
| 608 | + if (is_bool($ctx->singleton)){ |
|
| 609 | 609 | return $ctx->singleton; |
| 610 | 610 | } |
| 611 | 611 | |
| 612 | 612 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 613 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 613 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)){ |
|
| 614 | 614 | return true; |
| 615 | 615 | } |
| 616 | 616 | |
@@ -624,7 +624,7 @@ discard block |
||
| 624 | 624 | * @var Attribute\Finalize|null $attribute |
| 625 | 625 | */ |
| 626 | 626 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 627 | - if ($attribute === null) { |
|
| 627 | + if ($attribute === null){ |
|
| 628 | 628 | return null; |
| 629 | 629 | } |
| 630 | 630 | |
@@ -638,10 +638,10 @@ discard block |
||
| 638 | 638 | { |
| 639 | 639 | $scope = $this->scope; |
| 640 | 640 | |
| 641 | - while ($scope !== null) { |
|
| 642 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 643 | - if ($instance instanceof $class) { |
|
| 644 | - foreach ($inflectors as $inflector) { |
|
| 641 | + while ($scope !== null){ |
|
| 642 | + foreach ($this->state->inflectors as $class => $inflectors){ |
|
| 643 | + if ($instance instanceof $class){ |
|
| 644 | + foreach ($inflectors as $inflector){ |
|
| 645 | 645 | $instance = $inflector->getParametersCount() > 1 |
| 646 | 646 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 647 | 647 | : ($inflector->inflector)($instance); |
@@ -657,9 +657,9 @@ discard block |
||
| 657 | 657 | |
| 658 | 658 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 659 | 659 | { |
| 660 | - try { |
|
| 660 | + try{ |
|
| 661 | 661 | $this->resolver->validateArguments($reflection, $arguments); |
| 662 | - } catch (\Throwable) { |
|
| 662 | + }catch (\Throwable){ |
|
| 663 | 663 | return false; |
| 664 | 664 | } |
| 665 | 665 | |
@@ -95,20 +95,25 @@ discard block |
||
| 95 | 95 | // Aliases to prevent circular dependencies |
| 96 | 96 | $as = []; |
| 97 | 97 | $actor = $this; |
| 98 | - do { |
|
| 98 | + do |
|
| 99 | + { |
|
| 99 | 100 | $bindings = &$actor->state->bindings; |
| 100 | 101 | $singletons = &$actor->state->singletons; |
| 101 | 102 | $injectors = &$actor->state->injectors; |
| 102 | 103 | $binding = $bindings[$alias] ?? null; |
| 103 | - if (\array_key_exists($alias, $singletons)) { |
|
| 104 | + if (\array_key_exists($alias, $singletons)) |
|
| 105 | + { |
|
| 104 | 106 | $singleton = $singletons[$alias]; |
| 105 | 107 | $injector = $injectors[$alias] ?? null; |
| 106 | 108 | return \is_object($singleton::class) ? $singleton::class : null; |
| 107 | 109 | } |
| 108 | 110 | |
| 109 | - if ($binding !== null) { |
|
| 110 | - if ($followAlias && $binding::class === Alias::class) { |
|
| 111 | - if ($binding->alias === $alias) { |
|
| 111 | + if ($binding !== null) |
|
| 112 | + { |
|
| 113 | + if ($followAlias && $binding::class === Alias::class) |
|
| 114 | + { |
|
| 115 | + if ($binding->alias === $alias) |
|
| 116 | + { |
|
| 112 | 117 | break; |
| 113 | 118 | } |
| 114 | 119 | |
@@ -123,7 +128,8 @@ discard block |
||
| 123 | 128 | return $binding->getReturnClass(); |
| 124 | 129 | } |
| 125 | 130 | |
| 126 | - if (\array_key_exists($alias, $injectors)) { |
|
| 131 | + if (\array_key_exists($alias, $injectors)) |
|
| 132 | + { |
|
| 127 | 133 | $injector = $injectors[$alias]; |
| 128 | 134 | $binding = $bindings[$alias] ?? null; |
| 129 | 135 | return $alias; |
@@ -131,7 +137,8 @@ discard block |
||
| 131 | 137 | |
| 132 | 138 | // Go to parent scope |
| 133 | 139 | $parent = $actor->scope->getParentActor(); |
| 134 | - if ($parent === null) { |
|
| 140 | + if ($parent === null) |
|
| 141 | + { |
|
| 135 | 142 | break; |
| 136 | 143 | } |
| 137 | 144 | |
@@ -201,18 +208,23 @@ discard block |
||
| 201 | 208 | private function resolveInjector(Config\Injectable $binding, Ctx $ctx, array $arguments, Tracer $tracer) |
| 202 | 209 | { |
| 203 | 210 | $context = $ctx->context; |
| 204 | - try { |
|
| 211 | + try |
|
| 212 | + { |
|
| 205 | 213 | $reflection = $ctx->reflection ??= new \ReflectionClass($ctx->class); |
| 206 | - } catch (\ReflectionException $e) { |
|
| 214 | + } |
|
| 215 | + catch (\ReflectionException $e) |
|
| 216 | + { |
|
| 207 | 217 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 208 | 218 | } |
| 209 | 219 | |
| 210 | 220 | $injector = $binding->injector; |
| 211 | 221 | |
| 212 | - try { |
|
| 222 | + try |
|
| 223 | + { |
|
| 213 | 224 | $injectorInstance = \is_object($injector) ? $injector : $this->container->get($injector); |
| 214 | 225 | |
| 215 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 226 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 227 | + { |
|
| 216 | 228 | throw new InjectionException( |
| 217 | 229 | \sprintf( |
| 218 | 230 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -239,7 +251,8 @@ discard block |
||
| 239 | 251 | default => (string) $context, |
| 240 | 252 | }); |
| 241 | 253 | |
| 242 | - if (!$reflection->isInstance($instance)) { |
|
| 254 | + if (!$reflection->isInstance($instance)) |
|
| 255 | + { |
|
| 243 | 256 | throw new InjectionException( |
| 244 | 257 | \sprintf( |
| 245 | 258 | "Invalid injection response for '%s'.", |
@@ -249,12 +262,16 @@ discard block |
||
| 249 | 262 | } |
| 250 | 263 | |
| 251 | 264 | return $instance; |
| 252 | - } catch (TracedContainerException $e) { |
|
| 265 | + } |
|
| 266 | + catch (TracedContainerException $e) |
|
| 267 | + { |
|
| 253 | 268 | throw isset($injectorInstance) ? $e : $e::extendTracedException(\sprintf( |
| 254 | 269 | 'Can\'t resolve `%s`.', |
| 255 | 270 | $tracer->getRootAlias(), |
| 256 | 271 | ), $tracer->getTraces(), $e); |
| 257 | - } finally { |
|
| 272 | + } |
|
| 273 | + finally |
|
| 274 | + { |
|
| 258 | 275 | $this->state->bindings[$ctx->class] ??= $binding; |
| 259 | 276 | } |
| 260 | 277 | } |
@@ -266,18 +283,24 @@ discard block |
||
| 266 | 283 | array $arguments, |
| 267 | 284 | Tracer $tracer, |
| 268 | 285 | ): mixed { |
| 269 | - if ($binding->alias === $alias) { |
|
| 286 | + if ($binding->alias === $alias) |
|
| 287 | + { |
|
| 270 | 288 | $instance = $this->autowire( |
| 271 | 289 | new Ctx(alias: $alias, class: $binding->alias, context: $context, singleton: $binding->singleton && $arguments === []), |
| 272 | 290 | $arguments, |
| 273 | 291 | $this, |
| 274 | 292 | $tracer, |
| 275 | 293 | ); |
| 276 | - } else { |
|
| 277 | - try { |
|
| 294 | + } |
|
| 295 | + else |
|
| 296 | + { |
|
| 297 | + try |
|
| 298 | + { |
|
| 278 | 299 | //Binding is pointing to something else |
| 279 | 300 | $instance = $this->factory->make($binding->alias, $arguments, $context); |
| 280 | - } catch (TracedContainerException $e) { |
|
| 301 | + } |
|
| 302 | + catch (TracedContainerException $e) |
|
| 303 | + { |
|
| 281 | 304 | throw $e::extendTracedException( |
| 282 | 305 | $alias === $tracer->getRootAlias() |
| 283 | 306 | ? "Can't resolve `{$alias}`." |
@@ -296,7 +319,8 @@ discard block |
||
| 296 | 319 | |
| 297 | 320 | private function resolveProxy(Config\Proxy $binding, string $alias, \Stringable|string|null $context): mixed |
| 298 | 321 | { |
| 299 | - if ($context instanceof RetryContext) { |
|
| 322 | + if ($context instanceof RetryContext) |
|
| 323 | + { |
|
| 300 | 324 | return $binding->fallbackFactory === null |
| 301 | 325 | ? throw new RecursiveProxyException( |
| 302 | 326 | $alias, |
@@ -307,7 +331,8 @@ discard block |
||
| 307 | 331 | |
| 308 | 332 | $result = Proxy::create(new \ReflectionClass($binding->getReturnClass()), $context, new Attribute\Proxy()); |
| 309 | 333 | |
| 310 | - if ($binding->singleton) { |
|
| 334 | + if ($binding->singleton) |
|
| 335 | + { |
|
| 311 | 336 | $this->state->singletons[$alias] = $result; |
| 312 | 337 | } |
| 313 | 338 | |
@@ -321,7 +346,8 @@ discard block |
||
| 321 | 346 | array $arguments, |
| 322 | 347 | Tracer $tracer, |
| 323 | 348 | ): object { |
| 324 | - if ($arguments !== []) { |
|
| 349 | + if ($arguments !== []) |
|
| 350 | + { |
|
| 325 | 351 | // Avoid singleton cache |
| 326 | 352 | return $this->createInstance( |
| 327 | 353 | new Ctx(alias: $alias, class: $binding->value::class, context: $context, singleton: false), |
@@ -331,7 +357,8 @@ discard block |
||
| 331 | 357 | ); |
| 332 | 358 | } |
| 333 | 359 | |
| 334 | - if ($binding->singleton) { |
|
| 360 | + if ($binding->singleton) |
|
| 361 | + { |
|
| 335 | 362 | $this->state->singletons[$alias] = $binding->value; |
| 336 | 363 | } |
| 337 | 364 | |
@@ -348,9 +375,12 @@ discard block |
||
| 348 | 375 | $target = $binding->autowire->alias; |
| 349 | 376 | $ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 350 | 377 | |
| 351 | - if ($alias === $target) { |
|
| 378 | + if ($alias === $target) |
|
| 379 | + { |
|
| 352 | 380 | $instance = $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments), $this, $tracer); |
| 353 | - } else { |
|
| 381 | + } |
|
| 382 | + else |
|
| 383 | + { |
|
| 354 | 384 | $instance = $binding->autowire->resolve($this->factory, $arguments); |
| 355 | 385 | $this->validateConstraint($instance, $ctx); |
| 356 | 386 | } |
@@ -366,23 +396,30 @@ discard block |
||
| 366 | 396 | Tracer $tracer, |
| 367 | 397 | ): mixed { |
| 368 | 398 | $ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton && $arguments === [] ?: null); |
| 369 | - try { |
|
| 399 | + try |
|
| 400 | + { |
|
| 370 | 401 | $instance = $binding::class === Config\Factory::class && $binding->getParametersCount() === 0 |
| 371 | 402 | ? ($binding->factory)() |
| 372 | 403 | : $this->invoker->invoke($binding->factory, $arguments); |
| 373 | - } catch (NotCallableException $e) { |
|
| 404 | + } |
|
| 405 | + catch (NotCallableException $e) |
|
| 406 | + { |
|
| 374 | 407 | throw TracedContainerException::createWithTrace( |
| 375 | 408 | \sprintf('Invalid callable binding for `%s`.', $ctx->alias), |
| 376 | 409 | $tracer->getTraces(), |
| 377 | 410 | $e, |
| 378 | 411 | ); |
| 379 | - } catch (TracedContainerException $e) { |
|
| 412 | + } |
|
| 413 | + catch (TracedContainerException $e) |
|
| 414 | + { |
|
| 380 | 415 | throw $e::extendTracedException( |
| 381 | 416 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
| 382 | 417 | $tracer->getTraces(), |
| 383 | 418 | $e, |
| 384 | 419 | ); |
| 385 | - } catch (\Throwable $e) { |
|
| 420 | + } |
|
| 421 | + catch (\Throwable $e) |
|
| 422 | + { |
|
| 386 | 423 | throw NotFoundException::createWithTrace( |
| 387 | 424 | \sprintf("Can't resolve `%s`: factory invocation failed.", $tracer->getRootAlias()), |
| 388 | 425 | $tracer->getTraces(), |
@@ -390,7 +427,8 @@ discard block |
||
| 390 | 427 | ); |
| 391 | 428 | } |
| 392 | 429 | |
| 393 | - if (\is_object($instance)) { |
|
| 430 | + if (\is_object($instance)) |
|
| 431 | + { |
|
| 394 | 432 | $this->validateConstraint($instance, $ctx); |
| 395 | 433 | return $this->registerInstance($ctx, $instance); |
| 396 | 434 | } |
@@ -407,8 +445,10 @@ discard block |
||
| 407 | 445 | ): ?object { |
| 408 | 446 | $avoidCache = $arguments !== []; |
| 409 | 447 | |
| 410 | - if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) { |
|
| 411 | - try { |
|
| 448 | + if (($avoidCache || $binding->reference->get() === null) && \class_exists($alias)) |
|
| 449 | + { |
|
| 450 | + try |
|
| 451 | + { |
|
| 412 | 452 | $tracer->push(false, alias: $alias, source: \WeakReference::class, context: $context); |
| 413 | 453 | |
| 414 | 454 | $object = $this->createInstance( |
@@ -417,17 +457,22 @@ discard block |
||
| 417 | 457 | $this, |
| 418 | 458 | $tracer, |
| 419 | 459 | ); |
| 420 | - if ($avoidCache) { |
|
| 460 | + if ($avoidCache) |
|
| 461 | + { |
|
| 421 | 462 | return $object; |
| 422 | 463 | } |
| 423 | 464 | $binding->reference = \WeakReference::create($object); |
| 424 | - } catch (\Throwable) { |
|
| 465 | + } |
|
| 466 | + catch (\Throwable) |
|
| 467 | + { |
|
| 425 | 468 | throw ContainerException::createWithTrace(\sprintf( |
| 426 | 469 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 427 | 470 | $tracer->getRootAlias(), |
| 428 | 471 | $alias, |
| 429 | 472 | ), $tracer->getTraces()); |
| 430 | - } finally { |
|
| 473 | + } |
|
| 474 | + finally |
|
| 475 | + { |
|
| 431 | 476 | $tracer->pop(); |
| 432 | 477 | } |
| 433 | 478 | } |
@@ -443,13 +488,16 @@ discard block |
||
| 443 | 488 | object $instance, |
| 444 | 489 | Ctx $ctx, |
| 445 | 490 | ): void { |
| 446 | - if ($this->options->checkScope) { |
|
| 491 | + if ($this->options->checkScope) |
|
| 492 | + { |
|
| 447 | 493 | // Check scope name |
| 448 | 494 | $ctx->reflection ??= new \ReflectionClass($instance); |
| 449 | 495 | $scopeName = ($ctx->reflection->getAttributes(Attribute\Scope::class)[0] ?? null)?->newInstance()->name; |
| 450 | - if ($scopeName !== null) { |
|
| 496 | + if ($scopeName !== null) |
|
| 497 | + { |
|
| 451 | 498 | $scope = $this->scope; |
| 452 | - while ($scope->getScopeName() !== $scopeName) { |
|
| 499 | + while ($scope->getScopeName() !== $scopeName) |
|
| 500 | + { |
|
| 453 | 501 | $scope = $scope->getParentScope() ?? throw new BadScopeException($scopeName, $instance::class); |
| 454 | 502 | } |
| 455 | 503 | } |
@@ -476,26 +524,34 @@ discard block |
||
| 476 | 524 | Tracer $tracer, |
| 477 | 525 | ): object { |
| 478 | 526 | $class = $ctx->class; |
| 479 | - try { |
|
| 527 | + try |
|
| 528 | + { |
|
| 480 | 529 | $ctx->reflection = $reflection = new \ReflectionClass($class); |
| 481 | - } catch (\ReflectionException $e) { |
|
| 530 | + } |
|
| 531 | + catch (\ReflectionException $e) |
|
| 532 | + { |
|
| 482 | 533 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 483 | 534 | } |
| 484 | 535 | |
| 485 | 536 | // Check Scope attribute |
| 486 | 537 | $actor = $fallbackActor ?? $this; |
| 487 | - if ($this->options->checkScope) { # todo |
|
| 538 | + if ($this->options->checkScope) |
|
| 539 | + { |
|
| 540 | +# todo |
|
| 488 | 541 | $ar = ($reflection->getAttributes(Attribute\Scope::class)[0] ?? null); |
| 489 | - if ($ar !== null) { |
|
| 542 | + if ($ar !== null) |
|
| 543 | + { |
|
| 490 | 544 | /** @var Attribute\Scope $attr */ |
| 491 | 545 | $attr = $ar->newInstance(); |
| 492 | 546 | $scope = $this->scope; |
| 493 | 547 | $actor = $this; |
| 494 | 548 | // Go through all parent scopes |
| 495 | 549 | $needed = $actor; |
| 496 | - while ($attr->name !== $scope->getScopeName()) { |
|
| 550 | + while ($attr->name !== $scope->getScopeName()) |
|
| 551 | + { |
|
| 497 | 552 | $needed = $scope->getParentActor(); |
| 498 | - if ($needed === null) { |
|
| 553 | + if ($needed === null) |
|
| 554 | + { |
|
| 499 | 555 | throw new BadScopeException($attr->name, $class); |
| 500 | 556 | } |
| 501 | 557 | |
@@ -508,11 +564,13 @@ discard block |
||
| 508 | 564 | } # todo |
| 509 | 565 | |
| 510 | 566 | // We have to construct class using external injector when we know the exact context |
| 511 | - if ($arguments === [] && $actor->binder->hasInjector($class)) { |
|
| 567 | + if ($arguments === [] && $actor->binder->hasInjector($class)) |
|
| 568 | + { |
|
| 512 | 569 | return $actor->resolveInjector($actor->state->bindings[$ctx->class], $ctx, $arguments, $tracer); |
| 513 | 570 | } |
| 514 | 571 | |
| 515 | - if (!$reflection->isInstantiable()) { |
|
| 572 | + if (!$reflection->isInstantiable()) |
|
| 573 | + { |
|
| 516 | 574 | $itIs = match (true) { |
| 517 | 575 | $reflection->isEnum() => 'Enum', |
| 518 | 576 | $reflection->isAbstract() => 'Abstract class', |
@@ -526,8 +584,10 @@ discard block |
||
| 526 | 584 | |
| 527 | 585 | $constructor = $reflection->getConstructor(); |
| 528 | 586 | |
| 529 | - if ($constructor !== null) { |
|
| 530 | - try { |
|
| 587 | + if ($constructor !== null) |
|
| 588 | + { |
|
| 589 | + try |
|
| 590 | + { |
|
| 531 | 591 | $newScope = $this !== $actor; |
| 532 | 592 | $debug = [ |
| 533 | 593 | 'action' => 'resolve arguments', |
@@ -541,39 +601,54 @@ discard block |
||
| 541 | 601 | $tracer->push($newScope, ...$debug); |
| 542 | 602 | $tracer->push(true); |
| 543 | 603 | $args = $actor->resolver->resolveArguments($constructor, $arguments, $actor->options->validateArguments); |
| 544 | - } catch (ValidationException $e) { |
|
| 604 | + } |
|
| 605 | + catch (ValidationException $e) |
|
| 606 | + { |
|
| 545 | 607 | throw TracedContainerException::createWithTrace(\sprintf( |
| 546 | 608 | 'Can\'t resolve `%s`. %s', |
| 547 | 609 | $tracer->getRootAlias(), |
| 548 | 610 | $e->getMessage(), |
| 549 | 611 | ), $tracer->getTraces()); |
| 550 | - } catch (TracedContainerException $e) { |
|
| 612 | + } |
|
| 613 | + catch (TracedContainerException $e) |
|
| 614 | + { |
|
| 551 | 615 | throw $e::extendTracedException(\sprintf( |
| 552 | 616 | 'Can\'t resolve `%s`.', |
| 553 | 617 | $tracer->getRootAlias(), |
| 554 | 618 | ), $tracer->getTraces(), $e); |
| 555 | - } finally { |
|
| 619 | + } |
|
| 620 | + finally |
|
| 621 | + { |
|
| 556 | 622 | $tracer->pop($newScope); |
| 557 | 623 | $tracer->pop(false); |
| 558 | 624 | } |
| 559 | - try { |
|
| 625 | + try |
|
| 626 | + { |
|
| 560 | 627 | // Using constructor with resolved arguments |
| 561 | 628 | $tracer->push(false, call: "$class::__construct", arguments: $args); |
| 562 | 629 | $tracer->push(true); |
| 563 | 630 | $instance = new $class(...$args); |
| 564 | - } catch (\TypeError $e) { |
|
| 631 | + } |
|
| 632 | + catch (\TypeError $e) |
|
| 633 | + { |
|
| 565 | 634 | throw new WrongTypeException($constructor, $e); |
| 566 | - } catch (TracedContainerException $e) { |
|
| 635 | + } |
|
| 636 | + catch (TracedContainerException $e) |
|
| 637 | + { |
|
| 567 | 638 | throw $e::extendTracedException(\sprintf( |
| 568 | 639 | 'Can\'t resolve `%s`: failed constructing `%s`.', |
| 569 | 640 | $tracer->getRootAlias(), |
| 570 | 641 | $class, |
| 571 | 642 | ), $tracer->getTraces(), $e); |
| 572 | - } finally { |
|
| 643 | + } |
|
| 644 | + finally |
|
| 645 | + { |
|
| 573 | 646 | $tracer->pop(true); |
| 574 | 647 | $tracer->pop(false); |
| 575 | 648 | } |
| 576 | - } else { |
|
| 649 | + } |
|
| 650 | + else |
|
| 651 | + { |
|
| 577 | 652 | // No constructor specified |
| 578 | 653 | $instance = $reflection->newInstance(); |
| 579 | 654 | } |
@@ -605,12 +680,14 @@ discard block |
||
| 605 | 680 | */ |
| 606 | 681 | private function isSingleton(Ctx $ctx): bool |
| 607 | 682 | { |
| 608 | - if (is_bool($ctx->singleton)) { |
|
| 683 | + if (is_bool($ctx->singleton)) |
|
| 684 | + { |
|
| 609 | 685 | return $ctx->singleton; |
| 610 | 686 | } |
| 611 | 687 | |
| 612 | 688 | /** @psalm-suppress RedundantCondition https://github.com/vimeo/psalm/issues/9489 */ |
| 613 | - if ($ctx->reflection->implementsInterface(SingletonInterface::class)) { |
|
| 689 | + if ($ctx->reflection->implementsInterface(SingletonInterface::class)) |
|
| 690 | + { |
|
| 614 | 691 | return true; |
| 615 | 692 | } |
| 616 | 693 | |
@@ -624,7 +701,8 @@ discard block |
||
| 624 | 701 | * @var Attribute\Finalize|null $attribute |
| 625 | 702 | */ |
| 626 | 703 | $attribute = ($ctx->reflection->getAttributes(Attribute\Finalize::class)[0] ?? null)?->newInstance(); |
| 627 | - if ($attribute === null) { |
|
| 704 | + if ($attribute === null) |
|
| 705 | + { |
|
| 628 | 706 | return null; |
| 629 | 707 | } |
| 630 | 708 | |
@@ -638,10 +716,14 @@ discard block |
||
| 638 | 716 | { |
| 639 | 717 | $scope = $this->scope; |
| 640 | 718 | |
| 641 | - while ($scope !== null) { |
|
| 642 | - foreach ($this->state->inflectors as $class => $inflectors) { |
|
| 643 | - if ($instance instanceof $class) { |
|
| 644 | - foreach ($inflectors as $inflector) { |
|
| 719 | + while ($scope !== null) |
|
| 720 | + { |
|
| 721 | + foreach ($this->state->inflectors as $class => $inflectors) |
|
| 722 | + { |
|
| 723 | + if ($instance instanceof $class) |
|
| 724 | + { |
|
| 725 | + foreach ($inflectors as $inflector) |
|
| 726 | + { |
|
| 645 | 727 | $instance = $inflector->getParametersCount() > 1 |
| 646 | 728 | ? $this->invoker->invoke($inflector->inflector, [$instance]) |
| 647 | 729 | : ($inflector->inflector)($instance); |
@@ -657,9 +739,12 @@ discard block |
||
| 657 | 739 | |
| 658 | 740 | private function validateArguments(ContextFunction $reflection, array $arguments = []): bool |
| 659 | 741 | { |
| 660 | - try { |
|
| 742 | + try |
|
| 743 | + { |
|
| 661 | 744 | $this->resolver->validateArguments($reflection, $arguments); |
| 662 | - } catch (\Throwable) { |
|
| 745 | + } |
|
| 746 | + catch (\Throwable) |
|
| 747 | + { |
|
| 663 | 748 | return false; |
| 664 | 749 | } |
| 665 | 750 | |