|
| 1 | +<?php |
| 2 | +class PHPCtags |
| 3 | +{ |
| 4 | + private $mFile; |
| 5 | + |
| 6 | + private $mParser; |
| 7 | + |
| 8 | + public function __construct($file) { |
| 9 | + //@todo Check for existence |
| 10 | + $this->mFile = $file; |
| 11 | + $this->mParser = new PHPParser_Parser(new PHPParser_Lexer); |
| 12 | + } |
| 13 | + |
| 14 | + private function getAccess($node) |
| 15 | + { |
| 16 | + if ($node->isPrivate()) return 'private'; |
| 17 | + if ($node->isProtected()) return 'protected'; |
| 18 | + return 'public'; |
| 19 | + } |
| 20 | + |
| 21 | + private function struct($node, $class_name = NULL, $function_name = NULL) |
| 22 | + { |
| 23 | + static $structs = array(); |
| 24 | + |
| 25 | + $kind = $name = $line = $scope = $access = ''; |
| 26 | + if (is_array($node)) { |
| 27 | + foreach ($node as $subNode) { |
| 28 | + $this->struct($subNode, $class_name, $function_name); |
| 29 | + } |
| 30 | + } elseif ($node instanceof PHPParser_Node_Stmt_Class) { |
| 31 | + $kind = 'c'; |
| 32 | + $name = $node->name; |
| 33 | + $line = $node->getLine() - 1; |
| 34 | + foreach ($node as $subNode) { |
| 35 | + $this->struct($subNode, $name); |
| 36 | + } |
| 37 | + } elseif ($node instanceof PHPParser_Node_Stmt_Property) { |
| 38 | + $kind = 'v'; |
| 39 | + $prop = $node->props[0]; |
| 40 | + $name = $prop->name; |
| 41 | + $line = $prop->getLine() - 1; |
| 42 | + $scope = "class:" . $class_name; |
| 43 | + $access = $this->getAccess($node); |
| 44 | + } elseif ($node instanceof PHPParser_Node_Stmt_ClassConst) { |
| 45 | + $kind = 'd'; |
| 46 | + $cons = $node->consts[0]; |
| 47 | + $name = $cons->name; |
| 48 | + $line = $cons->getLine() - 1; |
| 49 | + $scope = "class:" . $class_name; |
| 50 | + } elseif ($node instanceof PHPParser_Node_Stmt_ClassMethod) { |
| 51 | + $kind = 'f'; |
| 52 | + $name = $node->name; |
| 53 | + $line = $node->getLine() - 1; |
| 54 | + $scope = "class:" . $class_name; |
| 55 | + $access = $this->getAccess($node); |
| 56 | + foreach ($node as $subNode) { |
| 57 | + $this->struct($subNode, $class_name, $name); |
| 58 | + } |
| 59 | + } elseif ($node instanceof PHPParser_Node_Stmt_Const) { |
| 60 | + $kind = 'd'; |
| 61 | + $cons = $node->consts[0]; |
| 62 | + $name = $cons->name; |
| 63 | + $line = $node->getLine() - 1; |
| 64 | + } elseif ($node instanceof PHPParser_Node_Stmt_Global) { |
| 65 | + $kind = 'v'; |
| 66 | + $prop = $node->vars[0]; |
| 67 | + $name = $prop->name; |
| 68 | + $line = $node->getLine() - 1; |
| 69 | + } elseif ($node instanceof PHPParser_Node_Stmt_Static) { |
| 70 | + //@todo |
| 71 | + } elseif ($node instanceof PHPParser_Node_Stmt_Declare) { |
| 72 | + //@todo |
| 73 | + } elseif ($node instanceof PHPParser_Node_Stmt_Function) { |
| 74 | + $kind = 'f'; |
| 75 | + $name = $node->name; |
| 76 | + $line = $node->getLine() - 1; |
| 77 | + foreach ($node as $subNode) { |
| 78 | + $this->struct($subNode, $class_name, $name); |
| 79 | + } |
| 80 | + } elseif ($node instanceof PHPParser_Node_Stmt_Trait) { |
| 81 | + //@todo |
| 82 | + } elseif ($node instanceof PHPParser_Node_Stmt_Interface) { |
| 83 | + $kind = 'i'; |
| 84 | + $name = $node->name; |
| 85 | + $line = $node->getLine() - 1; |
| 86 | + } elseif ($node instanceof PHPParser_Node_Stmt_Namespace) { |
| 87 | + //@todo |
| 88 | + } elseif ($node instanceof PHPParser_Node_Expr_Assign) { |
| 89 | + $kind = 'v'; |
| 90 | + $node = $node->var; |
| 91 | + $name = $node->name; |
| 92 | + $line = $node->getLine() - 1; |
| 93 | + if (!empty($class_name) && !empty($function_name)) { |
| 94 | + $scope = "function:" . $class_name . '::' . $function_name; |
| 95 | + } elseif (!empty($function_name)) { |
| 96 | + $scope = "function:" . $function_name; |
| 97 | + } |
| 98 | + } else { |
| 99 | + // we don't care the rest of them. |
| 100 | + } |
| 101 | + |
| 102 | + if(!empty($kind) && !empty($name) && !empty($line)) { |
| 103 | + $structs[] = array( |
| 104 | + 'kind' => $kind, |
| 105 | + 'name' => $name, |
| 106 | + 'line' => $line, |
| 107 | + 'scope' => $scope, |
| 108 | + 'access' => $access, |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + return $structs; |
| 113 | + } |
| 114 | + |
| 115 | + private function render($structs) |
| 116 | + { |
| 117 | + $str = ''; |
| 118 | + $lines = file($this->mFile); |
| 119 | + foreach ($structs as $stuct) { |
| 120 | + if (empty($stuct['name']) || empty($stuct['line']) || empty($stuct['kind'])) |
| 121 | + return; |
| 122 | + |
| 123 | + if ($stuct['kind'] == 'v') { |
| 124 | + $str .= "$" . $stuct['name']; |
| 125 | + } else { |
| 126 | + $str .= $stuct['name']; |
| 127 | + } |
| 128 | + $str .= "\t" . $this->mFile; |
| 129 | + $str .= "\t" . "/^" . rtrim($lines[$stuct['line']], "\n") . "$/;\""; |
| 130 | + $str .= "\t" . $stuct['kind']; |
| 131 | + $str .= "\t" . "line:" . $stuct['line']; |
| 132 | + !empty($stuct['scope']) && $str .= "\t" . $stuct['scope']; |
| 133 | + !empty($stuct['access']) && $str .= "\t" . "access:" . $stuct['access']; |
| 134 | + $str .= "\n"; |
| 135 | + } |
| 136 | + return $str; |
| 137 | + } |
| 138 | + |
| 139 | + public function export() |
| 140 | + { |
| 141 | + $code =file_get_contents($this->mFile); |
| 142 | + $stmts = $this->mParser->parse($code); |
| 143 | + $structs = $this->struct($stmts); |
| 144 | + echo $this->render($structs); |
| 145 | + } |
| 146 | +} |
0 commit comments