|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Reflection helper. |
|
5
|
|
|
*/ |
|
6
|
|
|
declare(strict_types=1); |
|
7
|
|
|
|
|
8
|
|
|
namespace HDNET\Autoloader\Utility; |
|
9
|
|
|
|
|
10
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
11
|
|
|
use TYPO3\CMS\Extbase\Reflection\ClassReflection; |
|
12
|
|
|
use TYPO3\CMS\Extbase\Reflection\MethodReflection; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Reflection helper. |
|
16
|
|
|
*/ |
|
17
|
|
|
class ReflectionUtility |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Create a new class reflection. Do not use the makeInstance or objectManager |
|
21
|
|
|
* because the reflection API is also used in front of the caching framework. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $className |
|
24
|
|
|
* |
|
25
|
|
|
* @return ClassReflection |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function createReflectionClass($className) |
|
28
|
|
|
{ |
|
29
|
|
|
return new ClassReflection($className); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check if the given class is instantiable. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $className |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function isInstantiable($className) |
|
40
|
|
|
{ |
|
41
|
|
|
return self::createReflectionClass($className) |
|
42
|
|
|
->isInstantiable(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the name of the parent class. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $className |
|
49
|
|
|
* |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function getParentClassName($className) |
|
53
|
|
|
{ |
|
54
|
|
|
return self::createReflectionClass($className) |
|
55
|
|
|
->getParentClass() |
|
56
|
|
|
->getName(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get all properties that are tagged with the given tag. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $className |
|
63
|
|
|
* @param string $tag |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getPropertiesTaggedWith($className, $tag) |
|
68
|
|
|
{ |
|
69
|
|
|
$classReflection = self::createReflectionClass($className); |
|
70
|
|
|
$properties = []; |
|
71
|
|
|
foreach ($classReflection->getProperties() as $property) { |
|
72
|
|
|
/** @var \TYPO3\CMS\Extbase\Reflection\PropertyReflection $property */ |
|
73
|
|
|
if ($property->isTaggedWith($tag)) { |
|
74
|
|
|
$properties[] = $property; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $properties; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get all properties that are tagged with the given tag. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $className |
|
85
|
|
|
* @param string $tag |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
|
|
public static function getPropertyNamesTaggedWith($className, $tag):array |
|
90
|
|
|
{ |
|
91
|
|
|
$classReflection = self::createReflectionClass($className); |
|
92
|
|
|
$properties = []; |
|
93
|
|
|
foreach ($classReflection->getProperties() as $property) { |
|
94
|
|
|
/** @var \TYPO3\CMS\Extbase\Reflection\PropertyReflection $property */ |
|
95
|
|
|
if ($property->isTaggedWith($tag)) { |
|
96
|
|
|
$properties[] = $property->getName(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $properties; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get all public methods of the given class. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $className |
|
107
|
|
|
* |
|
108
|
|
|
* @return MethodReflection[] |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function getPublicMethods($className) |
|
111
|
|
|
{ |
|
112
|
|
|
return self::createReflectionClass($className) |
|
113
|
|
|
->getMethods(\ReflectionMethod::IS_PUBLIC); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get first class tag information. |
|
118
|
|
|
* The trimmed value if the tag exists and FALSE if the tag do not exists. |
|
119
|
|
|
* |
|
120
|
|
|
* @param string $className |
|
121
|
|
|
* @param string $tag |
|
122
|
|
|
* |
|
123
|
|
|
* @return string|bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function getFirstTagValue(string $className, string $tag) |
|
126
|
|
|
{ |
|
127
|
|
|
$classReflection = self::createReflectionClass($className); |
|
128
|
|
|
if (!$classReflection->isTaggedWith($tag)) { |
|
129
|
|
|
return false; |
|
130
|
|
|
} |
|
131
|
|
|
$values = $classReflection->getTagValues($tag); |
|
132
|
|
|
if (\is_array($values)) { |
|
133
|
|
|
return \trim((string) $values[0]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the tag configuration from this method and respect multiple line and space configuration. |
|
141
|
|
|
* |
|
142
|
|
|
* @param MethodReflection|ClassReflection $reflectionObject |
|
143
|
|
|
* @param array $tagNames |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
public static function getTagConfiguration($reflectionObject, array $tagNames): array |
|
148
|
|
|
{ |
|
149
|
|
|
$tags = $reflectionObject->getTagsValues(); |
|
150
|
|
|
$configuration = []; |
|
151
|
|
|
foreach ($tagNames as $tagName) { |
|
152
|
|
|
$configuration[$tagName] = []; |
|
153
|
|
|
if (!\is_array($tags[$tagName])) { |
|
154
|
|
|
continue; |
|
155
|
|
|
} |
|
156
|
|
|
foreach ($tags[$tagName] as $c) { |
|
157
|
|
|
$configuration[$tagName] = \array_merge( |
|
158
|
|
|
$configuration[$tagName], |
|
159
|
|
|
GeneralUtility::trimExplode(' ', $c, true) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $configuration; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get public method names |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $className |
|
171
|
|
|
* @return array |
|
172
|
|
|
*/ |
|
173
|
|
|
public static function getPublicMethodNames(string $className):array{ |
|
174
|
|
|
$methods = self::getPublicMethods($className); |
|
175
|
|
|
$methodNames = []; |
|
176
|
|
|
foreach ($methods as $method) { |
|
177
|
|
|
$methodNames[] = $method->getName(); |
|
178
|
|
|
} |
|
179
|
|
|
return $methodNames; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get properties of the given class, that are als declared in the given class. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $className |
|
186
|
|
|
* |
|
187
|
|
|
* @return array |
|
188
|
|
|
*/ |
|
189
|
|
|
public static function getDeclaringProperties($className) |
|
190
|
|
|
{ |
|
191
|
|
|
$properties = []; |
|
192
|
|
|
$classReflection = self::createReflectionClass($className); |
|
193
|
|
|
foreach ($classReflection->getProperties() as $property) { |
|
194
|
|
|
/** @var \TYPO3\CMS\Extbase\Reflection\PropertyReflection $property */ |
|
195
|
|
|
if ($property->getDeclaringClass() |
|
196
|
|
|
->getName() === $classReflection->getName() |
|
197
|
|
|
) { |
|
198
|
|
|
$properties[] = $property->getName(); |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $properties; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|