thinphp5一键注册所有Auth规则

浏览:1459 发布日期:2016/11/22 分类:ThinkPHP5专区 关键字: thinkphp5 Auth认证 注册规则 注册权限 注册rule
代码中的方法的风格一定要写成如下图所示的形式
class Auto extends Base
{
    /**
     * 注册
     */
    public function register(){

        $modules = array('manager');  //模块名称
        $i = 0;
        foreach ($modules as $module) {
            $all_controller = $this->getController($module);
            foreach ($all_controller as $controller) {
                $controller_name = $controller;
                $all_action = $this->getAction($module, $controller_name);
                foreach ($all_action as $action) {
                    $tmp=strtolower(substr($controller, 0,-4));
                    $data[$i] = array(
                        'name' =>$tmp . '/' . strtolower($action),
                        'status' => 1,
                        'desc'=>$this->get_cc_desc($module,$controller,$action)
                    );
                    $i++;
                }
            }
        }
        $obj = new AuthRule();
        $msg = '';
        foreach ($data as $k => $v) {
            if (!$obj->where(array('name' => $v['name']))->find()) {
                if ($obj->insert(array('name' => $v['name'],'title'=>$v['desc']))) {
                    $msg = $msg . $v['name'] . '注册成功<br/>';
                } else {
                    $msg = $msg . $v['name'] . '注册失败<br/>';
                }
            } else {
                $obj->where(array("name"=>$v['name']))->update(array("title"=>$v['desc']));
                $msg = $msg . $v['name'] . '已注册<br/>';
            }
        }
        $this->assign("msg",$msg);
        return $this->fetch('System/register');
    }

    /**
     * @cc 获取所有控制器名称
     *
     * @param $module
     *
     * @return array|null
     */
    protected function getController($module){
        if(empty($module)) return null;
        $module_path = APP_PATH . '/' . $module . '/controller/';  //控制器路径
        if(!is_dir($module_path)) return null;
        $module_path .= '/*.php';
        $ary_files = glob($module_path);
        foreach ($ary_files as $file) {
            if (is_dir($file)) {
                continue;
            }else {
                $files[] = basename($file, config('DEFAULT_C_LAYER').'.class.php');
            }
        }
        return $files;
    }




    /**
     * @cc 获取所有方法名称
     *
     * @param $module
     * @param $controller
     *
     * @return array|null
     */
    protected function getAction($module, $controller){
        if(empty($controller)) return null;
        $content = file_get_contents(APP_PATH . '/'.$module.'/controller/'.$controller);

        preg_match_all("/.*?public.*?function(.*?)\(.*?\)/i", $content, $matches);
        $functions = $matches[1];

        //排除部分方法
        $inherents_functions = array('__construct','isAjax','display','show','fetch','buildHtml','assign','__set','get','__get','__isset','__call','error','success','ajaxReturn','redirect','__destruct','_empty');
        foreach ($functions as $func){
            $func = trim($func);
            if(!in_array($func, $inherents_functions)){
              if (strlen($func)>0)   $customer_functions[] = $func;
            }
        }
        return $customer_functions;
    }


    /**
     * @cc 获取函数的注释
     *
     * @param $module Home
     * @param $controller Auth
     * @param $action index
     *
     * @return string 注释
     *
     */
    protected function get_cc_desc($module,$controller,$action){
        $desc='app\\'.$module.'\controller\\'.$controller;
        $desc=substr($desc, 0,-4);
        $func  = new \ReflectionMethod(new $desc(),$action);
        $tmp   = $func->getDocComment();
        $flag  = preg_match_all('/@cc(.*?)\n/',$tmp,$tmp);
        $tmp   = trim($tmp[1][0]);
        $tmp   = $tmp !='' ? $tmp:'无';
        return $tmp;
    }
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>