Home  >  Article  >  php教程  >  自动获取所有模块的函数,用于RBAC节点和菜单数据

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

WBOY
WBOYOriginal
2016-06-07 11:44:441042browse

系统开发好以后,你知道系统里有多少个Module不?知道有多少个Action不?

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

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

代码来源:开源ThinkPHP代码生成器
应用主页:http://rockefys.github.io/commlib//生成模块结构信息 app/分组/模块/方法<br>     public function fetch_module(){<br>         $M = M('Module');<br>         $M->query("truncate table module");<br>         $app = $this->getAppName();<br>         $groups = $this->getGroup();<br>         $n=0;<br>         foreach ($groups as $group) {<br>             $modules = $this->getModule($group);<br>             foreach ($modules as $module) {<br>                 $module_name=$app.'://'.$group.'/'.$module;<br>                 $functions = $this->getFunction($module_name);<br>                 foreach ($functions as $function) {<br>                     $data[$n]['app'] = $app;<br>                     $data[$n]['group'] = $group;<br>                     $data[$n]['module'] = $module;<br>                     $data[$n]['function'] = $function;<br>                     ++$n;                                }<br>             }<br>         }<br>         $M->addAll($data);<br>         $this->success('所有分组/模块/方法已成功读取到module表中.');<br>     }<br>     protected function getAppName(){<br>         return APP_NAME;<br>     }<br> <br>     protected function getGroup(){<br>         $result = explode(',',C('APP_GROUP_LIST'));<br>         return $result;<br>     }<br> <br>     protected function getModule($group){<br>         if(empty($group))return null;<br>         $group_path=LIB_PATH.'Action/'.$group;<br>         if(!is_dir($group_path))return null;<br>         $group_path.='/*.class.php';<br>         $ary_files = glob($group_path);<br>         foreach ($ary_files as $file) {<br>             if (is_dir($file)) {<br>                 continue;<br>             }else {<br>                     $files[] = basename($file,'Action.class.php');<br>             }<br>         }<br>         return $files;<br> <br>     }<br> <br>     protected function getFunction($module){<br>         if(empty($module))return null;<br>         $action=A($module);<br>         $functions=get_class_methods($action);<br>         $inherents_functions = array(<br>             '_initialize','__construct','getActionName','isAjax','display','show','fetch',<br>             'buildHtml','assign','__set','get','__get','__isset',<br>             '__call','error','success','ajaxReturn','redirect','__destruct'<br>         );<br> <br>         foreach ($functions as $func){<br>             if(!in_array($func, $inherents_functions)){<br>                 $customer_functions[]=$func;<br>             }<br>         }<br>         return $customer_functions;<br>     }module表结构CREATE TABLE `module` (<br>   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,<br>   `name` varchar(45) DEFAULT NULL COMMENT '名称',<br>   `app` varchar(45) DEFAULT NULL COMMENT '项目',<br>   `group` varchar(45) DEFAULT NULL COMMENT '分组',<br>   `module` varchar(45) DEFAULT NULL COMMENT '模块',<br>   `function` varchar(45) DEFAULT NULL COMMENT '方法',<br>   `status` varchar(45) DEFAULT NULL COMMENT '状态',<br>   PRIMARY KEY (`id`)<br> ) ENGINE=InnoDB DEFAULT CHARSET=utf8$$思路讲解:很简单,
1、根据配置文件获取分组
2、遍历分组下的Action文件夹中的*Action.class.php
3、实例化Action,获取其所有方法,过滤掉tp本身的底层函数,
4、小手一挥,数据到手.
getFunction代码讲解:$action=A($module);<br> $functions=get_class_methods($action);//获取该类的所有方法<br> $inherents_functions = array(...);//tp自带的底层函数

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn