首页 > 其他 > 详细

CI框架学习之二 (引导文件解析)

时间:2015-03-23 16:09:50      阅读:527      评论:0      收藏:0      [点我收藏+]
<?php  
//如果未定义系统路径 直接退出
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 * 一个开源应用框架适用于 PHP 5.1.6 或更新
 * 版本 1.0
 */

// ------------------------------------------------------------------------

/**
 * 系统初始化文件
 *
 * 加载基础的类并执行请求
 */

/**
 *  定义CI 版本
 */
	define('CI_VERSION', '2.2.0');

/**
 * CI分支 (核心 = TRUE, 反应器模式 = FALSE)
 */
	define('CI_CORE', FALSE);

/*
 * ------------------------------------------------------
 *  加载系统全局函数
 * ------------------------------------------------------
 */
	require(BASEPATH.'core/Common.php');

/*
 * ------------------------------------------------------
 *  加载应用框架常量
 * ------------------------------------------------------
 */
	if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php'))
	{
		require(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
	}
	else
	{
		require(APPPATH.'config/constants.php');
	}

/*
 * ------------------------------------------------------
 *  设置一个定制的错误处理函数以便于我们能 记录 PHP 错误
 * ------------------------------------------------------
 */
	set_error_handler('_exception_handler');

	if ( ! is_php('5.3'))
	{
		@set_magic_quotes_runtime(0); // Kill magic quotes
	}

/*
 * ------------------------------------------------------
 *  设置子类前缀
 * ------------------------------------------------------
 * 从配置中读取
 */
	if (isset($assign_to_config['subclass_prefix']) AND $assign_to_config['subclass_prefix'] != '')
	{
		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);
	}

/*
 * ------------------------------------------------------
 *  启动计时器。。。 Benchmark是一个基准测试类
 * ------------------------------------------------------
 */
	$BM =& load_class('Benchmark', 'core');
	$BM->mark('total_execution_time_start');
	$BM->mark('loading_time:_base_classes_start');

/*
 * ------------------------------------------------------
 *  实例化 hooks 类 他是一个扩展框架的核心
 * ------------------------------------------------------
 */
	$EXT =& load_class('Hooks', 'core');

/*
 * ------------------------------------------------------
 *  "pre_system" 调用系统初始化前的钩子
 * ------------------------------------------------------
 */
	$EXT->_call_hook('pre_system');

/*
 * ------------------------------------------------------
 *  实例化配置类
 * ------------------------------------------------------
 */
	$CFG =& load_class('Config', 'core');

	// 是否有手动配置项在入口文件 index.php 中?
	if (isset($assign_to_config))
	{
		$CFG->_assign_to_config($assign_to_config);
	}

/*
 * ------------------------------------------------------
 *  实例化字符集 UTF-8 类
 * ------------------------------------------------------
 */

	$UNI =& load_class('Utf8', 'core');

/*
 * ------------------------------------------------------
 *  实例化 URI 类 该类主要负责处理url 函数
 * ------------------------------------------------------
 */
	$URI =& load_class('URI', 'core');

/*
 * ------------------------------------------------------
 *  实例化路由类 并设置路由
 * ------------------------------------------------------
 */
	$RTR =& load_class('Router', 'core');
	$RTR->_set_routing();

	// 设置路由覆盖已经存在的配置
	if (isset($routing))
	{
		$RTR->_set_overrides($routing);
	}

/*
 * ------------------------------------------------------
 *  实例化 output 输出打印类
 * ------------------------------------------------------
 */
	$OUT =& load_class('Output', 'core');

/*
 * ------------------------------------------------------
 *	是否有有效的缓存文件?  如果有 我们做...
 * ------------------------------------------------------
 */
	if ($EXT->_call_hook('cache_override') === FALSE)
	{
		if ($OUT->_display_cache($CFG, $URI) == TRUE)
		{
			exit;
		}
	}

/*
 * -----------------------------------------------------
 * 加载 security 类 为 xss 和 csrf 支持
 * -----------------------------------------------------
 */
	$SEC =& load_class('Security', 'core');

/*
 * ------------------------------------------------------
 *  加载 Input (输入) 类 然后 过滤全局变量
 * ------------------------------------------------------
 */
	$IN	=& load_class('Input', 'core');

/*
 * ------------------------------------------------------
 *  加载语言类
 * ------------------------------------------------------
 */
	$LANG =& load_class('Lang', 'core');

/*
 * ------------------------------------------------------
 *  加载 app 控制器 和 local 控制器
 * ------------------------------------------------------
 *
 */
	// 加载核心控制器文件
	require BASEPATH.'core/Controller.php';

	function &get_instance()
	{
		return CI_Controller::get_instance();
	}

    //加载应用核心控制器文件
	if (file_exists(APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php'))
	{
		require APPPATH.'core/'.$CFG->config['subclass_prefix'].'Controller.php';
	}

	// 加载本地 应用 controller
	// 路由类会自动验证控制器的有效性通过 router->_validate_request().
	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.');
	}
    
	//加载控制器
	include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().'.php');

	// 设置标记点
	$BM->mark('loading_time:_base_classes_end');

/*
 * ------------------------------------------------------
 *  安全检查
 * ------------------------------------------------------
 *  下划线的函数和CI控制器中的核心函数不能通过uri直接访问
 */
	$class  = $RTR->fetch_class();   //获取当前类
	$method = $RTR->fetch_method();  //获取当前函数

	if ( ! class_exists($class)
		OR strncmp($method, '_', 1) == 0
		OR in_array(strtolower($method), array_map('strtolower', get_class_methods('CI_Controller')))
		)
	{
		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');
			}
		}
		else
		{
			show_404("{$class}/{$method}");
		}
	}

/*
 * ------------------------------------------------------
 *  是否有 "pre_controller" 钩子?
 * ------------------------------------------------------
 */
	$EXT->_call_hook('pre_controller');

/*
 * ------------------------------------------------------
 *  实例化请求控制器 request
 * ------------------------------------------------------
 */
	// 标记开始控制器点
	$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');

	$CI = new $class();

/*
 * ------------------------------------------------------
 *  是否有 "post_controller_constructor" 钩子?
 * ------------------------------------------------------
 */
	$EXT->_call_hook('post_controller_constructor');

/*
 * ------------------------------------------------------
 *  调用 requested 方法
 * ------------------------------------------------------
 */
	// 是否有 "remap" 函数? 如果有, 我们调用它替代
	if (method_exists($CI, '_remap'))
	{
		$CI->_remap($method, array_slice($URI->rsegments, 2));
	}
	else
	{
		// 如果方法在CI类(应用类)中不存在
		if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
		{
			// 如果404重写页面配置存在.
			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');
					unset($CI);
					$CI = new $class();
				}
			}
			else
			{
				show_404("{$class}/{$method}");
			}
		}

		// 否则调用CI方法 调用请求方法.
		// 任何 URI 段 现在(除了 class/function) 将会传递给该方法 
		call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
	}


	// 标记终止点
	$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_end');

/*
 * ------------------------------------------------------
 *  是否有 "post_controller" 钩子?
 * ------------------------------------------------------
 */
	$EXT->_call_hook('post_controller');

/*
 * ------------------------------------------------------
 *  发送最后的渲染输出到浏览器
 * ------------------------------------------------------
 */
	if ($EXT->_call_hook('display_override') === FALSE)
	{
		$OUT->_display();
	}

/*
 * ------------------------------------------------------
 *  是否有"post_system" 钩子?
 * ------------------------------------------------------
 */
	$EXT->_call_hook('post_system');

/*
 * ------------------------------------------------------
 *  如果存在 关闭数据库连接
 * ------------------------------------------------------
 */
	if (class_exists('CI_DB') AND isset($CI->db))
	{
		$CI->db->close();
	}


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

CI框架学习之二 (引导文件解析)

原文:http://blog.csdn.net/wujiangwei567/article/details/44564879

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!