CodeIgniter 核心代码阅读-钩子文件Hooks.php
2013-05-30 15:29
513 查看
Hooker.php --- 钩子文件
钩子功能可以在全局范围内打开或关闭,您可以在 application/config/config.php 文件中设定:
钩子是在 application/config/hooks.php 文件中定义的。 每个钩子可以用以下格式的数组来定义:
以下是一组可用的挂钩点.
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
在最终着色页面发送到浏览器之后,浏览器接收完最终数据的系统执行末尾调用
<?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
在最终着色页面发送到浏览器之后,浏览器接收完最终数据的系统执行末尾调用
相关文章推荐
- CodeIgniter 核心代码阅读-安全文件Security.php
- CodeIgniter 核心代码阅读-URI解析文件URI.php
- CodeIgniter 核心代码阅读-入口文件index.php
- CodeIgniter 核心代码阅读-输出文件Output.php
- CodeIgniter 核心代码阅读-程序启动文件CodeIgniter.php
- CodeIgniter 核心代码阅读-配置文件Config.php
- CodeIgniter 核心代码阅读-公用函数文件Common.php
- CodeIgniter 核心代码阅读-监控文件Benchmark.php
- CodeIgniter 核心代码阅读-Utf8转码文件Utf8.php
- CodeIgniter 核心代码阅读-输入数据处理文件Input.php
- CodeIgniter 核心代码阅读-加载语言包Lang.php
- CodeIgniter 核心代码阅读-控制器超级类Controller.php
- CI框架源码完全分析之核心文件(钩子)Hooks.php
- CodeIgniter源码阅读笔记(5)——钩子Hooks.php
- CodeIgniter 核心代码阅读-加载类Loader.php
- CI框架源码完全分析之核心文件(钩子)Hooks.php
- codeigniter源码分析-核心文件CodeIgniter.php
- CodeIgniter框架源码学习之钩子类--Hooks.php
- CI框架 -- 核心文件 之 Hooks.php
- 服务器基于PHP CodeIgniter,Android基于Volley实现多文件/图片上传(含服务器,web版和android客户端完整代码)