@@ -38,9 +38,9 @@ discard block |
||
| 38 | 38 | * |
| 39 | 39 | * @psalm-param TResolver|object $resolver |
| 40 | 40 | */ |
| 41 | - public function bind(string $alias, string|array|callable|object $resolver): void |
|
| 41 | + public function bind(string $alias, string | array | callable | object $resolver): void |
|
| 42 | 42 | { |
| 43 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
| 43 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire){ |
|
| 44 | 44 | // array means = execute me, false = not singleton |
| 45 | 45 | $this->state->bindings[$alias] = [$resolver, false]; |
| 46 | 46 | |
@@ -57,9 +57,9 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @param TResolver|object $resolver |
| 59 | 59 | */ |
| 60 | - public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
|
| 60 | + public function bindSingleton(string $alias, string | array | callable | object $resolver): void |
|
| 61 | 61 | { |
| 62 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
| 62 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire){ |
|
| 63 | 63 | // direct binding to an instance |
| 64 | 64 | $this->state->bindings[$alias] = $resolver; |
| 65 | 65 | |
@@ -71,12 +71,12 @@ discard block |
||
| 71 | 71 | |
| 72 | 72 | public function hasInstance(string $alias): bool |
| 73 | 73 | { |
| 74 | - if (!$this->container->has($alias)) { |
|
| 74 | + if (!$this->container->has($alias)){ |
|
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | $bindings = &$this->state->bindings; |
| 78 | 78 | |
| 79 | - while (\is_string($bindings[$alias] ?? null)) { |
|
| 79 | + while (\is_string($bindings[$alias] ?? null)){ |
|
| 80 | 80 | //Checking alias tree |
| 81 | 81 | $alias = $bindings[$alias]; |
| 82 | 82 | } |
@@ -101,31 +101,31 @@ discard block |
||
| 101 | 101 | |
| 102 | 102 | public function hasInjector(string $class): bool |
| 103 | 103 | { |
| 104 | - try { |
|
| 104 | + try{ |
|
| 105 | 105 | $reflection = new \ReflectionClass($class); |
| 106 | - } catch (\ReflectionException $e) { |
|
| 106 | + }catch (\ReflectionException $e){ |
|
| 107 | 107 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 110 | + if (\array_key_exists($class, $this->state->injectors)){ |
|
| 111 | 111 | return $this->state->injectors[$class] !== null; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | 114 | if ( |
| 115 | 115 | $reflection->implementsInterface(InjectableInterface::class) |
| 116 | 116 | && $reflection->hasConstant('INJECTOR') |
| 117 | - ) { |
|
| 117 | + ){ |
|
| 118 | 118 | $this->state->injectors[$class] = $reflection->getConstant('INJECTOR'); |
| 119 | 119 | |
| 120 | 120 | return true; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | // check interfaces |
| 124 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 124 | + foreach ($this->state->injectors as $target => $injector){ |
|
| 125 | 125 | if ( |
| 126 | 126 | \class_exists($target, true) |
| 127 | 127 | && $reflection->isSubclassOf($target) |
| 128 | - ) { |
|
| 128 | + ){ |
|
| 129 | 129 | $this->state->injectors[$class] = $injector; |
| 130 | 130 | |
| 131 | 131 | return true; |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | if ( |
| 135 | 135 | \interface_exists($target, true) |
| 136 | 136 | && $reflection->implementsInterface($target) |
| 137 | - ) { |
|
| 137 | + ){ |
|
| 138 | 138 | $this->state->injectors[$class] = $injector; |
| 139 | 139 | |
| 140 | 140 | return true; |
@@ -40,7 +40,8 @@ discard block |
||
| 40 | 40 | */ |
| 41 | 41 | public function bind(string $alias, string|array|callable|object $resolver): void |
| 42 | 42 | { |
| 43 | - if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) { |
|
| 43 | + if (\is_array($resolver) || $resolver instanceof \Closure || $resolver instanceof Autowire) |
|
| 44 | + { |
|
| 44 | 45 | // array means = execute me, false = not singleton |
| 45 | 46 | $this->state->bindings[$alias] = [$resolver, false]; |
| 46 | 47 | |
@@ -59,7 +60,8 @@ discard block |
||
| 59 | 60 | */ |
| 60 | 61 | public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
| 61 | 62 | { |
| 62 | - if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) { |
|
| 63 | + if (\is_object($resolver) && !$resolver instanceof \Closure && !$resolver instanceof Autowire) |
|
| 64 | + { |
|
| 63 | 65 | // direct binding to an instance |
| 64 | 66 | $this->state->bindings[$alias] = $resolver; |
| 65 | 67 | |
@@ -71,12 +73,14 @@ discard block |
||
| 71 | 73 | |
| 72 | 74 | public function hasInstance(string $alias): bool |
| 73 | 75 | { |
| 74 | - if (!$this->container->has($alias)) { |
|
| 76 | + if (!$this->container->has($alias)) |
|
| 77 | + { |
|
| 75 | 78 | return false; |
| 76 | 79 | } |
| 77 | 80 | $bindings = &$this->state->bindings; |
| 78 | 81 | |
| 79 | - while (\is_string($bindings[$alias] ?? null)) { |
|
| 82 | + while (\is_string($bindings[$alias] ?? null)) |
|
| 83 | + { |
|
| 80 | 84 | //Checking alias tree |
| 81 | 85 | $alias = $bindings[$alias]; |
| 82 | 86 | } |
@@ -101,13 +105,17 @@ discard block |
||
| 101 | 105 | |
| 102 | 106 | public function hasInjector(string $class): bool |
| 103 | 107 | { |
| 104 | - try { |
|
| 108 | + try |
|
| 109 | + { |
|
| 105 | 110 | $reflection = new \ReflectionClass($class); |
| 106 | - } catch (\ReflectionException $e) { |
|
| 111 | + } |
|
| 112 | + catch (\ReflectionException $e) |
|
| 113 | + { |
|
| 107 | 114 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 108 | 115 | } |
| 109 | 116 | |
| 110 | - if (\array_key_exists($class, $this->state->injectors)) { |
|
| 117 | + if (\array_key_exists($class, $this->state->injectors)) |
|
| 118 | + { |
|
| 111 | 119 | return $this->state->injectors[$class] !== null; |
| 112 | 120 | } |
| 113 | 121 | |
@@ -121,7 +129,8 @@ discard block |
||
| 121 | 129 | } |
| 122 | 130 | |
| 123 | 131 | // check interfaces |
| 124 | - foreach ($this->state->injectors as $target => $injector) { |
|
| 132 | + foreach ($this->state->injectors as $target => $injector) |
|
| 133 | + { |
|
| 125 | 134 | if ( |
| 126 | 135 | \class_exists($target, true) |
| 127 | 136 | && $reflection->isSubclassOf($target) |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | ): array { |
| 50 | 50 | $state = new ResolvingState($reflection, $parameters); |
| 51 | 51 | |
| 52 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 52 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 53 | 53 | $this->resolveParameter($parameter, $state, $validate) |
| 54 | 54 | or |
| 55 | 55 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -63,47 +63,47 @@ discard block |
||
| 63 | 63 | $positional = true; |
| 64 | 64 | $variadic = false; |
| 65 | 65 | $parameters = $reflection->getParameters(); |
| 66 | - if (\count($parameters) === 0) { |
|
| 66 | + if (\count($parameters) === 0){ |
|
| 67 | 67 | return; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | $parameter = null; |
| 71 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 71 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
| 72 | 72 | // get related argument value |
| 73 | 73 | $key = \key($arguments); |
| 74 | 74 | |
| 75 | 75 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 76 | 76 | // But you can't send positional argument after named in any case |
| 77 | - if (\is_int($key) && !$positional) { |
|
| 77 | + if (\is_int($key) && !$positional){ |
|
| 78 | 78 | throw new PositionalArgumentException($reflection, $key); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | $positional = $positional && \is_int($key); |
| 82 | 82 | |
| 83 | - if (!$variadic) { |
|
| 83 | + if (!$variadic){ |
|
| 84 | 84 | $parameter = \array_shift($parameters); |
| 85 | 85 | $variadic = $parameter?->isVariadic() ?? false; |
| 86 | 86 | } |
| 87 | 87 | |
| 88 | - if ($parameter === null) { |
|
| 88 | + if ($parameter === null){ |
|
| 89 | 89 | throw new UnknownParameterException($reflection, $key); |
| 90 | 90 | } |
| 91 | 91 | $name = $parameter->getName(); |
| 92 | 92 | |
| 93 | - if (($positional || $variadic) && $key !== null) { |
|
| 93 | + if (($positional || $variadic) && $key !== null){ |
|
| 94 | 94 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 95 | 95 | $value = \array_shift($arguments); |
| 96 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 97 | - if ($parameter->isOptional()) { |
|
| 96 | + } elseif ($key === null || !\array_key_exists($name, $arguments)){ |
|
| 97 | + if ($parameter->isOptional()){ |
|
| 98 | 98 | continue; |
| 99 | 99 | } |
| 100 | 100 | throw new MissingRequiredArgumentException($reflection, $name); |
| 101 | - } else { |
|
| 101 | + }else{ |
|
| 102 | 102 | $value = &$arguments[$name]; |
| 103 | 103 | unset($arguments[$name]); |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 106 | + if (!$this->validateValueToParameter($parameter, $value)){ |
|
| 107 | 107 | throw new InvalidArgumentException($reflection, $name); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | |
| 112 | 112 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 113 | 113 | { |
| 114 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 114 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)){ |
|
| 115 | 115 | return true; |
| 116 | 116 | } |
| 117 | 117 | $type = $parameter->getType(); |
@@ -122,17 +122,17 @@ discard block |
||
| 122 | 122 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 123 | 123 | }; |
| 124 | 124 | |
| 125 | - foreach ($types as $t) { |
|
| 125 | + foreach ($types as $t){ |
|
| 126 | 126 | \assert($t instanceof ReflectionNamedType); |
| 127 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 127 | + if (!$this->validateValueNamedType($t, $value)){ |
|
| 128 | 128 | // If it is TypeIntersection |
| 129 | - if ($or) { |
|
| 129 | + if ($or){ |
|
| 130 | 130 | continue; |
| 131 | 131 | } |
| 132 | 132 | return false; |
| 133 | 133 | } |
| 134 | 134 | // If it is not type intersection then we can skip that value after first successful check |
| 135 | - if ($or) { |
|
| 135 | + if ($or){ |
|
| 136 | 136 | return true; |
| 137 | 137 | } |
| 138 | 138 | } |
@@ -147,7 +147,7 @@ discard block |
||
| 147 | 147 | { |
| 148 | 148 | $name = $type->getName(); |
| 149 | 149 | |
| 150 | - if ($type->isBuiltin()) { |
|
| 150 | + if ($type->isBuiltin()){ |
|
| 151 | 151 | return match ($name) { |
| 152 | 152 | 'mixed' => true, |
| 153 | 153 | 'string' => \is_string($value), |
@@ -180,13 +180,13 @@ discard block |
||
| 180 | 180 | |
| 181 | 181 | // Try to resolve parameter by name |
| 182 | 182 | $res = $state->resolveParameterByNameOrPosition($parameter, $isVariadic); |
| 183 | - if ($res !== [] || $isVariadic) { |
|
| 183 | + if ($res !== [] || $isVariadic){ |
|
| 184 | 184 | // validate |
| 185 | - if ($isVariadic) { |
|
| 186 | - foreach ($res as $k => &$v) { |
|
| 187 | - $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key: $k); |
|
| 185 | + if ($isVariadic){ |
|
| 186 | + foreach ($res as $k => &$v){ |
|
| 187 | + $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key : $k); |
|
| 188 | 188 | } |
| 189 | - } else { |
|
| 189 | + }else{ |
|
| 190 | 190 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
| 191 | 191 | } |
| 192 | 192 | |
@@ -194,39 +194,39 @@ discard block |
||
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | $error = null; |
| 197 | - if ($hasType) { |
|
| 197 | + if ($hasType){ |
|
| 198 | 198 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
| 199 | 199 | $reflectionType = $parameter->getType(); |
| 200 | 200 | |
| 201 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
| 201 | + if ($reflectionType instanceof ReflectionIntersectionType){ |
|
| 202 | 202 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
| 206 | - foreach ($types as $namedType) { |
|
| 207 | - try { |
|
| 208 | - if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) { |
|
| 206 | + foreach ($types as $namedType){ |
|
| 207 | + try{ |
|
| 208 | + if ($this->resolveNamedType($state, $parameter, $namedType, $validate)){ |
|
| 209 | 209 | return true; |
| 210 | 210 | } |
| 211 | - } catch (Throwable $e) { |
|
| 211 | + }catch (Throwable $e){ |
|
| 212 | 212 | $error = $e; |
| 213 | 213 | } |
| 214 | 214 | } |
| 215 | 215 | } |
| 216 | 216 | |
| 217 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 217 | + if ($parameter->isDefaultValueAvailable()){ |
|
| 218 | 218 | $argument = $parameter->getDefaultValue(); |
| 219 | 219 | $this->processArgument($state, $argument); |
| 220 | 220 | return true; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - if ($hasType && $parameter->allowsNull()) { |
|
| 223 | + if ($hasType && $parameter->allowsNull()){ |
|
| 224 | 224 | $argument = null; |
| 225 | 225 | $this->processArgument($state, $argument); |
| 226 | 226 | return true; |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | - if ($error === null) { |
|
| 229 | + if ($error === null){ |
|
| 230 | 230 | return false; |
| 231 | 231 | } |
| 232 | 232 | |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | ReflectionParameter $parameter, |
| 248 | 248 | ReflectionNamedType $typeRef, |
| 249 | 249 | bool $validate |
| 250 | - ) { |
|
| 250 | + ){ |
|
| 251 | 251 | return !$typeRef->isBuiltin() && $this->resolveObjectParameter( |
| 252 | 252 | $state, |
| 253 | 253 | $typeRef->getName(), |
@@ -290,15 +290,15 @@ discard block |
||
| 290 | 290 | ResolvingState $state, |
| 291 | 291 | mixed &$value, |
| 292 | 292 | ReflectionParameter $validateWith = null, |
| 293 | - int|string $key = null |
|
| 293 | + int | string $key = null |
|
| 294 | 294 | ): void { |
| 295 | 295 | // Resolve Autowire objects |
| 296 | - if ($value instanceof Autowire) { |
|
| 296 | + if ($value instanceof Autowire){ |
|
| 297 | 297 | $value = $value->resolve($this->factory); |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | // Validation |
| 301 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 301 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
| 302 | 302 | throw new InvalidArgumentException( |
| 303 | 303 | $validateWith->getDeclaringFunction(), |
| 304 | 304 | $validateWith->getName() |
@@ -49,7 +49,8 @@ discard block |
||
| 49 | 49 | ): array { |
| 50 | 50 | $state = new ResolvingState($reflection, $parameters); |
| 51 | 51 | |
| 52 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 52 | + foreach ($reflection->getParameters() as $parameter) |
|
| 53 | + { |
|
| 53 | 54 | $this->resolveParameter($parameter, $state, $validate) |
| 54 | 55 | or |
| 55 | 56 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -63,47 +64,59 @@ discard block |
||
| 63 | 64 | $positional = true; |
| 64 | 65 | $variadic = false; |
| 65 | 66 | $parameters = $reflection->getParameters(); |
| 66 | - if (\count($parameters) === 0) { |
|
| 67 | + if (\count($parameters) === 0) |
|
| 68 | + { |
|
| 67 | 69 | return; |
| 68 | 70 | } |
| 69 | 71 | |
| 70 | 72 | $parameter = null; |
| 71 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 73 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
| 74 | + { |
|
| 72 | 75 | // get related argument value |
| 73 | 76 | $key = \key($arguments); |
| 74 | 77 | |
| 75 | 78 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 76 | 79 | // But you can't send positional argument after named in any case |
| 77 | - if (\is_int($key) && !$positional) { |
|
| 80 | + if (\is_int($key) && !$positional) |
|
| 81 | + { |
|
| 78 | 82 | throw new PositionalArgumentException($reflection, $key); |
| 79 | 83 | } |
| 80 | 84 | |
| 81 | 85 | $positional = $positional && \is_int($key); |
| 82 | 86 | |
| 83 | - if (!$variadic) { |
|
| 87 | + if (!$variadic) |
|
| 88 | + { |
|
| 84 | 89 | $parameter = \array_shift($parameters); |
| 85 | 90 | $variadic = $parameter?->isVariadic() ?? false; |
| 86 | 91 | } |
| 87 | 92 | |
| 88 | - if ($parameter === null) { |
|
| 93 | + if ($parameter === null) |
|
| 94 | + { |
|
| 89 | 95 | throw new UnknownParameterException($reflection, $key); |
| 90 | 96 | } |
| 91 | 97 | $name = $parameter->getName(); |
| 92 | 98 | |
| 93 | - if (($positional || $variadic) && $key !== null) { |
|
| 99 | + if (($positional || $variadic) && $key !== null) |
|
| 100 | + { |
|
| 94 | 101 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 95 | 102 | $value = \array_shift($arguments); |
| 96 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 97 | - if ($parameter->isOptional()) { |
|
| 103 | + } |
|
| 104 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
| 105 | + { |
|
| 106 | + if ($parameter->isOptional()) |
|
| 107 | + { |
|
| 98 | 108 | continue; |
| 99 | 109 | } |
| 100 | 110 | throw new MissingRequiredArgumentException($reflection, $name); |
| 101 | - } else { |
|
| 111 | + } |
|
| 112 | + else |
|
| 113 | + { |
|
| 102 | 114 | $value = &$arguments[$name]; |
| 103 | 115 | unset($arguments[$name]); |
| 104 | 116 | } |
| 105 | 117 | |
| 106 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 118 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
| 119 | + { |
|
| 107 | 120 | throw new InvalidArgumentException($reflection, $name); |
| 108 | 121 | } |
| 109 | 122 | } |
@@ -111,7 +124,8 @@ discard block |
||
| 111 | 124 | |
| 112 | 125 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 113 | 126 | { |
| 114 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 127 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
| 128 | + { |
|
| 115 | 129 | return true; |
| 116 | 130 | } |
| 117 | 131 | $type = $parameter->getType(); |
@@ -122,17 +136,21 @@ discard block |
||
| 122 | 136 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 123 | 137 | }; |
| 124 | 138 | |
| 125 | - foreach ($types as $t) { |
|
| 139 | + foreach ($types as $t) |
|
| 140 | + { |
|
| 126 | 141 | \assert($t instanceof ReflectionNamedType); |
| 127 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 142 | + if (!$this->validateValueNamedType($t, $value)) |
|
| 143 | + { |
|
| 128 | 144 | // If it is TypeIntersection |
| 129 | - if ($or) { |
|
| 145 | + if ($or) |
|
| 146 | + { |
|
| 130 | 147 | continue; |
| 131 | 148 | } |
| 132 | 149 | return false; |
| 133 | 150 | } |
| 134 | 151 | // If it is not type intersection then we can skip that value after first successful check |
| 135 | - if ($or) { |
|
| 152 | + if ($or) |
|
| 153 | + { |
|
| 136 | 154 | return true; |
| 137 | 155 | } |
| 138 | 156 | } |
@@ -147,7 +165,8 @@ discard block |
||
| 147 | 165 | { |
| 148 | 166 | $name = $type->getName(); |
| 149 | 167 | |
| 150 | - if ($type->isBuiltin()) { |
|
| 168 | + if ($type->isBuiltin()) |
|
| 169 | + { |
|
| 151 | 170 | return match ($name) { |
| 152 | 171 | 'mixed' => true, |
| 153 | 172 | 'string' => \is_string($value), |
@@ -180,13 +199,18 @@ discard block |
||
| 180 | 199 | |
| 181 | 200 | // Try to resolve parameter by name |
| 182 | 201 | $res = $state->resolveParameterByNameOrPosition($parameter, $isVariadic); |
| 183 | - if ($res !== [] || $isVariadic) { |
|
| 202 | + if ($res !== [] || $isVariadic) |
|
| 203 | + { |
|
| 184 | 204 | // validate |
| 185 | - if ($isVariadic) { |
|
| 186 | - foreach ($res as $k => &$v) { |
|
| 205 | + if ($isVariadic) |
|
| 206 | + { |
|
| 207 | + foreach ($res as $k => &$v) |
|
| 208 | + { |
|
| 187 | 209 | $this->processArgument($state, $v, validateWith: $validate ? $parameter : null, key: $k); |
| 188 | 210 | } |
| 189 | - } else { |
|
| 211 | + } |
|
| 212 | + else |
|
| 213 | + { |
|
| 190 | 214 | $this->processArgument($state, $res[0], validateWith: $validate ? $parameter : null); |
| 191 | 215 | } |
| 192 | 216 | |
@@ -194,39 +218,49 @@ discard block |
||
| 194 | 218 | } |
| 195 | 219 | |
| 196 | 220 | $error = null; |
| 197 | - if ($hasType) { |
|
| 221 | + if ($hasType) |
|
| 222 | + { |
|
| 198 | 223 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $reflectionType */ |
| 199 | 224 | $reflectionType = $parameter->getType(); |
| 200 | 225 | |
| 201 | - if ($reflectionType instanceof ReflectionIntersectionType) { |
|
| 226 | + if ($reflectionType instanceof ReflectionIntersectionType) |
|
| 227 | + { |
|
| 202 | 228 | throw new UnsupportedTypeException($parameter->getDeclaringFunction(), $parameter->getName()); |
| 203 | 229 | } |
| 204 | 230 | |
| 205 | 231 | $types = $reflectionType instanceof ReflectionNamedType ? [$reflectionType] : $reflectionType->getTypes(); |
| 206 | - foreach ($types as $namedType) { |
|
| 207 | - try { |
|
| 208 | - if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) { |
|
| 232 | + foreach ($types as $namedType) |
|
| 233 | + { |
|
| 234 | + try |
|
| 235 | + { |
|
| 236 | + if ($this->resolveNamedType($state, $parameter, $namedType, $validate)) |
|
| 237 | + { |
|
| 209 | 238 | return true; |
| 210 | 239 | } |
| 211 | - } catch (Throwable $e) { |
|
| 240 | + } |
|
| 241 | + catch (Throwable $e) |
|
| 242 | + { |
|
| 212 | 243 | $error = $e; |
| 213 | 244 | } |
| 214 | 245 | } |
| 215 | 246 | } |
| 216 | 247 | |
| 217 | - if ($parameter->isDefaultValueAvailable()) { |
|
| 248 | + if ($parameter->isDefaultValueAvailable()) |
|
| 249 | + { |
|
| 218 | 250 | $argument = $parameter->getDefaultValue(); |
| 219 | 251 | $this->processArgument($state, $argument); |
| 220 | 252 | return true; |
| 221 | 253 | } |
| 222 | 254 | |
| 223 | - if ($hasType && $parameter->allowsNull()) { |
|
| 255 | + if ($hasType && $parameter->allowsNull()) |
|
| 256 | + { |
|
| 224 | 257 | $argument = null; |
| 225 | 258 | $this->processArgument($state, $argument); |
| 226 | 259 | return true; |
| 227 | 260 | } |
| 228 | 261 | |
| 229 | - if ($error === null) { |
|
| 262 | + if ($error === null) |
|
| 263 | + { |
|
| 230 | 264 | return false; |
| 231 | 265 | } |
| 232 | 266 | |
@@ -293,12 +327,14 @@ discard block |
||
| 293 | 327 | int|string $key = null |
| 294 | 328 | ): void { |
| 295 | 329 | // Resolve Autowire objects |
| 296 | - if ($value instanceof Autowire) { |
|
| 330 | + if ($value instanceof Autowire) |
|
| 331 | + { |
|
| 297 | 332 | $value = $value->resolve($this->factory); |
| 298 | 333 | } |
| 299 | 334 | |
| 300 | 335 | // Validation |
| 301 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 336 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
| 337 | + { |
|
| 302 | 338 | throw new InvalidArgumentException( |
| 303 | 339 | $validateWith->getDeclaringFunction(), |
| 304 | 340 | $validateWith->getName() |
@@ -54,19 +54,19 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 56 | 56 | { |
| 57 | - if (!isset($this->state->bindings[$alias])) { |
|
| 57 | + if (!isset($this->state->bindings[$alias])){ |
|
| 58 | 58 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 59 | - try { |
|
| 59 | + try{ |
|
| 60 | 60 | //No direct instructions how to construct class, make is automatically |
| 61 | 61 | return $this->autowire($alias, $parameters, $context); |
| 62 | - } finally { |
|
| 62 | + }finally{ |
|
| 63 | 63 | $this->tracer->pop(false); |
| 64 | 64 | } |
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | $avoidCache = $parameters !== []; |
| 68 | 68 | $binding = $this->state->bindings[$alias]; |
| 69 | - try { |
|
| 69 | + try{ |
|
| 70 | 70 | $this->tracer->push( |
| 71 | 71 | false, |
| 72 | 72 | action: 'resolve from binding', |
@@ -76,23 +76,23 @@ discard block |
||
| 76 | 76 | ); |
| 77 | 77 | $this->tracer->push(true); |
| 78 | 78 | |
| 79 | - if (\is_object($binding)) { |
|
| 80 | - if ($binding::class === WeakReference::class) { |
|
| 81 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
| 82 | - try { |
|
| 79 | + if (\is_object($binding)){ |
|
| 80 | + if ($binding::class === WeakReference::class){ |
|
| 81 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)){ |
|
| 82 | + try{ |
|
| 83 | 83 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 84 | 84 | $object = $this->createInstance($alias, $parameters, $context); |
| 85 | - if ($avoidCache) { |
|
| 85 | + if ($avoidCache){ |
|
| 86 | 86 | return $object; |
| 87 | 87 | } |
| 88 | 88 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 89 | - } catch (\Throwable) { |
|
| 89 | + }catch (\Throwable){ |
|
| 90 | 90 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 91 | 91 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 92 | 92 | $this->tracer->getRootAlias(), |
| 93 | 93 | $alias, |
| 94 | 94 | ))); |
| 95 | - } finally { |
|
| 95 | + }finally{ |
|
| 96 | 96 | $this->tracer->pop(); |
| 97 | 97 | } |
| 98 | 98 | } |
@@ -104,26 +104,26 @@ discard block |
||
| 104 | 104 | : $binding; |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - if (\is_string($binding)) { |
|
| 107 | + if (\is_string($binding)){ |
|
| 108 | 108 | //Binding is pointing to something else |
| 109 | 109 | return $this->make($binding, $parameters, $context); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | unset($this->state->bindings[$alias]); |
| 113 | - try { |
|
| 113 | + try{ |
|
| 114 | 114 | $instance = $binding[0] === $alias |
| 115 | 115 | ? $this->autowire($alias, $parameters, $context) |
| 116 | 116 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 117 | - } finally { |
|
| 117 | + }finally{ |
|
| 118 | 118 | /** @psalm-var class-string $alias */ |
| 119 | 119 | $this->state->bindings[$alias] = $binding; |
| 120 | 120 | } |
| 121 | - } finally { |
|
| 121 | + }finally{ |
|
| 122 | 122 | $this->tracer->pop(true); |
| 123 | 123 | $this->tracer->pop(false); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - if ($binding[1]) { |
|
| 126 | + if ($binding[1]){ |
|
| 127 | 127 | // Indicates singleton |
| 128 | 128 | /** @psalm-var class-string $alias */ |
| 129 | 129 | $this->state->bindings[$alias] = $instance; |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | && |
| 149 | 149 | (isset($this->state->injectors[$class]) || $this->binder->hasInjector($class)) |
| 150 | 150 | )) |
| 151 | - ) { |
|
| 151 | + ){ |
|
| 152 | 152 | throw new NotFoundException($this->tracer->combineTraceMessage(\sprintf( |
| 153 | 153 | 'Can\'t resolve `%s`: undefined class or binding `%s`.', |
| 154 | 154 | $this->tracer->getRootAlias(), |
@@ -177,18 +177,18 @@ discard block |
||
| 177 | 177 | array $parameters, |
| 178 | 178 | string $context = null |
| 179 | 179 | ): mixed { |
| 180 | - if (\is_string($target)) { |
|
| 180 | + if (\is_string($target)){ |
|
| 181 | 181 | // Reference |
| 182 | 182 | return $this->make($target, $parameters, $context); |
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - if ($target instanceof Autowire) { |
|
| 185 | + if ($target instanceof Autowire){ |
|
| 186 | 186 | return $target->resolve($this, $parameters); |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - try { |
|
| 189 | + try{ |
|
| 190 | 190 | return $this->invoker->invoke($target, $parameters); |
| 191 | - } catch (NotCallableException $e) { |
|
| 191 | + }catch (NotCallableException $e){ |
|
| 192 | 192 | throw new ContainerException( |
| 193 | 193 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 194 | 194 | $e->getCode(), |
@@ -212,20 +212,20 @@ discard block |
||
| 212 | 212 | */ |
| 213 | 213 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 214 | 214 | { |
| 215 | - try { |
|
| 215 | + try{ |
|
| 216 | 216 | $reflection = new \ReflectionClass($class); |
| 217 | - } catch (\ReflectionException $e) { |
|
| 217 | + }catch (\ReflectionException $e){ |
|
| 218 | 218 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | //We have to construct class using external injector when we know exact context |
| 222 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 222 | + if ($parameters === [] && $this->binder->hasInjector($class)){ |
|
| 223 | 223 | $injector = $this->state->injectors[$reflection->getName()]; |
| 224 | 224 | |
| 225 | - try { |
|
| 225 | + try{ |
|
| 226 | 226 | $injectorInstance = $this->container->get($injector); |
| 227 | 227 | |
| 228 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 228 | + if (!$injectorInstance instanceof InjectorInterface){ |
|
| 229 | 229 | throw new InjectionException( |
| 230 | 230 | \sprintf( |
| 231 | 231 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | * @psalm-suppress RedundantCondition |
| 241 | 241 | */ |
| 242 | 242 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 243 | - if (!$reflection->isInstance($instance)) { |
|
| 243 | + if (!$reflection->isInstance($instance)){ |
|
| 244 | 244 | throw new InjectionException( |
| 245 | 245 | \sprintf( |
| 246 | 246 | "Invalid injection response for '%s'.", |
@@ -250,12 +250,12 @@ discard block |
||
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | return $instance; |
| 253 | - } finally { |
|
| 253 | + }finally{ |
|
| 254 | 254 | $this->state->injectors[$reflection->getName()] = $injector; |
| 255 | 255 | } |
| 256 | 256 | } |
| 257 | 257 | |
| 258 | - if (!$reflection->isInstantiable()) { |
|
| 258 | + if (!$reflection->isInstantiable()){ |
|
| 259 | 259 | $itIs = match (true) { |
| 260 | 260 | $reflection->isEnum() => 'Enum', |
| 261 | 261 | $reflection->isAbstract() => 'Abstract class', |
@@ -268,12 +268,12 @@ discard block |
||
| 268 | 268 | |
| 269 | 269 | $constructor = $reflection->getConstructor(); |
| 270 | 270 | |
| 271 | - if ($constructor !== null) { |
|
| 272 | - try { |
|
| 271 | + if ($constructor !== null){ |
|
| 272 | + try{ |
|
| 273 | 273 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 274 | 274 | $this->tracer->push(true); |
| 275 | 275 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 276 | - } catch (ValidationException $e) { |
|
| 276 | + }catch (ValidationException $e){ |
|
| 277 | 277 | throw new ContainerException( |
| 278 | 278 | $this->tracer->combineTraceMessage( |
| 279 | 279 | \sprintf( |
@@ -283,22 +283,22 @@ discard block |
||
| 283 | 283 | ) |
| 284 | 284 | ), |
| 285 | 285 | ); |
| 286 | - } finally { |
|
| 286 | + }finally{ |
|
| 287 | 287 | $this->tracer->pop(true); |
| 288 | 288 | $this->tracer->pop(false); |
| 289 | 289 | } |
| 290 | - try { |
|
| 290 | + try{ |
|
| 291 | 291 | // Using constructor with resolved arguments |
| 292 | 292 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 293 | 293 | $this->tracer->push(true); |
| 294 | 294 | $instance = new $class(...$arguments); |
| 295 | - } catch (\TypeError $e) { |
|
| 295 | + }catch (\TypeError $e){ |
|
| 296 | 296 | throw new WrongTypeException($constructor, $e); |
| 297 | - } finally { |
|
| 297 | + }finally{ |
|
| 298 | 298 | $this->tracer->pop(true); |
| 299 | 299 | $this->tracer->pop(false); |
| 300 | 300 | } |
| 301 | - } else { |
|
| 301 | + }else{ |
|
| 302 | 302 | // No constructor specified |
| 303 | 303 | $instance = $reflection->newInstance(); |
| 304 | 304 | } |
@@ -319,9 +319,9 @@ discard block |
||
| 319 | 319 | private function registerInstance(object $instance): object |
| 320 | 320 | { |
| 321 | 321 | //Declarative singletons |
| 322 | - if ($instance instanceof SingletonInterface) { |
|
| 322 | + if ($instance instanceof SingletonInterface){ |
|
| 323 | 323 | $alias = $instance::class; |
| 324 | - if (!isset($this->state->bindings[$alias])) { |
|
| 324 | + if (!isset($this->state->bindings[$alias])){ |
|
| 325 | 325 | $this->state->bindings[$alias] = $instance; |
| 326 | 326 | } |
| 327 | 327 | } |
@@ -54,19 +54,24 @@ discard block |
||
| 54 | 54 | */ |
| 55 | 55 | public function make(string $alias, array $parameters = [], string $context = null): mixed |
| 56 | 56 | { |
| 57 | - if (!isset($this->state->bindings[$alias])) { |
|
| 57 | + if (!isset($this->state->bindings[$alias])) |
|
| 58 | + { |
|
| 58 | 59 | $this->tracer->push(false, action: 'autowire', alias: $alias, context: $context); |
| 59 | - try { |
|
| 60 | + try |
|
| 61 | + { |
|
| 60 | 62 | //No direct instructions how to construct class, make is automatically |
| 61 | 63 | return $this->autowire($alias, $parameters, $context); |
| 62 | - } finally { |
|
| 64 | + } |
|
| 65 | + finally |
|
| 66 | + { |
|
| 63 | 67 | $this->tracer->pop(false); |
| 64 | 68 | } |
| 65 | 69 | } |
| 66 | 70 | |
| 67 | 71 | $avoidCache = $parameters !== []; |
| 68 | 72 | $binding = $this->state->bindings[$alias]; |
| 69 | - try { |
|
| 73 | + try |
|
| 74 | + { |
|
| 70 | 75 | $this->tracer->push( |
| 71 | 76 | false, |
| 72 | 77 | action: 'resolve from binding', |
@@ -76,23 +81,32 @@ discard block |
||
| 76 | 81 | ); |
| 77 | 82 | $this->tracer->push(true); |
| 78 | 83 | |
| 79 | - if (\is_object($binding)) { |
|
| 80 | - if ($binding::class === WeakReference::class) { |
|
| 81 | - if (($avoidCache || $binding->get() === null) && \class_exists($alias)) { |
|
| 82 | - try { |
|
| 84 | + if (\is_object($binding)) |
|
| 85 | + { |
|
| 86 | + if ($binding::class === WeakReference::class) |
|
| 87 | + { |
|
| 88 | + if (($avoidCache || $binding->get() === null) && \class_exists($alias)) |
|
| 89 | + { |
|
| 90 | + try |
|
| 91 | + { |
|
| 83 | 92 | $this->tracer->push(false, alias: $alias, source: WeakReference::class, context: $context); |
| 84 | 93 | $object = $this->createInstance($alias, $parameters, $context); |
| 85 | - if ($avoidCache) { |
|
| 94 | + if ($avoidCache) |
|
| 95 | + { |
|
| 86 | 96 | return $object; |
| 87 | 97 | } |
| 88 | 98 | $binding = $this->state->bindings[$alias] = WeakReference::create($object); |
| 89 | - } catch (\Throwable) { |
|
| 99 | + } |
|
| 100 | + catch (\Throwable) |
|
| 101 | + { |
|
| 90 | 102 | throw new ContainerException($this->tracer->combineTraceMessage(\sprintf( |
| 91 | 103 | 'Can\'t resolve `%s`: can\'t instantiate `%s` from WeakReference binding.', |
| 92 | 104 | $this->tracer->getRootAlias(), |
| 93 | 105 | $alias, |
| 94 | 106 | ))); |
| 95 | - } finally { |
|
| 107 | + } |
|
| 108 | + finally |
|
| 109 | + { |
|
| 96 | 110 | $this->tracer->pop(); |
| 97 | 111 | } |
| 98 | 112 | } |
@@ -104,26 +118,33 @@ discard block |
||
| 104 | 118 | : $binding; |
| 105 | 119 | } |
| 106 | 120 | |
| 107 | - if (\is_string($binding)) { |
|
| 121 | + if (\is_string($binding)) |
|
| 122 | + { |
|
| 108 | 123 | //Binding is pointing to something else |
| 109 | 124 | return $this->make($binding, $parameters, $context); |
| 110 | 125 | } |
| 111 | 126 | |
| 112 | 127 | unset($this->state->bindings[$alias]); |
| 113 | - try { |
|
| 128 | + try |
|
| 129 | + { |
|
| 114 | 130 | $instance = $binding[0] === $alias |
| 115 | 131 | ? $this->autowire($alias, $parameters, $context) |
| 116 | 132 | : $this->evaluateBinding($alias, $binding[0], $parameters, $context); |
| 117 | - } finally { |
|
| 133 | + } |
|
| 134 | + finally |
|
| 135 | + { |
|
| 118 | 136 | /** @psalm-var class-string $alias */ |
| 119 | 137 | $this->state->bindings[$alias] = $binding; |
| 120 | 138 | } |
| 121 | - } finally { |
|
| 139 | + } |
|
| 140 | + finally |
|
| 141 | + { |
|
| 122 | 142 | $this->tracer->pop(true); |
| 123 | 143 | $this->tracer->pop(false); |
| 124 | 144 | } |
| 125 | 145 | |
| 126 | - if ($binding[1]) { |
|
| 146 | + if ($binding[1]) |
|
| 147 | + { |
|
| 127 | 148 | // Indicates singleton |
| 128 | 149 | /** @psalm-var class-string $alias */ |
| 129 | 150 | $this->state->bindings[$alias] = $instance; |
@@ -177,18 +198,23 @@ discard block |
||
| 177 | 198 | array $parameters, |
| 178 | 199 | string $context = null |
| 179 | 200 | ): mixed { |
| 180 | - if (\is_string($target)) { |
|
| 201 | + if (\is_string($target)) |
|
| 202 | + { |
|
| 181 | 203 | // Reference |
| 182 | 204 | return $this->make($target, $parameters, $context); |
| 183 | 205 | } |
| 184 | 206 | |
| 185 | - if ($target instanceof Autowire) { |
|
| 207 | + if ($target instanceof Autowire) |
|
| 208 | + { |
|
| 186 | 209 | return $target->resolve($this, $parameters); |
| 187 | 210 | } |
| 188 | 211 | |
| 189 | - try { |
|
| 212 | + try |
|
| 213 | + { |
|
| 190 | 214 | return $this->invoker->invoke($target, $parameters); |
| 191 | - } catch (NotCallableException $e) { |
|
| 215 | + } |
|
| 216 | + catch (NotCallableException $e) |
|
| 217 | + { |
|
| 192 | 218 | throw new ContainerException( |
| 193 | 219 | $this->tracer->combineTraceMessage(\sprintf('Invalid binding for `%s`.', $alias)), |
| 194 | 220 | $e->getCode(), |
@@ -212,20 +238,26 @@ discard block |
||
| 212 | 238 | */ |
| 213 | 239 | private function createInstance(string $class, array $parameters, string $context = null): object |
| 214 | 240 | { |
| 215 | - try { |
|
| 241 | + try |
|
| 242 | + { |
|
| 216 | 243 | $reflection = new \ReflectionClass($class); |
| 217 | - } catch (\ReflectionException $e) { |
|
| 244 | + } |
|
| 245 | + catch (\ReflectionException $e) |
|
| 246 | + { |
|
| 218 | 247 | throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
| 219 | 248 | } |
| 220 | 249 | |
| 221 | 250 | //We have to construct class using external injector when we know exact context |
| 222 | - if ($parameters === [] && $this->binder->hasInjector($class)) { |
|
| 251 | + if ($parameters === [] && $this->binder->hasInjector($class)) |
|
| 252 | + { |
|
| 223 | 253 | $injector = $this->state->injectors[$reflection->getName()]; |
| 224 | 254 | |
| 225 | - try { |
|
| 255 | + try |
|
| 256 | + { |
|
| 226 | 257 | $injectorInstance = $this->container->get($injector); |
| 227 | 258 | |
| 228 | - if (!$injectorInstance instanceof InjectorInterface) { |
|
| 259 | + if (!$injectorInstance instanceof InjectorInterface) |
|
| 260 | + { |
|
| 229 | 261 | throw new InjectionException( |
| 230 | 262 | \sprintf( |
| 231 | 263 | "Class '%s' must be an instance of InjectorInterface for '%s'.", |
@@ -240,7 +272,8 @@ discard block |
||
| 240 | 272 | * @psalm-suppress RedundantCondition |
| 241 | 273 | */ |
| 242 | 274 | $instance = $injectorInstance->createInjection($reflection, $context); |
| 243 | - if (!$reflection->isInstance($instance)) { |
|
| 275 | + if (!$reflection->isInstance($instance)) |
|
| 276 | + { |
|
| 244 | 277 | throw new InjectionException( |
| 245 | 278 | \sprintf( |
| 246 | 279 | "Invalid injection response for '%s'.", |
@@ -250,12 +283,15 @@ discard block |
||
| 250 | 283 | } |
| 251 | 284 | |
| 252 | 285 | return $instance; |
| 253 | - } finally { |
|
| 286 | + } |
|
| 287 | + finally |
|
| 288 | + { |
|
| 254 | 289 | $this->state->injectors[$reflection->getName()] = $injector; |
| 255 | 290 | } |
| 256 | 291 | } |
| 257 | 292 | |
| 258 | - if (!$reflection->isInstantiable()) { |
|
| 293 | + if (!$reflection->isInstantiable()) |
|
| 294 | + { |
|
| 259 | 295 | $itIs = match (true) { |
| 260 | 296 | $reflection->isEnum() => 'Enum', |
| 261 | 297 | $reflection->isAbstract() => 'Abstract class', |
@@ -268,12 +304,16 @@ discard block |
||
| 268 | 304 | |
| 269 | 305 | $constructor = $reflection->getConstructor(); |
| 270 | 306 | |
| 271 | - if ($constructor !== null) { |
|
| 272 | - try { |
|
| 307 | + if ($constructor !== null) |
|
| 308 | + { |
|
| 309 | + try |
|
| 310 | + { |
|
| 273 | 311 | $this->tracer->push(false, action: 'resolve arguments', signature: $constructor); |
| 274 | 312 | $this->tracer->push(true); |
| 275 | 313 | $arguments = $this->resolver->resolveArguments($constructor, $parameters); |
| 276 | - } catch (ValidationException $e) { |
|
| 314 | + } |
|
| 315 | + catch (ValidationException $e) |
|
| 316 | + { |
|
| 277 | 317 | throw new ContainerException( |
| 278 | 318 | $this->tracer->combineTraceMessage( |
| 279 | 319 | \sprintf( |
@@ -283,22 +323,31 @@ discard block |
||
| 283 | 323 | ) |
| 284 | 324 | ), |
| 285 | 325 | ); |
| 286 | - } finally { |
|
| 326 | + } |
|
| 327 | + finally |
|
| 328 | + { |
|
| 287 | 329 | $this->tracer->pop(true); |
| 288 | 330 | $this->tracer->pop(false); |
| 289 | 331 | } |
| 290 | - try { |
|
| 332 | + try |
|
| 333 | + { |
|
| 291 | 334 | // Using constructor with resolved arguments |
| 292 | 335 | $this->tracer->push(false, call: "$class::__construct", arguments: $arguments); |
| 293 | 336 | $this->tracer->push(true); |
| 294 | 337 | $instance = new $class(...$arguments); |
| 295 | - } catch (\TypeError $e) { |
|
| 338 | + } |
|
| 339 | + catch (\TypeError $e) |
|
| 340 | + { |
|
| 296 | 341 | throw new WrongTypeException($constructor, $e); |
| 297 | - } finally { |
|
| 342 | + } |
|
| 343 | + finally |
|
| 344 | + { |
|
| 298 | 345 | $this->tracer->pop(true); |
| 299 | 346 | $this->tracer->pop(false); |
| 300 | 347 | } |
| 301 | - } else { |
|
| 348 | + } |
|
| 349 | + else |
|
| 350 | + { |
|
| 302 | 351 | // No constructor specified |
| 303 | 352 | $instance = $reflection->newInstance(); |
| 304 | 353 | } |
@@ -319,9 +368,11 @@ discard block |
||
| 319 | 368 | private function registerInstance(object $instance): object |
| 320 | 369 | { |
| 321 | 370 | //Declarative singletons |
| 322 | - if ($instance instanceof SingletonInterface) { |
|
| 371 | + if ($instance instanceof SingletonInterface) |
|
| 372 | + { |
|
| 323 | 373 | $alias = $instance::class; |
| 324 | - if (!isset($this->state->bindings[$alias])) { |
|
| 374 | + if (!isset($this->state->bindings[$alias])) |
|
| 375 | + { |
|
| 325 | 376 | $this->state->bindings[$alias] = $instance; |
| 326 | 377 | } |
| 327 | 378 | } |
@@ -45,11 +45,11 @@ discard block |
||
| 45 | 45 | use DestructorTrait; |
| 46 | 46 | |
| 47 | 47 | private Internal\State $state; |
| 48 | - private ResolverInterface|Internal\Resolver $resolver; |
|
| 49 | - private FactoryInterface|Internal\Factory $factory; |
|
| 50 | - private ContainerInterface|Internal\Container $container; |
|
| 51 | - private BinderInterface|Internal\Binder $binder; |
|
| 52 | - private InvokerInterface|Internal\Invoker $invoker; |
|
| 48 | + private ResolverInterface | Internal\Resolver $resolver; |
|
| 49 | + private FactoryInterface | Internal\Factory $factory; |
|
| 50 | + private ContainerInterface | Internal\Container $container; |
|
| 51 | + private BinderInterface | Internal\Binder $binder; |
|
| 52 | + private InvokerInterface | Internal\Invoker $invoker; |
|
| 53 | 53 | |
| 54 | 54 | /** |
| 55 | 55 | * Container constructor. |
@@ -59,8 +59,8 @@ discard block |
||
| 59 | 59 | $constructor = new Internal\Registry($config, [ |
| 60 | 60 | 'state' => new Internal\State(), |
| 61 | 61 | ]); |
| 62 | - foreach ($config as $property => $class) { |
|
| 63 | - if (\property_exists($this, $property)) { |
|
| 62 | + foreach ($config as $property => $class){ |
|
| 63 | + if (\property_exists($this, $property)){ |
|
| 64 | 64 | $this->$property = $constructor->get($property, $class); |
| 65 | 65 | } |
| 66 | 66 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @psalm-suppress InvalidArgument, InvalidCast |
| 133 | 133 | */ |
| 134 | - public function get(string|Autowire $id, string $context = null): mixed |
|
| 134 | + public function get(string | Autowire $id, string $context = null): mixed |
|
| 135 | 135 | { |
| 136 | 136 | return $this->container->get($id, $context); |
| 137 | 137 | } |
@@ -145,26 +145,26 @@ discard block |
||
| 145 | 145 | { |
| 146 | 146 | $binds = &$this->state->bindings; |
| 147 | 147 | $cleanup = $previous = []; |
| 148 | - foreach ($bindings as $alias => $resolver) { |
|
| 149 | - if (isset($binds[$alias])) { |
|
| 148 | + foreach ($bindings as $alias => $resolver){ |
|
| 149 | + if (isset($binds[$alias])){ |
|
| 150 | 150 | $previous[$alias] = $binds[$alias]; |
| 151 | - } else { |
|
| 151 | + }else{ |
|
| 152 | 152 | $cleanup[] = $alias; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | $this->binder->bind($alias, $resolver); |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - try { |
|
| 158 | + try{ |
|
| 159 | 159 | return ContainerScope::getContainer() !== $this |
| 160 | 160 | ? ContainerScope::runScope($this, $scope) |
| 161 | 161 | : $scope($this); |
| 162 | - } finally { |
|
| 163 | - foreach ($previous as $alias => $resolver) { |
|
| 162 | + }finally{ |
|
| 163 | + foreach ($previous as $alias => $resolver){ |
|
| 164 | 164 | $binds[$alias] = $resolver; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - foreach ($cleanup as $alias) { |
|
| 167 | + foreach ($cleanup as $alias){ |
|
| 168 | 168 | unset($binds[$alias]); |
| 169 | 169 | } |
| 170 | 170 | } |
@@ -175,7 +175,7 @@ discard block |
||
| 175 | 175 | * for each method call), function array or Closure (executed every call). Only object resolvers |
| 176 | 176 | * supported by this method. |
| 177 | 177 | */ |
| 178 | - public function bind(string $alias, string|array|callable|object $resolver): void |
|
| 178 | + public function bind(string $alias, string | array | callable | object $resolver): void |
|
| 179 | 179 | { |
| 180 | 180 | $this->binder->bind($alias, $resolver); |
| 181 | 181 | } |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | * |
| 187 | 187 | * @psalm-param TResolver $resolver |
| 188 | 188 | */ |
| 189 | - public function bindSingleton(string $alias, string|array|callable|object $resolver): void |
|
| 189 | + public function bindSingleton(string $alias, string | array | callable | object $resolver): void |
|
| 190 | 190 | { |
| 191 | 191 | $this->binder->bindSingleton($alias, $resolver); |
| 192 | 192 | } |