审查视图

simplewind/vendor/weiwei/api-doc/src/Doc.php 11.7 KB
lihan authored
1 2 3 4 5
<?php
namespace Api\Doc;

class Doc
{
6 7 8 9
    protected $config = [
        'title' => 'APi接口文档',
        'version' => '1.0.0',
        'copyright' => '银河百荣科技',
lihan authored
10
        'controller' => [
lihan authored
11 12
            'app\portal\controller\IndexController',
            'app\escort\controller\EscortController',
13
            'app\activity\controller\ActivityController',
14 15
            'app\user\controller\CenterController',
            'app\team\controller\TeamController',
.  
lihan authored
16 17 18
            'app\order\controller\OrderController',
            'app\collect\controller\CollectController',
            'app\news\controller\NewsController'
lihan authored
19
        ],
20 21 22
        'password' => 'bronet',
        'static_path' => '',
        'filter_method' => ['_empty'],
lihan authored
23 24 25 26
        'return_format' => [
            'code' => "状态码20000/40000+",
            'msg' => "提示信息",
        ],
27 28
        'public_header' => [],
        'public_param' => []
lihan authored
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
    ];

    /**
     * 架构方法 设置参数
     * @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
55
     * @param  string $name 配置名称
lihan authored
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
     * @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 = [];
85 86
        foreach ($controller as $class) {
            if (class_exists($class)) {
lihan authored
87 88 89 90 91
                $module = [];
                $reflection = new \ReflectionClass($class);
                $doc_str = $reflection->getDocComment();
                $doc = new DocParser();
                $class_doc = $doc->parse($doc_str);
92
                $module = $class_doc;
lihan authored
93 94 95 96
                $module['class'] = $class;
                $method = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
                $filter_method = array_merge(['__construct'], $this->config['filter_method']);
                $module['actions'] = [];
97 98
                foreach ($method as $action) {
                    if (!in_array($action->name, $filter_method)) {
lihan authored
99 100
                        $doc = new DocParser();
                        $doc_str = $action->getDocComment();
101
                        if ($doc_str) {
lihan authored
102
                            $action_doc = $doc->parse($doc_str);
103 104 105
                            $action_doc['name'] = $class . "::" . $action->name;
                            if (array_key_exists('title', $action_doc)) {
                                if (array_key_exists('module', $action_doc)) {
lihan authored
106
                                    $key = array_search($action_doc['module'], array_column($module['actions'], 'title'));
107
                                    if ($key === false) {
lihan authored
108 109 110 111 112 113
                                        $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);
114
                                    } else {
lihan authored
115 116
                                        array_push($module['actions'][$key]['actions'], $action_doc);
                                    }
117
                                } else {
lihan authored
118 119 120 121 122 123
                                    array_push($module['actions'], $action_doc);
                                }
                            }
                        }
                    }
                }
124
                if (array_key_exists('group', $module)) {
lihan authored
125
                    $key = array_search($module['group'], array_column($list, 'title'));
126
                    if ($key === false) { //创建分组
lihan authored
127 128 129 130 131 132 133 134 135
                        $floder = [
                            'title' => $module['group'],
                            'description' => '',
                            'package' => '',
                            'class' => '',
                            'actions' => []
                        ];
                        array_push($floder['actions'], $module);
                        array_push($list, $floder);
136
                    } else {
lihan authored
137 138
                        array_push($list[$key]['actions'], $module);
                    }
139
                } else {
lihan authored
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
                    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);
161
                if (array_key_exists('group', $class_doc)) {
lihan authored
162
                    $key = array_search($class_doc['group'], array_column($list, 'title'));
163
                    if ($key === false) { //创建分组
lihan authored
164 165 166 167 168 169
                        $floder = [
                            'title' => $class_doc['group'],
                            'children' => []
                        ];
                        array_push($floder['children'], $class_doc);
                        array_push($list, $floder);
170
                    } else {
lihan authored
171 172
                        array_push($list[$key]['children'], $class_doc);
                    }
173
                } else {
lihan authored
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
                    array_push($list, $class_doc);
                }
            }
        }
        return $list;
    }

    /**
     * 获取类中指导方法注释详情
     * @param $class
     * @param $action
     * @return array
     */
    public function getInfo($class, $action)
    {
        $action_doc = [];
190
        if ($class && class_exists($class)) {
lihan authored
191 192 193 194
            $reflection = new \ReflectionClass($class);
            $doc_str = $reflection->getDocComment();
            $doc = new DocParser();
            $class_doc = $doc->parse($doc_str);
195
            $class_doc['header'] = isset($class_doc['header']) ? $class_doc['header'] : [];
lihan authored
196
            $class_doc['param'] = isset($class_doc['param']) ? $class_doc['param'] : [];
197
            if ($reflection->hasMethod($action)) {
lihan authored
198 199 200
                $method = $reflection->getMethod($action);
                $doc = new DocParser();
                $action_doc = $doc->parse($method->getDocComment());
201
                $action_doc['name'] = $class . "::" . $method->name;
lihan authored
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
                $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 = [];
218 219
        foreach ($controller as $class) {
            if (class_exists($class)) {
lihan authored
220 221 222
                $reflection = new \ReflectionClass($class);
                $method = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
                $filter_method = array_merge(['__construct'], $this->config['filter_method']);
223 224
                foreach ($method as $action) {
                    if (!in_array($action->name, $filter_method)) {
lihan authored
225 226
                        $doc = new DocParser();
                        $doc_str = $action->getDocComment();
227
                        if ($doc_str) {
lihan authored
228
                            $action_doc = $doc->parse($doc_str);
229 230 231 232 233
                            $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)) {
lihan authored
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                                array_push($list, $action_doc);
                            }
                        }
                    }
                }
            }
        }
        return $list;
    }

    /**
     * 格式化数组为json字符串-用于格式显示
     * @param array $doc
     * @return string
     */
    public function formatReturn($doc = [])
    {
        $json = '{<br>';
        $data = $this->config['return_format'];
253 254
        foreach ($data as $name => $value) {
            $json .= '&nbsp;&nbsp;"' . $name . '":' . $value . ',<br>';
lihan authored
255 256 257
        }
        $json .= '&nbsp;&nbsp;"data":{<br/>';
        $returns = isset($doc['return']) ? $doc['return'] : [];
258 259 260
        foreach ($returns as $val) {
            list($name, $value) = explode(":", trim($val));
            if (strpos($value, '@') != false) {
lihan authored
261
                $json .= $this->string2jsonArray($doc, $val, '&nbsp;&nbsp;&nbsp;&nbsp;');
262
            } else {
lihan authored
263 264 265 266 267 268 269 270 271 272 273 274 275 276
                $json .= '&nbsp;&nbsp;&nbsp;&nbsp;' . $this->string2json(trim($name), $value);
            }
        }
        $json .= '&nbsp;&nbsp;}<br/>';
        $json .= '}';
        return $json;
    }

    /**
     * 格式化json字符串-用于展示
     * @param $name
     * @param $val
     * @return string
     */
277 278 279 280 281 282
    private function string2json($name, $val)
    {
        if (strpos($val, '#') != false) {
            return '"' . $name . '": ["' . str_replace('#', '', $val) . '"],<br/>';
        } else {
            return '"' . $name . '":"' . $val . '",<br/>';
lihan authored
283 284 285 286 287 288 289 290 291 292
        }
    }

    /**
     * 递归转换数组为json字符格式-用于展示
     * @param $doc
     * @param $val
     * @param $space
     * @return string
     */
293 294 295
    private function string2jsonArray($doc, $val, $space)
    {
        list($name, $value) = explode(":", trim($val));
lihan authored
296
        $json = "";
297 298 299 300
        if (strpos($value, "@!") != false) {
            $json .= $space . '"' . $name . '":{//' . str_replace('@!', '', $value) . '<br/>';
        } else {
            $json .= $space . '"' . $name . '":[{//' . str_replace('@', '', $value) . '<br/>';
lihan authored
301 302
        }
        $return = isset($doc[$name]) ? $doc[$name] : [];
303 304 305 306 307 308
        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 . '&nbsp;&nbsp;');
                } else {
                    $json .= $space . '&nbsp;&nbsp;' . $this->string2json(trim($meatchs[1][$key]), $meatchs[2][$key]);
lihan authored
309 310 311
                }
            }
        }
312 313 314 315
        if (strpos($value, "@!") != false) {
            $json .= $space . "}<br/>";
        } else {
            $json .= $space . "}]<br/>";
lihan authored
316 317 318 319
        }
        return $json;
    }
}