php ci框架 hook,CodeIgniter框架钩子机制实现方法【hooks类】
本文实例讲述了CodeIgniter框架钩子机制实现方法。分享给大家供大家参考,具体如下:
记得上一次去到喜啦面试,面试官问我一个问题:codeigniter是如何实现钩子机制的?
当时答不上来,后来回来之后查了一些资料才明白,所以在这里记录一下:
codeigniter的钩子是这样实现的:首先在框架的核心文件system/core/CodeIniter.php文件的 122行,载入Hooks类,接着在该文件中定义了几个挂载点,比如pre_system(129行)、post_controller_constructor(295行)等,并在这些挂载点上面执行hooks类的_call_hook()方法。
另附codeigniter的hooks类的源代码:
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
* @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
* @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.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author EllisLab 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
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;
}
// --------------------------------------------------------------------
/**
* Call Hook
*
* Calls a particular hook
*
* @access private
* @param string the hook name
* @return mixed
*/
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
*
* @access private
* @param array the hook details
* @return bool
*/
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;
}
}
// END CI_Hooks class
/* End of file Hooks.php */
/* Location: ./system/core/Hooks.php */
可以看出codeigniter实现钩子机制的方式不够优雅,其实完全可以使用观察者模式来实现钩子机制,将挂载点当做监听的事件。
希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。
php ci框架 hook,CodeIgniter框架钩子机制实现方法【hooks类】相关推荐
- php ci hooks,CodeIgniter框架钩子机制实现方法【hooks类】
本文实例讲述了CodeIgniter框架钩子机制实现方法.分享给大家供大家参考,具体如下: 记得上一次去到喜啦面试,面试官问我一个问题:codeigniter是如何实现钩子机制的? 当时答不上来,后来 ...
- php ci框架下载,CodeIgniter框架的下载及安装,codeigniter框架_PHP教程
CodeIgniter框架的下载及安装,codeigniter框架 CodeIgniter 是一套给 PHP 网站开发者使用的应用程序开发框架和工具包.初次学习Php,网上搜了很多php框架,最后选择 ...
- php ci captcha使用,CodeIgniter框架验证码类库文件与用法示例
本文实例讲述了CodeIgniter框架验证码类库文件与用法.分享给大家供大家参考,具体如下: 折腾了我四五个小时,终于,ci的验证码类库成功的整出来了. 下面请看源码: 在application/l ...
- php ci rest,在CodeIgniter框架中使用RESTful服务
步骤3 分析example_api.php 接下来,我们打开application/controllers/example_api.php文件,仔细分析其代码. 首先,在CodeIgniter中,我们 ...
- php框架 postgresql,CodeIgniter框架中使用PostgreSQL的配置
近日,因参与PostgresQL中国开源网站的建设,使用了CodeIgniter这个PHP的框架,相对还是算简单的,不过其文档中只给了使用mysql数据库的配置,而我们需使用PostgreSQL,特此 ...
- CodeIgniter框架深入理解
. CodeIgniter框架深入理解 1.CodeIgniter是一个小巧但功能强大的 PHP 框架,作为一个简单而"优雅"的工具包,它可以为 PHP 程序员建立功能完善的 We ...
- Application应用框架思考(三) 之[插件机制]
Application应用框架思考(三) 之[插件机制] /// <summary> /// 单例类 /// //为子功能提供主框架信息接口 类 /// </summary> ...
- 什么是框架常见的框架?
1,什么是框架: 定义:框架是一堆包含了常量,方法,类等代码的集合,他是一个半成品的应用,只包含了一些项目开发的时候所用使用的底层架构,并不包含业务逻辑,框架还包含了一些优秀的设计模式,如单例模式,工 ...
- **【ci框架】精通CodeIgniter框架
http://blog.csdn.net/yanhui_wei/article/details/25803945 一.大纲 [php] view plaincopy 1.codeigniter框架的授 ...
最新文章
- 2017Android通用流行框架大全
- 数据中心真能促进乡村经济吗?
- 打破冷漠僵局文章_研究僵局–第1部分
- Firefox 和 Chrome 性能测试对比
- java rmi配置_Java、Spring配置RMI连接
- python元组读取到列表_python中读入二维csv格式的表格方法详解(以元组/列表形式表示)...
- 解决移动端 footer fixd 定位被键盘顶起来的方案
- setsebool命令和设置命令
- TensorFlow进阶:车牌号识别项目
- 深度学习标签制作简易Python代码
- 2019腾讯广告大赛总结
- LeetCode打家劫舍系列
- ASP.NET资源大全-知识分享
- Windows系统下的IE浏览器降级
- Cathy学习Java——GUI与正则表达式
- 搜狗壁纸停运了,但我还是想推荐它……(附个人的一点想法)
- 使用系统默认声音、震动
- 一种射频卡读写原理及实现
- 多客| Lazada上传产品为什么审核不通过?卖家应遵守哪些规则?
- 4Ps营销理论(The Marketing Theory of 4Ps)