Subversion Repositories fastphp

Rev

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