You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
7.3 KiB

3 years ago
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. /*
  5. 通过注释生成api文档
  6. */
  7. class FromCommentsController extends BaseController {
  8. public function generate(){
  9. //return ;
  10. header( 'Content-Type:text/html;charset=utf-8 ');
  11. $content = I("content") ;
  12. $api_key = I("api_key");
  13. $api_token = I("api_token");
  14. $item_id = D("ItemToken")->check($api_key , $api_token);
  15. if (!$item_id) {
  16. //没验证通过
  17. echo "\napi_key或者api_token不匹配\n\n";
  18. return false;
  19. }
  20. $content = str_replace("_this_and_change_", "&", $content);
  21. $p = "|/\*\*([\s\S]*)\*/|U";
  22. preg_match_all($p, $content , $matches) ;
  23. if ($matches && $matches[0]) {
  24. foreach ($matches[0] as $key => $value) {
  25. if (strstr($value,"@title") && strstr($value,"showdoc")) {
  26. $ret = $this->generate_one($item_id , $value);
  27. }
  28. }
  29. }
  30. if ($ret) {
  31. echo "\n 成功 \n\n ";
  32. }else{
  33. echo "失败";
  34. }
  35. }
  36. private function generate_one($item_id,$content){
  37. $array = $this->parse_content($content);
  38. $page_content = $this->toMarkdown($array);
  39. $page_title = $array['title'];
  40. $page_content = $page_content;
  41. $cat_name = $array['cat_name'];
  42. $s_number = $array['s_number'] ? $array['s_number'] : 99;
  43. $page_id = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$s_number);
  44. if ($page_id) {
  45. $ret = D("Page")->where(" page_id = '$page_id' ")->find();
  46. return $ret;
  47. }else{
  48. return false;
  49. }
  50. }
  51. //解析content,返回数组
  52. private function parse_content($content){
  53. $array = array() ;
  54. //解析标题
  55. $array['title'] = $this->parse_one_line("title" , $content);
  56. $array['method'] = $this->parse_one_line("method" , $content);
  57. $array['description'] = $this->parse_one_line("description" , $content);
  58. $array['url'] = $this->parse_one_line("url" , $content);
  59. //解析目录
  60. $array['cat_name']= $this->parse_one_line("catalog" , $content);
  61. //解析返回内容
  62. $return = $this->parse_one_line("return" , $content);
  63. $return = htmlspecialchars_decode($return);
  64. //判断是否是json数据
  65. if (!is_null(json_decode($return))) {
  66. //格式化下
  67. $return = $this->indent_json($return);
  68. }
  69. $array['return'] = $return ;
  70. //解析请求参数
  71. $array['param'] = $this->parse_muti_line('param' , $content);
  72. //解析请求header
  73. $array['header'] = $this->parse_muti_line('header' , $content);
  74. //解析返回参数
  75. $array['return_param'] = $this->parse_muti_line('return_param' , $content);
  76. $array['remark'] = $this->parse_one_line("remark" , $content);
  77. $array['s_number'] = $this->parse_one_line("number" , $content);
  78. //如果请求参数是json,则生成请求示例
  79. $json_param = $this->parse_one_line("json_param" , $content);
  80. $json_param = htmlspecialchars_decode($json_param);
  81. //判断是否是json数据
  82. if (!is_null(json_decode($json_param))) {
  83. //格式化下
  84. $json_param = $this->indent_json($json_param);
  85. }
  86. $array['json_param'] = $json_param ;
  87. return $array ;
  88. }
  89. //解析单行标签,如method、url
  90. private function parse_one_line($tag , $content){
  91. $p = '/@'.$tag.'.+/' ;
  92. preg_match($p, $content , $matches) ;
  93. //var_dump($p);
  94. //var_dump($matches);
  95. if ($matches && $matches[0]) {
  96. return trim(str_replace('@'.$tag, '', $matches[0]) );
  97. }
  98. return false;
  99. }
  100. //解析多行标签,如param
  101. private function parse_muti_line($tag , $content){
  102. $return =array() ;
  103. $array1 = explode("@", $content);
  104. foreach ($array1 as $key => $value) {
  105. $array2 = preg_split("/[\s]+/", trim($value));
  106. if (!empty($array2[0]) && $array2[0] == $tag) {
  107. unset($array2[0]);
  108. $return[] = array_values($array2);
  109. }
  110. }
  111. return $return;
  112. }
  113. /**
  114. * Indents a flat JSON string to make it more human-readable.
  115. *
  116. * @param string $json The original JSON string to process.
  117. *
  118. * @return string Indented version of the original JSON string.
  119. */
  120. private function indent_json($json) {
  121. $result = '';
  122. $pos = 0;
  123. $strLen = strlen($json);
  124. $indentStr = ' ';
  125. $newLine = "\n";
  126. $prevChar = '';
  127. $outOfQuotes = true;
  128. for ($i=0; $i<=$strLen; $i++) {
  129. // Grab the next character in the string.
  130. $char = substr($json, $i, 1);
  131. // Are we inside a quoted string?
  132. if ($char == '"' && $prevChar != '\\') {
  133. $outOfQuotes = !$outOfQuotes;
  134. // If this character is the end of an element,
  135. // output a new line and indent the next line.
  136. } else if(($char == '}' || $char == ']') && $outOfQuotes) {
  137. $result .= $newLine;
  138. $pos --;
  139. for ($j=0; $j<$pos; $j++) {
  140. $result .= $indentStr;
  141. }
  142. }
  143. // Add the character to the result string.
  144. $result .= $char;
  145. // If the last character was the beginning of an element,
  146. // output a new line and indent the next line.
  147. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  148. $result .= $newLine;
  149. if ($char == '{' || $char == '[') {
  150. $pos ++;
  151. }
  152. for ($j = 0; $j < $pos; $j++) {
  153. $result .= $indentStr;
  154. }
  155. }
  156. $prevChar = $char;
  157. }
  158. return $result;
  159. }
  160. //生成markdown文档内容
  161. private function toMarkdown($array){
  162. $content = '
  163. **简要描述:**
  164. - '.$array['description'].'
  165. **请求URL:**
  166. - ` '.$array['url'].' `
  167. **请求方式:**
  168. - '.$array['method'].' ';
  169. if ($array['header']) {
  170. $content .='
  171. **Header:**
  172. |Header名|是否必选|类型|说明|
  173. |:---- |:---|:----- |----- |'."\n";
  174. foreach ($array['header'] as $key => $value) {
  175. $content .= '|'.$value[0].' |'.$value[1].' |'.$value[2].' |'.$value[3].' |'."\n";
  176. }
  177. }
  178. if ($array['json_param']) {
  179. $content .= '
  180. **请求参数示例**
  181. ```
  182. '.$array['json_param'].'
  183. ```
  184. ';
  185. }
  186. if ($array['param']) {
  187. $content .='
  188. **参数:**
  189. |参数名|是否必选|类型|说明|
  190. |:---- |:---|:----- |----- |'."\n";
  191. foreach ($array['param'] as $key => $value) {
  192. $content .= '|'.$value[0].' |'.$value[1].' |'.$value[2].' |'.$value[3].' |'."\n";
  193. }
  194. }
  195. $content .= '
  196. **返回示例**
  197. ```
  198. '.$array['return'].'
  199. ```
  200. **返回参数说明**
  201. |参数名|类型|说明|
  202. |:----- |:-----|----- |'."\n";
  203. if ($array['return_param']) {
  204. foreach ($array['return_param'] as $key => $value) {
  205. $content .= '|'.$value[0].' |'.$value[1].' |'.$value[2]."\n";
  206. }
  207. }
  208. $content .= '
  209. **备注**
  210. - '.$array['remark'].'
  211. ';
  212. return $content;
  213. }
  214. }