Subversion Repositories fastphp

Rev

Rev 42 | Rev 46 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. // TODO: show full signature of each element?
  4.  
  5. error_reporting(0);
  6.  
  7. require_once __DIR__ . '/codeexplorer_api.inc.php';
  8.  
  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);
  20.  
  21. class MyFastPHPIcon extends FastPHPIcon {
  22.  
  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.         }
  43.  
  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));
  49.         }
  50.  
  51. }
  52.  
  53. class MyFastPHPCodeExplorer {
  54.  
  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();
  67.  
  68.                 if (!$token) {
  69.                         $icon->setType(ICON_TYPE_ERROR);
  70.                         FastPHPWriter::outputLeafNode($icon, 0, 'SYNTAX ERROR');
  71.                 }
  72.  
  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.                                 }
  84.                         }
  85.  
  86.                         $token = (!is_array($data)) ? null : $data[0];
  87.                         $value = (!is_array($data)) ? null : $data[1];
  88.                         $line  = (!is_array($data)) ? null : $data[2];
  89.  
  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.                                 }
  99.  
  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.                         }
  112.  
  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;
  121.  
  122.                                 $icon->setType(ICON_TYPE_CLASS);
  123.                                 FastPHPWriter::outputLeafNode($icon, $line, $desc);
  124.                                 $icon->reset();
  125.  
  126.                                 FastPHPWriter::outputIncreaseLevel();
  127.                         }
  128.  
  129.                         if ($wait_trait && ($token == T_STRING)) {
  130.                                 $desc = "Trait $value\n";
  131.                                 $wait_trait = false;
  132.                                 $levelAry[] = $dep;
  133.  
  134.                                 $icon->setType(ICON_TYPE_TRAIT);
  135.                                 FastPHPWriter::outputLeafNode($icon, $line, $desc);
  136.                                 $icon->reset();
  137.  
  138.                                 FastPHPWriter::outputIncreaseLevel();
  139.                         }
  140.  
  141.                         if ($wait_interface && ($token == T_STRING)) {
  142.                                 $desc = "Interface $value\n";
  143.                                 $wait_interface = false;
  144.                                 $levelAry[] = $dep;
  145.  
  146.                                 $icon->setType(ICON_TYPE_INTERFACE);
  147.                                 FastPHPWriter::outputLeafNode($icon, $line, $desc);
  148.                                 $icon->reset();
  149.  
  150.                                 FastPHPWriter::outputIncreaseLevel();
  151.                         }
  152.  
  153.                         if ($wait_const && ($token == T_STRING)) {
  154.                                 $desc = "const $value\n";
  155.                                 $wait_const = false;
  156.  
  157.                                 $icon->setType(ICON_TYPE_CONST);
  158.                                 FastPHPWriter::outputLeafNode($icon, $line, $desc);
  159.                                 $icon->reset();
  160.                         }
  161.  
  162.                         if ((!$wait_abstract_func_list_end) && (count($levelAry) > 0) && (count($insideFuncAry) == 0) && ($token == T_VARIABLE)) {
  163.                                 $desc = "$value\n";
  164.  
  165.                                 $icon->setType(ICON_TYPE_VAR);
  166.                                 FastPHPWriter::outputLeafNode($icon, $line, $desc);
  167.                                 $icon->reset();
  168.                         }
  169.  
  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();
  176.  
  177.                         if (($data == ';') || ($data == '{') || ($data == '}')) {
  178.                                 $wait_abstract_func_list_end = false;
  179.                                 $icon->reset();
  180.                         }
  181.  
  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.                         }
  197.  
  198.                         if ($token == T_CONST) {
  199.                                 $wait_const = true;
  200.                         }
  201.  
  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));
  209.                         }
  210.                 }
  211.         }
  212.  
  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));
  218.         }
  219.  
  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));
  224.         }
  225.  
  226.  
  227.         private static final function array_peek($array) {
  228.                 if (!isset($array[count($array)-1])) return null;
  229.                 return $array[count($array)-1];
  230.         }
  231. }
  232.  
  233. $parser = new MyFastPHPCodeExplorer($icon);
  234. while (true) {
  235.         try {
  236.                 $code = FastPHPReader::readCodeFromEditor();
  237.         } catch (FastPHPExitSignalReceivedException $e) {
  238.                 die();
  239.         }
  240.         FastPHPWriter::outputHeader();
  241.         $parser->handle($code);
  242.         FastPHPWriter::outputExit();
  243.         FastPHPWriter::signalOutputEnd();
  244.         sleep(1);
  245. }
  246.