1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Proxy; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Reflection; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
use ReflectionMethod; |
12
|
|
|
use ReflectionParameter; |
13
|
|
|
use Yiisoft\Proxy\Config\ClassConfig; |
14
|
|
|
use Yiisoft\Proxy\Config\MethodConfig; |
15
|
|
|
use Yiisoft\Proxy\Config\ParameterConfig; |
16
|
|
|
use Yiisoft\Proxy\Config\TypeConfig; |
17
|
|
|
|
18
|
|
|
final class ClassConfigFactory |
19
|
|
|
{ |
20
|
1 |
|
public function getIntergaceConfig(string $interfaceName): ClassConfig |
21
|
|
|
{ |
22
|
|
|
try { |
23
|
1 |
|
$reflection = new ReflectionClass($interfaceName); |
24
|
|
|
} catch (ReflectionException) { |
25
|
|
|
throw new InvalidArgumentException("$interfaceName must exist."); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
if (!$reflection->isInterface()) { |
29
|
|
|
throw new InvalidArgumentException("$interfaceName is not an interface."); |
30
|
|
|
} |
31
|
|
|
|
32
|
1 |
|
return new ClassConfig( |
33
|
|
|
isInterface: true, |
34
|
1 |
|
namespace: $reflection->getNamespaceName(), |
35
|
1 |
|
modifiers: Reflection::getModifierNames($reflection->getModifiers()), |
36
|
1 |
|
name: $reflection->getName(), |
37
|
1 |
|
shortName: $reflection->getShortName(), |
38
|
1 |
|
parent: (string) $reflection->getParentClass(), |
39
|
1 |
|
parents: $this->getParents($reflection), |
40
|
1 |
|
interfaces: $reflection->getInterfaceNames(), |
41
|
1 |
|
methods: $this->getMethodConfigs($reflection), |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
1 |
|
private function getParents(ReflectionClass $reflection): array |
49
|
|
|
{ |
50
|
1 |
|
$parents = []; |
51
|
1 |
|
while ($parent = $reflection->getParentClass()) { |
52
|
|
|
$parents[] = $parent->getName(); |
53
|
|
|
$reflection = $parent; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
return $parents; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return MethodConfig[] |
61
|
|
|
*/ |
62
|
1 |
|
private function getMethodConfigs(ReflectionClass $reflection): array |
63
|
|
|
{ |
64
|
1 |
|
$methods = []; |
65
|
1 |
|
foreach ($reflection->getMethods() as $method) { |
66
|
1 |
|
$methods[$method->getName()] = $this->getMethodConfig($method); |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return $methods; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
private function getMethodConfig(ReflectionMethod $method): MethodConfig |
73
|
|
|
{ |
74
|
1 |
|
return new MethodConfig( |
75
|
1 |
|
modifiers: Reflection::getModifierNames($method->getModifiers()), |
76
|
1 |
|
name: $method->getName(), |
77
|
1 |
|
parameters: $this->getMethodParameterConfigs($method), |
78
|
1 |
|
hasReturnType: $method->hasReturnType(), |
79
|
1 |
|
returnType: $this->getMethodTypeConfig($method), |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return ParameterConfig[] |
85
|
|
|
*/ |
86
|
1 |
|
private function getMethodParameterConfigs(ReflectionMethod $method): array |
87
|
|
|
{ |
88
|
1 |
|
$parameters = []; |
89
|
1 |
|
foreach ($method->getParameters() as $param) { |
90
|
1 |
|
$parameters[$param->getName()] = $this->getMethodParameterConfig($param); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $parameters; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
private function getMethodParameterConfig(ReflectionParameter $param): ParameterConfig |
97
|
|
|
{ |
98
|
1 |
|
return new ParameterConfig( |
99
|
1 |
|
hasType: $param->hasType(), |
100
|
1 |
|
type: $this->getMethodParameterTypeConfig($param), |
101
|
1 |
|
name: $param->getName(), |
102
|
1 |
|
allowsNull: $param->allowsNull(), |
103
|
1 |
|
isDefaultValueAvailable: $param->isDefaultValueAvailable(), |
104
|
1 |
|
isDefaultValueConstant: $param->isDefaultValueAvailable() ? $param->isDefaultValueConstant() : null, |
105
|
1 |
|
defaultValueConstantName: $param->isOptional() ? $param->getDefaultValueConstantName() : null, |
106
|
1 |
|
defaultValue: $param->isOptional() ? $param->getDefaultValue() : null, |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
private function getMethodParameterTypeConfig(ReflectionParameter $param): ?TypeConfig |
111
|
|
|
{ |
112
|
1 |
|
$type = $param->getType(); |
113
|
1 |
|
if (!$type) { |
114
|
1 |
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
return new TypeConfig( |
118
|
1 |
|
name: $type->getName(), |
|
|
|
|
119
|
1 |
|
allowsNull: $type->allowsNull(), |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
private function getMethodTypeConfig(ReflectionMethod $method): ?TypeConfig |
124
|
|
|
{ |
125
|
1 |
|
$returnType = $method->getReturnType(); |
126
|
1 |
|
if (!$returnType) { |
127
|
1 |
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
return new TypeConfig( |
131
|
1 |
|
name: $returnType->getName(), |
132
|
1 |
|
allowsNull: $returnType->allowsNull(), |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|