Builder.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace app\admin\command\Api\library;
  3. use think\Config;
  4. /**
  5. * @website https://github.com/calinrada/php-apidoc
  6. * @author Calin Rada <rada.calin@gmail.com>
  7. * @author Karson <karson@fastadmin.net>
  8. */
  9. class Builder
  10. {
  11. /**
  12. *
  13. * @var \think\View
  14. */
  15. public $view = null;
  16. /**
  17. * parse classes
  18. * @var array
  19. */
  20. protected $classes = [];
  21. /**
  22. *
  23. * @param array $classes
  24. */
  25. public function __construct($classes = [])
  26. {
  27. $this->classes = array_merge($this->classes, $classes);
  28. $this->view = new \think\View(Config::get('template'), Config::get('view_replace_str'));
  29. }
  30. protected function extractAnnotations()
  31. {
  32. foreach ($this->classes as $class) {
  33. $classAnnotation = Extractor::getClassAnnotations($class);
  34. // 如果忽略
  35. if (isset($classAnnotation['ApiInternal'])) {
  36. continue;
  37. }
  38. Extractor::getClassMethodAnnotations($class);
  39. //Extractor::getClassPropertyValues($class);
  40. }
  41. $allClassAnnotation = Extractor::getAllClassAnnotations();
  42. $allClassMethodAnnotation = Extractor::getAllClassMethodAnnotations();
  43. //$allClassPropertyValue = Extractor::getAllClassPropertyValues();
  44. // foreach ($allClassMethodAnnotation as $className => &$methods) {
  45. // foreach ($methods as &$method) {
  46. // //权重判断
  47. // if ($method && !isset($method['ApiWeigh']) && isset($allClassAnnotation[$className]['ApiWeigh'])) {
  48. // $method['ApiWeigh'] = $allClassAnnotation[$className]['ApiWeigh'];
  49. // }
  50. // }
  51. // }
  52. // unset($methods);
  53. return [$allClassAnnotation, $allClassMethodAnnotation];
  54. }
  55. protected function generateHeadersTemplate($docs)
  56. {
  57. if (!isset($docs['ApiHeaders'])) {
  58. return [];
  59. }
  60. $headerslist = array();
  61. foreach ($docs['ApiHeaders'] as $params) {
  62. $tr = array(
  63. 'name' => $params['name'] ?? '',
  64. 'type' => $params['type'] ?? 'string',
  65. 'sample' => $params['sample'] ?? '',
  66. 'required' => $params['required'] ?? false,
  67. 'description' => $params['description'] ?? '',
  68. );
  69. $headerslist[] = $tr;
  70. }
  71. return $headerslist;
  72. }
  73. protected function generateParamsTemplate($docs)
  74. {
  75. if (!isset($docs['ApiParams'])) {
  76. return [];
  77. }
  78. $typeArr = [
  79. 'integer' => 'number',
  80. 'file' => 'file',
  81. ];
  82. $paramslist = array();
  83. foreach ($docs['ApiParams'] as $params) {
  84. $type = strtolower($params['type'] ?? 'string');
  85. $inputtype = $typeArr[$type] ?? ($params['name'] == 'password' ? 'password' : 'text');
  86. $tr = array(
  87. 'name' => $params['name'],
  88. 'type' => $type,
  89. 'inputtype' => $inputtype,
  90. 'sample' => $params['sample'] ?? '',
  91. 'required' => $params['required'] ?? true,
  92. 'description' => $params['description'] ?? '',
  93. );
  94. $paramslist[] = $tr;
  95. }
  96. return $paramslist;
  97. }
  98. protected function generateReturnHeadersTemplate($docs)
  99. {
  100. if (!isset($docs['ApiReturnHeaders'])) {
  101. return [];
  102. }
  103. $headerslist = array();
  104. foreach ($docs['ApiReturnHeaders'] as $params) {
  105. $tr = array(
  106. 'name' => $params['name'] ?? '',
  107. 'type' => 'string',
  108. 'sample' => $params['sample'] ?? '',
  109. 'required' => isset($params['required']) && $params['required'] ? 'Yes' : 'No',
  110. 'description' => $params['description'] ?? '',
  111. );
  112. $headerslist[] = $tr;
  113. }
  114. return $headerslist;
  115. }
  116. protected function generateReturnParamsTemplate($st_params)
  117. {
  118. if (!isset($st_params['ApiReturnParams'])) {
  119. return [];
  120. }
  121. $paramslist = array();
  122. foreach ($st_params['ApiReturnParams'] as $params) {
  123. $tr = array(
  124. 'name' => $params['name'] ?? '',
  125. 'type' => $params['type'] ?? 'string',
  126. 'sample' => $params['sample'] ?? '',
  127. 'description' => $params['description'] ?? '',
  128. );
  129. $paramslist[] = $tr;
  130. }
  131. return $paramslist;
  132. }
  133. protected function generateBadgeForMethod($data)
  134. {
  135. $method = strtoupper(is_array($data['ApiMethod'][0]) ? $data['ApiMethod'][0]['data'] : $data['ApiMethod'][0]);
  136. $labes = array(
  137. 'POST' => 'label-primary',
  138. 'GET' => 'label-success',
  139. 'PUT' => 'label-warning',
  140. 'DELETE' => 'label-danger',
  141. 'PATCH' => 'label-default',
  142. 'OPTIONS' => 'label-info'
  143. );
  144. return $labes[$method] ?? $labes['GET'];
  145. }
  146. public function parse()
  147. {
  148. list($allClassAnnotations, $allClassMethodAnnotations) = $this->extractAnnotations();
  149. $sectorArr = [];
  150. foreach ($allClassAnnotations as $index => &$allClassAnnotation) {
  151. // 如果设置隐藏,则不显示在文档
  152. if (isset($allClassAnnotation['ApiInternal'])) {
  153. continue;
  154. }
  155. $sector = isset($allClassAnnotation['ApiSector']) ? $allClassAnnotation['ApiSector'][0] : $allClassAnnotation['ApiTitle'][0];
  156. $sectorArr[$sector] = isset($allClassAnnotation['ApiWeigh']) ? $allClassAnnotation['ApiWeigh'][0] : 0;
  157. }
  158. unset($allClassAnnotation);
  159. arsort($sectorArr);
  160. $routes = include_once CONF_PATH . 'route.php';
  161. $subdomain = false;
  162. if (config('url_domain_deploy') && isset($routes['__domain__']) && isset($routes['__domain__']['api']) && $routes['__domain__']['api']) {
  163. $subdomain = true;
  164. }
  165. $counter = 0;
  166. $section = null;
  167. $weigh = 0;
  168. $docsList = [];
  169. foreach ($allClassMethodAnnotations as $class => $methods) {
  170. foreach ($methods as $name => $docs) {
  171. if (isset($docs['ApiSector'][0])) {
  172. $section = is_array($docs['ApiSector'][0]) ? $docs['ApiSector'][0]['data'] : $docs['ApiSector'][0];
  173. } else {
  174. $section = $class;
  175. }
  176. if (0 === count($docs)) {
  177. continue;
  178. }
  179. $route = is_array($docs['ApiRoute'][0]) ? $docs['ApiRoute'][0]['data'] : $docs['ApiRoute'][0];
  180. if ($subdomain) {
  181. $route = substr($route, 4);
  182. }
  183. $docsList[$section][$name] = [
  184. 'id' => $counter,
  185. 'method' => is_array($docs['ApiMethod'][0]) ? $docs['ApiMethod'][0]['data'] : $docs['ApiMethod'][0],
  186. 'methodLabel' => $this->generateBadgeForMethod($docs),
  187. 'section' => $section,
  188. 'route' => $route,
  189. 'title' => is_array($docs['ApiTitle'][0]) ? $docs['ApiTitle'][0]['data'] : $docs['ApiTitle'][0],
  190. 'summary' => is_array($docs['ApiSummary'][0]) ? $docs['ApiSummary'][0]['data'] : $docs['ApiSummary'][0],
  191. 'body' => isset($docs['ApiBody'][0]) ? (is_array($docs['ApiBody'][0]) ? $docs['ApiBody'][0]['data'] : $docs['ApiBody'][0]) : '',
  192. 'headersList' => $this->generateHeadersTemplate($docs),
  193. 'paramsList' => $this->generateParamsTemplate($docs),
  194. 'returnHeadersList' => $this->generateReturnHeadersTemplate($docs),
  195. 'returnParamsList' => $this->generateReturnParamsTemplate($docs),
  196. 'weigh' => is_array($docs['ApiWeigh'][0]) ? $docs['ApiWeigh'][0]['data'] : $docs['ApiWeigh'][0],
  197. 'return' => isset($docs['ApiReturn']) ? (is_array($docs['ApiReturn'][0]) ? $docs['ApiReturn'][0]['data'] : $docs['ApiReturn'][0]) : '',
  198. 'needLogin' => $docs['ApiPermissionLogin'][0],
  199. 'needRight' => $docs['ApiPermissionRight'][0],
  200. ];
  201. $counter++;
  202. }
  203. }
  204. //重建排序
  205. foreach ($docsList as $index => &$methods) {
  206. $methodSectorArr = [];
  207. foreach ($methods as $name => $method) {
  208. $methodSectorArr[$name] = $method['weigh'] ?? 0;
  209. }
  210. arsort($methodSectorArr);
  211. $methods = array_merge(array_flip(array_keys($methodSectorArr)), $methods);
  212. }
  213. $docsList = array_merge(array_flip(array_keys($sectorArr)), $docsList);
  214. return $docsList;
  215. }
  216. public function getView()
  217. {
  218. return $this->view;
  219. }
  220. /**
  221. * 渲染
  222. * @param string $template
  223. * @param array $vars
  224. * @return string
  225. */
  226. public function render($template, $vars = [])
  227. {
  228. $docsList = $this->parse();
  229. return $this->view->display(file_get_contents($template), array_merge($vars, ['docsList' => $docsList]));
  230. }
  231. }