日期:2014-05-17 浏览次数:20632 次
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------
/**
* CodeIgniter Hooks Class
*
* Provides 提供 a mechanism 机制 to extend the base system without hacking.
* 用户手册地址:http://codeigniter.org.cn/user_guide/general/hooks.html
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/libraries/encryption.html
*/
class CI_Hooks {
/**
* Determines wether hooks are enabled
* 决定钩子是否启用
*
* @var bool
*/
var $enabled = FALSE;
/**
* List of all hooks set in config/hooks.php
*
* @var array
*/
var $hooks = array();
/**
* Determines wether hook is in progress, used to prevent 防止 infinte 无限 loops
*
* @var bool
*/
var $in_progress = FALSE;
/**
* Constructor
*
*/
function __construct()
{
$this->_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 t