Annotation.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace EasySwoole\HttpAnnotation\Tests\TestController;
  3. use EasySwoole\Component\Context\ContextManager;
  4. use EasySwoole\HttpAnnotation\AnnotationController;
  5. use EasySwoole\HttpAnnotation\AnnotationTag\Api;
  6. use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
  7. use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
  8. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFail;
  9. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFailParam;
  10. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup as ApiGroupTag;
  11. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupAuth;
  12. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
  13. use EasySwoole\HttpAnnotation\AnnotationTag\ApiRequestExample;
  14. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
  15. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
  16. use EasySwoole\HttpAnnotation\AnnotationTag\CircuitBreaker;
  17. use EasySwoole\HttpAnnotation\AnnotationTag\Context;
  18. use EasySwoole\HttpAnnotation\AnnotationTag\Di;
  19. use EasySwoole\HttpAnnotation\AnnotationTag\Inject;
  20. use EasySwoole\HttpAnnotation\AnnotationTag\InjectParamsContext;
  21. use EasySwoole\HttpAnnotation\AnnotationTag\Method;
  22. use EasySwoole\HttpAnnotation\AnnotationTag\Param;
  23. use EasySwoole\HttpAnnotation\Exception\Annotation\ParamValidateError;
  24. /**
  25. * Class ControllerA
  26. * @package EasySwoole\HttpAnnotation\Tests\TestController
  27. * @ApiGroupTag(groupName="GroupA")
  28. * @ApiGroupDescription("GroupA desc")
  29. * @ApiGroupAuth(name="groupParamA",required="")
  30. * @ApiGroupAuth(name="groupParamB",required="")
  31. */
  32. class Annotation extends AnnotationController
  33. {
  34. /**
  35. * @Di(key="di")
  36. */
  37. public $di;
  38. /**
  39. * @Context(key="context")
  40. */
  41. public $context;
  42. /**
  43. * @var Test $inject
  44. * @Inject(className="\EasySwoole\HttpAnnotation\Tests\TestController\Test", args={1,{1,2}})
  45. */
  46. public $inject;
  47. /**
  48. * @Api(path="/apiGroup/func",name="func")
  49. * @ApiAuth(name="apiAuth1")
  50. * @ApiAuth(name="apiAuth2")
  51. * @ApiDescription("func desc")
  52. * @ApiFail("func fail example1")
  53. * @ApiFail("func fail example2")
  54. * @ApiFailParam(name="failParam1")
  55. * @ApiFailParam(name="failParam2")
  56. * @ApiRequestExample("func request example1")
  57. * @ApiRequestExample("func request example2")
  58. * @ApiSuccess("func success example1")
  59. * @ApiSuccess("func success example2")
  60. * @ApiSuccessParam(name="successParam1")
  61. * @ApiSuccessParam(name="successParam2")
  62. * @CircuitBreaker(timeout=5.0)
  63. * @InjectParamsContext(key="requestData")
  64. * @Method(allow={POST,GET})
  65. * @Param(name="param1")
  66. * @Param(name="param2")
  67. */
  68. function func()
  69. {
  70. }
  71. function index()
  72. {
  73. $this->response()->write('index');
  74. }
  75. /**
  76. * @Method(allow={POST})
  77. */
  78. function allowPostMethod()
  79. {
  80. $this->response()->write('allowPostMethod');
  81. }
  82. /**
  83. * @ApiAuth(name="onRequestAuth1", description="onRequest-auth1")
  84. * @ApiAuth(name="onRequestAuth2", description="onRequest-auth2")
  85. * @Param(name="onRequestParam1", description="onRequest-param1")
  86. * @Param(name="onRequestParam2", description="onRequest-param2")
  87. * @param string|null $action
  88. * @return bool|null
  89. */
  90. protected function onRequest(?string $action): ?bool
  91. {
  92. return parent::onRequest($action); // TODO: Change the autogenerated stub
  93. }
  94. function onException(\Throwable $throwable): void
  95. {
  96. if($throwable instanceof ParamValidateError){
  97. $this->response()->write("PE-{$throwable->getValidate()->getError()->getField()}");
  98. }else{
  99. throw $throwable;
  100. }
  101. }
  102. /**
  103. * @Param(name="param1",required="",integer="")
  104. */
  105. function param1()
  106. {
  107. $this->response()->write($this->request()->getRequestParam('param1'));
  108. }
  109. /**
  110. * @Api(name="param2",path="/param2")
  111. * @Param(name="param1",required="",integer="")
  112. * @Param(name="param2",required="",integer="")
  113. */
  114. function param2()
  115. {
  116. $p1 = $this->request()->getRequestParam('param1');
  117. $p2 = $this->request()->getRequestParam('param2');
  118. $this->response()->write($p1 + $p2);
  119. }
  120. /**
  121. * @Param(name="param1",required="",integer="")
  122. * @Param(name="groupParamA",required="",integer="")
  123. */
  124. function param3()
  125. {
  126. //测试与api group的合并
  127. $p1 = $this->request()->getRequestParam('param1');
  128. $p2 = $this->request()->getRequestParam('groupParamA');
  129. $this->response()->write($p1 + $p2);
  130. }
  131. function paramExport1($groupParamA)
  132. {
  133. $this->response()->write($groupParamA);
  134. }
  135. /**
  136. * @Param(name="exp")
  137. */
  138. function paramExport2($groupParamA,$exp)
  139. {
  140. $this->response()->write($exp);
  141. }
  142. /**
  143. * @Param(name="param1",required="")
  144. * @InjectParamsContext(key="data",onlyParamTag=false);
  145. */
  146. function injectParam1()
  147. {
  148. $this->response()->write(implode("|",ContextManager::getInstance()->get('data')));
  149. }
  150. /**
  151. * @Param(name="param1",required="")
  152. * @InjectParamsContext(key="data");
  153. */
  154. function injectParam2()
  155. {
  156. $this->response()->write(implode("|",ContextManager::getInstance()->get('data')));
  157. }
  158. public function inject()
  159. {
  160. $this->response()->write($this->inject->index());
  161. }
  162. public function injectGetString()
  163. {
  164. $this->response()->write($this->inject->getString());
  165. }
  166. public function injectGetArray()
  167. {
  168. $this->response()->write(json_encode($this->inject->getArray()));
  169. }
  170. protected function gc()
  171. {
  172. //不调用父类重置成员属性,方便单元测试
  173. }
  174. }