ImportSwaggerController.class.php 9.4 KB

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