CodeIgniter框架钩子机制实现方法【hooks类】

本文实例讲述了CodeIgniter框架钩子机制实现方法分享给大家供大家参考,具体如下:

记得上一次去到喜啦面试,面试官问我一个问题:codeigniter是如何实现钩子机制的?

当时答不上来,后来回来之后查了一些资料才明白,所以在这里记录一下:

codeigniter的钩子是这样实现的:首先在框架的核心文件stem/core/CodeIniter.PHP文件的 122行,载入Hooks类,接着在该文件中定义了几个挂载点,比如pre_system(129行)、post_controller_constructor(295行)等,并在这些挂载点上面执行hooks类的_call_hook() 方法

另附codeigniter的hooks类的源代码

rush:PHP;"> // ------------------------------------------------------------------------

/**

/**

  • 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 con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> file
// there is <a href="https://www.jb51.cc/tag/nothing/" target="_blank" class="keywords">nothing</a> else to do

if ($CFG->item('enable_hooks') == FALSE)
{
  return;
}

// Grab the "hooks" de<a href="https://www.jb51.cc/tag/Finitio/" target="_blank" class="keywords">Finitio</a>n file.
// If there are no hooks,we're done.

if (defined('ENVIRONMENT') AND is_file(APPPATH.'con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>/'.ENVIRONMENT.'/hooks.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>'))
{
  include(APPPATH.'con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>/'.ENVIRONMENT.'/hooks.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>');
}
elseif (is_file(APPPATH.'con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>/hooks.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>'))
{
  include(APPPATH.'con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>/hooks.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>');
}


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;
$p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms    = '';

if (isset($data['class']) AND $data['class'] != '')
{
  $class = $data['class'];
}

if (isset($data['function']))
{
  $function = $data['function'];
}

if (isset($data['p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms']))
{
  $p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms = $data['p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms'];
}

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($p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms);
}
else
{
  if ( ! function_exists($function))
  {
    require($filepath);
  }

  $function($p<a href="https://www.jb51.cc/tag/ara/" target="_blank" class="keywords">ara</a>ms);
}

$this->in_progress = FALSE;
return TRUE;

}

}

// END CI_Hooks class

/ End of file Hooks.PHP /
/ Location: ./system/core/Hooks.PHP /

可以看出codeigniter实现钩子机制的方式不够优雅,其实完全可以使用观察者模式来实现钩子机制,将挂载点当做监听的事件。

更多关于CodeIgniter相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用