codeigniter源代码分析 - 钩子类 Hooks.php

钩子机制方便我们对CI处理流程中添加自己的处理

CodeIgniter.php 是CI的处理流程 这里有一些钩子可供我们操作  $EXT->_call_hook

line 39 pre_system 是在系统启动前的处理

line 58 cache_override 缓存输出前的操作 (处理结构返回的是FALSE 的话 缓存还是会输出并且退出整个system)

line 124 pre_controller 调用控制器之前

line 129 post_controller_constructor 控制器实例化之后

line 165 post_controller 控制器调用结束

line166 display_override view显示之前

line 170 post_system 系统调用之后

源代码:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

define('CI_VERSION', '2.1.4'); //版本
define('CI_CORE', FALSE);
//加载 Common 文件
require(BASEPATH.'core/Common.php');
// 根据鱼腥环境(index.php中定义的全局变量)加载配置文件 constants.php 文件里面定义了文件操作的mode 以及 目录权限
if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
{
	require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
}
else
{
	require(APPPATH.'config/constants.php');
}
set_error_handler('_exception_handler');//错误处理函数 在common.php中
if ( ! is_php('5.3'))
{
	// php版本低于5.3 关闭magic_quote(自动将IO数据添加转义符)功能,5.3以后这个功能就废除了
	@set_magic_quotes_runtime(0);
}
if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
{
	// 替换config的 subclass_prefix 项 这个项是 用户对ci核心类扩展类的类前缀
	get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
}
if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
{
	@set_time_limit(300);// 设置php 运行时间
}

//加载class BenchMark 标记程序 用以计算系统性能
$BM =& load_class('Benchmark', 'core');
$BM->mark('total_execution_time_start');
$BM->mark('loading_time:_base_classes_start');

// 加载钩子
$EXT =& load_class('Hooks', 'core'); 
$EXT->_call_hook('pre_system');// 调用钩子
// 加载配置
$CFG =& load_class('Config', 'core');
if (isset($assign_to_config))
{
	//index文件中对config的覆盖
	$CFG->_assign_to_config($assign_to_config);
}
$UNI =& load_class('Utf8', 'core');
$URI =& load_class('URI', 'core');
$RTR =& load_class('Router', 'core');
//获取URL中的请求信息
$RTR->_set_routing();
if (isset($routing))
{
	//index文件中$routing对路由的覆盖
	$RTR->_set_overrides($routing);
}
$OUT =& load_class('Output', 'core');
if ($EXT->_call_hook('cache_override') === FALSE)
{
	if ($OUT->_display_cache($CFG, $URI) == TRUE)
	{
		exit;
	}
}
$SEC =& load_class('Security', 'core');
$IN	=& load_class('Input', 'core');
$LANG =& load_class('Lang', 'core');
//加载CI_Controller
require BASEPATH.'core/Controller.php';

//全局函数 调用CI_Controller的get_instance方法, 返回CI_Controller的引用
function &get_instance()
{
	return CI_Controller::get_instance();
}
// 存在Controller扩展类加载之
if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
{
	require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
}
// 请求的controller文件不存在
if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php'))
{
	show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
}
// 加载请求的controller文件
include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');
$BM->mark('loading_time:_base_classes_end');
// 设置请求的class and method
$class  = $RTR->fetch_class();
$method = $RTR->fetch_method();

// 出现 请求的class 不存在 、方法是受保护的、方法不存在 等情况
if ( ! class_exists($class)
	OR strncmp($method, '_', 1) == 0
	OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
	)
{
	// 若 router中指定有处理404的控制器(controller)
	if ( ! empty($RTR->routes['404_override']))
	{
		// 解析出 class/method
		$x = explode('/', $RTR->routes['404_override']);
		$class = $x[0];
		//404_override default method is index
		$method = (isset($x[1]) ? $x[1] : 'index');
		// class没有被加载
		if ( ! class_exists($class))
		{
			// 文件不存在
			if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
			{
				show_404("{$class}/{$method}");
			}
			//加载404controller文件
			include_once(APPPATH.'controllers/'.$class.'.php');
		}
	}
	else
	{
		show_404("{$class}/{$method}");
	}
}
$EXT->_call_hook('pre_controller');
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');

// 实例化控制器类 若上面的检测情况出现 这里也会是实例化404的控制器
$CI = new $class();
$EXT->_call_hook('post_controller_constructor');
if (method_exists($CI, '_remap'))
{//存在_remap方法优先调用之
	$CI->_remap($method, array_slice($URI->rsegments, 2));
}
else
{
	// 方法不存在
	if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
	{
		if ( ! empty($RTR->routes['404_override']))
		{
			$x = explode('/', $RTR->routes['404_override']);
			$class = $x[0];
			$method = (isset($x[1]) ? $x[1] : 'index');
			if ( ! class_exists($class))
			{
				if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))
				{
					show_404("{$class}/{$method}");
				}
				include_once(APPPATH.'controllers/'.$class.'.php');
				// 重新为ci赋值
				unset($CI);
				$CI = new $class();
			}
		}
		else
		{
			show_404("{$class}/{$method}");
		}
	}
	// 调用方法
	call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
}
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');
$EXT->_call_hook('post_controller');
if ($EXT->_call_hook('display_override') === FALSE)
{
	$OUT->_display();
}
$EXT->_call_hook('post_system');
// 加载DB 关闭数据库连接
if (class_exists('CI_DB') AND isset($CI->db))
{
	$CI->db->close();
}

Code Tips:

方法_run_hook 防止重复加载、实例化class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值