Builder.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. $st_output = [];
  33. foreach ($this->classes as $class) {
  34. $classAnnotation = Extractor::getClassAnnotations($class);
  35. // 如果忽略
  36. if (isset($classAnnotation['ApiInternal'])) {
  37. continue;
  38. }
  39. $st_output[] = Extractor::getAllClassAnnotations($class);
  40. }
  41. return end($st_output);
  42. }
  43. protected function generateHeadersTemplate($docs)
  44. {
  45. if (!isset($docs['ApiHeaders'])) {
  46. return [];
  47. }
  48. $headerslist = array();
  49. foreach ($docs['ApiHeaders'] as $params) {
  50. $tr = array(
  51. 'name' => $params['name'],
  52. 'type' => $params['type'],
  53. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  54. 'required' => isset($params['required']) ? $params['required'] : false,
  55. 'description' => isset($params['description']) ? $params['description'] : '',
  56. );
  57. $headerslist[] = $tr;
  58. }
  59. return $headerslist;
  60. }
  61. protected function generateParamsTemplate($docs)
  62. {
  63. if (!isset($docs['ApiParams'])) {
  64. return [];
  65. }
  66. $paramslist = array();
  67. foreach ($docs['ApiParams'] as $params) {
  68. $tr = array(
  69. 'name' => $params['name'],
  70. 'type' => isset($params['type']) ? $params['type'] : 'string',
  71. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  72. 'required' => isset($params['required']) ? $params['required'] : true,
  73. 'description' => isset($params['description']) ? $params['description'] : '',
  74. );
  75. $paramslist[] = $tr;
  76. }
  77. return $paramslist;
  78. }
  79. protected function generateReturnHeadersTemplate($docs)
  80. {
  81. if (!isset($docs['ApiReturnHeaders'])) {
  82. return [];
  83. }
  84. $headerslist = array();
  85. foreach ($docs['ApiReturnHeaders'] as $params) {
  86. $tr = array(
  87. 'name' => $params['name'],
  88. 'type' => 'string',
  89. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  90. 'required' => isset($params['required']) && $params['required'] ? 'Yes' : 'No',
  91. 'description' => isset($params['description']) ? $params['description'] : '',
  92. );
  93. $headerslist[] = $tr;
  94. }
  95. return $headerslist;
  96. }
  97. protected function generateReturnParamsTemplate($st_params)
  98. {
  99. if (!isset($st_params['ApiReturnParams'])) {
  100. return [];
  101. }
  102. $paramslist = array();
  103. foreach ($st_params['ApiReturnParams'] as $params) {
  104. $tr = array(
  105. 'name' => $params['name'],
  106. 'type' => isset($params['type']) ? $params['type'] : 'string',
  107. 'sample' => isset($params['sample']) ? $params['sample'] : '',
  108. 'description' => isset($params['description']) ? $params['description'] : '',
  109. );
  110. $paramslist[] = $tr;
  111. }
  112. return $paramslist;
  113. }
  114. protected function generateBadgeForMethod($data)
  115. {
  116. $method = strtoupper(is_array($data['ApiMethod'][0]) ? $data['ApiMethod'][0]['data'] : $data['ApiMethod'][0]);
  117. $labes = array(
  118. 'POST' => 'label-primary',
  119. 'GET' => 'label-success',
  120. 'PUT' => 'label-warning',
  121. 'DELETE' => 'label-danger',
  122. 'PATCH' => 'label-default',
  123. 'OPTIONS' => 'label-info'
  124. );
  125. return isset($labes[$method]) ? $labes[$method] : $labes['GET'];
  126. }
  127. public function parse()
  128. {
  129. $annotations = $this->extractAnnotations();
  130. $counter = 0;
  131. $section = null;
  132. $docslist = [];
  133. foreach ($annotations as $class => $methods) {
  134. foreach ($methods as $name => $docs) {
  135. if (isset($docs['ApiSector'][0])) {
  136. $section = is_array($docs['ApiSector'][0]) ? $docs['ApiSector'][0]['data'] : $docs['ApiSector'][0];
  137. } else {
  138. $section = $class;
  139. }
  140. if (0 === count($docs)) {
  141. continue;
  142. }
  143. $docslist[$section][] = [
  144. 'id' => $counter,
  145. 'method' => is_array($docs['ApiMethod'][0]) ? $docs['ApiMethod'][0]['data'] : $docs['ApiMethod'][0],
  146. 'method_label' => $this->generateBadgeForMethod($docs),
  147. 'section' => $section,
  148. 'route' => is_array($docs['ApiRoute'][0]) ? $docs['ApiRoute'][0]['data'] : $docs['ApiRoute'][0],
  149. 'title' => is_array($docs['ApiTitle'][0]) ? $docs['ApiTitle'][0]['data'] : $docs['ApiTitle'][0],
  150. 'summary' => is_array($docs['ApiSummary'][0]) ? $docs['ApiSummary'][0]['data'] : $docs['ApiSummary'][0],
  151. 'body' => isset($docs['ApiBody'][0]) ? is_array($docs['ApiBody'][0]) ? $docs['ApiBody'][0]['data'] : $docs['ApiBody'][0] : '',
  152. 'headerslist' => $this->generateHeadersTemplate($docs),
  153. 'paramslist' => $this->generateParamsTemplate($docs),
  154. 'returnheaderslist' => $this->generateReturnHeadersTemplate($docs),
  155. 'returnparamslist' => $this->generateReturnParamsTemplate($docs),
  156. 'return' => isset($docs['ApiReturn']) ? is_array($docs['ApiReturn'][0]) ? $docs['ApiReturn'][0]['data'] : $docs['ApiReturn'][0] : '',
  157. ];
  158. $counter++;
  159. }
  160. }
  161. return $docslist;
  162. }
  163. public function getView()
  164. {
  165. return $this->view;
  166. }
  167. /**
  168. * 渲染
  169. * @param string $template
  170. * @param array $vars
  171. * @return string
  172. */
  173. public function render($template, $vars = [])
  174. {
  175. $docslist = $this->parse();
  176. return $this->view->display(file_get_contents($template), array_merge($vars, ['docslist' => $docslist]));
  177. }
  178. }