1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal\Proxy; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @internal |
9
|
|
|
*/ |
10
|
|
|
final class ProxyClassRenderer |
11
|
|
|
{ |
12
|
3 |
|
public static function renderClass(\ReflectionClass $type, $className): string |
13
|
|
|
{ |
14
|
3 |
|
$classShortName = \substr($className, \strrpos($className, '\\') + 1); |
15
|
3 |
|
$classNamespace = \substr($className, 0, \strrpos($className, '\\')); |
16
|
|
|
|
17
|
3 |
|
$interface = $type->getName(); |
18
|
3 |
|
$classBody = []; |
19
|
3 |
|
foreach ($type->getMethods() as $method) { |
20
|
3 |
|
if ($method->isConstructor()) { |
21
|
|
|
continue; |
22
|
|
|
} |
23
|
|
|
|
24
|
3 |
|
$hasRefs = false; |
25
|
3 |
|
$return = $method->hasReturnType() && (string)$method->getReturnType() === 'void' ? '' : 'return '; |
26
|
3 |
|
$call = ($method->isStatic() ? '::' : '->') . $method->getName(); |
27
|
3 |
|
$context = $method->isStatic() ? 'null' : '$this->__container_proxy_context'; |
28
|
|
|
|
29
|
3 |
|
$args = []; |
30
|
3 |
|
foreach ($method->getParameters() as $param) { |
31
|
1 |
|
$hasRefs = $hasRefs || $param->isPassedByReference(); |
32
|
1 |
|
$args[] = ($param->isVariadic() ? '...' : '') . '$'. $param->getName(); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
if (!$hasRefs && !$method->isVariadic()) { |
36
|
3 |
|
$classBody[] = self::renderMethod($method, <<<PHP |
37
|
3 |
|
{$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
38
|
3 |
|
'{$interface}', |
39
|
3 |
|
$context, |
40
|
3 |
|
){$call}(...\\func_get_args()); |
41
|
3 |
|
PHP); |
42
|
3 |
|
continue; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$argsStr = \implode(', ', $args); |
46
|
|
|
|
47
|
1 |
|
if ($method->isVariadic()) { |
48
|
1 |
|
$classBody[] = self::renderMethod($method, <<<PHP |
49
|
1 |
|
{$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
50
|
1 |
|
'{$interface}', |
51
|
1 |
|
$context, |
52
|
1 |
|
){$call}($argsStr); |
53
|
1 |
|
PHP); |
54
|
1 |
|
continue; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
$classBody[] = self::renderMethod($method, <<<PHP |
58
|
1 |
|
{$return}\\Spiral\\Core\\Internal\\Proxy\\Resolver::resolve( |
59
|
1 |
|
'{$interface}', |
60
|
1 |
|
$context, |
61
|
1 |
|
){$call}($argsStr, ...\\array_slice(\\func_get_args(), {$method->getNumberOfParameters()})); |
62
|
1 |
|
PHP); |
63
|
|
|
} |
64
|
3 |
|
$bodyStr = \implode("\n\n", $classBody); |
65
|
|
|
|
66
|
3 |
|
return <<<PHP |
67
|
3 |
|
namespace $classNamespace; |
68
|
|
|
|
69
|
3 |
|
final class $classShortName implements \\$interface { |
70
|
|
|
use \Spiral\Core\Internal\Proxy\ProxyTrait; |
71
|
|
|
|
72
|
3 |
|
$bodyStr |
73
|
|
|
} |
74
|
3 |
|
PHP; |
75
|
|
|
} |
76
|
|
|
|
77
|
8 |
|
public static function renderMethod(\ReflectionMethod $m, string $body = ''): string |
78
|
|
|
{ |
79
|
8 |
|
return \sprintf( |
80
|
8 |
|
"public%s function %s%s(%s)%s {\n%s\n}", |
81
|
8 |
|
$m->isStatic() ? ' static' : '', |
82
|
8 |
|
$m->returnsReference() ? '&' : '', |
83
|
8 |
|
$m->getName(), |
84
|
8 |
|
\implode(', ', \array_map([self::class, 'renderParameter'], $m->getParameters())), |
85
|
8 |
|
$m->hasReturnType() |
86
|
6 |
|
? ': ' . self::renderParameterTypes($m->getReturnType(), $m->getDeclaringClass()) |
87
|
8 |
|
: '', |
88
|
8 |
|
$body, |
89
|
8 |
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
26 |
|
public static function renderParameter(\ReflectionParameter $param): string |
93
|
|
|
{ |
94
|
26 |
|
return \ltrim(\sprintf( |
95
|
26 |
|
'%s %s%s%s%s', |
96
|
26 |
|
$param->hasType() ? self::renderParameterTypes($param->getType(), $param->getDeclaringClass()) : '', |
97
|
26 |
|
$param->isPassedByReference() ? '&' : '', |
98
|
26 |
|
$param->isVariadic() ? '...' : '', |
99
|
26 |
|
'$' . $param->getName(), |
100
|
26 |
|
$param->isOptional() && !$param->isVariadic() ? ' = ' . self::renderDefaultValue($param) : '', |
101
|
26 |
|
), ' '); |
102
|
|
|
} |
103
|
|
|
|
104
|
27 |
|
public static function renderParameterTypes(\ReflectionType $types, \ReflectionClass $class): string |
105
|
|
|
{ |
106
|
27 |
|
if ($types instanceof \ReflectionNamedType) { |
107
|
20 |
|
return ($types->allowsNull() && $types->getName() !== 'mixed' ? '?' : '') . ($types->isBuiltin() |
108
|
17 |
|
? $types->getName() |
109
|
20 |
|
: self::normalizeClassType($types, $class)); |
110
|
|
|
} |
111
|
|
|
|
112
|
8 |
|
[$separator, $types] = match (true) { |
113
|
8 |
|
$types instanceof \ReflectionUnionType => ['|', $types->getTypes()], |
|
|
|
|
114
|
8 |
|
$types instanceof \ReflectionIntersectionType => ['&', $types->getTypes()], |
|
|
|
|
115
|
8 |
|
default => throw new \Exception('Unknown type.'), |
116
|
8 |
|
}; |
117
|
|
|
|
118
|
8 |
|
$result = []; |
119
|
8 |
|
foreach ($types as $type) { |
120
|
8 |
|
$result[] = $type->isBuiltin() |
121
|
7 |
|
? $type->getName() |
122
|
3 |
|
: self::normalizeClassType($type, $class); |
123
|
|
|
} |
124
|
|
|
|
125
|
8 |
|
return \implode($separator, $result); |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
public static function renderDefaultValue(\ReflectionParameter $param): string |
129
|
|
|
{ |
130
|
1 |
|
if ($param->isDefaultValueConstant()) { |
131
|
|
|
$result = $param->getDefaultValueConstantName(); |
132
|
|
|
|
133
|
|
|
return \explode('::', $result)[0] === 'self' |
134
|
|
|
? $result |
135
|
|
|
: '\\' . $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
$cut = self::cutDefaultValue($param); |
139
|
|
|
|
140
|
1 |
|
return \str_starts_with($cut, 'new ') |
141
|
|
|
? $cut |
142
|
1 |
|
: \var_export($param->getDefaultValue(), true); |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
public static function normalizeClassType(\ReflectionNamedType $type, \ReflectionClass $class): string |
146
|
|
|
{ |
147
|
1 |
|
return '\\' . ($type->getName() === 'self' ? $class->getName() : $type->getName()); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
private static function cutDefaultValue(\ReflectionParameter $param): string |
151
|
|
|
{ |
152
|
1 |
|
$string = (string)$param; |
153
|
|
|
|
154
|
1 |
|
return \trim(\substr($string, \strpos($string, '=') + 1, -1)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|