@@ -12,15 +12,15 @@ |
||
12 | 12 | */ |
13 | 13 | final class Resolver |
14 | 14 | { |
15 | - public static function resolve(string $alias, \Stringable|string|null $context = null): object |
|
15 | + public static function resolve(string $alias, \Stringable | string | null $context = null): object |
|
16 | 16 | { |
17 | 17 | $c = ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
18 | 18 | |
19 | - try { |
|
19 | + try{ |
|
20 | 20 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
21 | 21 | 'Resolved `null` from the container.', |
22 | 22 | ); |
23 | - } catch (ContainerException $e) { |
|
23 | + }catch (ContainerException $e){ |
|
24 | 24 | throw new ContainerException( |
25 | 25 | // todo : find required scope |
26 | 26 | \sprintf('Unable to resolve `%s` in a Proxy.', $alias), |
@@ -16,11 +16,14 @@ |
||
16 | 16 | { |
17 | 17 | $c = ContainerScope::getContainer() ?? throw new ContainerException('Proxy is out of scope.'); |
18 | 18 | |
19 | - try { |
|
19 | + try |
|
20 | + { |
|
20 | 21 | $result = $c->get($alias, $context) ?? throw new ContainerException( |
21 | 22 | 'Resolved `null` from the container.', |
22 | 23 | ); |
23 | - } catch (ContainerException $e) { |
|
24 | + } |
|
25 | + catch (ContainerException $e) |
|
26 | + { |
|
24 | 27 | throw new ContainerException( |
25 | 28 | // todo : find required scope |
26 | 29 | \sprintf('Unable to resolve `%s` in a Proxy.', $alias), |
@@ -13,7 +13,7 @@ |
||
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 | |
18 | 18 | public function __call(string $name, array $arguments) |
19 | 19 | { |
@@ -19,19 +19,19 @@ discard block |
||
19 | 19 | * @param \ReflectionClass<TClass> $type |
20 | 20 | * @return TClass |
21 | 21 | */ |
22 | - public static function create(\ReflectionClass $type, \Stringable|string|null $context): object |
|
22 | + public static function create(\ReflectionClass $type, \Stringable | string | null $context): object |
|
23 | 23 | { |
24 | 24 | $interface = $type->getName(); |
25 | 25 | |
26 | - if (!\array_key_exists($interface, self::$cache)) { |
|
26 | + if (!\array_key_exists($interface, self::$cache)){ |
|
27 | 27 | /** @var class-string<TClass> $className */ |
28 | 28 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
29 | 29 | |
30 | - try { |
|
30 | + try{ |
|
31 | 31 | $classString = ProxyClassRenderer::renderClass($type, $className); |
32 | 32 | |
33 | 33 | eval($classString); |
34 | - } catch (\Throwable $e) { |
|
34 | + }catch (\Throwable $e){ |
|
35 | 35 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
36 | 36 | } |
37 | 37 | |
@@ -40,12 +40,12 @@ discard block |
||
40 | 40 | |
41 | 41 | // Store in cache without context |
42 | 42 | self::$cache[$interface] = $instance; |
43 | - } else { |
|
43 | + }else{ |
|
44 | 44 | /** @var TClass $instance */ |
45 | 45 | $instance = self::$cache[$interface]; |
46 | 46 | } |
47 | 47 | |
48 | - if ($context !== null) { |
|
48 | + if ($context !== null){ |
|
49 | 49 | $instance = clone $instance; |
50 | 50 | (static fn () => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
51 | 51 | } |
@@ -23,15 +23,19 @@ discard block |
||
23 | 23 | { |
24 | 24 | $interface = $type->getName(); |
25 | 25 | |
26 | - if (!\array_key_exists($interface, self::$cache)) { |
|
26 | + if (!\array_key_exists($interface, self::$cache)) |
|
27 | + { |
|
27 | 28 | /** @var class-string<TClass> $className */ |
28 | 29 | $className = "{$type->getNamespaceName()}\\{$type->getShortName()} SCOPED PROXY"; |
29 | 30 | |
30 | - try { |
|
31 | + try |
|
32 | + { |
|
31 | 33 | $classString = ProxyClassRenderer::renderClass($type, $className); |
32 | 34 | |
33 | 35 | eval($classString); |
34 | - } catch (\Throwable $e) { |
|
36 | + } |
|
37 | + catch (\Throwable $e) |
|
38 | + { |
|
35 | 39 | throw new \Error("Unable to create proxy for `{$interface}`: {$e->getMessage()}", 0, $e); |
36 | 40 | } |
37 | 41 | |
@@ -40,12 +44,15 @@ discard block |
||
40 | 44 | |
41 | 45 | // Store in cache without context |
42 | 46 | self::$cache[$interface] = $instance; |
43 | - } else { |
|
47 | + } |
|
48 | + else |
|
49 | + { |
|
44 | 50 | /** @var TClass $instance */ |
45 | 51 | $instance = self::$cache[$interface]; |
46 | 52 | } |
47 | 53 | |
48 | - if ($context !== null) { |
|
54 | + if ($context !== null) |
|
55 | + { |
|
49 | 56 | $instance = clone $instance; |
50 | 57 | (static fn () => $instance->__container_proxy_context = $context)->bindTo(null, $instance::class)(); |
51 | 58 | } |
@@ -16,23 +16,23 @@ discard block |
||
16 | 16 | |
17 | 17 | $interface = $type->getName(); |
18 | 18 | $classBody = []; |
19 | - foreach ($type->getMethods() as $method) { |
|
20 | - if ($method->isConstructor()) { |
|
19 | + foreach ($type->getMethods() as $method){ |
|
20 | + if ($method->isConstructor()){ |
|
21 | 21 | continue; |
22 | 22 | } |
23 | 23 | |
24 | 24 | $hasRefs = false; |
25 | 25 | $return = $method->hasReturnType() && (string)$method->getReturnType() === 'void' ? '' : 'return '; |
26 | - $call = ($method->isStatic() ? '::' : '->') . $method->getName(); |
|
26 | + $call = ($method->isStatic() ? '::' : '->').$method->getName(); |
|
27 | 27 | $context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
28 | 28 | |
29 | 29 | $args = []; |
30 | - foreach ($method->getParameters() as $param) { |
|
30 | + foreach ($method->getParameters() as $param){ |
|
31 | 31 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
32 | - $args[] = ($param->isVariadic() ? '...' : '') . '$' . $param->getName(); |
|
32 | + $args[] = ($param->isVariadic() ? '...' : '').'$'.$param->getName(); |
|
33 | 33 | } |
34 | 34 | |
35 | - if (!$hasRefs && !$method->isVariadic()) { |
|
35 | + if (!$hasRefs && !$method->isVariadic()){ |
|
36 | 36 | $classBody[] = self::renderMethod($method, <<<PHP |
37 | 37 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
38 | 38 | '{$interface}', |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | |
45 | 45 | $argsStr = \implode(', ', $args); |
46 | 46 | |
47 | - if ($method->isVariadic()) { |
|
47 | + if ($method->isVariadic()){ |
|
48 | 48 | $classBody[] = self::renderMethod($method, <<<PHP |
49 | 49 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
50 | 50 | '{$interface}', |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $m->getName(), |
84 | 84 | \implode(', ', \array_map([self::class, 'renderParameter'], $m->getParameters())), |
85 | 85 | $m->hasReturnType() |
86 | - ? ': ' . self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
|
86 | + ? ': '.self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
|
87 | 87 | : '', |
88 | 88 | $body, |
89 | 89 | ); |
@@ -96,15 +96,15 @@ discard block |
||
96 | 96 | $param->hasType() ? self::renderParameterTypes($param->getType(), $param->getDeclaringClass()) : '', |
97 | 97 | $param->isPassedByReference() ? '&' : '', |
98 | 98 | $param->isVariadic() ? '...' : '', |
99 | - '$' . $param->getName(), |
|
100 | - $param->isOptional() && !$param->isVariadic() ? ' = ' . self::renderDefaultValue($param) : '', |
|
99 | + '$'.$param->getName(), |
|
100 | + $param->isOptional() && !$param->isVariadic() ? ' = '.self::renderDefaultValue($param) : '', |
|
101 | 101 | ), ' '); |
102 | 102 | } |
103 | 103 | |
104 | 104 | public static function renderParameterTypes(\ReflectionType $types, \ReflectionClass $class): string |
105 | 105 | { |
106 | - if ($types instanceof \ReflectionNamedType) { |
|
107 | - return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '') . ($types->isBuiltin() |
|
106 | + if ($types instanceof \ReflectionNamedType){ |
|
107 | + return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '').($types->isBuiltin() |
|
108 | 108 | ? $types->getName() |
109 | 109 | : self::normalizeClassType($types, $class)); |
110 | 110 | } |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | }; |
117 | 117 | |
118 | 118 | $result = []; |
119 | - foreach ($types as $type) { |
|
119 | + foreach ($types as $type){ |
|
120 | 120 | $result[] = $type->isBuiltin() |
121 | 121 | ? $type->getName() |
122 | 122 | : self::normalizeClassType($type, $class); |
@@ -127,12 +127,12 @@ discard block |
||
127 | 127 | |
128 | 128 | public static function renderDefaultValue(\ReflectionParameter $param): string |
129 | 129 | { |
130 | - if ($param->isDefaultValueConstant()) { |
|
130 | + if ($param->isDefaultValueConstant()){ |
|
131 | 131 | $result = $param->getDefaultValueConstantName(); |
132 | 132 | |
133 | 133 | return \explode('::', $result)[0] === 'self' |
134 | 134 | ? $result |
135 | - : '\\' . $result; |
|
135 | + : '\\'.$result; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | $cut = self::cutDefaultValue($param); |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | |
145 | 145 | public static function normalizeClassType(\ReflectionNamedType $type, \ReflectionClass $class): string |
146 | 146 | { |
147 | - return '\\' . ($type->getName() === 'self' ? $class->getName() : $type->getName()); |
|
147 | + return '\\'.($type->getName() === 'self' ? $class->getName() : $type->getName()); |
|
148 | 148 | } |
149 | 149 | |
150 | 150 | private static function cutDefaultValue(\ReflectionParameter $param): string |
@@ -16,8 +16,10 @@ discard block |
||
16 | 16 | |
17 | 17 | $interface = $type->getName(); |
18 | 18 | $classBody = []; |
19 | - foreach ($type->getMethods() as $method) { |
|
20 | - if ($method->isConstructor()) { |
|
19 | + foreach ($type->getMethods() as $method) |
|
20 | + { |
|
21 | + if ($method->isConstructor()) |
|
22 | + { |
|
21 | 23 | continue; |
22 | 24 | } |
23 | 25 | |
@@ -27,12 +29,14 @@ discard block |
||
27 | 29 | $context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
28 | 30 | |
29 | 31 | $args = []; |
30 | - foreach ($method->getParameters() as $param) { |
|
32 | + foreach ($method->getParameters() as $param) |
|
33 | + { |
|
31 | 34 | $hasRefs = $hasRefs || $param->isPassedByReference(); |
32 | 35 | $args[] = ($param->isVariadic() ? '...' : '') . '$' . $param->getName(); |
33 | 36 | } |
34 | 37 | |
35 | - if (!$hasRefs && !$method->isVariadic()) { |
|
38 | + if (!$hasRefs && !$method->isVariadic()) |
|
39 | + { |
|
36 | 40 | $classBody[] = self::renderMethod($method, <<<PHP |
37 | 41 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
38 | 42 | '{$interface}', |
@@ -44,7 +48,8 @@ discard block |
||
44 | 48 | |
45 | 49 | $argsStr = \implode(', ', $args); |
46 | 50 | |
47 | - if ($method->isVariadic()) { |
|
51 | + if ($method->isVariadic()) |
|
52 | + { |
|
48 | 53 | $classBody[] = self::renderMethod($method, <<<PHP |
49 | 54 | {$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
50 | 55 | '{$interface}', |
@@ -103,7 +108,8 @@ discard block |
||
103 | 108 | |
104 | 109 | public static function renderParameterTypes(\ReflectionType $types, \ReflectionClass $class): string |
105 | 110 | { |
106 | - if ($types instanceof \ReflectionNamedType) { |
|
111 | + if ($types instanceof \ReflectionNamedType) |
|
112 | + { |
|
107 | 113 | return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '') . ($types->isBuiltin() |
108 | 114 | ? $types->getName() |
109 | 115 | : self::normalizeClassType($types, $class)); |
@@ -116,7 +122,8 @@ discard block |
||
116 | 122 | }; |
117 | 123 | |
118 | 124 | $result = []; |
119 | - foreach ($types as $type) { |
|
125 | + foreach ($types as $type) |
|
126 | + { |
|
120 | 127 | $result[] = $type->isBuiltin() |
121 | 128 | ? $type->getName() |
122 | 129 | : self::normalizeClassType($type, $class); |
@@ -127,7 +134,8 @@ discard block |
||
127 | 134 | |
128 | 135 | public static function renderDefaultValue(\ReflectionParameter $param): string |
129 | 136 | { |
130 | - if ($param->isDefaultValueConstant()) { |
|
137 | + if ($param->isDefaultValueConstant()) |
|
138 | + { |
|
131 | 139 | $result = $param->getDefaultValueConstantName(); |
132 | 140 | |
133 | 141 | return \explode('::', $result)[0] === 'self' |