This repository has been archived by the owner on Mar 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
FuncNav.php
109 lines (99 loc) · 3.01 KB
/
FuncNav.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
use PhpParser\Error;
use PhpParser\ParserFactory;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Node;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
class FuncNav extends NodeVisitorAbstract {
protected $popupList = [];
protected $namespace;
protected $class;
public function enterNode(Node $node) {
if ($node instanceof Namespace_) {
$this->namespace = $node->name;
return;
}
if ($node instanceof Class_) {
$this->class = empty($this->namespace) ? $node->name
: $this->namespace . '\\' . $node->name;
return;
}
if ($node instanceof Function_) {
$params = [];
foreach ($node->params as $param) {
if ($param->byRef) {
$params[] = '&' . $param->name;
} else {
$params[] = $param->name;
}
}
$this->popupList[] = [
'type' => 'func',
'name' => $node->name,
'params' => implode(", ", $params),
'line' => $node->getLine()
];
return;
}
if ($node instanceof ClassMethod) {
$params = [];
foreach ($node->params as $param) {
if ($param->byRef) {
$params[] = '&' . $param->name;
} else {
$params[] = $param->name;
}
}
$this->popupList[] = [
'type' => 'method',
'class' => $this->class,
'name' => $node->name,
'params' => implode(", ", $params),
'line' => $node->getLine()
];
}
}
public function leaveNode(Node $node)
{
if ($node instanceof Namespace_) {
$this->namespace = '';
return;
}
if ($node instanceof Class_) {
$this->class = '';
}
}
public function getPopupList()
{
return $this->popupList;
}
};
function get_funcNav()
{
$popupList = [];
$filename = $_GET['file'];
if (file_exists($filename)) {
$code = file_get_contents($filename);
if (!empty($code)) {
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$ast = $parser->parse($code);
} catch (Error $error) {
header('HTTP/1.0 500 Internal Server Error');
echo "Parse error: {$error->getMessage()}";
return;
}
$funcNav = new FuncNav();
$traverser = new NodeTraverser();
$traverser->addVisitor($funcNav);
$traverser->traverse($ast);
$popupList = $funcNav->getPopupList();
}
}
header('content-type: application/json');
echo json_encode($popupList);
}