FromCommentsController.class.php 6.6 KB

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