-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocParse.php
196 lines (177 loc) · 5.41 KB
/
DocParse.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
class DocParse
{
protected $property = [
'name'=>'interface name',
'description'=>'interface description',
'param'=>[],
'return'=>[]
];
public function parse($str){
if(preg_match('#^/\*\*(.*)\*/#s', $str, $matches) === false){
//error($this->tag,'invalid comment');
return false;
}
if(!isset($matches[1])){
return false;
}
$comment = $matches[1];
//Get all the lines and strip the * from the first character
if(preg_match_all('#^\s*\*(.*)#m', $comment, $lines) === false){
//error($this->tag,'invalid comment');
return false;
}
if(!isset($lines[1])){
return false;
}
$this->parseLines($lines[1]);
return true;
}
protected function parseLines($lines){
$description = [];
$name = false;
$value = '';
foreach($lines as $line){
$var = $this->parseLine($line,$name);
if($var === false){
continue;
}
if($var['name'] === false){
array_push($description,$var['value']);
}else{
if($var['isComplete']){
/**
* save prev buffer
*/
if(!empty($value)){
$this->setProperty($name,trim($value));
$value = '';
}
/**
* save current
*/
$name = $var['name'];
$this->setProperty($name,$var['value']);
}else{
$name = $var['name'];
if($var['value']){
$value .= PHP_EOL.$var['value'];
}
}
}
}
$this->setProperty('description',implode(PHP_EOL,$description));
if(!empty($value)){
$this->setProperty($name,$value);
}
return true;
}
protected function parseLine($line,$prevName = false){
$line = trim($line);
if(empty($line)){
return false;
}
$isComplete = true;
if(strpos($line, '@') === 0) {
$pos = strpos($line, ' ');
if($pos == false){
$name = substr($line, 1);
$value = false;
$isComplete = false;
}else{
$name = substr($line, 1, strpos($line, ' ') - 1);
$value = substr($line, strlen($name) + 2);
}
return [
'name'=>$name,
'value'=>$value,
'isComplete'=>$isComplete
];
}else{
$isComplete = false;
}
return [
'name'=>$prevName,
'value'=>$line,
'isComplete'=>$isComplete
];
}
protected function formatValue($value){
$map = explode(' ',$value,3);
if(empty($map)){
return false;
}
if(count($map) == 1){
return [
'type'=>'null',
'name'=>ltrim($map[0],'$'),
'description'=>'null'
];
}else if(count($map) == 2){
return [
'type'=>'null',
'name'=>ltrim($map[0],'$'),
'description'=>$map[1]
];
}else{
return [
'type'=>$map[0],
'name'=>ltrim($map[1],'$'),
'description'=>$map[2]
];
}
}
protected function setProperty($name,$value){
$isMulti = false;
if(in_array($name,['param','return'])){
$isMulti = true;
}
if($isMulti) {
$value = $this->formatValue($value);
if($value === false){
return false;
}
}
if($isMulti){
if(empty($this->property[$name])){
$this->property[$name] = [$value];
}else{
array_push($this->property[$name],$value);
}
}else{
$this->property[$name] = $value;
}
return true;
}
protected function buildValue($data){
$str = sprintf('| %s | %s | %s |'.PHP_EOL,lang('Name'),lang('Type'),lang('Description'));
$str .= '| --- | --- | --- |'.PHP_EOL;
foreach ($data as $row){
$str .= sprintf('| %s | %s | %s |'.PHP_EOL,$row['name'],$row['type'],$row['description']);
}
return $str;
}
/**
*
*/
public function markdown(){
$doc = [];
array_push($doc,'## '.$this->property['name']);
array_push($doc,$this->property['description']);
array_push($doc,PHP_EOL);
array_push($doc,'## '.lang('Params'));
array_push($doc,$this->buildValue($this->property['param']));
array_push($doc,PHP_EOL);
array_push($doc,'## '.lang('Return'));
array_push($doc,$this->buildValue($this->property['return']));
array_push($doc,PHP_EOL);
if(!empty($this->property['example'])){
array_push($doc,'## '.lang('Example'));
array_push($doc,'```');
array_push($doc,$this->property['example']);
array_push($doc,'```');
}
array_push($doc,PHP_EOL);
return implode(PHP_EOL,$doc);
}
}