有啥方案可以获取 TP5 项目中所有的自定义函数?

我们做TP5项目一般不试使用强制路由,这样接手别人项目时,太难了。。。我想获取所有他写的函数 整理一下 , 使用强制路由。。

@李山河
你看我吊吗啊
cnguu
最佳答案

@JeffLi

  • 获取所有类:get_declared_classes()
  • 获取类中所有方法:get_class_methods()
4年前 评论
讨论数量: 9
cnguu

你可以先获取所有定义的类,再获取类中的方法

4年前 评论
itxq

自定义函数,应该是统一写在某个或某几个文件中的吧。直接查看那几个文件不就可以了吗?

4年前 评论
你看我吊吗啊

@itxq 我是想提高效率

4年前 评论
你看我吊吗啊

@cnguu 怎么获取 、、我没搜到

4年前 评论
cnguu

@JeffLi

  • 获取所有类:get_declared_classes()
  • 获取类中所有方法:get_class_methods()
4年前 评论
你看我吊吗啊

@cnguu get_declared_classes() 只能获取当前class下引用到的类 哈哈 ,咋整 ,比如我请求的是名为user的controller ,那么获取的就是这个控制器用到的类 ,如图

file

4年前 评论
cnguu

@JeffLi 你想一下,为什么编辑器会有代码提示,还可以直接跳到对应的类中?原理是什么?

你还可以递归所有文件去获取,不过我还是推荐把整个项目看一遍

4年前 评论
你看我吊吗啊

@cnguu 看来只有这个方案了

4年前 评论

获取模块中所有控制器:

private function getControllers($module){
        if(empty($module)) {
            return null;
        }
        $modulePath = app()->getAppPath() . $module . '/controller/';
        if(!is_dir($modulePath)) {
            return null;
        }
        $modulePath .= '*.php';
        $matchFiles = glob($modulePath);

        $files = [];
        foreach ($matchFiles as $file) {
            if(is_dir($file)) {
                continue;
            } else {
                $files[] = basename($file, '.php');
            }
        }
        return $files;
    }

获取控制器所有操作:

private function getActions($module, $controller){
        if(empty($controller)) {
            return null;
        }
        $customerFunctions = [];
        $file = app()->getAppPath() . $module . '/controller/' . $controller . '.php';
        if(file_exists($file)) {
            $content = file_get_contents($file);
            preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
            $functions = $matches[1];

            $excludeFunc = ['__initialize', '__construct'];

            foreach ($functions as $func) {
                $func = trim($func);
                if(!in_array($func, $excludeFunc)) {
                    $customerFunctions[] = $func;
                }
            }
            return $customerFunctions;
        } else {
            Log::record('is not file: ' . $file);
            return false;
        }
    }

把所有需要的模块列出来,循环获取控制器,每个控制器循环获取里面的操作,再拼接成路由

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!