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.2 |
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 |
|
$matches = [ ]; |
28
|
2 |
|
$code = self::getMethodCode ( $r, $lines ); |
29
|
2 |
|
\preg_match_all ( '@(?:.*?)\$this\-\>loadView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches ); |
30
|
2 |
|
if (isset ( $matches [1] )) { |
31
|
2 |
|
return $matches [1]; |
32
|
|
|
} |
33
|
|
|
return [ ]; |
34
|
|
|
} |
35
|
|
|
|
36
|
2 |
|
public static function getMethodCode(\ReflectionMethod $r, $lines) { |
37
|
2 |
|
$str = ""; |
38
|
2 |
|
$count = \sizeof ( $lines ); |
39
|
2 |
|
$sLine = $r->getStartLine (); |
40
|
2 |
|
$eLine = $r->getEndLine (); |
41
|
2 |
|
if ($sLine == $eLine) |
42
|
|
|
return $lines [$sLine]; |
43
|
2 |
|
$min = \min ( $eLine, $count ); |
44
|
2 |
|
for($l = $sLine; $l < $min; $l ++) { |
45
|
2 |
|
$str .= $lines [$l]; |
46
|
|
|
} |
47
|
2 |
|
return $str; |
48
|
|
|
} |
49
|
|
|
|
50
|
9 |
|
public static function closure_dump(\Closure $c) { |
51
|
9 |
|
$str = 'function ('; |
52
|
9 |
|
$r = new \ReflectionFunction ( $c ); |
53
|
9 |
|
$params = array (); |
54
|
9 |
|
foreach ( $r->getParameters () as $p ) { |
55
|
9 |
|
$s = ''; |
56
|
9 |
|
if ($p->isArray ()) { |
57
|
|
|
$s .= 'array '; |
58
|
9 |
|
} else if ($p->getClass ()) { |
59
|
|
|
$s .= $p->getClass ()->name . ' '; |
60
|
|
|
} |
61
|
9 |
|
if ($p->isPassedByReference ()) { |
62
|
|
|
$s .= '&'; |
63
|
|
|
} |
64
|
9 |
|
$s .= '$' . $p->name; |
65
|
9 |
|
if ($p->isOptional ()) { |
66
|
|
|
$s .= ' = ' . \var_export ( $p->getDefaultValue (), TRUE ); |
67
|
|
|
} |
68
|
9 |
|
$params [] = $s; |
69
|
|
|
} |
70
|
9 |
|
$str .= \implode ( ', ', $params ); |
71
|
9 |
|
$str .= ')'; |
72
|
9 |
|
$lines = file ( $r->getFileName () ); |
73
|
9 |
|
$sLine = $r->getStartLine (); |
74
|
9 |
|
$eLine = $r->getEndLine (); |
75
|
9 |
|
if ($eLine === $sLine) { |
76
|
|
|
$str .= strstr ( strstr ( $lines [$sLine - 1], "{" ), "}", true ) . "}"; |
77
|
|
|
} else { |
78
|
9 |
|
$str .= strstr ( $lines [$sLine - 1], "{" ); |
79
|
9 |
|
for($l = $sLine; $l < $eLine - 1; $l ++) { |
80
|
9 |
|
$str .= $lines [$l]; |
81
|
|
|
} |
82
|
9 |
|
$str .= strstr ( $lines [$eLine - 1], "}", true ) . "}"; |
83
|
|
|
} |
84
|
9 |
|
$vars = $r->getStaticVariables (); |
85
|
9 |
|
foreach ( $vars as $k => $v ) { |
86
|
|
|
$str = str_replace ( '$' . $k, \var_export ( $v, true ), $str ); |
87
|
|
|
} |
88
|
9 |
|
return $str; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public static function getChildClasses($baseClass) { |
92
|
|
|
$children = [ ]; |
93
|
|
|
foreach ( \get_declared_classes () as $class ) { |
94
|
|
|
$rClass = new \ReflectionClass ( $class ); |
95
|
|
|
if ($rClass->isSubclassOf ( $baseClass )) { |
96
|
|
|
$children [] = $class; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
return $children; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|