ImportSwaggerController.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ImportSwaggerController extends BaseController {
  5. public function import(){
  6. $login_user = $this->checkLogin();
  7. $json = file_get_contents($_FILES["file"]["tmp_name"]) ;
  8. //$json = file_get_contents("../Public/swagger.json") ;//test
  9. $json_array = json_decode($json ,1 );
  10. unset($json);
  11. if ($json_array['info']) {
  12. $this->_fromSwaggerV2($json_array);
  13. return ;
  14. }
  15. $this->sendError(10303);
  16. }
  17. private function _fromSwaggerV2($json_array){
  18. $login_user = $this->checkLogin();
  19. // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
  20. $item_array = array(
  21. "item_name" => $json_array['info']['title'] ? $json_array['info']['title'] : 'from swagger' ,
  22. "item_type" => '1' ,
  23. "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
  24. "password" => time().rand(),
  25. "members" => array(),
  26. "pages" =>array(
  27. "pages" => array(),
  28. "catalogs" => array()
  29. )
  30. ) ;
  31. $level = 2 ;
  32. $item_array['pages']['pages'] = $this->_getPageByPaths($json_array['paths'] );
  33. D("Item")->import( json_encode($item_array) , $login_user['uid'] );
  34. //echo D("Item")->export(196053901215026 );
  35. //echo json_encode($item_array);
  36. $this->sendResult(array());
  37. }
  38. private function _getPageByPaths($paths){
  39. $return = array() ;
  40. foreach ($paths as $url => $value) {
  41. foreach ($value as $method => $value2) {
  42. $return[] = $this->_requestToDoc($method , $url , $value2);
  43. }
  44. }
  45. return $return ;
  46. }
  47. private function _requestToDoc($method , $url , $request){
  48. $return = array() ;
  49. $return['page_title'] = $request['summary'] ;
  50. $return['s_number'] = 99 ;
  51. $return['page_comments'] = '' ;
  52. $content = '
  53. **简要描述:**
  54. - '.$request['description'].'
  55. **请求URL:**
  56. - ` '.$url.' `
  57. **请求方式:**
  58. - '.$method.' ';
  59. if ($request['header']) {
  60. $content .='
  61. **Header:**
  62. |Header名|是否必选|类型|说明|
  63. |:---- |:---|:----- |----- |'."\n";
  64. foreach ($request['headerData'] as $key => $value) {
  65. $content .= '|'.$value["key"].' | | text | '.$value["value"].' |'."\n";
  66. }
  67. }
  68. if ($request['rawModeData']) {
  69. $content .= '
  70. **请求参数示例**
  71. ```
  72. '.$request['rawModeData'].'
  73. ```
  74. ';
  75. }
  76. if ($request['parameters']) {
  77. $content .='
  78. **参数:**
  79. |参数名|是否必选|类型|说明|
  80. |:---- |:---|:----- |----- |'."\n";
  81. foreach ($request['parameters'] as $key => $value) {
  82. $content .= '|'.$value["name"].' | '.($value["required"] ? '是' : '否' ).' |'.$value["type"].' | '.$value["description"].' |'."\n";
  83. }
  84. }
  85. if ($request['responses']['200']) {
  86. $content .= '
  87. **返回示例**
  88. ```
  89. '.$this->_indent_json(json_encode($request['responses']['200'])).'
  90. ```
  91. ';
  92. }
  93. $return['page_content'] = $content ;
  94. return $return ;
  95. }
  96. /**
  97. * Indents a flat JSON string to make it more human-readable.
  98. *
  99. * @param string $json The original JSON string to process.
  100. *
  101. * @return string Indented version of the original JSON string.
  102. */
  103. private function _indent_json($json) {
  104. $result = '';
  105. $pos = 0;
  106. $strLen = strlen($json);
  107. $indentStr = ' ';
  108. $newLine = "\n";
  109. $prevChar = '';
  110. $outOfQuotes = true;
  111. for ($i=0; $i<=$strLen; $i++) {
  112. // Grab the next character in the string.
  113. $char = substr($json, $i, 1);
  114. // Are we inside a quoted string?
  115. if ($char == '"' && $prevChar != '\\') {
  116. $outOfQuotes = !$outOfQuotes;
  117. // If this character is the end of an element,
  118. // output a new line and indent the next line.
  119. } else if(($char == '}' || $char == ']') && $outOfQuotes) {
  120. $result .= $newLine;
  121. $pos --;
  122. for ($j=0; $j<$pos; $j++) {
  123. $result .= $indentStr;
  124. }
  125. }
  126. // Add the character to the result string.
  127. $result .= $char;
  128. // If the last character was the beginning of an element,
  129. // output a new line and indent the next line.
  130. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  131. $result .= $newLine;
  132. if ($char == '{' || $char == '[') {
  133. $pos ++;
  134. }
  135. for ($j = 0; $j < $pos; $j++) {
  136. $result .= $indentStr;
  137. }
  138. }
  139. $prevChar = $char;
  140. }
  141. return $result;
  142. }
  143. }