AnnotationService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Doc\Services;
  3. use Illuminate\Support\Facades\Log;
  4. class AnnotationService
  5. {
  6. private $objRe;
  7. private $class;
  8. private $data;
  9. public function __construct($class)
  10. {
  11. $className = '';
  12. if (is_object($class)) {
  13. $className = get_class($class);
  14. } else if (is_string($class)) {
  15. $className = $class;
  16. } else {
  17. exit("class param error!");
  18. }
  19. $this->objRe = new \ReflectionClass($className);//此类的方法被原封不动的继承 可以直接调用
  20. $this->class = $className;
  21. }
  22. /**
  23. * 获取所有的注释文档
  24. * @return array
  25. */
  26. public function getAllDocComment()
  27. {
  28. $methods = $this->objRe->getMethods();
  29. $this->data = [];
  30. foreach ($methods as $item) {
  31. if ($item->isConstructor() || !$item->isPublic() || $item->class != $this->class) {
  32. continue;
  33. }
  34. //获取方法注释
  35. $doc = $item->getDocComment();
  36. $params = $this->getDocuments($doc);
  37. if (isset($params['api'])) {
  38. if (!empty($params) && !isset($params['group'])) {
  39. continue;
  40. }
  41. $this->formatDocument($params);
  42. $group = $params['group']['group'] . ($params['group']['child'] ? '-' . $params['group']['child'] : '');
  43. $data[$group][] = $params;
  44. }
  45. }
  46. return $this->data;
  47. }
  48. /**
  49. * 解析注释
  50. * @param $doc
  51. * @return array
  52. */
  53. private function getDocuments($doc)
  54. {
  55. $result = [];
  56. $list = explode('* @', $doc);
  57. foreach ($list as $item) {
  58. $item = str_replace('*/', '', $item);
  59. $item = str_replace('* ', '', $item);
  60. $item = trim($item);
  61. $line = explode(' ', $item);
  62. $defined = $line[0];
  63. $defined = str_replace(array("\r\n", "\r", "\n"), "", $defined);
  64. switch ($defined) {
  65. case 'api':
  66. $result[$defined] = [
  67. 'key' => $line[0] ?? '',
  68. 'method' => isset($line[1]) && in_array($line[1], ['get', 'post', 'put', 'delete', 'options', 'patch']) ? $line[1] : 'get',
  69. 'route' => $line[2] ?? '',
  70. 'comment' => $line[3] ?? ''
  71. ];
  72. break;
  73. case 'group':
  74. $result[$defined] = [
  75. 'key' => $line[0] ?? '',
  76. 'group' => isset($line[1]) ? str_replace(array("\r\n", "\r", "\n"), "", $line[1]) : '',
  77. 'child' => isset($line[2]) ? str_replace(array("\r\n", "\r", "\n"), "", $line[2]) : '',
  78. ];
  79. break;
  80. case 'param':
  81. $result[$defined][] = [
  82. 'desc' => $line[3] ?? '',
  83. 'example' => $line[5] ?? '',
  84. 'type' => (isset($line[1]) && $line[1] == 'file') ? 'file' : 'text',
  85. 'name' => $line[2] ?? '',
  86. 'required' => (isset($line[4]) && $line[4] == 'required') ? "1" : "0",
  87. ];
  88. break;
  89. case 'header':
  90. $result[$defined][] = [
  91. 'desc' => $line[3] ?? '',
  92. 'example' => $line[5] ?? '',
  93. 'name' => $line[3] ?? '',
  94. 'required' => (isset($line[4]) && $line[4] == 'required') ? "1" : "0",
  95. ];
  96. break;
  97. case 'success':
  98. $result[$defined][] = [
  99. 'key' => $line[0] ?? '',
  100. 'type' => $line[1] ?? '',
  101. 'field' => $line[2] ?? '',
  102. 'comment' => $line[3] ?? '',
  103. 'required' => (isset($line[4]) && $line[4] == 'required') ? true : false,
  104. ];
  105. break;
  106. case 'error':
  107. $result[$defined][] = [
  108. 'key' => $line[0] ?? '',
  109. 'code' => $line[1] ?? '',
  110. 'msg' => $line[2] ?? ''
  111. ];
  112. break;
  113. case 'paramExample':
  114. case 'successExample':
  115. unset($line[0]);
  116. $result[$defined] = implode(' ', $line);
  117. break;
  118. case 'desc':
  119. unset($line[0]);
  120. $result[$defined] = implode(' ', $line);
  121. break;
  122. }
  123. }
  124. return $result;
  125. }
  126. /**
  127. * 更改文档格式
  128. * @param $doc
  129. */
  130. private function formatDocument($doc)
  131. {
  132. if (!isset($this->data[$doc['group']['group'] . '-' . $doc['group']['child']])) {
  133. $this->data[$doc['group']['group'] . '-' . $doc['group']['child']] = [
  134. 'name' => $doc['group']['group'] . '-' . $doc['group']['child'],
  135. 'desc' => $doc['group']['group'] . '-' . $doc['group']['child'],
  136. 'add_time' => time(),
  137. 'up_time' => time(),
  138. 'list' => []
  139. ];
  140. }
  141. $this->data[$doc['group']['group'] . '-' . $doc['group']['child']]['list'][$doc['api']['comment']] = [
  142. 'method' => $doc['api']['method'],
  143. 'title' => $doc['api']['comment'] ? $doc['api']['comment'] : '-',
  144. 'path' => $doc['api']['route'] ? ($doc['api']['route'][0] == '/' ? $doc['api']['route'] : '/' . $doc['api']['route']) : '/undefine',
  145. 'res_body_type' => 'json',
  146. 'res_body' => $doc['successExample'] ?? '',
  147. 'req_body_is_json_schema' => false,
  148. 'req_params' => [],
  149. 'req_headers' => isset($doc['header']) && $doc['header'] ? $doc['header'] : [],
  150. 'type' => 'static',
  151. 'status' => 'done',
  152. 'desc' => $doc['desc'] ?? '',
  153. ];
  154. if ($doc['api']['method'] == 'post') {
  155. $this->data[$doc['group']['group'] . '-' . $doc['group']['child']]['list'][$doc['api']['comment']]['req_body_type'] = 'form';
  156. $this->data[$doc['group']['group'] . '-' . $doc['group']['child']]['list'][$doc['api']['comment']]['req_body_form'] = $doc['param'] ?? [];
  157. } else {
  158. $this->data[$doc['group']['group'] . '-' . $doc['group']['child']]['list'][$doc['api']['comment']]['req_query'] = $doc['param'] ?? [];
  159. }
  160. $this->data[$doc['group']['group'] . '-' . $doc['group']['child']]['list'][$doc['api']['comment']]['query_path'] = [
  161. 'path' => $doc['api']['route'],
  162. 'params' => []
  163. ];
  164. // $this->data[$doc['group']['group']][$doc['group']['child']][$doc['api']['comment']] = [
  165. // 'method' => $doc['api']['method'],
  166. // 'url' => $doc['api']['route'],
  167. // 'comment' => $doc['api']['comment'],
  168. // 'param' => $doc['param']??[],
  169. // 'success' => $doc['success']??[],
  170. // 'header' => $doc['header']??[],
  171. // 'successExample' => $doc['successExample']??''
  172. // ];
  173. }
  174. }