@@ -23,10 +23,10 @@ discard block |
||
| 23 | 23 | $root = new Container(); |
| 24 | 24 | $root->bind(LoggerInterface::class, KVLogger::class); |
| 25 | 25 | |
| 26 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
| 26 | + $root->runScope(new Scope(), static function (Container $c1){ |
|
| 27 | 27 | $c1->bind(LoggerInterface::class, FileLogger::class); |
| 28 | 28 | |
| 29 | - $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger) { |
|
| 29 | + $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger){ |
|
| 30 | 30 | // from the $root container |
| 31 | 31 | self::assertInstanceOf(KVLogger::class, $carrier->logger); |
| 32 | 32 | // from the $c1 container |
@@ -40,7 +40,7 @@ discard block |
||
| 40 | 40 | $root = new Container(); |
| 41 | 41 | $root->bind(LoggerInterface::class, KVLogger::class); |
| 42 | 42 | |
| 43 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
| 43 | + $root->runScope(new Scope(), static function (Container $c1){ |
|
| 44 | 44 | $c1->bind(LoggerInterface::class, FileLogger::class); |
| 45 | 45 | |
| 46 | 46 | self::assertInstanceOf( |
@@ -23,10 +23,12 @@ discard block |
||
| 23 | 23 | $root = new Container(); |
| 24 | 24 | $root->bind(LoggerInterface::class, KVLogger::class); |
| 25 | 25 | |
| 26 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
| 26 | + $root->runScope(new Scope(), static function (Container $c1) |
|
| 27 | + { |
|
| 27 | 28 | $c1->bind(LoggerInterface::class, FileLogger::class); |
| 28 | 29 | |
| 29 | - $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger) { |
|
| 30 | + $c1->runScope(new Scope(), static function (LoggerCarrier $carrier, LoggerInterface $logger) |
|
| 31 | + { |
|
| 30 | 32 | // from the $root container |
| 31 | 33 | self::assertInstanceOf(KVLogger::class, $carrier->logger); |
| 32 | 34 | // from the $c1 container |
@@ -40,7 +42,8 @@ discard block |
||
| 40 | 42 | $root = new Container(); |
| 41 | 43 | $root->bind(LoggerInterface::class, KVLogger::class); |
| 42 | 44 | |
| 43 | - $root->runScope(new Scope(), static function (Container $c1) { |
|
| 45 | + $root->runScope(new Scope(), static function (Container $c1) |
|
| 46 | + { |
|
| 44 | 47 | $c1->bind(LoggerInterface::class, FileLogger::class); |
| 45 | 48 | |
| 46 | 49 | self::assertInstanceOf( |
@@ -21,22 +21,22 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public static function create( |
| 23 | 23 | \ReflectionClass $type, |
| 24 | - \Stringable|string|null $context, |
|
| 24 | + \Stringable | string | null $context, |
|
| 25 | 25 | \Spiral\Core\Attribute\Proxy $attribute, |
| 26 | 26 | ): object { |
| 27 | 27 | $interface = $type->getName(); |
| 28 | 28 | |
| 29 | - if (!\array_key_exists($interface, self::$cache)) { |
|
| 29 | + if (!\array_key_exists($interface, self::$cache)){ |
|
| 30 | 30 | /** @var class-string<TClass> $className */ |
| 31 | 31 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
| 32 | 32 | |
| 33 | - try { |
|
| 33 | + try{ |
|
| 34 | 34 | $classString = ProxyClassRenderer::renderClass($type, $className, $attribute->magicCall ? [ |
| 35 | 35 | Proxy\MagicCallTrait::class, |
| 36 | 36 | ] : []); |
| 37 | 37 | |
| 38 | 38 | eval($classString); |
| 39 | - } catch (\Throwable $e) { |
|
| 39 | + }catch (\Throwable $e){ |
|
| 40 | 40 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
| 41 | 41 | } |
| 42 | 42 | |
@@ -45,12 +45,12 @@ discard block |
||
| 45 | 45 | |
| 46 | 46 | // Store in cache without context |
| 47 | 47 | self::$cache[$interface] = $instance; |
| 48 | - } else { |
|
| 48 | + }else{ |
|
| 49 | 49 | /** @var TClass $instance */ |
| 50 | 50 | $instance = self::$cache[$interface]; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - if ($context !== null) { |
|
| 53 | + if ($context !== null){ |
|
| 54 | 54 | $instance = clone $instance; |
| 55 | 55 | (static fn() => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
| 56 | 56 | } |
@@ -26,17 +26,21 @@ discard block |
||
| 26 | 26 | ): object { |
| 27 | 27 | $interface = $type->getName(); |
| 28 | 28 | |
| 29 | - if (!\array_key_exists($interface, self::$cache)) { |
|
| 29 | + if (!\array_key_exists($interface, self::$cache)) |
|
| 30 | + { |
|
| 30 | 31 | /** @var class-string<TClass> $className */ |
| 31 | 32 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
| 32 | 33 | |
| 33 | - try { |
|
| 34 | + try |
|
| 35 | + { |
|
| 34 | 36 | $classString = ProxyClassRenderer::renderClass($type, $className, $attribute->magicCall ? [ |
| 35 | 37 | Proxy\MagicCallTrait::class, |
| 36 | 38 | ] : []); |
| 37 | 39 | |
| 38 | 40 | eval($classString); |
| 39 | - } catch (\Throwable $e) { |
|
| 41 | + } |
|
| 42 | + catch (\Throwable $e) |
|
| 43 | + { |
|
| 40 | 44 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
| 41 | 45 | } |
| 42 | 46 | |
@@ -45,12 +49,15 @@ discard block |
||
| 45 | 49 | |
| 46 | 50 | // Store in cache without context |
| 47 | 51 | self::$cache[$interface] = $instance; |
| 48 | - } else { |
|
| 52 | + } |
|
| 53 | + else |
|
| 54 | + { |
|
| 49 | 55 | /** @var TClass $instance */ |
| 50 | 56 | $instance = self::$cache[$interface]; |
| 51 | 57 | } |
| 52 | 58 | |
| 53 | - if ($context !== null) { |
|
| 59 | + if ($context !== null) |
|
| 60 | + { |
|
| 54 | 61 | $instance = clone $instance; |
| 55 | 62 | (static fn() => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
| 56 | 63 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | ): array { |
| 54 | 54 | $state = new ResolvingState($reflection, $parameters); |
| 55 | 55 | |
| 56 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 56 | + foreach ($reflection->getParameters() as $parameter){ |
|
| 57 | 57 | $this->resolveParameter($parameter, $state, $validate) |
| 58 | 58 | or |
| 59 | 59 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -67,47 +67,47 @@ discard block |
||
| 67 | 67 | $positional = true; |
| 68 | 68 | $variadic = false; |
| 69 | 69 | $parameters = $reflection->getParameters(); |
| 70 | - if (\count($parameters) === 0) { |
|
| 70 | + if (\count($parameters) === 0){ |
|
| 71 | 71 | return; |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | $parameter = null; |
| 75 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 75 | + while (\count($parameters) > 0 || \count($arguments) > 0){ |
|
| 76 | 76 | // get related argument value |
| 77 | 77 | $key = \key($arguments); |
| 78 | 78 | |
| 79 | 79 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 80 | 80 | // But you can't send positional argument after named in any case |
| 81 | - if (\is_int($key) && !$positional) { |
|
| 81 | + if (\is_int($key) && !$positional){ |
|
| 82 | 82 | throw new PositionalArgumentException($reflection, $key); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $positional = $positional && \is_int($key); |
| 86 | 86 | |
| 87 | - if (!$variadic) { |
|
| 87 | + if (!$variadic){ |
|
| 88 | 88 | $parameter = \array_shift($parameters); |
| 89 | 89 | $variadic = $parameter?->isVariadic() ?? false; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | - if ($parameter === null) { |
|
| 92 | + if ($parameter === null){ |
|
| 93 | 93 | throw new UnknownParameterException($reflection, $key); |
| 94 | 94 | } |
| 95 | 95 | $name = $parameter->getName(); |
| 96 | 96 | |
| 97 | - if (($positional || $variadic) && $key !== null) { |
|
| 97 | + if (($positional || $variadic) && $key !== null){ |
|
| 98 | 98 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 99 | 99 | $value = \array_shift($arguments); |
| 100 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 101 | - if ($parameter->isOptional()) { |
|
| 100 | + } elseif ($key === null || !\array_key_exists($name, $arguments)){ |
|
| 101 | + if ($parameter->isOptional()){ |
|
| 102 | 102 | continue; |
| 103 | 103 | } |
| 104 | 104 | throw new MissingRequiredArgumentException($reflection, $name); |
| 105 | - } else { |
|
| 105 | + }else{ |
|
| 106 | 106 | $value = &$arguments[$name]; |
| 107 | 107 | unset($arguments[$name]); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 110 | + if (!$this->validateValueToParameter($parameter, $value)){ |
|
| 111 | 111 | throw new InvalidArgumentException($reflection, $name); |
| 112 | 112 | } |
| 113 | 113 | } |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | |
| 116 | 116 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 117 | 117 | { |
| 118 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 118 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)){ |
|
| 119 | 119 | return true; |
| 120 | 120 | } |
| 121 | 121 | $type = $parameter->getType(); |
@@ -126,17 +126,17 @@ discard block |
||
| 126 | 126 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 127 | 127 | }; |
| 128 | 128 | |
| 129 | - foreach ($types as $t) { |
|
| 129 | + foreach ($types as $t){ |
|
| 130 | 130 | \assert($t instanceof ReflectionNamedType); |
| 131 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 131 | + if (!$this->validateValueNamedType($t, $value)){ |
|
| 132 | 132 | // If it is TypeIntersection |
| 133 | - if ($or) { |
|
| 133 | + if ($or){ |
|
| 134 | 134 | continue; |
| 135 | 135 | } |
| 136 | 136 | return false; |
| 137 | 137 | } |
| 138 | 138 | // If it is not type intersection then we can skip that value after first successful check |
| 139 | - if ($or) { |
|
| 139 | + if ($or){ |
|
| 140 | 140 | return true; |
| 141 | 141 | } |
| 142 | 142 | } |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | { |
| 152 | 152 | $name = $type->getName(); |
| 153 | 153 | |
| 154 | - if ($type->isBuiltin()) { |
|
| 154 | + if ($type->isBuiltin()){ |
|
| 155 | 155 | return match ($name) { |
| 156 | 156 | 'mixed' => true, |
| 157 | 157 | 'string' => \is_string($value), |
@@ -184,13 +184,13 @@ discard block |
||
| 184 | 184 | |
| 185 | 185 | // Try to resolve parameter by name |
| 186 | 186 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
| 187 | - if ($res !== [] || $isVariadic) { |
|
| 187 | + if ($res !== [] || $isVariadic){ |
|
| 188 | 188 | // validate |
| 189 | - if ($isVariadic) { |
|
| 190 | - foreach ($res as $k => &$v) { |
|
| 191 | - $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
|
| 189 | + if ($isVariadic){ |
|
| 190 | + foreach ($res as $k => &$v){ |
|
| 191 | + $this->processArgument($state, $v, validateWith: $validate ? $param : null, key : $k); |
|
| 192 | 192 | } |
| 193 | - } else { |
|
| 193 | + }else{ |
|
| 194 | 194 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
| 195 | 195 | } |
| 196 | 196 | |
@@ -198,40 +198,40 @@ discard block |
||
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | $error = null; |
| 201 | - while ($hasType) { |
|
| 201 | + while ($hasType){ |
|
| 202 | 202 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $refType */ |
| 203 | 203 | $refType = $param->getType(); |
| 204 | 204 | |
| 205 | - if ($refType::class === ReflectionNamedType::class) { |
|
| 206 | - if ($refType->isBuiltin()) { |
|
| 205 | + if ($refType::class === ReflectionNamedType::class){ |
|
| 206 | + if ($refType->isBuiltin()){ |
|
| 207 | 207 | break; |
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | if (\interface_exists($refType->getName()) && !empty( |
| 211 | 211 | $attrs = $param->getAttributes(ProxyAttribute::class) |
| 212 | - )) { |
|
| 212 | + )){ |
|
| 213 | 213 | $proxy = Proxy::create(new \ReflectionClass($refType->getName()), $param, $attrs[0]->newInstance()); |
| 214 | 214 | $this->processArgument($state, $proxy); |
| 215 | 215 | return true; |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - try { |
|
| 219 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
| 218 | + try{ |
|
| 219 | + if ($this->resolveObject($state, $refType, $param, $validate)){ |
|
| 220 | 220 | return true; |
| 221 | 221 | } |
| 222 | - } catch (Throwable $e) { |
|
| 222 | + }catch (Throwable $e){ |
|
| 223 | 223 | $error = $e; |
| 224 | 224 | } |
| 225 | 225 | break; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - if ($refType::class === ReflectionUnionType::class) { |
|
| 229 | - foreach ($refType->getTypes() as $namedType) { |
|
| 230 | - try { |
|
| 231 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
| 228 | + if ($refType::class === ReflectionUnionType::class){ |
|
| 229 | + foreach ($refType->getTypes() as $namedType){ |
|
| 230 | + try{ |
|
| 231 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)){ |
|
| 232 | 232 | return true; |
| 233 | 233 | } |
| 234 | - } catch (Throwable $e) { |
|
| 234 | + }catch (Throwable $e){ |
|
| 235 | 235 | $error = $e; |
| 236 | 236 | } |
| 237 | 237 | } |
@@ -241,19 +241,19 @@ discard block |
||
| 241 | 241 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - if ($param->isDefaultValueAvailable()) { |
|
| 244 | + if ($param->isDefaultValueAvailable()){ |
|
| 245 | 245 | $argument = $param->getDefaultValue(); |
| 246 | 246 | $this->processArgument($state, $argument); |
| 247 | 247 | return true; |
| 248 | 248 | } |
| 249 | 249 | |
| 250 | - if ($hasType && $param->allowsNull()) { |
|
| 250 | + if ($hasType && $param->allowsNull()){ |
|
| 251 | 251 | $argument = null; |
| 252 | 252 | $this->processArgument($state, $argument); |
| 253 | 253 | return true; |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | - if ($error === null) { |
|
| 256 | + if ($error === null){ |
|
| 257 | 257 | return false; |
| 258 | 258 | } |
| 259 | 259 | |
@@ -291,15 +291,15 @@ discard block |
||
| 291 | 291 | ResolvingState $state, |
| 292 | 292 | mixed &$value, |
| 293 | 293 | ReflectionParameter $validateWith = null, |
| 294 | - int|string $key = null |
|
| 294 | + int | string $key = null |
|
| 295 | 295 | ): void { |
| 296 | 296 | // Resolve Autowire objects |
| 297 | - if ($value instanceof Autowire) { |
|
| 297 | + if ($value instanceof Autowire){ |
|
| 298 | 298 | $value = $value->resolve($this->factory); |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | // Validation |
| 302 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 302 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)){ |
|
| 303 | 303 | throw new InvalidArgumentException( |
| 304 | 304 | $validateWith->getDeclaringFunction(), |
| 305 | 305 | $validateWith->getName() |
@@ -53,7 +53,8 @@ discard block |
||
| 53 | 53 | ): array { |
| 54 | 54 | $state = new ResolvingState($reflection, $parameters); |
| 55 | 55 | |
| 56 | - foreach ($reflection->getParameters() as $parameter) { |
|
| 56 | + foreach ($reflection->getParameters() as $parameter) |
|
| 57 | + { |
|
| 57 | 58 | $this->resolveParameter($parameter, $state, $validate) |
| 58 | 59 | or |
| 59 | 60 | throw new ArgumentResolvingException($reflection, $parameter->getName()); |
@@ -67,47 +68,59 @@ discard block |
||
| 67 | 68 | $positional = true; |
| 68 | 69 | $variadic = false; |
| 69 | 70 | $parameters = $reflection->getParameters(); |
| 70 | - if (\count($parameters) === 0) { |
|
| 71 | + if (\count($parameters) === 0) |
|
| 72 | + { |
|
| 71 | 73 | return; |
| 72 | 74 | } |
| 73 | 75 | |
| 74 | 76 | $parameter = null; |
| 75 | - while (\count($parameters) > 0 || \count($arguments) > 0) { |
|
| 77 | + while (\count($parameters) > 0 || \count($arguments) > 0) |
|
| 78 | + { |
|
| 76 | 79 | // get related argument value |
| 77 | 80 | $key = \key($arguments); |
| 78 | 81 | |
| 79 | 82 | // For a variadic parameter it's no sense - named or positional argument will be sent |
| 80 | 83 | // But you can't send positional argument after named in any case |
| 81 | - if (\is_int($key) && !$positional) { |
|
| 84 | + if (\is_int($key) && !$positional) |
|
| 85 | + { |
|
| 82 | 86 | throw new PositionalArgumentException($reflection, $key); |
| 83 | 87 | } |
| 84 | 88 | |
| 85 | 89 | $positional = $positional && \is_int($key); |
| 86 | 90 | |
| 87 | - if (!$variadic) { |
|
| 91 | + if (!$variadic) |
|
| 92 | + { |
|
| 88 | 93 | $parameter = \array_shift($parameters); |
| 89 | 94 | $variadic = $parameter?->isVariadic() ?? false; |
| 90 | 95 | } |
| 91 | 96 | |
| 92 | - if ($parameter === null) { |
|
| 97 | + if ($parameter === null) |
|
| 98 | + { |
|
| 93 | 99 | throw new UnknownParameterException($reflection, $key); |
| 94 | 100 | } |
| 95 | 101 | $name = $parameter->getName(); |
| 96 | 102 | |
| 97 | - if (($positional || $variadic) && $key !== null) { |
|
| 103 | + if (($positional || $variadic) && $key !== null) |
|
| 104 | + { |
|
| 98 | 105 | /** @psalm-suppress ReferenceReusedFromConfusingScope */ |
| 99 | 106 | $value = \array_shift($arguments); |
| 100 | - } elseif ($key === null || !\array_key_exists($name, $arguments)) { |
|
| 101 | - if ($parameter->isOptional()) { |
|
| 107 | + } |
|
| 108 | + elseif ($key === null || !\array_key_exists($name, $arguments)) |
|
| 109 | + { |
|
| 110 | + if ($parameter->isOptional()) |
|
| 111 | + { |
|
| 102 | 112 | continue; |
| 103 | 113 | } |
| 104 | 114 | throw new MissingRequiredArgumentException($reflection, $name); |
| 105 | - } else { |
|
| 115 | + } |
|
| 116 | + else |
|
| 117 | + { |
|
| 106 | 118 | $value = &$arguments[$name]; |
| 107 | 119 | unset($arguments[$name]); |
| 108 | 120 | } |
| 109 | 121 | |
| 110 | - if (!$this->validateValueToParameter($parameter, $value)) { |
|
| 122 | + if (!$this->validateValueToParameter($parameter, $value)) |
|
| 123 | + { |
|
| 111 | 124 | throw new InvalidArgumentException($reflection, $name); |
| 112 | 125 | } |
| 113 | 126 | } |
@@ -115,7 +128,8 @@ discard block |
||
| 115 | 128 | |
| 116 | 129 | private function validateValueToParameter(ReflectionParameter $parameter, mixed $value): bool |
| 117 | 130 | { |
| 118 | - if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) { |
|
| 131 | + if (!$parameter->hasType() || ($parameter->allowsNull() && $value === null)) |
|
| 132 | + { |
|
| 119 | 133 | return true; |
| 120 | 134 | } |
| 121 | 135 | $type = $parameter->getType(); |
@@ -126,17 +140,21 @@ discard block |
||
| 126 | 140 | $type instanceof ReflectionIntersectionType => [false, $type->getTypes()], |
| 127 | 141 | }; |
| 128 | 142 | |
| 129 | - foreach ($types as $t) { |
|
| 143 | + foreach ($types as $t) |
|
| 144 | + { |
|
| 130 | 145 | \assert($t instanceof ReflectionNamedType); |
| 131 | - if (!$this->validateValueNamedType($t, $value)) { |
|
| 146 | + if (!$this->validateValueNamedType($t, $value)) |
|
| 147 | + { |
|
| 132 | 148 | // If it is TypeIntersection |
| 133 | - if ($or) { |
|
| 149 | + if ($or) |
|
| 150 | + { |
|
| 134 | 151 | continue; |
| 135 | 152 | } |
| 136 | 153 | return false; |
| 137 | 154 | } |
| 138 | 155 | // If it is not type intersection then we can skip that value after first successful check |
| 139 | - if ($or) { |
|
| 156 | + if ($or) |
|
| 157 | + { |
|
| 140 | 158 | return true; |
| 141 | 159 | } |
| 142 | 160 | } |
@@ -151,7 +169,8 @@ discard block |
||
| 151 | 169 | { |
| 152 | 170 | $name = $type->getName(); |
| 153 | 171 | |
| 154 | - if ($type->isBuiltin()) { |
|
| 172 | + if ($type->isBuiltin()) |
|
| 173 | + { |
|
| 155 | 174 | return match ($name) { |
| 156 | 175 | 'mixed' => true, |
| 157 | 176 | 'string' => \is_string($value), |
@@ -184,13 +203,18 @@ discard block |
||
| 184 | 203 | |
| 185 | 204 | // Try to resolve parameter by name |
| 186 | 205 | $res = $state->resolveParameterByNameOrPosition($param, $isVariadic); |
| 187 | - if ($res !== [] || $isVariadic) { |
|
| 206 | + if ($res !== [] || $isVariadic) |
|
| 207 | + { |
|
| 188 | 208 | // validate |
| 189 | - if ($isVariadic) { |
|
| 190 | - foreach ($res as $k => &$v) { |
|
| 209 | + if ($isVariadic) |
|
| 210 | + { |
|
| 211 | + foreach ($res as $k => &$v) |
|
| 212 | + { |
|
| 191 | 213 | $this->processArgument($state, $v, validateWith: $validate ? $param : null, key: $k); |
| 192 | 214 | } |
| 193 | - } else { |
|
| 215 | + } |
|
| 216 | + else |
|
| 217 | + { |
|
| 194 | 218 | $this->processArgument($state, $res[0], validateWith: $validate ? $param : null); |
| 195 | 219 | } |
| 196 | 220 | |
@@ -198,40 +222,54 @@ discard block |
||
| 198 | 222 | } |
| 199 | 223 | |
| 200 | 224 | $error = null; |
| 201 | - while ($hasType) { |
|
| 225 | + while ($hasType) |
|
| 226 | + { |
|
| 202 | 227 | /** @var ReflectionIntersectionType|ReflectionUnionType|ReflectionNamedType $refType */ |
| 203 | 228 | $refType = $param->getType(); |
| 204 | 229 | |
| 205 | - if ($refType::class === ReflectionNamedType::class) { |
|
| 206 | - if ($refType->isBuiltin()) { |
|
| 230 | + if ($refType::class === ReflectionNamedType::class) |
|
| 231 | + { |
|
| 232 | + if ($refType->isBuiltin()) |
|
| 233 | + { |
|
| 207 | 234 | break; |
| 208 | 235 | } |
| 209 | 236 | |
| 210 | 237 | if (\interface_exists($refType->getName()) && !empty( |
| 211 | 238 | $attrs = $param->getAttributes(ProxyAttribute::class) |
| 212 | - )) { |
|
| 239 | + )) |
|
| 240 | + { |
|
| 213 | 241 | $proxy = Proxy::create(new \ReflectionClass($refType->getName()), $param, $attrs[0]->newInstance()); |
| 214 | 242 | $this->processArgument($state, $proxy); |
| 215 | 243 | return true; |
| 216 | 244 | } |
| 217 | 245 | |
| 218 | - try { |
|
| 219 | - if ($this->resolveObject($state, $refType, $param, $validate)) { |
|
| 246 | + try |
|
| 247 | + { |
|
| 248 | + if ($this->resolveObject($state, $refType, $param, $validate)) |
|
| 249 | + { |
|
| 220 | 250 | return true; |
| 221 | 251 | } |
| 222 | - } catch (Throwable $e) { |
|
| 252 | + } |
|
| 253 | + catch (Throwable $e) |
|
| 254 | + { |
|
| 223 | 255 | $error = $e; |
| 224 | 256 | } |
| 225 | 257 | break; |
| 226 | 258 | } |
| 227 | 259 | |
| 228 | - if ($refType::class === ReflectionUnionType::class) { |
|
| 229 | - foreach ($refType->getTypes() as $namedType) { |
|
| 230 | - try { |
|
| 231 | - if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) { |
|
| 260 | + if ($refType::class === ReflectionUnionType::class) |
|
| 261 | + { |
|
| 262 | + foreach ($refType->getTypes() as $namedType) |
|
| 263 | + { |
|
| 264 | + try |
|
| 265 | + { |
|
| 266 | + if (!$namedType->isBuiltin() && $this->resolveObject($state, $namedType, $param, $validate)) |
|
| 267 | + { |
|
| 232 | 268 | return true; |
| 233 | 269 | } |
| 234 | - } catch (Throwable $e) { |
|
| 270 | + } |
|
| 271 | + catch (Throwable $e) |
|
| 272 | + { |
|
| 235 | 273 | $error = $e; |
| 236 | 274 | } |
| 237 | 275 | } |
@@ -241,19 +279,22 @@ discard block |
||
| 241 | 279 | throw new UnsupportedTypeException($param->getDeclaringFunction(), $param->getName()); |
| 242 | 280 | } |
| 243 | 281 | |
| 244 | - if ($param->isDefaultValueAvailable()) { |
|
| 282 | + if ($param->isDefaultValueAvailable()) |
|
| 283 | + { |
|
| 245 | 284 | $argument = $param->getDefaultValue(); |
| 246 | 285 | $this->processArgument($state, $argument); |
| 247 | 286 | return true; |
| 248 | 287 | } |
| 249 | 288 | |
| 250 | - if ($hasType && $param->allowsNull()) { |
|
| 289 | + if ($hasType && $param->allowsNull()) |
|
| 290 | + { |
|
| 251 | 291 | $argument = null; |
| 252 | 292 | $this->processArgument($state, $argument); |
| 253 | 293 | return true; |
| 254 | 294 | } |
| 255 | 295 | |
| 256 | - if ($error === null) { |
|
| 296 | + if ($error === null) |
|
| 297 | + { |
|
| 257 | 298 | return false; |
| 258 | 299 | } |
| 259 | 300 | |
@@ -294,12 +335,14 @@ discard block |
||
| 294 | 335 | int|string $key = null |
| 295 | 336 | ): void { |
| 296 | 337 | // Resolve Autowire objects |
| 297 | - if ($value instanceof Autowire) { |
|
| 338 | + if ($value instanceof Autowire) |
|
| 339 | + { |
|
| 298 | 340 | $value = $value->resolve($this->factory); |
| 299 | 341 | } |
| 300 | 342 | |
| 301 | 343 | // Validation |
| 302 | - if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) { |
|
| 344 | + if ($validateWith !== null && !$this->validateValueToParameter($validateWith, $value)) |
|
| 345 | + { |
|
| 303 | 346 | throw new InvalidArgumentException( |
| 304 | 347 | $validateWith->getDeclaringFunction(), |
| 305 | 348 | $validateWith->getName() |
@@ -11,33 +11,33 @@ discard block |
||
| 11 | 11 | { |
| 12 | 12 | public static function renderClass(\ReflectionClass $type, string $className, array $traits = []): string |
| 13 | 13 | { |
| 14 | - if (\str_contains($className, '\\')) { |
|
| 14 | + if (\str_contains($className, '\\')){ |
|
| 15 | 15 | $classShortName = \substr($className, \strrpos($className, '\\') + 1); |
| 16 | - $classNamespaceStr = 'namespace ' . \substr($className, 0, \strrpos($className, '\\')) . ';'; |
|
| 17 | - } else { |
|
| 16 | + $classNamespaceStr = 'namespace '.\substr($className, 0, \strrpos($className, '\\')).';'; |
|
| 17 | + }else{ |
|
| 18 | 18 | $classShortName = $className; |
| 19 | 19 | $classNamespaceStr = ''; |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | 22 | $interface = $type->getName(); |
| 23 | 23 | $classBody = []; |
| 24 | - foreach ($type->getMethods() as $method) { |
|
| 25 | - if ($method->isConstructor()) { |
|
| 24 | + foreach ($type->getMethods() as $method){ |
|
| 25 | + if ($method->isConstructor()){ |
|
| 26 | 26 | continue; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | $hasRefs = false; |
| 30 | 30 | $return = $method->hasReturnType() && (string)$method->getReturnType() === 'void' ? '' : 'return '; |
| 31 | - $call = ($method->isStatic() ? '::' : '->') . $method->getName(); |
|
| 31 | + $call = ($method->isStatic() ? '::' : '->').$method->getName(); |
|
| 32 | 32 | $context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
| 33 | 33 | |
| 34 | 34 | $args = []; |
| 35 | - foreach ($method->getParameters() as $param) { |
|
| 35 | + foreach ($method->getParameters() as $param){ |
|
| 36 | 36 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
| 37 | - $args[] = ($param->isVariadic() ? '...' : '') . '$'. $param->getName(); |
|
| 37 | + $args[] = ($param->isVariadic() ? '...' : '').'$'.$param->getName(); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - if (!$hasRefs && !$method->isVariadic()) { |
|
| 40 | + if (!$hasRefs && !$method->isVariadic()){ |
|
| 41 | 41 | $classBody[] = self::renderMethod($method, <<<PHP |
| 42 | 42 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
| 43 | 43 | '{$interface}', |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | |
| 50 | 50 | $argsStr = \implode(', ', $args); |
| 51 | 51 | |
| 52 | - if ($method->isVariadic()) { |
|
| 52 | + if ($method->isVariadic()){ |
|
| 53 | 53 | $classBody[] = self::renderMethod($method, <<<PHP |
| 54 | 54 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
| 55 | 55 | '{$interface}', |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | $bodyStr = \implode("\n\n", $classBody); |
| 70 | 70 | |
| 71 | - $traitsStr = $traits === [] ? '' : \implode("\n ", \array_map(fn(string $trait): string => |
|
| 72 | - 'use \\' . \ltrim($trait, '\\') . ';', $traits)); |
|
| 71 | + $traitsStr = $traits === [] ? '' : \implode("\n ", \array_map(fn(string $trait) : string => |
|
| 72 | + 'use \\'.\ltrim($trait, '\\').';', $traits)); |
|
| 73 | 73 | return <<<PHP |
| 74 | 74 | $classNamespaceStr |
| 75 | 75 | |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | $m->getName(), |
| 92 | 92 | \implode(', ', \array_map([self::class, 'renderParameter'], $m->getParameters())), |
| 93 | 93 | $m->hasReturnType() |
| 94 | - ? ': ' . self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
|
| 94 | + ? ': '.self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
|
| 95 | 95 | : '', |
| 96 | 96 | $body, |
| 97 | 97 | ); |
@@ -104,15 +104,15 @@ discard block |
||
| 104 | 104 | $param->hasType() ? self::renderParameterTypes($param->getType(), $param->getDeclaringClass()) : '', |
| 105 | 105 | $param->isPassedByReference() ? '&' : '', |
| 106 | 106 | $param->isVariadic() ? '...' : '', |
| 107 | - '$' . $param->getName(), |
|
| 108 | - $param->isOptional() && !$param->isVariadic() ? ' = ' . self::renderDefaultValue($param) : '', |
|
| 107 | + '$'.$param->getName(), |
|
| 108 | + $param->isOptional() && !$param->isVariadic() ? ' = '.self::renderDefaultValue($param) : '', |
|
| 109 | 109 | ), ' '); |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | public static function renderParameterTypes(\ReflectionType $types, \ReflectionClass $class): string |
| 113 | 113 | { |
| 114 | - if ($types instanceof \ReflectionNamedType) { |
|
| 115 | - return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '') . ($types->isBuiltin() |
|
| 114 | + if ($types instanceof \ReflectionNamedType){ |
|
| 115 | + return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '').($types->isBuiltin() |
|
| 116 | 116 | ? $types->getName() |
| 117 | 117 | : self::normalizeClassType($types, $class)); |
| 118 | 118 | } |
@@ -124,7 +124,7 @@ discard block |
||
| 124 | 124 | }; |
| 125 | 125 | |
| 126 | 126 | $result = []; |
| 127 | - foreach ($types as $type) { |
|
| 127 | + foreach ($types as $type){ |
|
| 128 | 128 | $result[] = $type->isBuiltin() |
| 129 | 129 | ? $type->getName() |
| 130 | 130 | : self::normalizeClassType($type, $class); |
@@ -135,12 +135,12 @@ discard block |
||
| 135 | 135 | |
| 136 | 136 | public static function renderDefaultValue(\ReflectionParameter $param): string |
| 137 | 137 | { |
| 138 | - if ($param->isDefaultValueConstant()) { |
|
| 138 | + if ($param->isDefaultValueConstant()){ |
|
| 139 | 139 | $result = $param->getDefaultValueConstantName(); |
| 140 | 140 | |
| 141 | 141 | return \explode('::', $result)[0] === 'self' |
| 142 | 142 | ? $result |
| 143 | - : '\\' . $result; |
|
| 143 | + : '\\'.$result; |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $cut = self::cutDefaultValue($param); |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | |
| 153 | 153 | public static function normalizeClassType(\ReflectionNamedType $type, \ReflectionClass $class): string |
| 154 | 154 | { |
| 155 | - return '\\' . ($type->getName() === 'self' ? $class->getName() : $type->getName()); |
|
| 155 | + return '\\'.($type->getName() === 'self' ? $class->getName() : $type->getName()); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | private static function cutDefaultValue(\ReflectionParameter $param): string |
@@ -11,18 +11,23 @@ discard block |
||
| 11 | 11 | { |
| 12 | 12 | public static function renderClass(\ReflectionClass $type, string $className, array $traits = []): string |
| 13 | 13 | { |
| 14 | - if (\str_contains($className, '\\')) { |
|
| 14 | + if (\str_contains($className, '\\')) |
|
| 15 | + { |
|
| 15 | 16 | $classShortName = \substr($className, \strrpos($className, '\\') + 1); |
| 16 | 17 | $classNamespaceStr = 'namespace ' . \substr($className, 0, \strrpos($className, '\\')) . ';'; |
| 17 | - } else { |
|
| 18 | + } |
|
| 19 | + else |
|
| 20 | + { |
|
| 18 | 21 | $classShortName = $className; |
| 19 | 22 | $classNamespaceStr = ''; |
| 20 | 23 | } |
| 21 | 24 | |
| 22 | 25 | $interface = $type->getName(); |
| 23 | 26 | $classBody = []; |
| 24 | - foreach ($type->getMethods() as $method) { |
|
| 25 | - if ($method->isConstructor()) { |
|
| 27 | + foreach ($type->getMethods() as $method) |
|
| 28 | + { |
|
| 29 | + if ($method->isConstructor()) |
|
| 30 | + { |
|
| 26 | 31 | continue; |
| 27 | 32 | } |
| 28 | 33 | |
@@ -32,12 +37,14 @@ discard block |
||
| 32 | 37 | $context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
| 33 | 38 | |
| 34 | 39 | $args = []; |
| 35 | - foreach ($method->getParameters() as $param) { |
|
| 40 | + foreach ($method->getParameters() as $param) |
|
| 41 | + { |
|
| 36 | 42 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
| 37 | 43 | $args[] = ($param->isVariadic() ? '...' : '') . '$'. $param->getName(); |
| 38 | 44 | } |
| 39 | 45 | |
| 40 | - if (!$hasRefs && !$method->isVariadic()) { |
|
| 46 | + if (!$hasRefs && !$method->isVariadic()) |
|
| 47 | + { |
|
| 41 | 48 | $classBody[] = self::renderMethod($method, <<<PHP |
| 42 | 49 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
| 43 | 50 | '{$interface}', |
@@ -49,7 +56,8 @@ discard block |
||
| 49 | 56 | |
| 50 | 57 | $argsStr = \implode(', ', $args); |
| 51 | 58 | |
| 52 | - if ($method->isVariadic()) { |
|
| 59 | + if ($method->isVariadic()) |
|
| 60 | + { |
|
| 53 | 61 | $classBody[] = self::renderMethod($method, <<<PHP |
| 54 | 62 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
| 55 | 63 | '{$interface}', |
@@ -111,7 +119,8 @@ discard block |
||
| 111 | 119 | |
| 112 | 120 | public static function renderParameterTypes(\ReflectionType $types, \ReflectionClass $class): string |
| 113 | 121 | { |
| 114 | - if ($types instanceof \ReflectionNamedType) { |
|
| 122 | + if ($types instanceof \ReflectionNamedType) |
|
| 123 | + { |
|
| 115 | 124 | return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '') . ($types->isBuiltin() |
| 116 | 125 | ? $types->getName() |
| 117 | 126 | : self::normalizeClassType($types, $class)); |
@@ -124,7 +133,8 @@ discard block |
||
| 124 | 133 | }; |
| 125 | 134 | |
| 126 | 135 | $result = []; |
| 127 | - foreach ($types as $type) { |
|
| 136 | + foreach ($types as $type) |
|
| 137 | + { |
|
| 128 | 138 | $result[] = $type->isBuiltin() |
| 129 | 139 | ? $type->getName() |
| 130 | 140 | : self::normalizeClassType($type, $class); |
@@ -135,7 +145,8 @@ discard block |
||
| 135 | 145 | |
| 136 | 146 | public static function renderDefaultValue(\ReflectionParameter $param): string |
| 137 | 147 | { |
| 138 | - if ($param->isDefaultValueConstant()) { |
|
| 148 | + if ($param->isDefaultValueConstant()) |
|
| 149 | + { |
|
| 139 | 150 | $result = $param->getDefaultValueConstantName(); |
| 140 | 151 | |
| 141 | 152 | return \explode('::', $result)[0] === 'self' |
@@ -13,5 +13,5 @@ |
||
| 13 | 13 | trait ProxyTrait |
| 14 | 14 | { |
| 15 | 15 | private static string $__container_proxy_alias; |
| 16 | - private \Stringable|string|null $__container_proxy_context = null; |
|
| 16 | + private \Stringable | string | null $__container_proxy_context = null; |
|
| 17 | 17 | } |
@@ -17,6 +17,6 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | public function __construct( |
| 19 | 19 | public bool $magicCall = false, |
| 20 | - ) { |
|
| 20 | + ){ |
|
| 21 | 21 | } |
| 22 | 22 | } |