dd

自动获取所有模块的函数,用于RBAC节点和菜单数据

jerry thinkphp 2015年11月18日 收藏
系统开发好以后,你知道系统里有多少个Module不?知道有多少个Action不?

你的RBAC是不是需要用这些数据?你的菜单是不是需要用这些数据?

亲,厌倦了手动录入吗?你找我算是对人了。

代码来源:开源ThinkPHP代码生成器
应用主页:http://rockefys.github.io/commlib
//生成模块结构信息 app/分组/模块/方法
    public function fetch_module(){
        $M = M('Module');
        $M->query("truncate table module");
        $app = $this->getAppName();
        $groups = $this->getGroup();
        $n=0;
        foreach ($groups as $group) {
            $modules = $this->getModule($group);
            foreach ($modules as $module) {
                $module_name=$app.'://'.$group.'/'.$module;
                $functions = $this->getFunction($module_name);
                foreach ($functions as $function) {
                    $data[$n]['app'] = $app;
                    $data[$n]['group'] = $group;
                    $data[$n]['module'] = $module;
                    $data[$n]['function'] = $function;
                    ++$n;                                }
            }
        }
        $M->addAll($data);
        $this->success('所有分组/模块/方法已成功读取到module表中.');
    }
    protected function getAppName(){
        return APP_NAME;
    }

    protected function getGroup(){
        $result = explode(',',C('APP_GROUP_LIST'));
        return $result;
    }

    protected function getModule($group){
        if(empty($group))return null;
        $group_path=LIB_PATH.'Action/'.$group;
        if(!is_dir($group_path))return null;
        $group_path.='/*.class.php';
        $ary_files = glob($group_path);
        foreach ($ary_files as $file) {
            if (is_dir($file)) {
                continue;
            }else {
                    $files[] = basename($file,'Action.class.php');
            }
        }
        return $files;

    }

    protected function getFunction($module){
        if(empty($module))return null;
        $action=A($module);
        $functions=get_class_methods($action);
        $inherents_functions = array(
            '_initialize','__construct','getActionName','isAjax','display','show','fetch',
            'buildHtml','assign','__set','get','__get','__isset',
            '__call','error','success','ajaxReturn','redirect','__destruct'
        );

        foreach ($functions as $func){
            if(!in_array($func, $inherents_functions)){
                $customer_functions[]=$func;
            }
        }
        return $customer_functions;
    }
module表结构
CREATE TABLE `module` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(45) DEFAULT NULL COMMENT '名称',
  `app` varchar(45) DEFAULT NULL COMMENT '项目',
  `group` varchar(45) DEFAULT NULL COMMENT '分组',
  `module` varchar(45) DEFAULT NULL COMMENT '模块',
  `function` varchar(45) DEFAULT NULL COMMENT '方法',
  `status` varchar(45) DEFAULT NULL COMMENT '状态',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
思路讲解:很简单,
1、根据配置文件获取分组
2、遍历分组下的Action文件夹中的*Action.class.php
3、实例化Action,获取其所有方法,过滤掉tp本身的底层函数,
4、小手一挥,数据到手.
getFunction代码讲解:
$action=A($module);
$functions=get_class_methods($action);//获取该类的所有方法
$inherents_functions = array(...);//tp自带的底层函数
dd