注解文档

Easyswoole允许通过注解控制器的注解标签,生成文档。

控制器输出文档

  1. namespace App\HttpController;
  2. use EasySwoole\HttpAnnotation\AnnotationController;
  3. use EasySwoole\HttpAnnotation\Utility\AnnotationDoc;
  4. class Index extends AnnotationController
  5. {
  6. function index()
  7. {
  8. $doc = new AnnotationDoc();
  9. $string = $doc->scan2Html(EASYSWOOLE_ROOT.'/App/HttpController');
  10. $this->response()->withAddedHeader('Content-type',"text/html;charset=utf-8");
  11. $this->response()->write($string);
  12. }
  13. }

例如在以上的代码中,我们就是直接扫描Easyswoole默认的控制器目录下的全部注解并输出对应文档,用户可以自己去做文档权限控制,或者是对应的目录限制。

生成离线文档

在项目根目录下执行如下命令:

  1. php vendor/bin/easy-doc App/HttpController

即可生成对应的离线文档。

注意,仅当有@Api标记的控制器方法才会被渲染到文档中。

注意事项

有的同学在注解@ApiSuccess中,写入了plainTextjsonArray,会导致注解失败.有以下注意事项:

执行composer info需保证 easyswoole/annotation版本>=2.0.3

plaintText用法为:

MyAnnotation(myProperty=r"{"code":200}") 也就是格式为 r"{RAW}" 字母r+双引号。

json Array用法为:

@PropertyTag(input={"code":2,"result":[{"name":1}]}) 可以直接解析为完整的数组。

注解示例1

  1. namespace App\HttpController;
  2. use EasySwoole\HttpAnnotation\AnnotationController;
  3. use EasySwoole\HttpAnnotation\AnnotationTag\Api;
  4. use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
  5. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFail;
  6. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
  7. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupAuth;
  8. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
  9. use EasySwoole\HttpAnnotation\AnnotationTag\ApiRequestExample;
  10. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
  11. use EasySwoole\HttpAnnotation\AnnotationTag\Param;
  12. /**
  13. * Class Api
  14. * @package App\HttpController
  15. * @ApiGroup(groupName="Api")
  16. * @ApiGroupDescription("这是我的API全局描述")
  17. * @ApiGroupAuth(name="userToken",from={POST},required="",description="用户登录后,服务端返回的token,用于API鉴权")
  18. */
  19. class UserApi extends AnnotationController
  20. {
  21. /**
  22. * @Api(name="update",path="/userApi/update")
  23. * @ApiDescription("更新用户基础资料")
  24. * @ApiRequestExample("curl http://127.0.0.1:9501/userApi/update?name=es&age=11")
  25. * @Param(name="userId",description="用户id")
  26. * @Param(name="account",description="用户account")
  27. * @ApiSuccess({"code":200,"result":{"userId":2,"account":"zyf","username":"es","phone":"xxxx","avatar":null,"createTime":1595837009,"isDelete":null,"deleteTime":null,"user_token":"2-bc429ab40a7a2ebc-1596008468"},"msg":"登录成功"})
  28. * @ApiFail({"code":400,"result":null,"msg":"字段非法"})
  29. */
  30. function update()
  31. {
  32. }
  33. }

注解示例2

  1. use EasySwoole\Component\Context\ContextManager;
  2. use EasySwoole\HttpAnnotation\AnnotationController;
  3. use EasySwoole\HttpAnnotation\AnnotationTag\Api;
  4. use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
  5. use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
  6. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFail;
  7. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFailParam;
  8. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup as ApiGroupTag;
  9. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupAuth;
  10. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
  11. use EasySwoole\HttpAnnotation\AnnotationTag\ApiRequestExample;
  12. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
  13. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
  14. use EasySwoole\HttpAnnotation\AnnotationTag\CircuitBreaker;
  15. use EasySwoole\HttpAnnotation\AnnotationTag\Context;
  16. use EasySwoole\HttpAnnotation\AnnotationTag\Di;
  17. use EasySwoole\HttpAnnotation\AnnotationTag\InjectParamsContext;
  18. use EasySwoole\HttpAnnotation\AnnotationTag\Method;
  19. use EasySwoole\HttpAnnotation\AnnotationTag\Param;
  20. use EasySwoole\HttpAnnotation\Exception\Annotation\ParamValidateError;
  21. /**
  22. * Class ControllerA
  23. * @package EasySwoole\HttpAnnotation\Tests\TestController
  24. * @ApiGroupTag(groupName="GroupA")
  25. * @ApiGroupDescription("GroupA desc")
  26. * @ApiGroupAuth(name="groupParamA",required="")
  27. * @ApiGroupAuth(name="groupParamB",required="")
  28. */
  29. class Annotation extends AnnotationController
  30. {
  31. /**
  32. * @Di(key="di")
  33. */
  34. public $di;
  35. /**
  36. * @Context(key="context")
  37. */
  38. public $context;
  39. /**
  40. * @Api(path="/apiGroup/func",name="func")
  41. * @ApiAuth(name="apiAuth1")
  42. * @ApiAuth(name="apiAuth2")
  43. * @ApiDescription("func desc")
  44. * @ApiFail("func fail example1")
  45. * @ApiFail("func fail example2")
  46. * @ApiFailParam(name="failParam1")
  47. * @ApiFailParam(name="failParam2")
  48. * @ApiRequestExample("func request example1")
  49. * @ApiRequestExample("func request example2")
  50. * @ApiSuccess("func success example1")
  51. * @ApiSuccess("func success example2")
  52. * @ApiSuccessParam(name="successParam1")
  53. * @ApiSuccessParam(name="successParam2")
  54. * @CircuitBreaker(timeout=5.0)
  55. * @InjectParamsContext(key="requestData")
  56. * @Method(allow={POST,GET})
  57. * @Param(name="param1")
  58. * @Param(name="param2")
  59. */
  60. function func()
  61. {
  62. }
  63. }