Rev 70 | Rev 92 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
70 | daniel-mar | 1 | <?php |
27 | daniel-mar | 2 | |
36 | daniel-mar | 3 | // TODO: show full signature of each element? |
4 | |||
27 | daniel-mar | 5 | error_reporting(0); |
6 | |||
43 | daniel-mar | 7 | require_once __DIR__ . '/codeexplorer_api.inc.php'; |
33 | daniel-mar | 8 | |
43 | daniel-mar | 9 | define('ICON_TYPE_FUNCTION', 1); |
10 | define('ICON_TYPE_CONSTRUCTOR', 2); |
||
11 | define('ICON_TYPE_DESTRUCTOR', 3); |
||
12 | define('ICON_TYPE_MAGICMETHOD', 4); |
||
13 | define('ICON_TYPE_CLASS', 5); |
||
14 | define('ICON_TYPE_TRAIT', 6); |
||
15 | define('ICON_TYPE_INTERFACE', 7); |
||
16 | define('ICON_TYPE_VAR', 8); |
||
17 | define('ICON_TYPE_CONST', 9); |
||
18 | define('ICON_TYPE_TODO', 10); |
||
19 | define('ICON_TYPE_ERROR', 11); |
||
27 | daniel-mar | 20 | |
43 | daniel-mar | 21 | class MyFastPHPIcon extends FastPHPIcon { |
27 | daniel-mar | 22 | |
43 | daniel-mar | 23 | public function imageIndex() { |
24 | if (($this->getType() == ICON_TYPE_CLASS) && (!$this->isAbstract())) return 0; // class |
||
25 | else if (($this->getType() == ICON_TYPE_CLASS) && ( $this->isAbstract())) return 1; // abstract class |
||
26 | else if (($this->getType() == ICON_TYPE_INTERFACE) ) return 2; // interface |
||
27 | else if (($this->getType() == ICON_TYPE_TRAIT) ) return 3; // trait |
||
28 | else if (($this->getType() == ICON_TYPE_CONST) && ($this->isPrivate()) ) return 4; // private const |
||
29 | else if (($this->getType() == ICON_TYPE_VAR) && ($this->isPrivate()) ) return 5; // private var |
||
30 | else if (($this->isMethod()) && ($this->isPrivate()) && (!$this->isAbstract())) return 6; // private function |
||
31 | else if (($this->isMethod()) && ($this->isPrivate()) && ( $this->isAbstract())) return 7; // private abstract function |
||
32 | else if (($this->getType() == ICON_TYPE_CONST) && ($this->isProtected()) ) return 8; // protected const |
||
33 | else if (($this->getType() == ICON_TYPE_VAR) && ($this->isProtected()) ) return 9; // protected var |
||
34 | else if (($this->isMethod()) && ($this->isProtected()) && (!$this->isAbstract())) return 10; // protected function |
||
35 | else if (($this->isMethod()) && ($this->isProtected()) && ( $this->isAbstract())) return 11; // protected abstract function |
||
36 | else if (($this->getType() == ICON_TYPE_CONST) && ($this->isPublic()) ) return 12; // public const |
||
37 | else if (($this->getType() == ICON_TYPE_VAR) && ($this->isPublic()) ) return 13; // public var |
||
38 | else if (($this->isMethod()) && ($this->isPublic()) && (!$this->isAbstract())) return 14; // public function |
||
39 | else if (($this->isMethod()) && ($this->isPublic()) && ( $this->isAbstract())) return 15; // public abstract function |
||
49 | daniel-mar | 40 | else if (($this->getType() == ICON_TYPE_TODO) ) return 16; // To-Do comment |
43 | daniel-mar | 41 | else return -1; |
42 | } |
||
37 | daniel-mar | 43 | |
43 | daniel-mar | 44 | public function isMethod() { |
45 | return (($this->getType() == ICON_TYPE_FUNCTION) || |
||
70 | daniel-mar | 46 | ($this->getType() == ICON_TYPE_CONSTRUCTOR) || |
47 | ($this->getType() == ICON_TYPE_DESTRUCTOR) || |
||
48 | ($this->getType() == ICON_TYPE_MAGICMETHOD)); |
||
37 | daniel-mar | 49 | } |
50 | |||
43 | daniel-mar | 51 | } |
27 | daniel-mar | 52 | |
43 | daniel-mar | 53 | class MyFastPHPCodeExplorer { |
27 | daniel-mar | 54 | |
43 | daniel-mar | 55 | public function handle($code) { |
74 | daniel-mar | 56 | // Quick'n'Dirty fix to correctly parse the line |
57 | // test(XYZ::class) |
||
58 | $code = str_replace('::class', '', $code); |
||
59 | |||
60 | // Quick'n'Dirty fix to correctly parse the line |
||
61 | // $verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret={$secret}&response={$response}"); |
||
62 | $code = str_replace('{$', '{', $code); |
||
63 | |||
43 | daniel-mar | 64 | $token = token_get_all($code); |
65 | $wait_function = false; |
||
66 | $wait_const = false; |
||
67 | $wait_class = false; |
||
68 | $wait_trait = false; |
||
70 | daniel-mar | 69 | $icon = new MyFastPHPIcon(); |
43 | daniel-mar | 70 | $wait_interface = false; |
71 | $wait_abstract_func_list_end = false; |
||
72 | $levelAry = array(); |
||
73 | $dep = 0; |
||
74 | $insideFuncAry = array(); |
||
29 | daniel-mar | 75 | |
43 | daniel-mar | 76 | if (!$token) { |
77 | $icon->setType(ICON_TYPE_ERROR); |
||
78 | FastPHPWriter::outputLeafNode($icon, 0, 'SYNTAX ERROR'); |
||
27 | daniel-mar | 79 | } |
80 | |||
43 | daniel-mar | 81 | foreach ($token as $data) { |
82 | if ($data == '{') $dep++; |
||
83 | if ($data == '}') { |
||
84 | $dep--; |
||
85 | if ((count($levelAry) > 0) && (self::array_peek($levelAry) == $dep)) { |
||
86 | array_pop($levelAry); |
||
87 | FastPHPWriter::outputDecreaseLevel(); |
||
88 | } |
||
89 | if ((count($insideFuncAry) > 0) && (self::array_peek($insideFuncAry) == $dep)) { |
||
90 | array_pop($insideFuncAry); |
||
91 | } |
||
36 | daniel-mar | 92 | } |
27 | daniel-mar | 93 | |
43 | daniel-mar | 94 | $token = (!is_array($data)) ? null : $data[0]; |
95 | $value = (!is_array($data)) ? null : $data[1]; |
||
96 | $line = (!is_array($data)) ? null : $data[2]; |
||
36 | daniel-mar | 97 | |
46 | daniel-mar | 98 | if ($value == '${') $dep++; |
99 | |||
55 | daniel-mar | 100 | if ($wait_function && ($data == '{')) { |
101 | $wait_function = false; // Anonymous functions do not have a name |
||
102 | } |
||
103 | |||
43 | daniel-mar | 104 | if ($wait_function && ($token == T_STRING)) { |
105 | $wait_function = false; |
||
106 | if ($icon->isAbstract()) { |
||
107 | $desc = "abstract function $value()"; |
||
108 | $wait_abstract_func_list_end = true; |
||
109 | } else { |
||
110 | $desc = "function $value()"; |
||
111 | $insideFuncAry[] = $dep; |
||
112 | } |
||
27 | daniel-mar | 113 | |
43 | daniel-mar | 114 | if ($value == '__construct') { // TODO: auch eine methode mit dem namen der klasse soll eine konstruktor sein |
70 | daniel-mar | 115 | $icon->setType(ICON_TYPE_CONSTRUCTOR); |
43 | daniel-mar | 116 | } else if ($value == '__destruct') { |
70 | daniel-mar | 117 | $icon->setType(ICON_TYPE_DESTRUCTOR); |
43 | daniel-mar | 118 | } else if (substr($value, 0, 2) == '__') { |
70 | daniel-mar | 119 | $icon->setType(ICON_TYPE_MAGICMETHOD); |
43 | daniel-mar | 120 | } else { |
70 | daniel-mar | 121 | $icon->setType(ICON_TYPE_FUNCTION); |
43 | daniel-mar | 122 | } |
123 | FastPHPWriter::outputLeafNode($icon, $line, $desc); |
||
124 | $icon->reset(); |
||
125 | } |
||
33 | daniel-mar | 126 | |
43 | daniel-mar | 127 | if ($wait_class && ($token == T_STRING)) { |
70 | daniel-mar | 128 | if ($icon->isAbstract()) { |
43 | daniel-mar | 129 | $desc = "Abstract Class $value\n"; |
130 | } else { |
||
131 | $desc = "Class $value\n"; |
||
132 | } |
||
133 | $wait_class = false; |
||
134 | $levelAry[] = $dep; |
||
36 | daniel-mar | 135 | |
43 | daniel-mar | 136 | $icon->setType(ICON_TYPE_CLASS); |
137 | FastPHPWriter::outputLeafNode($icon, $line, $desc); |
||
138 | $icon->reset(); |
||
33 | daniel-mar | 139 | |
43 | daniel-mar | 140 | FastPHPWriter::outputIncreaseLevel(); |
141 | } |
||
33 | daniel-mar | 142 | |
43 | daniel-mar | 143 | if ($wait_trait && ($token == T_STRING)) { |
144 | $desc = "Trait $value\n"; |
||
145 | $wait_trait = false; |
||
146 | $levelAry[] = $dep; |
||
36 | daniel-mar | 147 | |
43 | daniel-mar | 148 | $icon->setType(ICON_TYPE_TRAIT); |
149 | FastPHPWriter::outputLeafNode($icon, $line, $desc); |
||
150 | $icon->reset(); |
||
33 | daniel-mar | 151 | |
43 | daniel-mar | 152 | FastPHPWriter::outputIncreaseLevel(); |
153 | } |
||
33 | daniel-mar | 154 | |
43 | daniel-mar | 155 | if ($wait_interface && ($token == T_STRING)) { |
156 | $desc = "Interface $value\n"; |
||
157 | $wait_interface = false; |
||
158 | $levelAry[] = $dep; |
||
33 | daniel-mar | 159 | |
70 | daniel-mar | 160 | $icon->setType(ICON_TYPE_INTERFACE); |
43 | daniel-mar | 161 | FastPHPWriter::outputLeafNode($icon, $line, $desc); |
162 | $icon->reset(); |
||
33 | daniel-mar | 163 | |
43 | daniel-mar | 164 | FastPHPWriter::outputIncreaseLevel(); |
165 | } |
||
33 | daniel-mar | 166 | |
43 | daniel-mar | 167 | if ($wait_const && ($token == T_STRING)) { |
168 | $desc = "const $value\n"; |
||
169 | $wait_const = false; |
||
27 | daniel-mar | 170 | |
43 | daniel-mar | 171 | $icon->setType(ICON_TYPE_CONST); |
172 | FastPHPWriter::outputLeafNode($icon, $line, $desc); |
||
173 | $icon->reset(); |
||
174 | } |
||
33 | daniel-mar | 175 | |
43 | daniel-mar | 176 | if ((!$wait_abstract_func_list_end) && (count($levelAry) > 0) && (count($insideFuncAry) == 0) && ($token == T_VARIABLE)) { |
177 | $desc = "$value\n"; |
||
27 | daniel-mar | 178 | |
43 | daniel-mar | 179 | $icon->setType(ICON_TYPE_VAR); |
180 | FastPHPWriter::outputLeafNode($icon, $line, $desc); |
||
181 | $icon->reset(); |
||
182 | } |
||
33 | daniel-mar | 183 | |
43 | daniel-mar | 184 | if ($token == T_PRIVATE) $icon->setPrivate(); |
185 | if ($token == T_PROTECTED) $icon->setProtected(); |
||
186 | if ($token == T_PUBLIC) $icon->setPublic(); |
||
187 | if ($token == T_ABSTRACT) $icon->setAbstract(); |
||
188 | if ($token == T_FINAL) $icon->setFinal(); |
||
189 | if ($token == T_STATIC) $icon->setStatic(); |
||
29 | daniel-mar | 190 | |
43 | daniel-mar | 191 | if (($data == ';') || ($data == '{') || ($data == '}')) { |
192 | $wait_abstract_func_list_end = false; |
||
193 | $icon->reset(); |
||
194 | } |
||
27 | daniel-mar | 195 | |
43 | daniel-mar | 196 | if ($token == T_FUNCTION) { |
197 | $wait_function = true; |
||
198 | } |
||
199 | if ($token == T_CLASS) { |
||
200 | $wait_class = true; |
||
201 | $dep = 0; |
||
202 | } |
||
203 | if ($token == T_INTERFACE) { |
||
204 | $wait_interface = true; |
||
205 | $dep = 0; |
||
206 | } |
||
207 | if ($token == T_TRAIT) { |
||
208 | $wait_trait = true; |
||
209 | $dep = 0; |
||
210 | } |
||
27 | daniel-mar | 211 | |
43 | daniel-mar | 212 | if ($token == T_CONST) { |
213 | $wait_const = true; |
||
214 | } |
||
36 | daniel-mar | 215 | |
43 | daniel-mar | 216 | if (($token == T_COMMENT) && self::isToDoDescription($value)) { |
51 | daniel-mar | 217 | $comment_lines = explode("\n", trim($value)); |
218 | foreach ($comment_lines as $line_no => $comment_line) { |
||
219 | if (self::isToDoDescription($comment_line)) { |
||
220 | // Because a To-Do-entry can stand everywhere (e.g. between a "private" and a "function" keyword) |
||
221 | // we shall not alter the $icon instance |
||
222 | $todoIcon = clone $icon; |
||
223 | $todoIcon->setType(ICON_TYPE_TODO); |
||
224 | FastPHPWriter::outputLeafNode($todoIcon, $line+$line_no, self::stripComment($comment_line)); |
||
225 | unset($todoIcon); |
||
226 | } |
||
227 | } |
||
42 | daniel-mar | 228 | } |
37 | daniel-mar | 229 | } |
230 | } |
||
27 | daniel-mar | 231 | |
70 | daniel-mar | 232 | private static function isToDoDescription($comment) { |
48 | daniel-mar | 233 | return ((stripos($comment, 'TODO') !== false) || |
234 | (stripos($comment, 'BUGBUG') !== false) || |
||
235 | (stripos($comment, 'FIXME') !== false) || |
||
236 | (stripos($comment, 'XXX') !== false)); |
||
37 | daniel-mar | 237 | } |
238 | |||
43 | daniel-mar | 239 | private static function stripComment($x) { |
51 | daniel-mar | 240 | $x = trim($x); |
43 | daniel-mar | 241 | if (substr($x, 0, 1) == '#') return trim(substr($x, 1)); |
242 | if (substr($x, 0, 2) == '//') return trim(substr($x, 2)); |
||
243 | if (substr($x, 0, 2) == '/*') return trim(substr($x, 2, strlen($x)-4)); |
||
51 | daniel-mar | 244 | return $x; |
37 | daniel-mar | 245 | } |
246 | |||
247 | |||
43 | daniel-mar | 248 | private static final function array_peek($array) { |
249 | if (!isset($array[count($array)-1])) return null; |
||
250 | return $array[count($array)-1]; |
||
37 | daniel-mar | 251 | } |
43 | daniel-mar | 252 | } |
37 | daniel-mar | 253 | |
43 | daniel-mar | 254 | $parser = new MyFastPHPCodeExplorer($icon); |
255 | while (true) { |
||
256 | try { |
||
257 | $code = FastPHPReader::readCodeFromEditor(); |
||
258 | } catch (FastPHPExitSignalReceivedException $e) { |
||
259 | die(); |
||
37 | daniel-mar | 260 | } |
43 | daniel-mar | 261 | FastPHPWriter::outputHeader(); |
262 | $parser->handle($code); |
||
263 | FastPHPWriter::outputExit(); |
||
264 | FastPHPWriter::signalOutputEnd(); |
||
265 | sleep(1); |
||
27 | daniel-mar | 266 | } |