1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base; |
4
|
|
|
|
5
|
|
|
use Ubiquity\domains\DDDManager; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Ubiquity\utils\base$UIntrospection |
9
|
|
|
* This class is part of Ubiquity |
10
|
|
|
* |
11
|
|
|
* @author jcheron <[email protected]> |
12
|
|
|
* @version 1.0.10 |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
class UIntrospection { |
16
|
3 |
|
|
17
|
3 |
|
public static function getClassCode($classname) { |
18
|
3 |
|
$r = new \ReflectionClass($classname); |
19
|
3 |
|
$lines = \file($r->getFileName()); |
20
|
|
|
return $lines; |
21
|
|
|
} |
22
|
1 |
|
|
23
|
1 |
|
public static function getFileName($classname) { |
24
|
1 |
|
$r = new \ReflectionClass($classname); |
25
|
|
|
return $r->getFileName(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public static function getMethodAtLine($class, $line) { |
29
|
|
|
$r = new \ReflectionClass($class); |
30
|
|
|
$methods = $r->getMethods(); |
31
|
|
|
foreach ($methods as $method) { |
32
|
|
|
if ($method->getStartLine() <= $line && $line <= $method->getEndLine()) { |
33
|
|
|
return $method; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
return null; |
37
|
|
|
} |
38
|
2 |
|
|
39
|
2 |
|
public static function getLoadedViews(\ReflectionMethod $r, $lines) { |
40
|
2 |
|
$result = []; |
41
|
2 |
|
$code = self::getMethodCode($r, $lines); |
42
|
2 |
|
\preg_match_all('@(?:.*?)\$this\-\>loadView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches); |
43
|
2 |
|
if (isset($matches[1]) && \sizeof($matches[1]) > 0) { |
44
|
|
|
$result = array_merge($result, $matches[1]); |
45
|
2 |
|
} |
46
|
2 |
|
\preg_match_all('@(?:.*?)\$this\-\>jquery\-\>renderView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches); |
47
|
2 |
|
if (isset($matches[1])) { |
48
|
|
|
$result = array_merge($result, $matches[1]); |
49
|
2 |
|
} |
50
|
2 |
|
if (\strpos($code, '$this->loadDefaultView') !== false || strpos($code, '$this->jquery->renderDefaultView') !== false) { |
51
|
|
|
$result[] = DDDManager::getViewNamespace() . $r->getDeclaringClass()->getShortName() . '/' . $r->getName() . '.html'; |
52
|
2 |
|
} |
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
3 |
|
|
56
|
3 |
|
public static function getMethodCode(\ReflectionMethod $r, $lines) { |
57
|
3 |
|
$str = ''; |
58
|
3 |
|
$count = \count($lines); |
59
|
3 |
|
$sLine = $r->getStartLine() - 1; |
60
|
3 |
|
$eLine = $r->getEndLine(); |
61
|
|
|
if ($sLine == $eLine) |
62
|
3 |
|
return $lines[$sLine]; |
63
|
3 |
|
$min = \min($eLine, $count); |
64
|
3 |
|
for ($l = $sLine; $l < $min; $l++) { |
65
|
|
|
$str .= $lines[$l]; |
66
|
3 |
|
} |
67
|
|
|
return $str; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function getMethodEffectiveParameters($code, $methodName) { |
71
|
|
|
$tokens = \token_get_all($code); |
72
|
|
|
$parenthesis = 0; |
73
|
|
|
$result = []; |
74
|
|
|
$status = ''; |
75
|
|
|
$current = ''; |
76
|
|
|
foreach ($tokens as $tokenArray) { |
77
|
|
|
if (\is_array($tokenArray)) { |
78
|
|
|
if ($tokenArray[0] === T_STRING && $tokenArray[1] === $methodName) { |
79
|
|
|
$status = 'find'; |
80
|
|
|
} elseif ($status === 'open') { |
81
|
|
|
$current .= $tokenArray[1]; |
82
|
|
|
} |
83
|
|
|
} elseif (\is_string($tokenArray)) { |
84
|
|
|
if ($tokenArray === '(' && $status === 'find') { |
85
|
|
|
$status = 'open'; |
86
|
|
|
$current = ''; |
87
|
|
|
$parenthesis++; |
88
|
|
|
} elseif ($status === 'open') { |
89
|
|
|
if ($tokenArray === '(') { |
90
|
|
|
$current .= $tokenArray; |
91
|
|
|
$parenthesis++; |
92
|
|
|
} elseif ($tokenArray === ',' && $parenthesis === 1) { |
93
|
|
|
$result[] = \trim($current); |
94
|
|
|
$current = ''; |
95
|
|
|
} elseif ($tokenArray === ')') { |
96
|
|
|
$parenthesis--; |
97
|
|
|
if ($parenthesis === 0) { |
98
|
|
|
if ($current != '') { |
99
|
|
|
$result[] = \trim($current); |
100
|
|
|
} |
101
|
|
|
return $result; |
102
|
|
|
} |
103
|
|
|
$current .= $tokenArray; |
104
|
|
|
} else { |
105
|
|
|
$current .= $tokenArray; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $result; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function implementsMethod($object, $methodName, $baseDeclaringClass) { |
114
|
|
|
$m = new \ReflectionMethod($object, $methodName); |
115
|
|
|
return $m->getDeclaringClass()->getName() !== $baseDeclaringClass; |
116
|
|
|
} |
117
|
11 |
|
|
118
|
11 |
|
public static function closure_dump(\Closure $c) { |
119
|
11 |
|
$str = 'function ('; |
120
|
11 |
|
$r = new \ReflectionFunction($c); |
121
|
11 |
|
$params = array(); |
122
|
11 |
|
foreach ($r->getParameters() as $p) { |
123
|
11 |
|
$s = ''; |
124
|
11 |
|
$type = $p->getType(); |
125
|
11 |
|
$isArray = $type && $type->getName() === 'array'; |
126
|
|
|
if ($isArray) { |
127
|
11 |
|
$s .= 'array '; |
128
|
|
|
} else if ($type) { |
129
|
|
|
$class = !$type->isBuiltin() ? new \ReflectionClass($type->getName()) : null; |
130
|
|
|
if ($class != null) { |
131
|
|
|
$s .= $class . ' '; |
132
|
|
|
} |
133
|
11 |
|
} |
134
|
|
|
if ($p->isPassedByReference()) { |
135
|
|
|
$s .= '&'; |
136
|
11 |
|
} |
137
|
11 |
|
$s .= '$' . $p->name; |
138
|
11 |
|
if ($p->isOptional()) { |
139
|
|
|
$s .= ' = ' . \var_export($p->getDefaultValue(), true); |
140
|
11 |
|
} |
141
|
|
|
$params[] = $s; |
142
|
11 |
|
} |
143
|
11 |
|
$str .= \implode(', ', $params); |
144
|
11 |
|
$str .= ')'; |
145
|
11 |
|
$lines = file($r->getFileName()); |
146
|
11 |
|
$sLine = $r->getStartLine(); |
147
|
11 |
|
$eLine = $r->getEndLine(); |
148
|
|
|
if ($sLine < count($lines)) { |
149
|
|
|
if ($eLine === $sLine) { |
150
|
|
|
$match = \strstr($lines[$sLine - 1], "function"); |
151
|
11 |
|
$str .= \strstr(\strstr($match, "{"), "}", true) . "}"; |
152
|
11 |
|
} else { |
153
|
11 |
|
$str .= \strrchr($lines[$sLine - 1], "{"); |
154
|
|
|
for ($l = $sLine; $l < $eLine - 1; $l++) { |
155
|
11 |
|
$str .= $lines[$l]; |
156
|
|
|
} |
157
|
11 |
|
$str .= \strstr($lines[$eLine - 1], "}", true) . "}"; |
158
|
|
|
} |
159
|
11 |
|
} |
160
|
1 |
|
$vars = $r->getStaticVariables(); |
161
|
|
|
|
162
|
11 |
|
foreach ($vars as $k => $v) { |
163
|
|
|
$str = \str_replace('$' . $k, \var_export($v, true), $str); |
164
|
|
|
} |
165
|
|
|
return $str; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public static function getChildClasses($baseClass) { |
169
|
|
|
$children = []; |
170
|
|
|
foreach (\get_declared_classes() as $class) { |
171
|
|
|
$rClass = new \ReflectionClass($class); |
172
|
|
|
if ($rClass->isSubclassOf($baseClass)) { |
173
|
|
|
$children[] = $class; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return $children; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|