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