|
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
|
2 |
|
$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
|
|
|
public static function implementsMethod($object, $methodName, $baseDeclaringClass) { |
|
58
|
|
|
$m = new \ReflectionMethod ( $object, $methodName ); |
|
59
|
|
|
return $m->getDeclaringClass ()->getName () !== $baseDeclaringClass; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
10 |
|
public static function closure_dump(\Closure $c) { |
|
63
|
10 |
|
$str = 'function ('; |
|
64
|
10 |
|
$r = new \ReflectionFunction ( $c ); |
|
65
|
10 |
|
$params = array (); |
|
66
|
10 |
|
foreach ( $r->getParameters () as $p ) { |
|
67
|
10 |
|
$s = ''; |
|
68
|
10 |
|
if ($p->isArray ()) { |
|
69
|
|
|
$s .= 'array '; |
|
70
|
10 |
|
} else if ($p->getClass ()) { |
|
71
|
|
|
$s .= $p->getClass ()->name . ' '; |
|
72
|
|
|
} |
|
73
|
10 |
|
if ($p->isPassedByReference ()) { |
|
74
|
|
|
$s .= '&'; |
|
75
|
|
|
} |
|
76
|
10 |
|
$s .= '$' . $p->name; |
|
77
|
10 |
|
if ($p->isOptional ()) { |
|
78
|
|
|
$s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE ); |
|
79
|
|
|
} |
|
80
|
10 |
|
$params [] = $s; |
|
81
|
|
|
} |
|
82
|
10 |
|
$str .= \implode ( ', ', $params ); |
|
83
|
10 |
|
$str .= ')'; |
|
84
|
10 |
|
$lines = file ( $r->getFileName () ); |
|
85
|
10 |
|
$sLine = $r->getStartLine (); |
|
86
|
10 |
|
$eLine = $r->getEndLine (); |
|
87
|
10 |
|
if ($eLine === $sLine) { |
|
88
|
|
|
$match = \strstr ( $lines [$sLine - 1], "function" ); |
|
89
|
|
|
$str .= \strstr ( \strstr ( $match, "{" ), "}", true ) . "}"; |
|
90
|
|
|
} else { |
|
91
|
10 |
|
$str .= \strrchr ( $lines [$sLine - 1], "{" ); |
|
92
|
10 |
|
for($l = $sLine; $l < $eLine - 1; $l ++) { |
|
93
|
10 |
|
$str .= $lines [$l]; |
|
94
|
|
|
} |
|
95
|
10 |
|
$str .= \strstr ( $lines [$eLine - 1], "}", true ) . "}"; |
|
96
|
|
|
} |
|
97
|
10 |
|
$vars = $r->getStaticVariables (); |
|
98
|
10 |
|
foreach ( $vars as $k => $v ) { |
|
99
|
1 |
|
$str = \str_replace ( '$' . $k, \var_export ( $v, true ), $str ); |
|
100
|
|
|
} |
|
101
|
10 |
|
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
|
|
|
|