if ( ! isset($data['filepath']) OR ! isset($data['filename']))
{
return FALSE;
}
$filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
if ( ! file_exists($filepath))
{
return FALSE;
}
(4). 到这里,已经基本确认钩子程序的位置了,这里有两种情况:
a. 预定义的hook中class参数为空,表明使用的是过程式的调用方式,则直接执行hook文件中的function xxx
b. class参数不为空,提供的是面向对象的方式,则实际的钩子程序是$class->$function .同样,如果既没有设置class,也没有设置function参数,则无法执行hook,直接返回:
$class= FALSE;
$function= FALSE;
$params= '';
/* 获取 hook class */
if (isset($data['class']) AND $data['class'] != '')
{
$class = $data['class'];
}
/* 获取 hook function */
if (isset($data['function']))
{
$function = $data['function'];
}
/* 获取传递的 hook 参数 */
if (isset($data['params']))
{
$params = $data['params'];
}
/* 如果class和function都不存在,则无法定位hook程序,直接返回 */
if ($class === FALSE AND $function === FALSE)
{
return FALSE;
}
(5). 设置执行标志in_progress,并执行上述两种情况下的hook:
/* 面向对象的设置方式 */
if ($class !== FALSE)
{
if ( ! class_exists($class))
{
require($filepath);
}
$HOOK = new $class;
$HOOK->$function($params);
}
/* 过程式的执行方式 */
else
{
if ( ! function_exists($function))
{
require($filepath);
}
$function($params);
}
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Hooks {
/**
* Determines wether hooks are enabled
*
* @var bool
*/
var $enabled= FALSE;
/**
* List of all hooks set in config/hooks.php
*
*/
var $hooks= array();
/**
* Determines wether hook is in progress, used to prevent infinte loops
*
*/
var $in_progress= FALSE;
/**
* Constructor
*/
function __construct()
{
$this->_initialize();
log_message('debug', "Hooks Class Initialized");
}
/**
* Initialize the Hooks Preferences
*
* @accessprivate
* @returnvoid
*/
function _initialize()
{
$CFG =& load_class('Config', 'core');
// If hooks are not enabled in the config file
// there is nothing else to do
if ($CFG->item('enable_hooks') == FALSE)
{
return;
}
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;
}
$this->hooks =& $hook;
$this->enabled = TRUE;
}
/**
* Call Hook
*
* Calls a particular hook
*
* @accessprivate
* @paramstringthe hook name
* @returnmixed
*/
function _call_hook($which = '')
{
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
*
* @accessprivate
* @paramarraythe hook details
* @returnbool
*/
function _run_hook($data)
{
if ( ! is_array($data))
{
return FALSE;
}
// If the script being called happens to have the same hook call within it a loop can happen
if ($this->in_progress == TRUE)
{
return;
}
if ( ! isset($data['filepath']) OR ! isset($data['filename']))
{
return FALSE;
}
$filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
if ( ! file_exists($filepath))
{
return FALSE;
}
$class= FALSE;
$function= FALSE;
$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;
}
$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);
}
$this->in_progress = FALSE;
return TRUE;
}
}