|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yiisoft\Proxy; |
|
4
|
|
|
|
|
5
|
|
|
final class ClassConfigurator |
|
6
|
|
|
{ |
|
7
|
|
|
public function getInterfaceConfig(string $interface): ClassConfig |
|
8
|
|
|
{ |
|
9
|
|
|
$reflection = new \ReflectionClass($interface); |
|
10
|
|
|
if (!$reflection->isInterface()) { |
|
11
|
|
|
throw new \InvalidArgumentException("$interface is not an interface"); |
|
12
|
|
|
} |
|
13
|
|
|
$config = $this->getReflectionConfig($reflection); |
|
14
|
|
|
|
|
15
|
|
|
return $config; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function getClassConfig(string $class): ClassConfig |
|
19
|
|
|
{ |
|
20
|
|
|
$reflection = new \ReflectionClass($class); |
|
21
|
|
|
if ($reflection->isInterface()) { |
|
22
|
|
|
throw new \InvalidArgumentException("$class is not a class"); |
|
23
|
|
|
} |
|
24
|
|
|
$config = $this->getReflectionConfig($reflection); |
|
25
|
|
|
|
|
26
|
|
|
return $config; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getReflectionConfig(\ReflectionClass $reflection): ClassConfig |
|
30
|
|
|
{ |
|
31
|
|
|
$config = $this->getReflectionClassConfig($reflection); |
|
32
|
|
|
foreach ($reflection->getMethods() as $method) { |
|
33
|
|
|
$methodName = $method->getName(); |
|
34
|
|
|
$config->methods[$methodName] = $this->getMethodConfig($method); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $config; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getReflectionClassConfig(\ReflectionClass $reflection): ClassConfig |
|
41
|
|
|
{ |
|
42
|
|
|
$config = new ClassConfig(); |
|
43
|
|
|
$config->isInterface = $reflection->isInterface(); |
|
44
|
|
|
$config->namespace = $reflection->getNamespaceName(); |
|
45
|
|
|
$config->modifiers = \Reflection::getModifierNames($reflection->getModifiers()); |
|
46
|
|
|
$config->name = $reflection->getName(); |
|
47
|
|
|
$config->shortName = $reflection->getShortName(); |
|
48
|
|
|
$config->parent = $reflection->getParentClass(); |
|
49
|
|
|
$config->parents = $this->getClassParents($reflection); |
|
50
|
|
|
$config->interfaces = $reflection->getInterfaceNames(); |
|
51
|
|
|
|
|
52
|
|
|
return $config; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getClassParents(\ReflectionClass $reflection): array |
|
56
|
|
|
{ |
|
57
|
|
|
$parents = []; |
|
58
|
|
|
while ($parent = $reflection->getParentClass()) { |
|
59
|
|
|
$parents[] = $parent->getName(); |
|
60
|
|
|
$reflection = $parent; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $parents; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function getMethodConfig(\ReflectionMethod $method): MethodConfig |
|
67
|
|
|
{ |
|
68
|
|
|
$config = new MethodConfig(); |
|
69
|
|
|
$config->name = $method->getName(); |
|
70
|
|
|
$config->modifiers = \Reflection::getModifierNames($method->getModifiers()); |
|
71
|
|
|
$config->hasReturnType = $method->hasReturnType(); |
|
72
|
|
|
if ($config->hasReturnType) { |
|
73
|
|
|
$config->returnType = new TypeConfig(); |
|
74
|
|
|
$config->returnType->name = $method->getReturnType()->getName(); |
|
75
|
|
|
$config->returnType->allowsNull = $method->getReturnType()->allowsNull(); |
|
76
|
|
|
} |
|
77
|
|
|
$config->parameters = []; |
|
78
|
|
|
foreach ($method->getParameters() as $param) { |
|
79
|
|
|
$config->parameters[$param->getName()] = $this->getMethodParameterConfig($param); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $config; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function getMethodParameterConfig(\ReflectionParameter $param): ParameterConfig |
|
86
|
|
|
{ |
|
87
|
|
|
$config = new ParameterConfig(); |
|
88
|
|
|
$config->name = $param->getName(); |
|
89
|
|
|
$config->hasType = $param->hasType(); |
|
90
|
|
|
if ($param->hasType()) { |
|
91
|
|
|
$config->type = new TypeConfig(); |
|
92
|
|
|
$config->type->name = $param->getType()->getName(); |
|
93
|
|
|
$config->type->allowsNull = $param->getType()->allowsNull(); |
|
94
|
|
|
} |
|
95
|
|
|
$config->allowsNull = $param->allowsNull(); |
|
96
|
|
|
$this->getDefaultValues($param, $config); |
|
97
|
|
|
|
|
98
|
|
|
return $config; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function getDefaultValues(\ReflectionParameter $param, ParameterConfig $config): void |
|
102
|
|
|
{ |
|
103
|
|
|
$config->isDefaultValueAvailable = $param->isDefaultValueAvailable(); |
|
104
|
|
|
if ($config->isDefaultValueAvailable) { |
|
105
|
|
|
$config->isDefaultValueConstant = $param->isDefaultValueConstant(); |
|
106
|
|
|
if ($config->isDefaultValueConstant) { |
|
107
|
|
|
$config->defaultValueConstantName = $param->getDefaultValueConstantName(); |
|
108
|
|
|
} else { |
|
109
|
|
|
$config->defaultValue = $param->getDefaultValue(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|