Subversion Repositories fastphp

Rev

Rev 43 | Rev 48 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 daniel-mar 1
<?php
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
40
                else if (($this->getType() == ICON_TYPE_TODO)                                                         ) return 16; // ToDo comment
41
                else                                                                                                    return -1;
42
        }
37 daniel-mar 43
 
43 daniel-mar 44
        public function isMethod() {
45
                return (($this->getType() == ICON_TYPE_FUNCTION)    ||
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) {
56
                $token = token_get_all($code);
57
                $wait_function = false;
58
                $wait_const = false;
59
                $wait_class = false;
60
                $wait_trait = false;
61
                $icon = new MyFastPHPIcon();
62
                $wait_interface = false;
63
                $wait_abstract_func_list_end = false;
64
                $levelAry = array();
65
                $dep = 0;
66
                $insideFuncAry = array();
29 daniel-mar 67
 
43 daniel-mar 68
                if (!$token) {
69
                        $icon->setType(ICON_TYPE_ERROR);
70
                        FastPHPWriter::outputLeafNode($icon, 0, 'SYNTAX ERROR');
27 daniel-mar 71
                }
72
 
43 daniel-mar 73
                foreach ($token as $data) {
74
                        if ($data == '{') $dep++;
75
                        if ($data == '}') {
76
                                $dep--;
77
                                if ((count($levelAry) > 0) && (self::array_peek($levelAry) == $dep)) {
78
                                        array_pop($levelAry);
79
                                        FastPHPWriter::outputDecreaseLevel();
80
                                }
81
                                if ((count($insideFuncAry) > 0) && (self::array_peek($insideFuncAry) == $dep)) {
82
                                        array_pop($insideFuncAry);
83
                                }
36 daniel-mar 84
                        }
27 daniel-mar 85
 
43 daniel-mar 86
                        $token = (!is_array($data)) ? null : $data[0];
87
                        $value = (!is_array($data)) ? null : $data[1];
88
                        $line  = (!is_array($data)) ? null : $data[2];
36 daniel-mar 89
 
46 daniel-mar 90
                        if ($value == '${') $dep++;
91
 
43 daniel-mar 92
                        if ($wait_function && ($token == T_STRING)) {
93
                                $wait_function = false;
94
                                if ($icon->isAbstract()) {
95
                                        $desc = "abstract function $value()";
96
                                        $wait_abstract_func_list_end = true;
97
                                } else {
98
                                        $desc = "function $value()";
99
                                        $insideFuncAry[] = $dep;
100
                                }
27 daniel-mar 101
 
43 daniel-mar 102
                                if ($value == '__construct') { // TODO: auch eine methode mit dem namen der klasse soll eine konstruktor sein
103
                                        $icon->setType(ICON_TYPE_CONSTRUCTOR);
104
                                } else if ($value == '__destruct') {
105
                                        $icon->setType(ICON_TYPE_DESTRUCTOR);
106
                                } else if (substr($value, 0, 2) == '__') {
107
                                        $icon->setType(ICON_TYPE_MAGICMETHOD);
108
                                } else {
109
                                        $icon->setType(ICON_TYPE_FUNCTION);
110
                                }
111
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
112
                                $icon->reset();
113
                        }
33 daniel-mar 114
 
43 daniel-mar 115
                        if ($wait_class && ($token == T_STRING)) {
116
                                if ($icon->isAbstract()) {
117
                                        $desc = "Abstract Class $value\n";
118
                                } else {
119
                                        $desc = "Class $value\n";
120
                                }
121
                                $wait_class = false;
122
                                $levelAry[] = $dep;
36 daniel-mar 123
 
43 daniel-mar 124
                                $icon->setType(ICON_TYPE_CLASS);
125
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
126
                                $icon->reset();
33 daniel-mar 127
 
43 daniel-mar 128
                                FastPHPWriter::outputIncreaseLevel();
129
                        }
33 daniel-mar 130
 
43 daniel-mar 131
                        if ($wait_trait && ($token == T_STRING)) {
132
                                $desc = "Trait $value\n";
133
                                $wait_trait = false;
134
                                $levelAry[] = $dep;
36 daniel-mar 135
 
43 daniel-mar 136
                                $icon->setType(ICON_TYPE_TRAIT);
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_interface && ($token == T_STRING)) {
144
                                $desc = "Interface $value\n";
145
                                $wait_interface = false;
146
                                $levelAry[] = $dep;
33 daniel-mar 147
 
43 daniel-mar 148
                                $icon->setType(ICON_TYPE_INTERFACE);
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_const && ($token == T_STRING)) {
156
                                $desc = "const $value\n";
157
                                $wait_const = false;
27 daniel-mar 158
 
43 daniel-mar 159
                                $icon->setType(ICON_TYPE_CONST);
160
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
161
                                $icon->reset();
162
                        }
33 daniel-mar 163
 
43 daniel-mar 164
                        if ((!$wait_abstract_func_list_end) && (count($levelAry) > 0) && (count($insideFuncAry) == 0) && ($token == T_VARIABLE)) {
165
                                $desc = "$value\n";
27 daniel-mar 166
 
43 daniel-mar 167
                                $icon->setType(ICON_TYPE_VAR);
168
                                FastPHPWriter::outputLeafNode($icon, $line, $desc);
169
                                $icon->reset();
170
                        }
33 daniel-mar 171
 
43 daniel-mar 172
                        if ($token == T_PRIVATE)   $icon->setPrivate();
173
                        if ($token == T_PROTECTED) $icon->setProtected();
174
                        if ($token == T_PUBLIC)    $icon->setPublic();
175
                        if ($token == T_ABSTRACT)  $icon->setAbstract();
176
                        if ($token == T_FINAL)     $icon->setFinal();
177
                        if ($token == T_STATIC)    $icon->setStatic();
29 daniel-mar 178
 
43 daniel-mar 179
                        if (($data == ';') || ($data == '{') || ($data == '}')) {
180
                                $wait_abstract_func_list_end = false;
181
                                $icon->reset();
182
                        }
27 daniel-mar 183
 
43 daniel-mar 184
                        if ($token == T_FUNCTION) {
185
                                $wait_function = true;
186
                        }
187
                        if ($token == T_CLASS) {
188
                                $wait_class = true;
189
                                $dep = 0;
190
                        }
191
                        if ($token == T_INTERFACE) {
192
                                $wait_interface = true;
193
                                $dep = 0;
194
                        }
195
                        if ($token == T_TRAIT) {
196
                                $wait_trait = true;
197
                                $dep = 0;
198
                        }
27 daniel-mar 199
 
43 daniel-mar 200
                        if ($token == T_CONST) {
201
                                $wait_const = true;
202
                        }
36 daniel-mar 203
 
43 daniel-mar 204
                        if (($token == T_COMMENT) && self::isToDoDescription($value)) {
205
                                // Because a TO-DO-entry can stand everywhere (e.g. between a "private" and a "function")
206
                                // we shall not alter the $icon instance
207
                                $todoIcon = clone $icon;
208
                                $todoIcon->setType(ICON_TYPE_TODO);
209
                                unset($todoIcon);
210
                                FastPHPWriter::outputLeafNode($todoIcon, $line, self::stripComment($value));
42 daniel-mar 211
                        }
37 daniel-mar 212
                }
213
        }
27 daniel-mar 214
 
43 daniel-mar 215
        private static function isToDoDescription($comment) {
216
                return ((stripos($value, 'TODO')   !== false) ||
217
                        (stripos($value, 'BUGBUG') !== false) ||
218
                        (stripos($value, 'FIXME')  !== false) ||
219
                        (stripos($value, 'XXX')    !== false));
37 daniel-mar 220
        }
221
 
43 daniel-mar 222
        private static function stripComment($x) {
223
                if (substr($x, 0, 1) == '#') return trim(substr($x, 1));
224
                if (substr($x, 0, 2) == '//') return trim(substr($x, 2));
225
                if (substr($x, 0, 2) == '/*') return trim(substr($x, 2, strlen($x)-4));
37 daniel-mar 226
        }
227
 
228
 
43 daniel-mar 229
        private static final function array_peek($array) {
230
                if (!isset($array[count($array)-1])) return null;
231
                return $array[count($array)-1];
37 daniel-mar 232
        }
43 daniel-mar 233
}
37 daniel-mar 234
 
43 daniel-mar 235
$parser = new MyFastPHPCodeExplorer($icon);
236
while (true) {
237
        try {
238
                $code = FastPHPReader::readCodeFromEditor();
239
        } catch (FastPHPExitSignalReceivedException $e) {
240
                die();
37 daniel-mar 241
        }
43 daniel-mar 242
        FastPHPWriter::outputHeader();
243
        $parser->handle($code);
244
        FastPHPWriter::outputExit();
245
        FastPHPWriter::signalOutputEnd();
246
        sleep(1);
27 daniel-mar 247
}