您的位置:首页 > 编程语言 > PHP开发

CodeIgniter 核心代码阅读-钩子文件Hooks.php

2013-05-30 15:29 513 查看
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

在最终着色页面发送到浏览器之后,浏览器接收完最终数据的系统执行末尾调用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: