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