一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - CodeIgniter框架鉤子機制實現方法【hooks類】

CodeIgniter框架鉤子機制實現方法【hooks類】

2019-09-10 11:55dongxie548 PHP教程

這篇文章主要介紹了CodeIgniter框架鉤子機制實現方法,結合具體的hooks類文件描述了鉤子機制的原理與相關操作技巧,需要的朋友可以參考下

本文實例講述了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;
  }

}

// END CI_Hooks class

/* End of file Hooks.php */
/* Location: ./system/core/Hooks.php */

可以看出codeigniter實現鉤子機制的方式不夠優雅,其實完全可以使用觀察者模式來實現鉤子機制,將掛載點當做監聽的事件。

希望本文所述對大家基于CodeIgniter框架的PHP程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 好爽好粗| 青青青视频蜜桃一区二区 | 亚洲 欧美 国产 综合久久 | 久久青青草视频在线观 | 日本老师动漫 | 男人资源站 | 欧美贵妇videos办公室360 | 久久学生精品国产自在拍 | 日本艳鉧动漫1~6在线观看 | 婷婷中文网 | 国内精品久久久久久久 | 国产男技师 | 色五月天天 | 色哟哟哟在线精品观看视频 | 男人的天堂在线观看入口 | 久久re视频这里精品一本到99 | 国产成人精品午夜在线播放 | 情趣内衣情趣玩具play | 美女脱了内裤打开腿让人羞羞软件 | 精选国产AV精选一区二区三区 | 99久久久久国产 | 国产一区二区三区在线 | 国产精品免费久久久久影院小说 | 高清毛片一区二区三区 | 天天碰夜夜操 | 亚洲欧美成人综合在线 | 亚洲瑟瑟网 | 欧美在线视频 一区二区 | 男人日女人的b | 欧美撒尿屁股嘘嘘撒尿 | 国产精品国色综合久久 | miaa076深田咏美在线 | 波多野结衣黑人系列在线观看 | 经典欧美gifxxoo动态图暗网 | 亚洲春黄在线观看 | 欧美高清milf在线播放 | 欧美成人免费观看bbb | 日本一区二区三区久久精品 | 日本在线视频播放 | 日韩欧美国产在线 | 欧美日韩国产超高清免费看片 |