-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassParser.php
153 lines (141 loc) · 3.67 KB
/
ClassParser.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
/**
* Class ClassParser
*/
class ClassParser {
private $classes = array();
private $extends = array();
private $implements = array();
const STATE_CLASS_HEAD = 100001;
const STATE_FUNCTION_HEAD = 100002;
/**
* @return array
*/
public function getClasses() {
return $this->classes;
}
/**
* @param $interface
* @return array
*/
public function getClassesImplementing($interface) {
$implementers = array();
if (isset($this->implements[$interface])) {
foreach($this->implements[$interface] as $name) {
$implementers[$name] = $this->classes[$name];
}
}
return $implementers;
}
/**
* @param $class
* @return array
*/
public function getClassesExtending($class) {
$extenders = array();
if (isset($this->extends[$class])) {
foreach($this->extends[$class] as $name) {
$extenders[$name] = $this->classes[$name];
}
}
return $extenders;
}
/**
* @param $file
*/
public function parse($file) {
$file = realpath($file);
$tokens = token_get_all(file_get_contents($file));
$classes = array();
$si = NULL;
$depth = 0;
$mod = array();
$doc = NULL;
$state = NULL;
$line = NULL;
foreach ($tokens as $idx => &$token) {
if (is_array($token)) {
switch ($token[0]) {
case T_DOC_COMMENT:
$doc = $token[1];
break;
case T_PUBLIC:
case T_PRIVATE:
case T_STATIC:
case T_ABSTRACT:
case T_PROTECTED:
$mod[] = $token[1];
break;
case T_CLASS:
case T_FUNCTION:
$state = $token[0];
$line = $token[2];
break;
case T_EXTENDS:
case T_IMPLEMENTS:
switch ($state) {
case self::STATE_CLASS_HEAD:
case T_EXTENDS:
$state = $token[0];
break;
}
break;
case T_STRING:
switch ($state) {
case T_CLASS:
$state = self::STATE_CLASS_HEAD;
$si = $token[1];
$classes[] = array('name' => $token[1], 'modifiers' => $mod, 'line' => $line, 'doc' => $doc);
break;
case T_FUNCTION:
$state = self::STATE_FUNCTION_HEAD;
$clsc = count($classes);
if ($depth>0 && $clsc) {
$classes[$clsc-1]['functions'][$token[1]] = array('modifiers' => $mod, 'line' => $line, 'doc' => $doc);
}
break;
case T_IMPLEMENTS:
case T_EXTENDS:
$clsc = count($classes);
$classes[$clsc-1][$state==T_IMPLEMENTS ? 'implements' : 'extends'][] = $token[1];
break;
}
break;
}
}
else {
switch ($token) {
case '{':
$depth++;
break;
case '}':
$depth--;
break;
}
switch ($token) {
case '{':
case '}':
case ';':
$state = 0;
$doc = NULL;
$mod = array();
break;
}
}
}
foreach ($classes as $class) {
$class['file'] = $file;
$this->classes[$class['name']] = $class;
if (!empty($class['implements'])) {
foreach ($class['implements'] as $name) {
$this->implements[$name][] = $class['name'];
}
}
if (!empty($class['extends'])) {
foreach ($class['extends'] as $name) {
$this->extends[$name][] = $class['name'];
}
}
}
}
}