项目作者: bobitluo

项目描述 :
一个基于 phpDocumentor 生成 Swagger2.0 json 的PHP文档生成器
高级语言: PHP
项目地址: git://github.com/bobitluo/php-swagger.git
创建时间: 2019-08-07T03:39:44Z
项目社区:https://github.com/bobitluo/php-swagger

开源协议:MIT License

下载


php-swagger

Introduction

一个基于 phpDocumentor 生成 Swagger2.0 json 的PHP文档生成器

Installation

  1. composer require bobitluo/php-swagger

Usage

  1. $options = [
  2. 'title' => 'API title', // API系统标题
  3. 'description' => 'API description', // API系统描述
  4. 'version' => '1.0.0', // API版本号
  5. 'host' => '{yourhost.com}', // API系统域名
  6. 'schemes' => [ // API支持的SCHEME列表
  7. 'https',
  8. ],
  9. 'securityDefinitions' => [ // API支持的认证列表。无需验证时设置为[]。详情见:https://swagger.io/docs/specification/2-0/authentication/
  10. 'ApiKeyAuth' => [
  11. 'type' => 'apiKey',
  12. 'in' => 'header',
  13. 'name' => 'Authorization',
  14. ],
  15. ],
  16. 'security' => [ // API默认全局使用的认证定义。无需验证时设置为[]。详情见:https://swagger.io/docs/specification/2-0/authentication/
  17. [
  18. 'ApiKeyAuth' => [],
  19. ],
  20. ],
  21. 'controller_prefix' => '', // 控制器类名前缀。默认值为''
  22. 'controller_postfix' => '', // 控制器类名后缀。默认值为''
  23. 'action_prefix' => '', // Action方法名前缀。默认值为空''
  24. 'action_postfix' => '', // Action方法名后缀。默认值为空''
  25. ];
  26. \bobitluo\Php\Swagger\Options::getInstance( $options );
  27. $directory = new \bobitluo\Php\Swagger\Directory('{your_controller_dir}', function($uriPath){
  28. return preg_replace('/(\/controllers)$/', '', $uriPath);
  29. });
  30. echo $directory->build();

PHP注释样例

  1. /**
  2. * @package 用户
  3. */
  4. class UserController {
  5. /**
  6. * 登录
  7. *
  8. * 支持密码和验证码两种方式登录
  9. *
  10. * @package 用户
  11. * @http-method post
  12. *
  13. * @param string $login_type* 登录类型(password,sms_code) password
  14. * @param int $cellphone* 手机号 13800138000
  15. * @param string $password* 密码 123456
  16. * @param array $options[] 选项数组 10
  17. * @param array $ext[id][] 扩展ID数组
  18. * @param array $ext[name][] 扩展名称数组
  19. *
  20. * @return json 200 成功
  21. *
  22. * 字段解释:
  23. *
  24. * 名称 | 类型 | 示例 | 描述
  25. * ------- | ---- | ---- | ----
  26. * expires | int | 1565796956 | 凭证过期时间戳
  27. * type | string | Bearer | 凭证类型
  28. * token | string | xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | 登录凭证
  29. *
  30. * 返回样例:
  31. *
  32. *
  1. * {
  2. * "ret_code": 200,
  3. * "ret_msg": "success",
  4. * "result": {
  5. * "expires": 1565796956,
  6. * "type": "Bearer",
  7. * "token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  8. * }
  9. * }
  10. * ```
  11. * @return json 401 认证失败
  12. */
  13. public function loginAction() {
  14. ...

```

PHP注释描述

name description example comment
title 接口标题 登录 建议简短
descritpion 接口描述 支持密码和验证码两种方式登录 可多行
@package 接口所在的分类 @package 用户 SwaggerUI中会根据分类分组显示。
可在Controller类注释中添加此类所有Action的默认分类
@http-method 接口请求方法 @http-method post 目前仅较好的支持get, post
@param 接口参数 @param string $cellphone* 手机号 13800138000 数据类型 参数名($开头,末尾加*表示必填) 参数描述 默认值
@return 接口返回 @return json 200 成功

字段解释:

名称 \
类型 \ 示例 \ 描述
——— \
——— \ ——— \ ——-
expires \
int \ 1565796956 \ 凭证过期时间戳
@return 标签后紧跟接口返回类型(如:json, xml),返回码,返回码描述。后续多行支持Markdown格式的内容