php框架 生成ACL

用php框架开发中,经常要用到acl, 手动一个一个录入控制器,动作,描述 到数据库太麻烦..

以下代码,复制到测试控制器上, 前提是你的函数名都是规范的phpdoc格式, 如下格式:
 1     /**
 2      * 生成acl
 3      */
 4     function makeAcl(){
 5         $controller = dirname(__FILE__);
 6         $aclData = array();
 7         $needAuth = array('member', 'test'); //需要添加到数据库里的,白名单模式
 8         spClass('m_acl')->runSql('TRUNCATE TABLE  `cym_acl`');
 9         foreach(glob($controller . "\*.php") as $v){
10             $current = file_get_contents($v);
11             preg_match_all('/[\{\}]\s+\/\*\*\s+\*\s(.+)[\s\S]+?function (\w+)\(.*\)/', $current, $out);
12             preg_match('/\/\*\*\s+\*\s(.+)[\s\S]+?class (\w+) /', $current, $out1);
13             if(count($out) === 3 && count($out1) === 3){
14                 $contrl = $out1[2];
15                 $aclData[$contrl]['describe'] = $out1[1];
16                 //创建控制器pid=0
17                 $id = in_array($contrl, $needAuth) ? spClass('m_acl')->create(array('visit' => $contrl, 'describe' => $out1[1], 'pid' => 0)) : 0;
18                 for($i = 0; $i < count($out[1]); $i++){
19                     $aclData[$contrl][] = array('describe' => $out[1][$i], 'action' => $out[2][$i]);
20                     //创建动作
21                     if($id > 0){
22                         spClass('m_acl')->create(array('visit' => $out[2][$i], 'describe' => $out[1][$i], 'pid' => $id));
23                     }
24                 }
25             }
26         }
27         dump($aclData);
28         exit;
29     }

对于动作名的控制器名都加了suffix的,只要手工改改循环体就好了,注strstr第三个函数要php 5.2以上版本才行!

生成的是数组.


posted @ 2012-10-21 00:27  无嗔  阅读(1217)  评论(0编辑  收藏  举报