1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Ubiquity\utils\base$UIntrospection |
7
|
|
|
* This class is part of Ubiquity |
8
|
|
|
* |
9
|
|
|
* @author jcheron <[email protected]> |
10
|
|
|
* @version 1.0.7 |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class UIntrospection { |
14
|
|
|
|
15
|
2 |
|
public static function getClassCode($classname) { |
16
|
2 |
|
$r = new \ReflectionClass ( $classname ); |
17
|
2 |
|
$lines = file ( $r->getFileName () ); |
18
|
2 |
|
return $lines; |
19
|
|
|
} |
20
|
|
|
|
21
|
1 |
|
public static function getFileName($classname) { |
22
|
1 |
|
$r = new \ReflectionClass ( $classname ); |
23
|
1 |
|
return $r->getFileName (); |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public static function getLoadedViews(\ReflectionMethod $r, $lines) { |
27
|
2 |
|
$result = [ ]; |
28
|
2 |
|
$code = self::getMethodCode ( $r, $lines ); |
29
|
2 |
|
\preg_match_all ( '@(?:.*?)\$this\-\>loadView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches ); |
30
|
2 |
|
if (isset ( $matches [1] ) && \sizeof ( $matches [1] ) > 0) { |
31
|
1 |
|
$result = array_merge ( $result, $matches [1] ); |
32
|
|
|
} |
33
|
2 |
|
\preg_match_all ( '@(?:.*?)\$this\-\>jquery\-\>renderView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches ); |
34
|
2 |
|
if (isset ( $matches [1] )) { |
35
|
2 |
|
$result = array_merge ( $result, $matches [1] ); |
36
|
|
|
} |
37
|
2 |
|
if (\strpos ( $code, '$this->loadDefaultView' ) !== false || strpos ( $code, '$this->jquery->renderDefaultView' ) !== false) { |
38
|
|
|
$result [] = $r->getDeclaringClass ()->getShortName () . '/' . $r->getName () . '.html'; |
39
|
|
|
} |
40
|
2 |
|
return $result; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public static function getMethodCode(\ReflectionMethod $r, $lines) { |
44
|
2 |
|
$str = ""; |
45
|
2 |
|
$count = \sizeof ( $lines ); |
46
|
2 |
|
$sLine = $r->getStartLine (); |
47
|
2 |
|
$eLine = $r->getEndLine (); |
48
|
2 |
|
if ($sLine == $eLine) |
49
|
|
|
return $lines [$sLine]; |
50
|
2 |
|
$min = \min ( $eLine, $count ); |
51
|
2 |
|
for($l = $sLine; $l < $min; $l ++) { |
52
|
2 |
|
$str .= $lines [$l]; |
53
|
|
|
} |
54
|
2 |
|
return $str; |
55
|
|
|
} |
56
|
|
|
|
57
|
11 |
|
public static function implementsMethod($object, $methodName, $baseDeclaringClass) { |
58
|
11 |
|
$m = new \ReflectionMethod ( $object, $methodName ); |
59
|
11 |
|
return $m->getDeclaringClass ()->getName () !== $baseDeclaringClass; |
60
|
11 |
|
} |
61
|
11 |
|
|
62
|
11 |
|
public static function closure_dump(\Closure $c) { |
63
|
11 |
|
$str = 'function ('; |
64
|
|
|
$r = new \ReflectionFunction ( $c ); |
65
|
11 |
|
$params = array (); |
66
|
|
|
foreach ( $r->getParameters () as $p ) { |
67
|
|
|
$s = ''; |
68
|
11 |
|
if ($p->isArray ()) { |
69
|
|
|
$s .= 'array '; |
70
|
|
|
} else if ($p->getClass ()) { |
71
|
11 |
|
$s .= $p->getClass ()->name . ' '; |
72
|
11 |
|
} |
73
|
|
|
if ($p->isPassedByReference ()) { |
74
|
|
|
$s .= '&'; |
75
|
11 |
|
} |
76
|
|
|
$s .= '$' . $p->name; |
77
|
11 |
|
if ($p->isOptional ()) { |
78
|
11 |
|
$s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE ); |
79
|
11 |
|
} |
80
|
11 |
|
$params [] = $s; |
81
|
11 |
|
} |
82
|
11 |
|
$str .= \implode ( ', ', $params ); |
83
|
|
|
$str .= ')'; |
84
|
|
|
$lines = file ( $r->getFileName () ); |
85
|
|
|
$sLine = $r->getStartLine (); |
86
|
11 |
|
$eLine = $r->getEndLine (); |
87
|
11 |
|
if ($eLine === $sLine) { |
88
|
11 |
|
$match = \strstr ( $lines [$sLine - 1], "function" ); |
89
|
|
|
$str .= \strstr ( \strstr ( $match, "{" ), "}", true ) . "}"; |
90
|
11 |
|
} else { |
91
|
|
|
$str .= \strrchr ( $lines [$sLine - 1], "{" ); |
92
|
11 |
|
for($l = $sLine; $l < $eLine - 1; $l ++) { |
93
|
11 |
|
$str .= $lines [$l]; |
94
|
1 |
|
} |
95
|
|
|
$str .= \strstr ( $lines [$eLine - 1], "}", true ) . "}"; |
96
|
11 |
|
} |
97
|
|
|
$vars = $r->getStaticVariables (); |
98
|
|
|
foreach ( $vars as $k => $v ) { |
99
|
|
|
$str = \str_replace ( '$' . $k, \var_export ( $v, true ), $str ); |
100
|
|
|
} |
101
|
|
|
return $str; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public static function getChildClasses($baseClass) { |
105
|
|
|
$children = [ ]; |
106
|
|
|
foreach ( \get_declared_classes () as $class ) { |
107
|
|
|
$rClass = new \ReflectionClass ( $class ); |
108
|
|
|
if ($rClass->isSubclassOf ( $baseClass )) { |
109
|
|
|
$children [] = $class; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return $children; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|