Builder.php 9.1 KB

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