FromCommentsController.class.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. $p = "|/\*\*([\s\S]*)\*/|U";
  21. preg_match_all($p, $content , $matches) ;
  22. if ($matches && $matches[0]) {
  23. foreach ($matches[0] as $key => $value) {
  24. if (strstr($value,"@title") && strstr($value,"showdoc")) {
  25. $ret = $this->generate_one($item_id , $value);
  26. }
  27. }
  28. }
  29. if ($ret) {
  30. echo "\n 成功 \n\n ";
  31. }else{
  32. echo "失败";
  33. }
  34. }
  35. private function generate_one($item_id,$content){
  36. $array = $this->parse_content($content);
  37. $page_content = $this->toMarkdown($array);
  38. $page_title = $array['title'];
  39. $page_content = $page_content;
  40. $cat_name = $array['cat_name'];
  41. $s_number = $array['s_number'] ? $array['s_number'] : 99;
  42. $page_id = D("Page")->update_by_content($item_id,$page_title,$page_content,$cat_name,$s_number);
  43. if ($page_id) {
  44. $ret = D("Page")->where(" page_id = '$page_id' ")->find();
  45. return $ret;
  46. }else{
  47. return false;
  48. }
  49. }
  50. //解析content,返回数组
  51. private function parse_content($content){
  52. $array = array() ;
  53. //解析标题
  54. $array['title'] = $this->parse_one_line("title" , $content);
  55. $array['method'] = $this->parse_one_line("method" , $content);
  56. $array['description'] = $this->parse_one_line("description" , $content);
  57. $array['url'] = $this->parse_one_line("url" , $content);
  58. //解析目录
  59. $array['cat_name']= $this->parse_one_line("catalog" , $content);
  60. //解析返回内容
  61. $return = $this->parse_one_line("return" , $content);
  62. $return = htmlspecialchars_decode($return);
  63. //判断是否是json数据
  64. if (!is_null(json_decode($return))) {
  65. //格式化下
  66. $return = $this->indent_json($return);
  67. }
  68. $array['return'] = $return ;
  69. //解析请求参数
  70. $array['param'] = $this->parse_muti_line('param' , $content);
  71. //解析返回参数
  72. $array['return_param'] = $this->parse_muti_line('return_param' , $content);
  73. $array['remark'] = $this->parse_one_line("remark" , $content);
  74. $array['s_number'] = $this->parse_one_line("number" , $content);
  75. return $array ;
  76. }
  77. //解析单行标签,如method、url
  78. private function parse_one_line($tag , $content){
  79. $p = '/@'.$tag.'.+/' ;
  80. preg_match($p, $content , $matches) ;
  81. //var_dump($p);
  82. //var_dump($matches);
  83. if ($matches && $matches[0]) {
  84. return trim(str_replace('@'.$tag, '', $matches[0]) );
  85. }
  86. return false;
  87. }
  88. //解析多行标签,如param
  89. private function parse_muti_line($tag , $content){
  90. $return =array() ;
  91. $array1 = explode("@", $content);
  92. foreach ($array1 as $key => $value) {
  93. $array2 = preg_split("/[\s]+/", trim($value));
  94. if (!empty($array2[0]) && $array2[0] == $tag) {
  95. unset($array2[0]);
  96. $return[] = array_values($array2);
  97. }
  98. }
  99. return $return;
  100. }
  101. /**
  102. * Indents a flat JSON string to make it more human-readable.
  103. *
  104. * @param string $json The original JSON string to process.
  105. *
  106. * @return string Indented version of the original JSON string.
  107. */
  108. private function indent_json($json) {
  109. $result = '';
  110. $pos = 0;
  111. $strLen = strlen($json);
  112. $indentStr = ' ';
  113. $newLine = "\n";
  114. $prevChar = '';
  115. $outOfQuotes = true;
  116. for ($i=0; $i<=$strLen; $i++) {
  117. // Grab the next character in the string.
  118. $char = substr($json, $i, 1);
  119. // Are we inside a quoted string?
  120. if ($char == '"' && $prevChar != '\\') {
  121. $outOfQuotes = !$outOfQuotes;
  122. // If this character is the end of an element,
  123. // output a new line and indent the next line.
  124. } else if(($char == '}' || $char == ']') && $outOfQuotes) {
  125. $result .= $newLine;
  126. $pos --;
  127. for ($j=0; $j<$pos; $j++) {
  128. $result .= $indentStr;
  129. }
  130. }
  131. // Add the character to the result string.
  132. $result .= $char;
  133. // If the last character was the beginning of an element,
  134. // output a new line and indent the next line.
  135. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  136. $result .= $newLine;
  137. if ($char == '{' || $char == '[') {
  138. $pos ++;
  139. }
  140. for ($j = 0; $j < $pos; $j++) {
  141. $result .= $indentStr;
  142. }
  143. }
  144. $prevChar = $char;
  145. }
  146. return $result;
  147. }
  148. //生成markdown文档内容
  149. private function toMarkdown($array){
  150. $content = '
  151. **简要描述:**
  152. - '.$array['description'].'
  153. **请求URL:**
  154. - ` '.$array['url'].' `
  155. **请求方式:**
  156. - '.$array['method'].'
  157. **参数:**
  158. |参数名|是否必选|类型|说明|
  159. |:---- |:---|:----- |----- |'."\n";
  160. if ($array['param']) {
  161. foreach ($array['param'] as $key => $value) {
  162. $content .= '|'.$value[0].' |'.$value[1].' |'.$value[2].' |'.$value[3].' |'."\n";
  163. }
  164. }
  165. $content .= '
  166. **返回示例**
  167. ```
  168. '.$array['return'].'
  169. ```
  170. **返回参数说明**
  171. |参数名|类型|说明|
  172. |:----- |:-----|----- |'."\n";
  173. if ($array['return_param']) {
  174. foreach ($array['return_param'] as $key => $value) {
  175. $content .= '|'.$value[0].' |'.$value[1].' |'.$value[2]."\n";
  176. }
  177. }
  178. $content .= '
  179. **备注**
  180. - '.$array['remark'].'
  181. ';
  182. return $content;
  183. }
  184. }