Builder.php 9.4 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. $inputtype = $params['type'] && isset($typeArr[$params['type']]) ? $typeArr[$params['type']] : ($params['name'] == 'password' ? 'password' : 'text');
  85. $tr = array(
  86. 'name' => $params['name'],
  87. 'type' => $params['type'] ?? 'string',
  88. 'inputtype' => $inputtype,
  89. 'sample' => $params['sample'] ?? '',
  90. 'required' => $params['required'] ?? true,
  91. 'description' => $params['description'] ?? '',
  92. );
  93. $paramslist[] = $tr;
  94. }
  95. return $paramslist;
  96. }
  97. protected function generateReturnHeadersTemplate($docs)
  98. {
  99. if (!isset($docs['ApiReturnHeaders'])) {
  100. return [];
  101. }
  102. $headerslist = array();
  103. foreach ($docs['ApiReturnHeaders'] as $params) {
  104. $tr = array(
  105. 'name' => $params['name'] ?? '',
  106. 'type' => 'string',
  107. 'sample' => $params['sample'] ?? '',
  108. 'required' => isset($params['required']) && $params['required'] ? 'Yes' : 'No',
  109. 'description' => $params['description'] ?? '',
  110. );
  111. $headerslist[] = $tr;
  112. }
  113. return $headerslist;
  114. }
  115. protected function generateReturnParamsTemplate($st_params)
  116. {
  117. if (!isset($st_params['ApiReturnParams'])) {
  118. return [];
  119. }
  120. $paramslist = array();
  121. foreach ($st_params['ApiReturnParams'] as $params) {
  122. $tr = array(
  123. 'name' => $params['name'] ?? '',
  124. 'type' => $params['type'] ?? 'string',
  125. 'sample' => $params['sample'] ?? '',
  126. 'description' => $params['description'] ?? '',
  127. );
  128. $paramslist[] = $tr;
  129. }
  130. return $paramslist;
  131. }
  132. protected function generateBadgeForMethod($data)
  133. {
  134. $method = strtoupper(is_array($data['ApiMethod'][0]) ? $data['ApiMethod'][0]['data'] : $data['ApiMethod'][0]);
  135. $labes = array(
  136. 'POST' => 'label-primary',
  137. 'GET' => 'label-success',
  138. 'PUT' => 'label-warning',
  139. 'DELETE' => 'label-danger',
  140. 'PATCH' => 'label-default',
  141. 'OPTIONS' => 'label-info'
  142. );
  143. return isset($labes[$method]) ? $labes[$method] : $labes['GET'];
  144. }
  145. public function parse()
  146. {
  147. list($allClassAnnotations, $allClassMethodAnnotations) = $this->extractAnnotations();
  148. $sectorArr = [];
  149. foreach ($allClassAnnotations as $index => &$allClassAnnotation) {
  150. // 如果设置隐藏,则不显示在文档
  151. if (isset($allClassAnnotation['ApiInternal'])) {
  152. continue;
  153. }
  154. $sector = isset($allClassAnnotation['ApiSector']) ? $allClassAnnotation['ApiSector'][0] : $allClassAnnotation['ApiTitle'][0];
  155. $sectorArr[$sector] = isset($allClassAnnotation['ApiWeigh']) ? $allClassAnnotation['ApiWeigh'][0] : 0;
  156. }
  157. unset($allClassAnnotation);
  158. arsort($sectorArr);
  159. $routes = include_once CONF_PATH . 'route.php';
  160. $subdomain = false;
  161. if (config('url_domain_deploy') && isset($routes['__domain__']) && isset($routes['__domain__']['api']) && $routes['__domain__']['api']) {
  162. $subdomain = true;
  163. }
  164. $counter = 0;
  165. $section = null;
  166. $weigh = 0;
  167. $docsList = [];
  168. foreach ($allClassMethodAnnotations as $class => $methods) {
  169. foreach ($methods as $name => $docs) {
  170. if (isset($docs['ApiSector'][0])) {
  171. $section = is_array($docs['ApiSector'][0]) ? $docs['ApiSector'][0]['data'] : $docs['ApiSector'][0];
  172. } else {
  173. $section = $class;
  174. }
  175. if (0 === count($docs)) {
  176. continue;
  177. }
  178. $route = is_array($docs['ApiRoute'][0]) ? $docs['ApiRoute'][0]['data'] : $docs['ApiRoute'][0];
  179. if ($subdomain) {
  180. $route = substr($route, 4);
  181. }
  182. $docsList[$section][$name] = [
  183. 'id' => $counter,
  184. 'method' => is_array($docs['ApiMethod'][0]) ? $docs['ApiMethod'][0]['data'] : $docs['ApiMethod'][0],
  185. 'methodLabel' => $this->generateBadgeForMethod($docs),
  186. 'section' => $section,
  187. 'route' => $route,
  188. 'title' => is_array($docs['ApiTitle'][0]) ? $docs['ApiTitle'][0]['data'] : $docs['ApiTitle'][0],
  189. 'summary' => is_array($docs['ApiSummary'][0]) ? $docs['ApiSummary'][0]['data'] : $docs['ApiSummary'][0],
  190. 'body' => isset($docs['ApiBody'][0]) ? (is_array($docs['ApiBody'][0]) ? $docs['ApiBody'][0]['data'] : $docs['ApiBody'][0]) : '',
  191. 'headersList' => $this->generateHeadersTemplate($docs),
  192. 'paramsList' => $this->generateParamsTemplate($docs),
  193. 'returnHeadersList' => $this->generateReturnHeadersTemplate($docs),
  194. 'returnParamsList' => $this->generateReturnParamsTemplate($docs),
  195. 'weigh' => is_array($docs['ApiWeigh'][0]) ? $docs['ApiWeigh'][0]['data'] : $docs['ApiWeigh'][0],
  196. 'return' => isset($docs['ApiReturn']) ? (is_array($docs['ApiReturn'][0]) ? $docs['ApiReturn'][0]['data'] : $docs['ApiReturn'][0]) : '',
  197. 'needLogin' => $docs['ApiPermissionLogin'][0],
  198. 'needRight' => $docs['ApiPermissionRight'][0],
  199. ];
  200. $counter++;
  201. }
  202. }
  203. //重建排序
  204. foreach ($docsList as $index => &$methods) {
  205. $methodSectorArr = [];
  206. foreach ($methods as $name => $method) {
  207. $methodSectorArr[$name] = isset($method['weigh']) ? $method['weigh'] : 0;
  208. }
  209. arsort($methodSectorArr);
  210. $methods = array_merge(array_flip(array_keys($methodSectorArr)), $methods);
  211. }
  212. $docsList = array_merge(array_flip(array_keys($sectorArr)), $docsList);
  213. return $docsList;
  214. }
  215. public function getView()
  216. {
  217. return $this->view;
  218. }
  219. /**
  220. * 渲染
  221. * @param string $template
  222. * @param array $vars
  223. * @return string
  224. */
  225. public function render($template, $vars = [])
  226. {
  227. $docsList = $this->parse();
  228. return $this->view->display(file_get_contents($template), array_merge($vars, ['docsList' => $docsList]));
  229. }
  230. }