FromCommentsController.class.php 6.9 KB

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