有關(guān)CodeIgniter源碼分析的后續(xù)的更新移動(dòng)到了我的GitHub
在CI框架中CodeIgniter.php
才是整個(gè)框架核心內(nèi)容的啟動(dòng)器,代表證整個(gè)框架的加載和運(yùn)行流程涧黄。我理解的CI框架的流程圖如下圖所示蹬敲。
加載和運(yùn)行流程
淡藍(lán)色部分表示的是框架的核心庫(kù),淡橙色部分是用戶定義的鉤子類擴(kuò)展框架的核心舶得,在之后的文章對(duì)Hook.php文件進(jìn)行分析時(shí)會(huì)講到掰烟。比較特殊的鉤子類是$hook['cache_override'],這個(gè)鉤子的含義是實(shí)用用戶自己定義的方式來(lái)替代輸出類中的_display_cache()方法沐批,實(shí)現(xiàn)自定義的緩存顯示機(jī)制媚赖。相對(duì)其他鉤子而言,這個(gè)鉤子完成的工作不是“擴(kuò)展”珠插,而是“替換”惧磺。
下面我們以一種更細(xì)粒度的方式來(lái)認(rèn)識(shí)CodeIgniter.php
的執(zhí)行過(guò)程,其中具體的核心庫(kù)將在接下來(lái)的文章中單獨(dú)開篇進(jìn)行詳細(xì)分析捻撑。
設(shè)置版本號(hào)
const CI_VERSION = '3.1.8';
加載系統(tǒng)常量和公共函數(shù)
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/constants.php')){
require_once(APPPATH.'config/'.ENVIRONMENT.'/constants.php');
}
if (file_exists(APPPATH.'config/constants.php')){
require_once(APPPATH.'config/constants.php');
}
require_once(BASEPATH.'core/Common.php');
低于PHP5.4版本的安全處理
if ( ! is_php('5.4'))
{
ini_set('magic_quotes_runtime', 0);
if ((bool) ini_get('register_globals'))
{
$_protected = array(
'_SERVER',
'_GET',
'_POST',
'_FILES',
'_REQUEST',
'_SESSION',
'_ENV',
'_COOKIE',
'GLOBALS',
'HTTP_RAW_POST_DATA',
'system_path',
'application_folder',
'view_folder',
'_protected',
'_registered'
);
$_registered = ini_get('variables_order');
foreach (array('E' => '_ENV', 'G' => '_GET', 'P' => '_POST', 'C' => '_COOKIE', 'S' => '_SERVER') as $key => $superglobal)
{
if (strpos($_registered, $key) === FALSE)
{
continue;
}
foreach (array_keys($$superglobal) as $var)
{
if (isset($GLOBALS[$var]) && ! in_array($var, $_protected, TRUE))
{
$GLOBALS[$var] = NULL;
}
}
}
}
}
設(shè)置系統(tǒng)錯(cuò)誤處理Handler
set_error_handler('_error_handler');
set_exception_handler('_exception_handler');
register_shutdown_function('_shutdown_handler');
加載框架的核心類庫(kù)
框架核心類庫(kù)和文件名稱對(duì)應(yīng)如下表所示磨隘,具體內(nèi)容將在后續(xù)文章中展開分析,其中日志類Log.php
和配置類Config.php
不再分析顾患。
功能 | 文件名稱 | 注釋 |
---|---|---|
公共函數(shù) | Common.php | |
基準(zhǔn)類 | Benchmark.php | |
鉤子 | Hook.php | |
UTF8編碼轉(zhuǎn)換類 | UTF8.php | |
地址解析類 | URI.php | |
輸入類 | Input.php | |
輸出類 | Output.php | |
控制器類 | Controller.php | |
加載類 | Loader.php | |
安全類 | Security.php |
多字節(jié)支持
部分系統(tǒng)庫(kù)重寫
緩存調(diào)用
if ($EXT->call_hook('cache_override') === FALSE
&& $OUT->_display_cache($CFG, $URI) === TRUE){
exit;
}
404錯(cuò)誤處理
$e404 = FALSE;
$class = ucfirst($RTR->class);
$method = $RTR->method;
if (empty($class) OR ! file_exists(APPPATH.'controllers/'.$RTR->directory.$class.'.php')){
$e404 = TRUE;
} else {
require_once(APPPATH.'controllers/'.$RTR->directory.$class.'.php');
if ( ! class_exists($class, FALSE) OR $method[0] === '_' OR method_exists('CI_Controller', $method)){
$e404 = TRUE;
} elseif (method_exists($class, '_remap')){
$params = array($method, array_slice($URI->rsegments, 2));
$method = '_remap';
} elseif ( ! method_exists($class, $method)){
$e404 = TRUE;
} elseif ( ! is_callable(array($class, $method))){
$reflection = new ReflectionMethod($class, $method);
if ( ! $reflection->isPublic() OR $reflection->isConstructor()){
$e404 = TRUE;
}
}
}
if ($e404){
if ( ! empty($RTR->routes['404_override'])){
if (sscanf($RTR->routes['404_override'], '%[^/]/%s', $error_class, $error_method) !== 2){
$error_method = 'index';
}
$error_class = ucfirst($error_class);
if ( ! class_exists($error_class, FALSE) {
if (file_exists(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php')){
require_once(APPPATH.'controllers/'.$RTR->directory.$error_class.'.php');
$e404 = ! class_exists($error_class, FALSE);
} elseif ( ! empty($RTR->directory) && file_exists(APPPATH.'controllers/'.$error_class.'.php')){
require_once(APPPATH.'controllers/'.$error_class.'.php');
if (($e404 = ! class_exists($error_class, FALSE)) === FALSE){
$RTR->directory = '';
}
}
}
else{
$e404 = FALSE;
}
}
if ( ! $e404)
{
$class = $error_class;
$method = $error_method;
$URI->rsegments = array(
1 => $class,
2 => $method
);
}
else
{
show_404($RTR->directory.$class.'/'.$method);
}
}
if ($method !== '_remap')
{
$params = array_slice($URI->rsegments, 2);
}