-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdoc.php
executable file
·212 lines (186 loc) · 7.39 KB
/
doc.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
$service = $_GET['m'];
if (empty($service)) {
die('缺少参数:m');
}
include_once('config/config.php'); //加载配置文件
list($className, $methodName) = explode('.', $service);
include_once(CURRENT_CLASS_DIR.$className.'.php');
//获取返回结果
$rMethod = new ReflectionMethod($className, $methodName);
$docComment = $rMethod->getDocComment();
$docCommentArr = explode("\n", $docComment);
//获取接口参数
$rules = [];
if (method_exists($className,'getRules')) {
$classObj = new $className();
$rulesArr = $classObj::getRules();
$rules = $rulesArr[$methodName];
}
//定义类型
$typeMaps = array(
'string' => '字符串',
'int' => '整型',
'float' => '浮点型',
'boolean' => '布尔型',
'date' => '日期',
'array' => '数组',
'fixed' => '固定值',
'enum' => '枚举类型',
'object' => '对象',
);
$description = '//请检测函数标题描述';
$descComment = '//请使用@desc 注释';
if (!empty($docCommentArr)) {
foreach ($docCommentArr as $comment) {
$comment = trim($comment);
//标题描述
if (strpos($comment, '@') === false && strpos($comment, '/') === false) {
$description = substr($comment, strpos($comment, '*') + 1);
continue;
}
//@desc注释
$pos = stripos($comment, '@desc');
if ($pos !== false) {
$descComment = substr($comment, $pos + 5);
continue;
}
//@return注释
$pos = stripos($comment, '@return');
if ($pos === false) {
continue;
}
$returnCommentArr = explode(' ', substr($comment, $pos + 8));
//将数组中的空值过滤掉,同时将需要展示的值返回
$returnCommentArr = array_values(array_filter($returnCommentArr));
if (count($returnCommentArr) < 2) {
continue;
}
if (!isset($returnCommentArr[2])) {
$returnCommentArr[2] = ''; //可选的字段说明
} else {
//兼容处理有空格的注释
$returnCommentArr[2] = implode(' ', array_slice($returnCommentArr, 2));
}
$returns[] = $returnCommentArr;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $service?> <?php echo ' | '.PRODUCT_NAME;?></title>
<link rel="stylesheet" href="assets/css/semantic.min.css">
</head>
<body>
<div class="ui large top fixed menu transition visible" style="display: flex !important;">
<div class="ui container">
<div class="header item">API_DOC<code>(1.0)</code></div>
<a class="item" href="list_class.php">文件列表</a>
<a class="item" href="list_method.php?f=<?php echo $_GET['f'];?>">接口列表</a>
<a class="active item">文档详情</a>
<a class="item" href="wiki.php">使用说明</a>
</div>
</div>
<div class="ui text container" style="max-width: none !important; margin-top: 50px;">
<div class="ui floating message">
<h2 class='ui header'>接口:<?php echo ($service ? '<a href="list_method.php?m='.$service.'&f='.$_GET['f'].'&reload=1">'.$service.'</a>' : '--'); ?></h2>
<br/>
<span class='ui teal tag label'>
<?php echo ($description ? $description : '//请检测函数标题描述');?>
</span>
<div class="ui raised segment">
<span class="ui red ribbon label">接口说明</span>
<div class="ui message">
<p> <?php echo ($descComment ? $descComment : '//请使用@desc 注释');?></p>
</div>
</div>
<h3>接口参数</h3>
<table class="ui red celled striped table" >
<thead>
<tr>
<th>参数名字</th>
<th>类型</th>
<th>是否必须</th>
<th>默认值</th>
<th>其他</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($rules)) {
foreach ($rules as $key => $rule) {
$name = $rule['name'];
if (!isset($rule['type'])) {
$rule['type'] = 'string';
}
$type = isset($typeMaps[$rule['type']]) ? $typeMaps[$rule['type']] : $rule['type'];
$require = isset($rule['require']) && $rule['require'] ? '<font color="red">必须</font>' : '可选';
$default = isset($rule['default']) ? $rule['default'] : '';
if ($default === NULL) {
$default = 'NULL';
} else if (is_array($default)) {
$default = json_encode($default);
} else if (!is_string($default)) {
$default = var_export($default, true);
}
$other = '';
if (isset($rule['min'])) {
$other .= ' 最小:' . $rule['min'];
}
if (isset($rule['max'])) {
$other .= ' 最大:' . $rule['max'];
}
if (isset($rule['range'])) {
$other .= ' 范围:' . implode('/', $rule['range']);
}
$desc = isset($rule['desc']) ? trim($rule['desc']) : '';
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$type.'</td>';
echo '<td>'.$require.'</td>';
echo '<td>'.$default.'</td>';
echo '<td>'.$other.'</td>';
echo '<td>'.$desc.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
<h3>返回结果</h3>
<table class="ui green celled striped table" >
<thead>
<tr>
<th>返回字段</th>
<th>类型</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($returns)) {
foreach ($returns as $item) {
$name = $item[1];
$type = isset($typeMaps[$item[0]]) ? $typeMaps[$item[0]] : $item[0];
$detail = $item[2];
echo '<tr>';
echo '<td>'.$name.'</td>';
echo '<td>'.$type.'</td>';
echo '<td>'.$detail.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
<div class="ui blue message">
<strong>温馨提示:</strong> 此接口参数列表根据后台代码自动生成。
</div>
</div>
<p><?php echo COPYRIGHT?><p>
</div>
</body>
</html>