~~~
<?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 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);
}
// 執行相應程序完畢后,重新把當前hook的狀態改為非運行中
// 以讓它可以再次被觸發。
$this->in_progress = FALSE;
return TRUE;
}
}
// END CI_Hooks class
/* End of file Hooks.php */
/* Location: ./system/core/Hooks.php */
~~~