1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if (!function_exists('short_class_name')) { |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @param $object |
7
|
|
|
* @return string |
8
|
|
|
*/ |
9
|
|
|
function short_class_name($object): string |
10
|
|
|
{ |
11
|
|
|
$objectProperties = new \ReflectionClass($object); |
12
|
|
|
|
13
|
|
|
return $objectProperties->getShortName(); |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if (!function_exists('class_constants')) { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get all class constants that starts with $startsWithFilter |
21
|
|
|
* or if empty get all class constants. |
22
|
|
|
* @param $object |
23
|
|
|
* @param string $startsWithFilter |
24
|
|
|
* @return array |
25
|
|
|
* @see https://github.com/spatie-custom/blender/blob/master/app/Foundation/helpers.php |
26
|
|
|
*/ |
27
|
|
|
function class_constants($object, string $startsWithFilter = ''): array |
28
|
|
|
{ |
29
|
|
|
$objectProperties = new \ReflectionClass($object); |
30
|
|
|
|
31
|
|
|
$constants = $objectProperties->getConstants(); |
32
|
|
|
|
33
|
|
|
if ($startsWithFilter == '') { |
34
|
|
|
return $constants; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return array_filter($constants, function ($key) use ($startsWithFilter) { |
38
|
|
|
return starts_with(strtolower($key), strtolower($startsWithFilter)); |
39
|
|
|
}, ARRAY_FILTER_USE_KEY); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!function_exists('class_uses_recursive')) { |
44
|
|
|
/** |
45
|
|
|
* Returns all traits used by a class, it's subclasses and trait of their traits |
46
|
|
|
* |
47
|
|
|
* @param string $class |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
function class_uses_recursive($class) |
51
|
|
|
{ |
52
|
|
|
$results = []; |
53
|
|
|
foreach (array_merge([$class => $class], class_parents($class)) as $class) { |
54
|
|
|
$results += trait_uses_recursive($class); |
55
|
|
|
} |
56
|
|
|
return array_unique($results); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!function_exists('class_basename')) { |
61
|
|
|
/** |
62
|
|
|
* Get the class "basename" of the given object / class. |
63
|
|
|
* |
64
|
|
|
* @param string|object $class |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
function class_basename($class) |
68
|
|
|
{ |
69
|
|
|
$class = is_object($class) ? get_class($class) : $class; |
70
|
|
|
return basename(str_replace('\\', '/', $class)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the class name from a php file |
76
|
|
|
* |
77
|
|
|
* @param string $filePath |
78
|
|
|
* |
79
|
|
|
* @return string|null |
80
|
|
|
* @see https://github.com/laradic/support/blob/master/src/Util.php |
81
|
|
|
*/ |
82
|
|
|
function getClassNameFromFile($filePath) |
83
|
|
|
{ |
84
|
|
|
$tokens = token_get_all(file_get_contents($filePath)); |
85
|
|
|
for ($i = 0; $i < count($tokens); $i++) { |
|
|
|
|
86
|
|
|
if ($tokens[$i][0] === T_TRAIT || $tokens[$i][0] === T_INTERFACE) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
if ($tokens[$i][0] === T_CLASS) { |
90
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
91
|
|
|
if ($tokens[$j] === '{') { |
92
|
|
|
return $tokens[$i + 2][1]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the namespace of the php file |
102
|
|
|
* |
103
|
|
|
* @param $filePath |
104
|
|
|
* |
105
|
|
|
* @return string|null |
106
|
|
|
* @see https://github.com/laradic/support/blob/master/src/Util.php |
107
|
|
|
*/ |
108
|
|
|
function getNamespaceFromFile($filePath) |
109
|
|
|
{ |
110
|
|
|
$namespace = ''; |
111
|
|
|
$tokens = token_get_all(file_get_contents($filePath)); |
112
|
|
|
for ($i = 0; $i < count($tokens); $i++) { |
|
|
|
|
113
|
|
View Code Duplication |
if ($tokens[$i][0] === T_NAMESPACE) { |
|
|
|
|
114
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
115
|
|
|
if ($tokens[$j][0] === T_STRING) { |
116
|
|
|
$namespace .= '\\' . $tokens[$j][1]; |
117
|
|
|
} else { |
118
|
|
|
if ($tokens[$j] === '{' || $tokens[$j] === ';') { |
119
|
|
|
return $namespace; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the namespace, classes, interfaces and traits of the php file |
130
|
|
|
* |
131
|
|
|
* @param $filePath |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
* @see https://github.com/laradic/support/blob/master/src/Util.php |
135
|
|
|
*/ |
136
|
|
|
function getPhpDefinitionsFromFile($filePath) |
137
|
|
|
{ |
138
|
|
|
$classes = []; |
139
|
|
|
$traits = []; |
140
|
|
|
$interfaces = []; |
141
|
|
|
$fp = fopen($filePath, 'r'); |
142
|
|
|
$trait = $interface = $class = $namespace = $buffer = ''; |
|
|
|
|
143
|
|
|
$i = 0; |
144
|
|
|
while (!$class) { |
145
|
|
|
if (feof($fp)) { |
146
|
|
|
break; |
147
|
|
|
} |
148
|
|
|
$buffer .= fread($fp, 512); |
149
|
|
|
$tokens = token_get_all($buffer); |
150
|
|
|
if (strpos($buffer, '{') === false) { |
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
for (; $i < count($tokens); $i++) { |
|
|
|
|
154
|
|
View Code Duplication |
if ($tokens[$i][0] === T_NAMESPACE) { |
|
|
|
|
155
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
156
|
|
|
if ($tokens[$j][0] === T_STRING) { |
157
|
|
|
$namespace .= '\\' . $tokens[$j][1]; |
158
|
|
|
} else { |
159
|
|
|
if ($tokens[$j] === '{' || $tokens[$j] === ';') { |
160
|
|
|
break; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
View Code Duplication |
if ($tokens[$i][0] === T_CLASS) { |
|
|
|
|
166
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
167
|
|
|
if ($tokens[$j] === '{') { |
168
|
|
|
$class = $tokens[$i + 2][1]; |
169
|
|
|
$classes[] = $class; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
View Code Duplication |
if ($tokens[$i][0] === T_INTERFACE) { |
|
|
|
|
174
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
175
|
|
|
if ($tokens[$j] === '{') { |
176
|
|
|
$interface = $tokens[$i + 2][1]; |
177
|
|
|
$interfaces[] = $interface; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
View Code Duplication |
if ($tokens[$i][0] === T_TRAIT) { |
|
|
|
|
182
|
|
|
for ($j = $i + 1; $j < count($tokens); $j++) { |
|
|
|
|
183
|
|
|
if ($tokens[$j] === '{') { |
184
|
|
|
$trait = $tokens[$i + 2][1]; |
185
|
|
|
$traits[] = $trait; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
return compact('namespace', 'classes', 'traits', 'interfaces'); |
192
|
|
|
} |
193
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: