DocParser.php
4.2 KB
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
<?php
namespace Api\Doc;
class DocParser
{
private $params = array ();
/**
* 解析注释
* @param string $doc
* @return array
*/
public function parse($doc = '') {
if ($doc == '') {
return $this->params;
}
// Get the comment
if (preg_match ( '#^/\*\*(.*)\*/#s', $doc, $comment ) === false)
return $this->params;
$comment = trim ( $comment [1] );
// Get all the lines and strip the * from the first character
if (preg_match_all ( '#^\s*\*(.*)#m', $comment, $lines ) === false)
return $this->params;
$this->parseLines ( $lines [1] );
return $this->params;
}
private function parseLines($lines) {
$desc = [];
foreach ( $lines as $line ) {
$parsedLine = $this->parseLine ( $line ); // Parse the line
if ($parsedLine === false && ! isset ( $this->params ['description'] )) {
if (isset ( $desc )) {
// Store the first line in the short description
$this->params ['description'] = implode ( PHP_EOL, $desc );
}
$desc = array ();
} elseif ($parsedLine !== false) {
$desc [] = $parsedLine; // Store the line in the long description
}
}
$desc = implode ( ' ', $desc );
if (! empty ( $desc ))
$this->params ['long_description'] = $desc;
}
private function parseLine($line) {
// trim the whitespace from the line
$line = trim ( $line );
if (empty ( $line ))
return false; // Empty line
if (strpos ( $line, '@' ) === 0) {
if (strpos ( $line, ' ' ) > 0) {
// Get the parameter name
$param = substr ( $line, 1, strpos ( $line, ' ' ) - 1 );
$value = substr ( $line, strlen ( $param ) + 2 ); // Get the value
} else {
$param = substr ( $line, 1 );
$value = '';
}
// Parse the line and return false if the parameter is valid
if ($this->setParam ( $param, $value ))
return false;
}
return $line;
}
private function setParam($param, $value) {
if ($param == 'param' || $param == 'header')
$value = $this->formatParam( $value );
if ($param == 'class')
list ( $param, $value ) = $this->formatClass ( $value );
if($param == 'return' || $param == 'param' || $param == 'header'){
$this->params [$param][] = $value;
}else if (empty ( $this->params [$param] )) {
$this->params [$param] = $value;
} else {
$this->params [$param] = $this->params [$param] . $value;
}
return true;
}
private function formatClass($value) {
$r = preg_split ( "[\(|\)]", $value );
if (is_array ( $r )) {
$param = $r [0];
parse_str ( $r [1], $value );
foreach ( $value as $key => $val ) {
$val = explode ( ',', $val );
if (count ( $val ) > 1)
$value [$key] = $val;
}
} else {
$param = 'Unknown';
}
return array (
$param,
$value
);
}
private function formatParam($string) {
$string = $string." ";
if(preg_match_all('/(\w+):(.*?)[\s\n]/s', $string, $meatchs)){
$param = [];
foreach ($meatchs[1] as $key=>$value){
$param[$meatchs[1][$key]] = $this->getParamType($meatchs[2][$key]);
}
return $param;
}else{
return ''.$string;
}
}
private function getParamType($type){
$typeMaps = [
'string' => '字符串',
'int' => '整型',
'float' => '浮点型',
'boolean' => '布尔型',
'date' => '日期',
'array' => '数组',
'fixed' => '固定值',
'enum' => '枚举类型',
'object' => '对象',
];
return array_key_exists($type,$typeMaps) ? $typeMaps[$type] : $type;
}
}