Doc.php
11.8 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace Api\Doc;
class Doc
{
protected $config = [
'title'=>'APi接口文档',
'version'=>'1.0.0',
'copyright'=>'Powered By Zhangweiwei',
'password' => '',
'static_path' => 'http://192.168.1.15/simplewind/vendor/weiwei/api-doc/src/assets',
'controller' => [
'api\\portal\\controller\\IndexController',
'api\\portal\\controller\\ArticleController',
'api\\portal\\controller\\UserController',
'api\\portal\\controller\\UserneedController',
'api\\portal\\controller\\IntermediaryneedController',
'api\\portal\\controller\\IntermediaryController',
'app\\portal\\controller\\CommonController',
'api\\portal\\controller\\OrderController',
],
'filter_method'=>['_empty'],
'return_format' => [
'status' => "200/300/301/302",
'message' => "提示信息",
]
];
/**
* 架构方法 设置参数
* @access public
* @param array $config 配置参数
*/
public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
}
/**
* 使用 $this->name 获取配置
* @access public
* @param string $name 配置名称
* @return mixed 配置值
*/
public function __get($name)
{
return $this->config[$name];
}
/**
* 设置验证码配置
* @access public
* @param string $name 配置名称
* @param string $value 配置值
* @return void
*/
public function __set($name, $value)
{
if (isset($this->config[$name])) {
$this->config[$name] = $value;
}
}
/**
* 检查配置
* @access public
* @param string $name 配置名称
* @return bool
*/
public function __isset($name)
{
return isset($this->config[$name]);
}
/**
* 获取接口列表
* @return array
*/
public function getList()
{
$controller = $this->config['controller'];
$list = [];
foreach ($controller as $class)
{
if(class_exists($class))
{
$module = [];
$reflection = new \ReflectionClass($class);
$doc_str = $reflection->getDocComment();
$doc = new DocParser();
$class_doc = $doc->parse($doc_str);
$module = $class_doc;
$module['class'] = $class;
$method = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
$filter_method = array_merge(['__construct'], $this->config['filter_method']);
$module['actions'] = [];
foreach ($method as $action){
if(!in_array($action->name, $filter_method))
{
$doc = new DocParser();
$doc_str = $action->getDocComment();
if($doc_str)
{
$action_doc = $doc->parse($doc_str);
$action_doc['name'] = $class."::".$action->name;
if(array_key_exists('title', $action_doc)){
if(array_key_exists('module', $action_doc)){
$key = array_search($action_doc['module'], array_column($module['actions'], 'title'));
if($key === false){
$action = $module;
$action['title'] = $action_doc['module'];
$action['module'] = $action_doc['module'];
$action['actions'] = [];
array_push($action['actions'], $action_doc);
array_push($module['actions'], $action);
}else{
array_push($module['actions'][$key]['actions'], $action_doc);
}
}else{
array_push($module['actions'], $action_doc);
}
}
}
}
}
if(array_key_exists('group', $module)){
$key = array_search($module['group'], array_column($list, 'title'));
if($key === false){ //创建分组
$floder = [
'title' => $module['group'],
'description' => '',
'package' => '',
'class' => '',
'actions' => []
];
array_push($floder['actions'], $module);
array_push($list, $floder);
}else{
array_push($list[$key]['actions'], $module);
}
}else{
array_push($list, $module);
}
}
}
return $list;
}
/**
* 文档目录列表
* @return array
*/
public function getModuleList()
{
$controller = $this->config['controller'];
$list = [];
foreach ($controller as $class) {
if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
$doc_str = $reflection->getDocComment();
$doc = new DocParser();
$class_doc = $doc->parse($doc_str);
if(array_key_exists('group', $class_doc)){
$key = array_search($class_doc['group'], array_column($list, 'title'));
if($key === false){ //创建分组
$floder = [
'title' => $class_doc['group'],
'children' => []
];
array_push($floder['children'], $class_doc);
array_push($list, $floder);
}
else
{
array_push($list[$key]['children'], $class_doc);
}
}else{
array_push($list, $class_doc);
}
}
}
return $list;
}
/**
* 获取类中指导方法注释详情
* @param $class
* @param $action
* @return array
*/
public function getInfo($class, $action)
{
$action_doc = [];
if($class && class_exists($class)){
$reflection = new \ReflectionClass($class);
$doc_str = $reflection->getDocComment();
$doc = new DocParser();
$class_doc = $doc->parse($doc_str);
$class_doc['header'] = isset($class_doc['header'])? $class_doc['header'] : [];
$class_doc['param'] = isset($class_doc['param']) ? $class_doc['param'] : [];
if($reflection->hasMethod($action)) {
$method = $reflection->getMethod($action);
$doc = new DocParser();
$action_doc = $doc->parse($method->getDocComment());
$action_doc['name'] = $class."::".$method->name;
$action_doc['header'] = isset($action_doc['header']) ? array_merge($class_doc['header'], $action_doc['header']) : $class_doc['header'];
$action_doc['param'] = isset($action_doc['param']) ? array_merge($class_doc['param'], $action_doc['param']) : $class_doc['param'];
}
}
return $action_doc;
}
/**
* 文档列表搜素
* @param string $keyword
* @return array
*/
public function searchList($keyword = "")
{
$controller = $this->config['controller'];
$list = [];
foreach ($controller as $class)
{
if(class_exists($class))
{
$reflection = new \ReflectionClass($class);
$method = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
$filter_method = array_merge(['__construct'], $this->config['filter_method']);
foreach ($method as $action){
if(!in_array($action->name, $filter_method))
{
$doc = new DocParser();
$doc_str = $action->getDocComment();
if($doc_str)
{
$action_doc = $doc->parse($doc_str);
$action_doc['name'] = $class."::".$action->name;
if((isset($action_doc['title']) && strpos($action_doc['title'], $keyword) !== false)
|| (isset($action_doc['description']) && strpos($action_doc['description'], $keyword) !== false)
|| (isset($action_doc['author']) && strpos($action_doc['author'], $keyword) !== false)
|| (isset($action_doc['url']) && strpos($action_doc['url'], $keyword) !== false))
{
array_push($list, $action_doc);
}
}
}
}
}
}
return $list;
}
/**
* 格式化数组为json字符串-用于格式显示
* @param array $doc
* @return string
*/
public function formatReturn($doc = [])
{
$json = '{<br>';
$data = $this->config['return_format'];
foreach ($data as $name=>$value) {
$json .= ' "'.$name.'":'.$value.',<br>';
}
$json .= ' "data":{<br/>';
$returns = isset($doc['return']) ? $doc['return'] : [];
foreach ($returns as $val)
{
list($name, $value) = explode(":", trim($val));
if(strpos($value, '@') != false){
$json .= $this->string2jsonArray($doc, $val, ' ');
}else{
$json .= ' ' . $this->string2json(trim($name), $value);
}
}
$json .= ' }<br/>';
$json .= '}';
return $json;
}
/**
* 格式化json字符串-用于展示
* @param $name
* @param $val
* @return string
*/
private function string2json($name, $val){
if(strpos($val,'#') != false){
return '"'.$name.'": ["'.str_replace('#','',$val).'"],<br/>';
}else {
return '"'.$name.'":"'.$val.'",<br/>';
}
}
/**
* 递归转换数组为json字符格式-用于展示
* @param $doc
* @param $val
* @param $space
* @return string
*/
private function string2jsonArray($doc, $val, $space){
list($name, $value) = explode(":", trim($val));
$json = "";
if(strpos($value, "@!") != false){
$json .= $space.'"'.$name.'":{//'.str_replace('@!','',$value).'<br/>';
}else{
$json .= $space.'"'.$name.'":[{//'.str_replace('@','',$value).'<br/>';
}
$return = isset($doc[$name]) ? $doc[$name] : [];
if(preg_match_all('/(\w+):(.*?)[\s\n]/s', $return." ", $meatchs)){
foreach ($meatchs[0] as $key=>$v){
if(strpos($meatchs[2][$key],'@') != false){
$json .= $this->string2jsonArray($doc,$v,$space.' ');
} else{
$json .= $space.' '. $this->string2json(trim($meatchs[1][$key]), $meatchs[2][$key]);
}
}
}
if(strpos($value, "@!") != false){
$json .= $space."}<br/>";
}else{
$json .= $space."}]<br/>";
}
return $json;
}
}