FromCommentsController.class.php 7.2 KB

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