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