Builder.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <karsonzhang@163.com>
  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. }
  40. $allClassAnnotation = Extractor::getAllClassAnnotations();
  41. $allClassMethodAnnotation = Extractor::getAllClassMethodAnnotations();
  42. // foreach ($allClassMethodAnnotation as $className => &$methods) {
  43. // foreach ($methods as &$method) {
  44. // //权重判断
  45. // if ($method && !isset($method['ApiWeigh']) && isset($allClassAnnotation[$className]['ApiWeigh'])) {
  46. // $method['ApiWeigh'] = $allClassAnnotation[$className]['ApiWeigh'];
  47. // }
  48. // }
  49. // }
  50. // unset($methods);
  51. return [$allClassAnnotation, $allClassMethodAnnotation];
  52. }
  53. protected function generateHeadersTemplate($docs)
  54. {
  55. if (!isset($docs['ApiHeaders'])) {
  56. return [];
  57. }
  58. $headerslist = array();
  59. foreach ($docs['ApiHeaders'] as $params) {
  60. $tr = array(
  61. 'name' => $params['name'],
  62. 'type' => $params['type'],
  63. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  64. 'required' => isset($params['required']) ? $params['required'] : false,
  65. 'description' => isset($params['description']) ? $params['description'] : '',
  66. );
  67. $headerslist[] = $tr;
  68. }
  69. return $headerslist;
  70. }
  71. protected function generateParamsTemplate($docs)
  72. {
  73. if (!isset($docs['ApiParams'])) {
  74. return [];
  75. }
  76. $paramslist = array();
  77. foreach ($docs['ApiParams'] as $params) {
  78. $tr = array(
  79. 'name' => $params['name'],
  80. 'type' => isset($params['type']) ? $params['type'] : 'string',
  81. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  82. 'required' => isset($params['required']) ? $params['required'] : true,
  83. 'description' => isset($params['description']) ? $params['description'] : '',
  84. );
  85. $paramslist[] = $tr;
  86. }
  87. return $paramslist;
  88. }
  89. protected function generateReturnHeadersTemplate($docs)
  90. {
  91. if (!isset($docs['ApiReturnHeaders'])) {
  92. return [];
  93. }
  94. $headerslist = array();
  95. foreach ($docs['ApiReturnHeaders'] as $params) {
  96. $tr = array(
  97. 'name' => $params['name'],
  98. 'type' => 'string',
  99. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  100. 'required' => isset($params['required']) && $params['required'] ? 'Yes' : 'No',
  101. 'description' => isset($params['description']) ? $params['description'] : '',
  102. );
  103. $headerslist[] = $tr;
  104. }
  105. return $headerslist;
  106. }
  107. protected function generateReturnParamsTemplate($st_params)
  108. {
  109. if (!isset($st_params['ApiReturnParams'])) {
  110. return [];
  111. }
  112. $paramslist = array();
  113. foreach ($st_params['ApiReturnParams'] as $params) {
  114. $tr = array(
  115. 'name' => $params['name'],
  116. 'type' => isset($params['type']) ? $params['type'] : 'string',
  117. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  118. 'description' => isset($params['description']) ? $params['description'] : '',
  119. );
  120. $paramslist[] = $tr;
  121. }
  122. return $paramslist;
  123. }
  124. protected function generateBadgeForMethod($data)
  125. {
  126. $method = strtoupper(is_array($data['ApiMethod'][0]) ? $data['ApiMethod'][0]['data'] : $data['ApiMethod'][0]);
  127. $labes = array(
  128. 'POST' => 'label-primary',
  129. 'GET' => 'label-success',
  130. 'PUT' => 'label-warning',
  131. 'DELETE' => 'label-danger',
  132. 'PATCH' => 'label-default',
  133. 'OPTIONS' => 'label-info'
  134. );
  135. return isset($labes[$method]) ? $labes[$method] : $labes['GET'];
  136. }
  137. public function parse()
  138. {
  139. list($allClassAnnotations, $allClassMethodAnnotations) = $this->extractAnnotations();
  140. $sectorArr = [];
  141. foreach ($allClassAnnotations as $index => $allClassAnnotation) {
  142. $sector = isset($allClassAnnotation['ApiSector']) ? $allClassAnnotation['ApiSector'][0] : $allClassAnnotation['ApiTitle'][0];
  143. $sectorArr[$sector] = isset($allClassAnnotation['ApiWeigh']) ? $allClassAnnotation['ApiWeigh'][0] : 0;
  144. }
  145. arsort($sectorArr);
  146. $routes = include_once CONF_PATH . 'route.php';
  147. $subdomain = false;
  148. if (config('url_domain_deploy') && isset($routes['__domain__']) && isset($routes['__domain__']['api']) && $routes['__domain__']['api']) {
  149. $subdomain = true;
  150. }
  151. $counter = 0;
  152. $section = null;
  153. $weigh = 0;
  154. $docslist = [];
  155. foreach ($allClassMethodAnnotations as $class => $methods) {
  156. foreach ($methods as $name => $docs) {
  157. if (isset($docs['ApiSector'][0])) {
  158. $section = is_array($docs['ApiSector'][0]) ? $docs['ApiSector'][0]['data'] : $docs['ApiSector'][0];
  159. } else {
  160. $section = $class;
  161. }
  162. if (0 === count($docs)) {
  163. continue;
  164. }
  165. $route = is_array($docs['ApiRoute'][0]) ? $docs['ApiRoute'][0]['data'] : $docs['ApiRoute'][0];
  166. if ($subdomain) {
  167. $route = substr($route, 4);
  168. }
  169. $docslist[$section][$name] = [
  170. 'id' => $counter,
  171. 'method' => is_array($docs['ApiMethod'][0]) ? $docs['ApiMethod'][0]['data'] : $docs['ApiMethod'][0],
  172. 'method_label' => $this->generateBadgeForMethod($docs),
  173. 'section' => $section,
  174. 'route' => $route,
  175. 'title' => is_array($docs['ApiTitle'][0]) ? $docs['ApiTitle'][0]['data'] : $docs['ApiTitle'][0],
  176. 'summary' => is_array($docs['ApiSummary'][0]) ? $docs['ApiSummary'][0]['data'] : $docs['ApiSummary'][0],
  177. 'body' => isset($docs['ApiBody'][0]) ? is_array($docs['ApiBody'][0]) ? $docs['ApiBody'][0]['data'] : $docs['ApiBody'][0] : '',
  178. 'headerslist' => $this->generateHeadersTemplate($docs),
  179. 'paramslist' => $this->generateParamsTemplate($docs),
  180. 'returnheaderslist' => $this->generateReturnHeadersTemplate($docs),
  181. 'returnparamslist' => $this->generateReturnParamsTemplate($docs),
  182. 'weigh' => is_array($docs['ApiWeigh'][0]) ? $docs['ApiWeigh'][0]['data'] : $docs['ApiWeigh'][0],
  183. 'return' => isset($docs['ApiReturn']) ? is_array($docs['ApiReturn'][0]) ? $docs['ApiReturn'][0]['data'] : $docs['ApiReturn'][0] : '',
  184. ];
  185. $counter++;
  186. }
  187. }
  188. //重建排序
  189. foreach ($docslist as $index => &$methods) {
  190. $methodSectorArr = [];
  191. foreach ($methods as $name => $method) {
  192. $methodSectorArr[$name] = isset($method['weigh']) ? $method['weigh'] : 0;
  193. }
  194. arsort($methodSectorArr);
  195. $methods = array_merge(array_flip(array_keys($methodSectorArr)), $methods);
  196. }
  197. $docslist = array_merge(array_flip(array_keys($sectorArr)), $docslist);
  198. return $docslist;
  199. }
  200. public function getView()
  201. {
  202. return $this->view;
  203. }
  204. /**
  205. * 渲染
  206. * @param string $template
  207. * @param array $vars
  208. * @return string
  209. */
  210. public function render($template, $vars = [])
  211. {
  212. $docslist = $this->parse();
  213. return $this->view->display(file_get_contents($template), array_merge($vars, ['docslist' => $docslist]));
  214. }
  215. }