Laravel使用命令根据注释一键生成接口名称文档

1、接口注释模板

/**
     * @api {POST} /admin/addAdmin 添加管理员
     * @apiVersion 1.0.0
     * @apiGrouP 管理员
     * @apiParam {String} phone 必填-手机号
     * @apiParam {String} nickname 必填-昵称
     * @apiParam {String} password 必填-密码
     * @apiParam {File} image 选填-头像
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     *
     * @apiSampleRequest off
     * @apiDescription
     * 【author】:浪里小白龙
     * 【createTime】:9:59 2023-07-10
     */
    public function addAdmin(AdminRule $request, AdminService $service)
    {
        $data['phone'] = $request->input('phone');
        $data['nickname'] = $request->input('nickname');
        $data['password'] = $request->input('password');
        $data['image'] = $request->file('image')??'';

        return $this->back($service->addAdmin($data));
    }

2、在namespace App\Console\Commands下创建OperationType类:

truncate();
        DB::statement('SET FOREIGN_KEY_CHECKS=1;');

        foreach (Route::getRoutes()->getRoutes() as $route)
        {
            $action = $route->getAction();

            // 过滤排除路径in_array('get', $route->methods) ||
            if (in_array($route->uri(), $this->exception)) {
                continue;
            }

            // 验证控制器并执行同步
            if (array_key_exists('controller', $action))
            {
                // 获取注释
                $reflector = new ReflectionClass($route->getController());
                $comment = $reflector->getMethod($route->getActionMethod())->getDocComment();

                // 组合kv
                if (preg_match_all('/@(\w+)\s+(.*)\r?\n/m', $comment, $matches)){
                    $result = array_combine($matches[1], $matches[2]);
                }

                // 整理导入数据
                if ($result && isset($result['api'])) {
                    $row = explode(" ",$result['api']);
                    if (empty($row[2])) $row[2]='';
                    $name =  trim($row[2]);
                    $path = trim($row[1]);
                    $path = substr_replace($path, '',0,1);
                    $method = strtolower($route->methods[0]);
                    $module_str = substr($path, 0, 2);
                    if ($module_str == 'wx') {
                        $module = 2;//小程序
                    } else {
                        $module = 1;
                    }
                    $this->createOperationType($name, $path, $method, $module);
                }
            }
        }
    }

    /**
     * 创建权限
     *
     * @param [type] $name 方法名称
     * @param [type] $path 控制器名称和方法名称
     * @param [type] $method 请求方式
     * @param $module int 所属模块:1后台 2小程序
     * @return \App\Model\OperationType
     */
    private function createOperationType($name, $path, $method, $module)
    {
        return OperationType::create([
            'name' => $name,
            'path' => $path,
            'method' => $method,
            'module' => $module,
        ]);
    }
}

3、创建对应的model:App\Model\OperationType

4、指令命令将:数据自动写入数据库

php artisan operation:sync

//该命令获取到的参数
Array
(
    [api] => {POST} /admin/addAdmin 添加管理员
    [apiVersion] => 1.0.0
    [apiGrouP] => 管理员
    [apiParam] => {File} image 选填-头像
    [apiSuccessExample] => {json} Success-Response:
    [apiSampleRequest] => off
    [apiDescription] => * 【author】:浪里小白龙
)

数据库生成的结果:


image.png

你可能感兴趣的:(Laravel使用命令根据注释一键生成接口名称文档)