123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- <?php
- namespace app\doc;
- class Doc
- {
- protected $ext = ".php";
- private $FolderDescFile = "AFolderDesc.php";//文件夹说明文件
- protected $config = [];
- /**
- * 架构方法 设置参数
- *
- * @access public
- *
- * @param array $config 配置参数
- */
- public function __construct($config = [])
- {
- $this->config = array_merge($this->config, $config);
- }
- /**
- * 获取配置
- * @access public
- * @param string $name 配置名称
- * @return mixed 配置值
- */
- public function __get($name = null)
- {
- if ($name) {
- return $this->config[$name];
- } else {
- return $this->config;
- }
- }
- /**
- * 设置配置
- * @access public
- * @param string $name 配置名称
- * @param string $value 配置值
- * @return void
- */
- public function __set($name, $value)
- {
- if (isset($this->config[$name])) {
- $this->config[$name] = $value;
- }
- }
- /**
- * 检查配置
- * @access public
- * @param string $name 配置名称
- * @return bool
- */
- public function __isset($name)
- {
- return isset($this->config[$name]);
- }
- public function getList($file,$type)
- {
- if($type=='folder'){
- return $this->getFileList($file);
- }elseif($type=='file'){
- return $this->getActionList($file);
- }
- }
- /**
- * @title: 获取文件下的公共方法
- * @desc:
- * @param {*} $file
- * @return {*}
- * @Author: Rock
- * @Date: 2021-07-13 23:42:17
- * @LastEditTime: Do not edit
- */
- public function getActionList($file)
- {
- $class = str_replace(root_path(),'',$file);
- $class = str_replace('/','\\',$class);
- return $this->getMethod($class);
- }
- /**
- * @title: 获取目录下的文件夹和文件
- * @desc:
- * @param {*} $path
- * @return {*}
- * @Author: Rock
- * @Date: 2021-07-13 20:49:52
- * @LastEditTime: Do not edit
- */
- public function getFileList($path)
- {
- $list = [];
- $files = $this->getChildFile($path);
- foreach($files as $value){
- $file = $path.DS.$value;
- if(is_file($file)){
- $classpath = $path.DS.basename($value,$this->ext);
- $item = $this->parseCls($classpath);
- $item['type'] = 'file';
- //检测是否有公共的方法
- $children = $this->getMethod($item['class']);
- if(count($children)>0){
- $item['hasChild'] = true;
- $list[] = $item;
- }
- }elseif(is_dir($file)){
- $classFile = $file.DS.$this->FolderDescFile;
- if(!is_file($classFile)){
- $this->createDescFile($classFile,$value);
- }
- $classpath = $path.DS.$value.DS.basename($this->FolderDescFile,$this->ext);
- $item = $this->parseCls($classpath);
- $item['type'] = 'folder';
- $item['path'] = $file;
- //检测是否有子文件
- $children = $this->getChildFile($file);
- if(count($children)>0){
- $item['hasChild'] = true;
- $list[] = $item;
- }
- }
- }
- return $list;
- }
- /**
- * @title: 创建文件夹说明文件
- * @desc:
- * @param {*} $file
- * @param {*} $name
- * @return {*}
- * @Author: Rock
- * @Date: 2021-07-13 21:55:55
- * @LastEditTime: Do not edit
- */
- private function createDescFile($file,$name)
- {
- $dir = dirname($file);
- chmod($dir,0777);
- $classname = basename($this->FolderDescFile,$this->ext);
- $namespace = str_replace(root_path(),"",$dir);
- $namespace = str_replace('/','\\',$namespace);
- $content = "<?php\r\nnamespace $namespace;\r\n /**\r\n * @title: $name\r\n */ \r\nclass $classname\r\n{\r\n\r\n}";
- $f = fopen($file,"w");
- file_put_contents($file,$content);
- }
- /**
- * @title: 解析类并返回类的信息
- * @desc:
- * @param {*}
- * @return {*}
- * @Author: Rock
- * @Date: 2021-07-13 20:54:25
- * @LastEditTime: Do not edit
- */
- private function parseCls($classpath)
- {
- $name = basename($classpath);
- $class = str_replace(root_path(),'',$classpath);
- $class = str_replace('/','\\',$class);
- $info = [];
- if (class_exists($class)) {
- $reflection = new \ReflectionClass($class);
- $doc_str = $reflection->getDocComment();
- $doc = new Parser();
- $class_doc = $doc->parse_class($doc_str);
- $info['class'] = $class;
- $info['name'] = $name;
- $info['path'] = $classpath;
- $info['title'] = isset($class_doc['title'])?$class_doc['title']:$name;
- $info['icon'] = isset($class_doc['icon'])?$class_doc['icon']:'';
- $info['desc'] = isset($class_doc['desc'])?$class_doc['desc']:'';
- $info['author'] = isset($class_doc['Author'])?$class_doc['Author']:'无名';
- $info['LastEditTime'] = isset($class_doc['LastEditTime'])?$class_doc['LastEditTime']:'';
- }else{
- $info['class'] = $class;
- $info['name'] = $name;
- $info['path'] = $classpath;
- $info['title'] = $name;
- $info['icon'] = '';
- $info['desc'] = '';
- $info['author'] = '无名';
- $info['LastEditTime'] = '';
- }
- $info['is_leaf'] = false;
- return $info;
- }
- /**
- * @title: 获取类里面的公共方法
- * @desc:
- * @param {*} $class
- * @return {*}
- * @Author: Rock
- * @Date: 2021-07-14 09:27:20
- * @LastEditTime: Do not edit
- */
- private function getMethod($class)
- {
- $list = [];
- if(class_exists($class)){
- $doc = new Parser();
- $reflection = new \ReflectionClass($class);
- //获取公共方法
- $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
- //过滤不需要解析的方法以及非当前类的方法(父级方法)
- $filter_method = array_merge(['__construct','initialize','_initialize','__destruct'], $this->config['filter_method']);
- foreach ($methods as $key => $action) {
- if (!in_array($action->name, $filter_method) && $action->class === $class) {
- if ($doc->parse_action($action)){
- $temp = $doc->parse_action($action);
- if(!empty($temp['title'])){
- $info = $doc->parse_action($action);
- $item = [
- 'name' => $action->name,
- 'class' => $class,
- 'path' => root_path().$class.'\\'.$action->name,
- 'title' => $info['title'],
- 'icon' => isset($info['icon'])?$info['icon']:'',
- 'desc' => isset($info['desc'])?$info['desc']:'',
- 'author' => isset($class_doc['Author'])?$class_doc['Author']:'无名',
- 'LastEditTime' => isset($info['LastEditTime'])?$info['LastEditTime']:'',
- 'type' => 'action',
- 'is_leaf' => true,
- ];
- $list[] = $item;
- }
- }
- }
- }
- }
- return $list;
- }
- /**
- * @title: 获取目录下的文件及文件夹
- * @desc:
- * @param {*} $path
- * @return {*}
- * @Author: Rock
- * @Date: 2021-07-14 09:27:49
- * @LastEditTime: Do not edit
- */
- private function getChildFile($path)
- {
- $list = [];
- $files = scandir($path);
- foreach($files as $value){
- if($value!='.' && $value!='..'){
- $list[] = $value;
- }
- }
- return $list;
- }
- /**
- * 获取接口详情
- * @param string $class
- * @param string $action
- *
- * @return array|bool
- */
- public function get_api_detail($class='',$action='')
- {
- $method = (new \ReflectionClass($class))->getMethod($action);
- return (new Parser())->parse_action($method);
- }
- }
|