用户
 找回密码
 入住 CI 中国社区
搜索
查看: 4014|回复: 1
收起左侧

[中级] CodeIgniter 源码分析之 Hook.php

[复制链接]
发表于 2013-4-10 17:47:06 | 显示全部楼层 |阅读模式
原文: http://blog.163.com/wu_guoqing/b ... 018201281664619496/
作者: Calix

PHP复制代码
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------
 
/**
 * CodeIgniter Hooks Class
 */

class CI_Hooks {
 
 /**
  * Determines wether hooks are enabled
  */

 var $enabled  = FALSE;
 
 /**
  * List of all hooks set in config/hooks.php
  */

 var $hooks   = array();
 
 /**
  * Determines wether hook is in progress, used to prevent infinte loops
  */

 var $in_progress = FALSE;
 
 /**
  * Constructor
  */

 function __construct()
 {
  $this->_initialize();
  log_message('debug', "Hooks Class Initialized");
 }
 
 // --------------------------------------------------------------------
 
 /**
  * Initialize the Hooks Preferences
  */

 function _initialize()
 {
  $CFG =& load_class('Config', 'core');
 
 
  //如果配置文件中设置了是不允许hooks,则直接返回退出本函数。
  if ($CFG->item('enable_hooks') == FALSE)
  {
   return;
  }
 
  //要使用到的钩子,必须在配置目录下的hooks.php里面定义好。否则无法使用。
  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;
  }
 
  //把钩子信息都保存到Hook组件中。
  $this->hooks =& $hook;
  $this->enabled = TRUE;
 }
 
 // --------------------------------------------------------------------
 
 /**
  * Call Hook
  * 外部其实就是调用这个_call_hook函数进行调用钩子程序。而此方法中再调用_run_hook去执行相应的钩子。
  */

 function _call_hook($which = '')
 {
  if ( ! $this->enabled OR ! isset($this->hooks[$which]))
  {
   return FALSE;
  }
 
  //同一个位置可以执行多个hook
  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
  */

 function _run_hook($data)
 {
  //一般来说,这个$data会有:类名,方法名,参数,类文件路径等参数。
  if ( ! is_array($data))
  {
   return FALSE;
  }
 
  // -----------------------------------
  // Safety - Prevents run-away loops
  // -----------------------------------
 
 
  //如果调用某一个hook,执行某些脚本,而有可能这些脚本里面再会触发其它hook,如果这个其它hook里面又包含了当前
  //的hook,那么就会进入死循环,这个in_progress的存在就是阻止这种情况。
  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
  // -----------------------------------
 
  //在开始执行钩子相应的程序之前,先把当前hook的状态设为正在运行中。
  $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);
  }
 
  //执行相应程序完毕后,重新把当前hook的状态改为非运行中,以让它可以再次被触发。
  $this->in_progress = FALSE;
  return TRUE;
 }
 
}
 
复制代码
发表于 2014-9-23 16:39:20 | 显示全部楼层
很简洁的钩子,实现了强大的功能,如果调用方法时可以传参就更完美了

本版积分规则