CodeIgniter框架钩子机制实现方法【hooks类】
发布:smiling 来源: PHP粉丝网 添加日期:2018-11-15 09:53:33 浏览: 评论:0
本文实例讲述了CodeIgniter框架钩子机制实现方法。分享给大家供大家参考,具体如下:
记得上一次去到喜啦面试,面试官问我一个问题:codeigniter是如何实现钩子机制的?
当时答不上来,后来回来之后查了一些资料才明白,所以在这里记录一下:
codeigniter的钩子是这样实现的:首先在框架的核心文件system/core/CodeIniter.php文件的 122行,载入Hooks类,接着在该文件中定义了几个挂载点,比如pre_system(129行)、post_controller_constructor(295行)等,并在这些挂载点上面执行hooks类的_call_hook() 方法。
另附codeigniter的hooks类的源代码:
- <!--?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- /**
- * 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;
- } //phpfensi.com
- }
- // END CI_Hooks class
- /* End of file Hooks.php */
- /* Location: ./system/core/Hooks.php */
可以看出codeigniter实现钩子机制的方式不够优雅,其实完全可以使用观察者模式来实现钩子机制,将挂载点当做监听的事件。
Tags: CodeIgniter hooks类
相关文章
- ·CodeIgniter创建一个简单的注册系统(2014-07-19)
- ·Codeigniter利用PHPExcel导出Excel文件(2014-07-26)
- ·codeigniter使用技巧批量插入数据实例方法分享(2020-08-23)
- ·codeigniter自带数据库类使用方法说明(2020-11-05)
- ·CodeIgniter框架提示Disallowed Key Characters的解决办法(2020-11-19)
- ·Codeigniter中禁止A Database Error Occurred错误提示的方法(2021-02-16)
- ·使用配置类定义Codeigniter全局变量(2021-02-16)
- ·codeigniter数据库操作函数汇总(2021-02-16)
- ·Codeigniter操作数据库表的优化写法总结(2021-02-16)
- ·Codeigniter注册登录代码示例(2021-02-16)
- ·使用CodeIgniter的类库做图片上传(2021-02-16)
- ·codeigniter集成ucenter1.6双向通信的解决办法(2021-02-16)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)