Hooker.php --- 鉤子文件
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Hooks {
//鉤子啓用/停用標記
var $enabled = FALSE;
//配置文件中定義的所有鉤子
var $hooks = array();
//是否正在執行鉤子中的函數
var $in_progress = FALSE;
//構造函數
function __construct()
{
$this->_initialize();
log_message('debug', "Hooks Class Initialized");
}
//實例化hooks
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;
}
// Grab the "hooks" definition file.
// If there are no hooks, we're done.
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;
}
//調用hooks中定義的類、方法
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;
}
//執行調用hooks中定義的類、方法
function _run_hook($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
if ($this->in_progress == TRUE)
{
return;
}
// -----------------------------------
// 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 = '';
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
// -----------------------------------
$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;
}
}
鉤子功能可以在全局範圍內打開或關閉,您可以在 application/config/config.php 文件中設定:
$config['enable_hooks'] = TRUE;
鉤子是在 application/config/hooks.php 文件中定義的。 每個鉤子可以用以下格式的數組來定義:
$hook['pre_controller'] = array(
'class' => 'MyClass',// 你希望調用的類名.如果你更喜歡使用過程函數代替類的話,此項保留爲空.
'function' => 'Myfunction',// 你希望調用的函數名.
'filename' => 'Myclass.php',// 包含有你的類/函數的文件名.
'filepath' => 'hooks',// 包含你的腳本的目錄名.
'params' => array('beer', 'wine', 'snacks')// 你希望傳遞給腳本的任何參數.
);
以下是一組可用的掛鉤點.
pre_system
系統執行的早期調用.僅僅在benchmark 和 hooks 類 加載完畢的時候. 沒有執行路由或者其它的過程.
pre_controller
在調用你的任何控制器之前調用.此時所用的基礎類,路由選擇和安全性檢查都已完成.
post_controller_constructor
在你的控制器實例化之後,任何方法調用之前調用.
post_controller
在你的控制器完全運行之後調用.
display_override
覆蓋_display()函數, 用來在系統執行末尾向web瀏覽器發送最終頁面.這允許你用自己的方法來顯示.注意,你需要通過 $this->CI =& get_instance() 引用 CI 超級對象,然後這樣的最終數據可以通過調用 $this->CI->output->get_output() 來獲得。
cache_override
可以讓你調用自己的函數來取代output類中的_display_cache() 函數.這可以讓你使用自己的緩存顯示方法
post_system
在最終着色頁面發送到瀏覽器之後,瀏覽器接收完最終數據的系統執行末尾調用