CodeIgniter.php----加载需要的类库,CI框架所有操作都在这里执行
一个简单的页面需要加载的类库如下所示:
[0] => D:\wamp\www\CodeIgniter_2.1.3\system\core\CodeIgniter.php
[1] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Common.php
[2] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Benchmark.php
[3] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Hooks.php
[4] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Config.php
[5] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Utf8.php
[6] => D:\wamp\www\CodeIgniter_2.1.3\system\core\URI.php
[7] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Router.php
[8] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Output.php
[9] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Security.php
[10] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Input.php
[11] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Lang.php
[12] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Controller.php
[13] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Loader.php
[14] => D:\wamp\www\CodeIgniter_2.1.3\system\core\Model.php
[15] => D:\wamp\www\CodeIgniter_2.1.3\system\database\DB.php
[16] => D:\wamp\www\CodeIgniter_2.1.3\system\database\DB_driver.php
[17] => D:\wamp\www\CodeIgniter_2.1.3\system\database\DB_active_rec.php
[18] => D:\wamp\www\CodeIgniter_2.1.3\system\database\drivers\mysql\mysql_driver.php
[19] => D:\wamp\www\CodeIgniter_2.1.3\application\config\constants.php
[20] => D:\wamp\www\CodeIgniter_2.1.3\application\config\config.php
[21] => D:\wamp\www\CodeIgniter_2.1.3\application\config\routes.php
[22] => D:\wamp\www\CodeIgniter_2.1.3\application\config\mimes.php
[23] => D:\wamp\www\CodeIgniter_2.1.3\application\controllers\news.php
[24] => D:\wamp\www\CodeIgniter_2.1.3\application\config\autoload.php
[25] => D:\wamp\www\CodeIgniter_2.1.3\application\models\news_model.php
[26] => D:\wamp\www\CodeIgniter_2.1.3\application\config\database.php
CodeIgniter核心类库加载顺序:
Common.php->Benchmark.php->Hooks.php->Config.php->Utf8.php->URI.php->Router.php->Output.php->Securit.php->Input.php->Lang.php->Controller.php->Loader.php
CodeIgniter.php源码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//ci版本号
define('CI_VERSION', '2.1.3');
//ci分支
define('CI_CORE', FALSE);
//加载全局函数
require(BASEPATH.'core/Common.php');
//加载框架定义的常量,可在APPPATH.'config/constants.php'文件中定义常量
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');
//关闭魔术引号,本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除。
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']));
}
//设置PHP脚本的最大执行时间
if (function_exists("set_time_limit") == TRUE AND @ini_get("safe_mode") == 0)
{
@set_time_limit(300);
}
//实例化计时类
$BM =& load_class('Benchmark', 'core');
$BM->mark('total_execution_time_start');
$BM->mark('loading_time:_base_classes_start');
//实例化钩子类
$EXT =& load_class('Hooks', 'core');
//钩子--仅仅在benchmark 和 hooks 类 加载完毕的时候. 没有执行路由或者其它的过程.
$EXT->_call_hook('pre_system');
//实例化config类
$CFG =& load_class('Config', 'core');
// Do we have any manually set config items in the index.php file?
if (isset($assign_to_config))
{
$CFG->_assign_to_config($assign_to_config);
}
//实例化UTF-8类
$UNI =& load_class('Utf8', 'core');
//实例化URI类
$URI =& load_class('URI', 'core');
//实例化Router类,并设置路由
$RTR =& load_class('Router', 'core');
$RTR->_set_routing();
// Set any routing overrides that may exist in the main index file
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类
$SEC =& load_class('Security', 'core');
//实例化Input类
$IN =& load_class('Input', 'core');
//实例化Lang类
$LANG =& load_class('Lang', 'core');
//加载CI_Controller类
require BASEPATH.'core/Controller.php';
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';
}
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');
// Set a mark point for benchmarking
$BM->mark('loading_time:_base_classes_end');
//安全检查如果控制器不存在或者方法名不存在,则显示404
//控制器类名
$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}");
}
}
//钩子--在调用任何控制器之前调用.此时所用的基础类,路由选择和安全性检查都已完成.
$EXT->_call_hook('pre_controller');
//实例化请求的控制器
// Mark a start point so we can benchmark the controller
$BM->mark('controller_execution_time_( '.$class.' / '.$method.' )_start');
$CI = new $class();
//钩子--在控制器实例化之后,任何方法调用之前调用.
$EXT->_call_hook('post_controller_constructor');
//执行请求的方法
//如果控制器中定义了_remap方法,则执行remap方法
if (method_exists($CI, '_remap'))
{
$CI->_remap($method, array_slice($URI->rsegments, 2));
}
else
{
// is_callable() returns TRUE on some versions of PHP 5 for private and protected
// methods, so we'll use this workaround for consistent behavior
if ( ! in_array(strtolower($method), array_map('strtolower', get_class_methods($CI))))
{
// Check and see if we are using a 404 override and use it.
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}");
}
}
// Call the requested method.
// Any URI segments present (besides the class/function) will be passed to the method for convenience
call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));
}
// Mark a benchmark end point
$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');
//关闭数据路连接
if (class_exists('CI_DB') AND isset($CI->db))
{
$CI->db->close();
}