Doc.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace app\doc;
  3. class Doc
  4. {
  5. protected $ext = ".php";
  6. private $FolderDescFile = "AFolderDesc.php";//文件夹说明文件
  7. protected $config = [];
  8. /**
  9. * 架构方法 设置参数
  10. *
  11. * @access public
  12. *
  13. * @param array $config 配置参数
  14. */
  15. public function __construct($config = [])
  16. {
  17. $this->config = array_merge($this->config, $config);
  18. }
  19. /**
  20. * 获取配置
  21. * @access public
  22. * @param string $name 配置名称
  23. * @return mixed 配置值
  24. */
  25. public function __get($name = null)
  26. {
  27. if ($name) {
  28. return $this->config[$name];
  29. } else {
  30. return $this->config;
  31. }
  32. }
  33. /**
  34. * 设置配置
  35. * @access public
  36. * @param string $name 配置名称
  37. * @param string $value 配置值
  38. * @return void
  39. */
  40. public function __set($name, $value)
  41. {
  42. if (isset($this->config[$name])) {
  43. $this->config[$name] = $value;
  44. }
  45. }
  46. /**
  47. * 检查配置
  48. * @access public
  49. * @param string $name 配置名称
  50. * @return bool
  51. */
  52. public function __isset($name)
  53. {
  54. return isset($this->config[$name]);
  55. }
  56. public function getList($file,$type)
  57. {
  58. if($type=='folder'){
  59. return $this->getFileList($file);
  60. }elseif($type=='file'){
  61. return $this->getActionList($file);
  62. }
  63. }
  64. /**
  65. * @title: 获取文件下的公共方法
  66. * @desc:
  67. * @param {*} $file
  68. * @return {*}
  69. * @Author: Rock
  70. * @Date: 2021-07-13 23:42:17
  71. * @LastEditTime: Do not edit
  72. */
  73. public function getActionList($file)
  74. {
  75. $class = str_replace(root_path(),'',$file);
  76. $class = str_replace('/','\\',$class);
  77. return $this->getMethod($class);
  78. }
  79. /**
  80. * @title: 获取目录下的文件夹和文件
  81. * @desc:
  82. * @param {*} $path
  83. * @return {*}
  84. * @Author: Rock
  85. * @Date: 2021-07-13 20:49:52
  86. * @LastEditTime: Do not edit
  87. */
  88. public function getFileList($path)
  89. {
  90. $list = [];
  91. $files = $this->getChildFile($path);
  92. foreach($files as $value){
  93. $file = $path.DS.$value;
  94. if(is_file($file)){
  95. $classpath = $path.DS.basename($value,$this->ext);
  96. $item = $this->parseCls($classpath);
  97. $item['type'] = 'file';
  98. //检测是否有公共的方法
  99. $children = $this->getMethod($item['class']);
  100. if(count($children)>0){
  101. $item['hasChild'] = true;
  102. $list[] = $item;
  103. }
  104. }elseif(is_dir($file)){
  105. $classFile = $file.DS.$this->FolderDescFile;
  106. if(!is_file($classFile)){
  107. $this->createDescFile($classFile,$value);
  108. }
  109. $classpath = $path.DS.$value.DS.basename($this->FolderDescFile,$this->ext);
  110. $item = $this->parseCls($classpath);
  111. $item['type'] = 'folder';
  112. $item['path'] = $file;
  113. //检测是否有子文件
  114. $children = $this->getChildFile($file);
  115. if(count($children)>0){
  116. $item['hasChild'] = true;
  117. $list[] = $item;
  118. }
  119. }
  120. }
  121. return $list;
  122. }
  123. /**
  124. * @title: 创建文件夹说明文件
  125. * @desc:
  126. * @param {*} $file
  127. * @param {*} $name
  128. * @return {*}
  129. * @Author: Rock
  130. * @Date: 2021-07-13 21:55:55
  131. * @LastEditTime: Do not edit
  132. */
  133. private function createDescFile($file,$name)
  134. {
  135. $dir = dirname($file);
  136. chmod($dir,0777);
  137. $classname = basename($this->FolderDescFile,$this->ext);
  138. $namespace = str_replace(root_path(),"",$dir);
  139. $namespace = str_replace('/','\\',$namespace);
  140. $content = "<?php\r\nnamespace $namespace;\r\n /**\r\n * @title: $name\r\n */ \r\nclass $classname\r\n{\r\n\r\n}";
  141. $f = fopen($file,"w");
  142. file_put_contents($file,$content);
  143. }
  144. /**
  145. * @title: 解析类并返回类的信息
  146. * @desc:
  147. * @param {*}
  148. * @return {*}
  149. * @Author: Rock
  150. * @Date: 2021-07-13 20:54:25
  151. * @LastEditTime: Do not edit
  152. */
  153. private function parseCls($classpath)
  154. {
  155. $name = basename($classpath);
  156. $class = str_replace(root_path(),'',$classpath);
  157. $class = str_replace('/','\\',$class);
  158. $info = [];
  159. if (class_exists($class)) {
  160. $reflection = new \ReflectionClass($class);
  161. $doc_str = $reflection->getDocComment();
  162. $doc = new Parser();
  163. $class_doc = $doc->parse_class($doc_str);
  164. $info['class'] = $class;
  165. $info['name'] = $name;
  166. $info['path'] = $classpath;
  167. $info['title'] = isset($class_doc['title'])?$class_doc['title']:$name;
  168. $info['icon'] = isset($class_doc['icon'])?$class_doc['icon']:'';
  169. $info['desc'] = isset($class_doc['desc'])?$class_doc['desc']:'';
  170. $info['author'] = isset($class_doc['Author'])?$class_doc['Author']:'无名';
  171. $info['LastEditTime'] = isset($class_doc['LastEditTime'])?$class_doc['LastEditTime']:'';
  172. }else{
  173. $info['class'] = $class;
  174. $info['name'] = $name;
  175. $info['path'] = $classpath;
  176. $info['title'] = $name;
  177. $info['icon'] = '';
  178. $info['desc'] = '';
  179. $info['author'] = '无名';
  180. $info['LastEditTime'] = '';
  181. }
  182. $info['is_leaf'] = false;
  183. return $info;
  184. }
  185. /**
  186. * @title: 获取类里面的公共方法
  187. * @desc:
  188. * @param {*} $class
  189. * @return {*}
  190. * @Author: Rock
  191. * @Date: 2021-07-14 09:27:20
  192. * @LastEditTime: Do not edit
  193. */
  194. private function getMethod($class)
  195. {
  196. $list = [];
  197. if(class_exists($class)){
  198. $doc = new Parser();
  199. $reflection = new \ReflectionClass($class);
  200. //获取公共方法
  201. $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
  202. //过滤不需要解析的方法以及非当前类的方法(父级方法)
  203. $filter_method = array_merge(['__construct','initialize','_initialize','__destruct'], $this->config['filter_method']);
  204. foreach ($methods as $key => $action) {
  205. if (!in_array($action->name, $filter_method) && $action->class === $class) {
  206. if ($doc->parse_action($action)){
  207. $temp = $doc->parse_action($action);
  208. if(!empty($temp['title'])){
  209. $info = $doc->parse_action($action);
  210. $item = [
  211. 'name' => $action->name,
  212. 'class' => $class,
  213. 'path' => root_path().$class.'\\'.$action->name,
  214. 'title' => $info['title'],
  215. 'icon' => isset($info['icon'])?$info['icon']:'',
  216. 'desc' => isset($info['desc'])?$info['desc']:'',
  217. 'author' => isset($class_doc['Author'])?$class_doc['Author']:'无名',
  218. 'LastEditTime' => isset($info['LastEditTime'])?$info['LastEditTime']:'',
  219. 'type' => 'action',
  220. 'is_leaf' => true,
  221. ];
  222. $list[] = $item;
  223. }
  224. }
  225. }
  226. }
  227. }
  228. return $list;
  229. }
  230. /**
  231. * @title: 获取目录下的文件及文件夹
  232. * @desc:
  233. * @param {*} $path
  234. * @return {*}
  235. * @Author: Rock
  236. * @Date: 2021-07-14 09:27:49
  237. * @LastEditTime: Do not edit
  238. */
  239. private function getChildFile($path)
  240. {
  241. $list = [];
  242. $files = scandir($path);
  243. foreach($files as $value){
  244. if($value!='.' && $value!='..'){
  245. $list[] = $value;
  246. }
  247. }
  248. return $list;
  249. }
  250. /**
  251. * 获取接口详情
  252. * @param string $class
  253. * @param string $action
  254. *
  255. * @return array|bool
  256. */
  257. public function get_api_detail($class='',$action='')
  258. {
  259. $method = (new \ReflectionClass($class))->getMethod($action);
  260. return (new Parser())->parse_action($method);
  261. }
  262. }