ImportSwaggerController.class.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class ImportSwaggerController extends BaseController {
  5. public $json_array = array();
  6. public $url_pre = '';
  7. public function import(){
  8. $login_user = $this->checkLogin();
  9. $json = file_get_contents($_FILES["file"]["tmp_name"]) ;
  10. //$json = file_get_contents("../Public/swagger.json") ;//test
  11. $json_array = json_decode($json ,1 );
  12. unset($json);
  13. if ($json_array['info']) {
  14. $this->json_array = $json_array ;
  15. $this->url_pre = $json_array['schemes'][0]."://".$json_array['host'].$json_array['basePath'] ;
  16. $this->_fromSwaggerV2($json_array);
  17. return ;
  18. }
  19. $this->sendError(10303);
  20. }
  21. private function _fromSwaggerV2($json_array){
  22. $login_user = $this->checkLogin();
  23. // TODO 这里需要检查下合法性。比如关键字检查/黑名单检查/字符串过滤
  24. $from = I("from") ? I("from") : '' ;
  25. $item_array = array(
  26. "item_name" => $json_array['info']['title'] ? $json_array['info']['title'] : 'from swagger' ,
  27. "item_type" => ($from == 'runapi') ? '3': '1' ,
  28. "item_description" => $json_array['info']['description'] ? $json_array['info']['description'] :'',
  29. "password" => time().rand(),
  30. "members" => array(),
  31. "pages" =>array(
  32. "pages" => array(),
  33. "catalogs" => $this->_getAllTagsLogs($json_array)
  34. )
  35. ) ;
  36. $level = 2 ;
  37. // $item_array['pages']['catalogs'][0]['pages'] = $this->_getPageByPaths($json_array);
  38. $item_id = D("Item")->import( json_encode($item_array) , $login_user['uid'] );
  39. //echo D("Item")->export(196053901215026 );
  40. //echo json_encode($item_array);
  41. $this->sendResult(array('item_id' => $item_id));
  42. }
  43. private function _getAllTagsLogs($json_array) {
  44. $catalogsMap = array(
  45. "fromSwagger" => array("cat_name" =>'from swagger', "pages" =>array())
  46. );
  47. $paths = $json_array['paths'] ;
  48. foreach ($paths as $url => $value) {
  49. foreach ($value as $method => $value2) {
  50. $tags = isset($value2["tags"]) ? $value2["tags"] : array();
  51. if ($tags == array()){
  52. $page = $this->_requestToDoc($method, $url, $value2, $json_array);
  53. if($page['page_title']){
  54. $catalogsMap["fromSwagger"]["pages"][] = $page;
  55. }
  56. }else{
  57. foreach ($tags as $tag){
  58. if (!key_exists($tag, $catalogsMap)) {
  59. $page = $this->_requestToDoc($method, $url, $value2, $json_array);
  60. if ($page["page_title"] != "" && $page["page_content"] != ""){
  61. $catalogsMap[$tag] = array("cat_name" => $tag, "pages" => array($page));
  62. }
  63. }else{
  64. // 存在则page merge
  65. $page = $this->_requestToDoc($method, $url, $value2, $json_array);
  66. if ($page["page_title"] != "" && $page["page_content"] != ""){
  67. $catalogsMap[$tag]["pages"][] = $page;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. $catalogs = array();
  75. foreach ($catalogsMap as $key => $value){
  76. $catalogs[] = $value;
  77. }
  78. return $catalogs;
  79. }
  80. private function _getPageByPaths($json_array){
  81. $return = array() ;
  82. $paths = $json_array['paths'] ;
  83. foreach ($paths as $url => $value) {
  84. foreach ($value as $method => $value2) {
  85. $return[] = $this->_requestToDoc($method , $url , $value2 , $json_array);
  86. }
  87. }
  88. return $return ;
  89. }
  90. private function _requestToDoc($method , $url , $request , $json_array){
  91. $from = I("from") ? I("from") : '' ;
  92. $res = $this->_requestToApi($method , $url , $request , $json_array);
  93. if($from == 'runapi'){
  94. return $res ;
  95. }else{
  96. $res['page_content'] = D("Page")->runapiToMd($res['page_content']);
  97. return $res ;
  98. }
  99. }
  100. private function _requestToApi($method , $url , $request , $json_array){
  101. $return = array() ;
  102. $return['page_title'] = $request['summary'] ? $request['summary']: $request['operationId'] ;
  103. $return['s_number'] = 99 ;
  104. $return['page_comments'] = '' ;
  105. $content_array = array(
  106. "info"=>array(
  107. "from" => 'runapi' ,
  108. "type" => 'api' ,
  109. "title" => $request['summary'] ? $request['summary']: $request['operationId'] ,
  110. "description" => $request['description'] ,
  111. "method" => strtolower($method) ,
  112. "url" => $this->url_pre . $url ,
  113. "remark" => '' ,
  114. ),
  115. "request" =>array(
  116. "params"=> array(
  117. 'mode' => "formdata",
  118. 'json' => "",
  119. 'jsonDesc' => array(),
  120. 'urlencoded' => array(),
  121. 'formdata' => array(),
  122. ),
  123. "headers"=> array(),
  124. "cookies"=> array(),
  125. "auth"=> array(),
  126. ),
  127. "response" =>array(),
  128. "extend" =>array(),
  129. );
  130. if ($request['headerData']) {
  131. $tmp_array = array();
  132. foreach ($request['headerData'] as $key => $value) {
  133. $content_array['request']['headers'][] = array(
  134. "name" =>$value["key"],
  135. "type" =>'string',
  136. "value" =>$value["value"],
  137. "require" =>'1',
  138. "remark" =>'',
  139. );
  140. }
  141. }
  142. if ($request['parameters']) {
  143. foreach ($request['parameters'] as $key => $value) {
  144. // 如果in字段是body的话,应该就是参数为json的情况了
  145. if($value["in"] == 'body'){
  146. $ref_str = $value['schema']['$ref'] ;
  147. //如果含有引用标识,则获取引用
  148. if($ref_str){
  149. $ref_array = $this->_getDefinition($ref_str);
  150. }else{
  151. $ref_array = $value['schema'] ;
  152. }
  153. $json_array = $this->_definitionToJsonArray($ref_array);
  154. $json_str = $this->_jsonArrayToStr($json_array);
  155. $content_array['request']['params']['mode'] = 'json';
  156. $content_array['request']['params']['json'] = $json_str;
  157. $content_array['request']['params']['jsonDesc'] = $json_array;
  158. }else{
  159. $content_array['request']['params']['formdata'][] = array(
  160. "name" =>$value["name"],
  161. "type" =>'string',
  162. "value" =>$value["value"],
  163. "require" =>'1',
  164. "remark" =>$value["description"],
  165. );
  166. }
  167. }
  168. }
  169. //处理返回结果情况
  170. if($request['responses'] && $request['responses']['200']){
  171. $ref_str = $request['responses']['200']['schema']['$ref'] ;
  172. //如果含有引用标识,则获取引用
  173. if($ref_str){
  174. $ref_array = $this->_getDefinition($ref_str);
  175. }else{
  176. $ref_array = $request['responses']['200']['schema'] ;
  177. }
  178. $json_array = $this->_definitionToJsonArray($ref_array);
  179. $json_str = $this->_jsonArrayToStr($json_array);
  180. $content_array['response']['responseExample'] = $json_str;
  181. $content_array['response']['responseParamsDesc'] = $json_array;
  182. }
  183. $return['page_content'] = json_encode($content_array);
  184. return $return ;
  185. }
  186. // 获取引用,返回数组。
  187. //$ref_str 是swagger里引用的字符串,比如"#/definitions/Petoo"
  188. private function _getDefinition($ref_str){
  189. $json_array = $this->json_array ;
  190. $str_array = explode('#/definitions/',$ref_str);
  191. $path = $str_array1[1];
  192. $target_array = $json_array['definitions'][$str_array[1]] ;
  193. if($target_array){
  194. return $target_array ;
  195. }
  196. return false;
  197. }
  198. //把引用类型的数组转换成纯json数组
  199. private function _definitionToJsonArray($ref_array){
  200. $res = array() ;
  201. foreach ($ref_array['properties'] as $key => $value) {
  202. $res[] = array(
  203. "name" =>$key,
  204. "type" =>'string',
  205. "value" =>'',
  206. "require" =>'1',
  207. "remark" =>$value["title"],
  208. );
  209. }
  210. return $res ;
  211. }
  212. // 把json数组转成纯json字符串
  213. private function _jsonArrayToStr($json_array){
  214. $res_array = array() ;
  215. foreach ($json_array as $key => $value) {
  216. $res_array[$value['name']] = '' ;
  217. }
  218. return json_encode($res_array) ;
  219. }
  220. }