1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\utils\base; |
4
|
|
|
|
5
|
|
|
class UIntrospection { |
6
|
|
|
|
7
|
1 |
|
public static function getClassCode($classname) { |
8
|
1 |
|
$r=new \ReflectionClass($classname); |
9
|
1 |
|
$lines=file($r->getFileName()); |
10
|
1 |
|
return $lines; |
11
|
|
|
} |
12
|
|
|
|
13
|
1 |
|
public static function getFileName($classname) { |
14
|
1 |
|
$r=new \ReflectionClass($classname); |
15
|
1 |
|
return $r->getFileName(); |
16
|
|
|
} |
17
|
|
|
|
18
|
2 |
|
public static function getLoadedViews(\ReflectionMethod $r, $lines) { |
19
|
2 |
|
$matches=[ ]; |
20
|
2 |
|
$code=self::getMethodCode($r, $lines); |
21
|
2 |
|
\preg_match_all('@(?:.*?)\$this\-\>loadView\([\'\"](.+?)[\'\"](?:.*?)@s', $code, $matches); |
22
|
2 |
|
if (isset($matches[1])) { |
23
|
2 |
|
return $matches[1]; |
24
|
|
|
} |
25
|
|
|
return [ ]; |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
public static function getMethodCode(\ReflectionMethod $r, $lines) { |
29
|
2 |
|
$str=""; |
30
|
2 |
|
$count=\sizeof($lines); |
31
|
2 |
|
$sLine=$r->getStartLine(); |
32
|
2 |
|
$eLine=$r->getEndLine(); |
33
|
2 |
|
if ($sLine == $eLine) |
34
|
|
|
return $lines[$sLine]; |
35
|
2 |
|
$min=\min($eLine, $count); |
36
|
2 |
|
for($l=$sLine; $l < $min; $l++) { |
37
|
2 |
|
$str.=$lines[$l]; |
38
|
|
|
} |
39
|
2 |
|
return $str; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public static function closure_dump(\Closure $c) { |
43
|
2 |
|
$str='function ('; |
44
|
2 |
|
$r=new \ReflectionFunction($c); |
45
|
2 |
|
$params=array (); |
46
|
2 |
|
foreach ( $r->getParameters() as $p ) { |
47
|
2 |
|
$s=''; |
48
|
2 |
|
if ($p->isArray()) { |
49
|
|
|
$s.='array '; |
50
|
2 |
|
} else if ($p->getClass()) { |
51
|
|
|
$s.=$p->getClass()->name . ' '; |
52
|
|
|
} |
53
|
2 |
|
if ($p->isPassedByReference()) { |
54
|
|
|
$s.='&'; |
55
|
|
|
} |
56
|
2 |
|
$s.='$' . $p->name; |
57
|
2 |
|
if ($p->isOptional()) { |
58
|
|
|
$s.=' = ' . \var_export($p->getDefaultValue(), TRUE); |
59
|
|
|
} |
60
|
2 |
|
$params[]=$s; |
61
|
|
|
} |
62
|
2 |
|
$str.=\implode(', ', $params); |
63
|
2 |
|
$str.=')'; |
64
|
2 |
|
$lines=file($r->getFileName()); |
65
|
2 |
|
$sLine=$r->getStartLine(); |
66
|
2 |
|
$eLine=$r->getEndLine(); |
67
|
2 |
|
if($eLine===$sLine){ |
68
|
2 |
|
$str.=strstr(strstr($lines[$sLine-1],"{"),"}",true)."}"; |
69
|
|
|
}else{ |
70
|
2 |
|
$str.=strstr($lines[$sLine-1],"{"); |
71
|
2 |
|
for($l=$sLine; $l < $eLine-1; $l++) { |
72
|
2 |
|
$str.=$lines[$l]; |
73
|
|
|
} |
74
|
2 |
|
$str.=strstr($lines[$eLine-1],"}",true)."}"; |
75
|
|
|
} |
76
|
2 |
|
return $str; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function getChildClasses($baseClass){ |
80
|
|
|
$children = []; |
81
|
|
|
foreach(\get_declared_classes() as $class){ |
82
|
|
|
echo $class."<br>"; |
83
|
|
|
$rClass=new \ReflectionClass($class); |
84
|
|
|
if($rClass->isSubclassOf($baseClass)){ |
85
|
|
|
$children[] = $class; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
return $children; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|