有關(guān)CodeIgniter源碼分析的后續(xù)的更新移動到了我的GitHub
在CI框架中CodeIgniter.php才是整個框架核心內(nèi)容的啟動器,代表證整個框架的加載和運行流程。我理解的CI框架的流程圖如下圖所示。
加載和運行流程
淡藍色部分表示的是框架的核心庫,淡橙色部分是用戶定義的鉤子類擴展框架的核心,在之后的文章對Hook.php文件進行分析時會講到。比較特殊的鉤子類是$hook['cache_override'],這個鉤子的含義是實用用戶自己定義的方式來替代輸出類中的_display_cache()方法,實現(xiàn)自定義的緩存顯示機制。相對其他鉤子而言,這個鉤子完成的工作不是“擴展”,而是“替換”。
下面我們以一種更細粒度的方式來認識CodeIgniter.php的執(zhí)行過程,其中具體的核心庫將在接下來的文章中單獨開篇進行詳細分析。

設(shè)置版本號
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)錯誤處理Handler
set_error_handler('_error_handler');
set_exception_handler('_exception_handler');
register_shutdown_function('_shutdown_handler');
加載框架的核心類庫
框架核心類庫和文件名稱對應(yīng)如下表所示,具體內(nèi)容將在后續(xù)文章中展開分析,其中日志類Log.php和配置類Config.php不再分析。
| 功能 | 文件名稱 | 注釋 |
|---|---|---|
| 公共函數(shù) | Common.php | |
| 基準類 | Benchmark.php | |
| 鉤子 | Hook.php | |
| UTF8編碼轉(zhuǎn)換類 | UTF8.php | |
| 地址解析類 | URI.php | |
| 輸入類 | Input.php | |
| 輸出類 | Output.php | |
| 控制器類 | Controller.php | |
| 加載類 | Loader.php | |
| 安全類 | Security.php |
多字節(jié)支持
部分系統(tǒng)庫重寫
緩存調(diào)用
if ($EXT->call_hook('cache_override') === FALSE
&& $OUT->_display_cache($CFG, $URI) === TRUE){
exit;
}
404錯誤處理
$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);
}