您好,欢迎访问一九零五行业门户网

CI框架源码阅读---------钩子类hooks.php_PHP教程

[php] 
_initialize();  
        log_message('debug', hooks class initialized);  
    }
// --------------------------------------------------------------------
/** 
     * initialize the hooks preferences 参数,首选项 
     * 初始化钩子 
     * @access  private 
     * @return  void 
     */  
    function _initialize()  
    {  
        $cfg =& load_class('config', 'core');
// if hooks are not enabled in the config file  
        // there is nothing else to do  
        // 如果配置文件中设置了是不允许hooks,则直接返回退出本函数。  
        if ($cfg->item('enable_hooks') == false)  
        {  
            return;  
        }
// grab the hooks definition file.  
        // 抓取钩子的定义文件  
        // if there are no hooks, we're done.  
        // 如果没有定义hooks.php没有定义$hook数组我们直接返回
if (defined('environment') and is_file(apppath.'config/'.environment.'/hooks.php'))  
        {  
            include(apppath.'config/'.environment.'/hooks.php');  
        }  
        elseif (is_file(apppath.'config/hooks.php'))  
        {  
            include(apppath.'config/hooks.php');  
        }
if ( ! isset($hook) or ! is_array($hook))  
        {  
            return;  
        }
// 将hooks.php 中的$hook数组引用到$this->hooks  
        // 开启$this->enabled  
        $this->hooks =& $hook;  
        $this->enabled = true;  
    }
// --------------------------------------------------------------------
/** 
     * call hook 
     * 外部其实就是调用这个_call_hook函数进行调用钩子程序。 
     * 而此方法中再调用_run_hook去执行相应的钩子。 
     * calls a particular hook 
     * 
     * @access  private 
     * @param   string  the hook name 
     * @return  mixed 
     */  
    function _call_hook($which = '')  
    {  
        // 判断$this->enabled 是否开启 和 要调用的钩子是否在$htis->hooks中存在。  
        if ( ! $this->enabled or ! isset($this->hooks[$which]))  
        {  
            return false;  
        }
// 判断要调用的钩子是否是一个二维数组,如果是就遍历执行。  
        // 如果不是二维数组就直接执行  
        // 这里说明,在一个挂钩点可以执行多个钩子,就是通过定义二维数组来实现的。  
        if (isset($this->hooks[$which][0]) and is_array($this->hooks[$which][0]))  
        {  
            foreach ($this->hooks[$which] as $val)  
            {  
                $this->_run_hook($val);  
            }  
        }  
        else  
        {  
            $this->_run_hook($this->hooks[$which]);  
        }
return true;  
    }
// --------------------------------------------------------------------
/** 
     * run hook 
     * 运行钩子 
     * runs a particular 特别的 hook 
     *  
     * @access  private 
     * @param   array   the hook details 
     * @return  bool 
     */  
    function _run_hook($data)  
    {  
        /* 
         * $data 就是我们在apppath/config/hook.php 定义的hook数组 
         * $hook['pre_controller'] = array( 
         *        'class'    => 'myclass', 
         *        'function' => 'myfunction', 
         *        'filename' => 'myclass.php', 
         *        'filepath' => 'hooks', 
         *        'params'   => array('beer', 'wine', 'snacks') 
         *         ); 
         * 
         * 由于每一个钩子肯定是由数组组成的 
         * 所以这里就判断$data是不是数组如果不是则返回 
         *  
         */  
        if ( ! is_array($data))  
        {  
            return false;  
        }
// -----------------------------------  
        // safety - prevents run-away loops  
        // -----------------------------------
// if the script being called happens to have the same  
        // hook call within it a loop can happen  
        // 如果调用某一个hook,执行某些脚本,而有可能这些脚本里面再会触发其它hook  
        // 如果这个其它hook里面又包含了当前  
        // 的hook,那么就会进入死循环,这个in_progress的存在就是阻止这种情况。
if ($this->in_progress == true)  
        {  
            return;  
        }
// -----------------------------------  
        // 取出data里面的数据,加载  apppath.$data['filepath'].$data['filename'];  
        // set file path  
        // -----------------------------------
if ( ! isset($data['filepath']) or ! isset($data['filename']))  
        {  
            return false;  
        }
$filepath = apppath.$data['filepath'].'/'.$data['filename'];
if ( ! file_exists($filepath))  
        {  
            return false;  
        }
// -----------------------------------  
        // set class/function name  
        // -----------------------------------
$class      = false;  
        $function   = false;  
        $params     = '';  
        // 取出$hooks 中的class function params   
        if (isset($data['class']) and $data['class'] != '')  
        {  
            $class = $data['class'];  
        }
if (isset($data['function']))  
        {  
            $function = $data['function'];  
        }
if (isset($data['params']))  
        {  
            $params = $data['params'];  
        }
if ($class === false and $function === false)  
        {  
            return false;  
        }
// -----------------------------------  
        // set the in_progress flag  
        // 在开始执行钩子相应的程序之前,先把当前hook的状态设为正在运行中。  
        // -----------------------------------
$this->in_progress = true;
// -----------------------------------  
        // call the requested class and/or function  
        // 包含钩子文件并实例化类,调用函数  
        // -----------------------------------
if ($class !== false)  
        {  
            if ( ! class_exists($class))  
            {  
                require($filepath);  
            }
$hook = new $class;  
            $hook->$function($params);  
        }  
        else  
        {  
            if ( ! function_exists($function))  
            {  
                require($filepath);  
            }
$function($params);  
        }  www.2cto.com
        // 执行相应程序完毕后,重新把当前hook的状态改为非运行中  
        // 以让它可以再次被触发。  
        $this->in_progress = false;  
        return true;  
    }
}
// end ci_hooks class
/* end of file hooks.php */  
/* location: ./system/core/hooks.php */
http://www.bkjia.com/phpjc/477711.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/477711.htmltecharticle[php] ?php if ( ! defined(basepath)) exit(no direct script access allowed); /** * codeigniter * * an open source application development framework for php 5.1.6 or newer * * @packag...
其它类似信息

推荐信息