codeigniter源代码分析之CI系统运行流程 CodeIgniter.php

来源:互联网 发布:淘宝免费推广 编辑:程序博客网 时间:2024/05/16 11:29

CodeIgniter.php 是过程的,主要描述的是CI整个处理流程 加载class 调用class的method 以及等等

带注释源码如下

<?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_Controllerrequire 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) == 0OR 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();}

总结:

定义版本、加载constants配置文件(主要用于文件操作)、设置错误处理函数、关闭magic_quotes、设置脚本运行时间、index.php 提供的覆盖config routes

然后就是加载Common.php 里面是一些全局用到的函数

通过Common.php中的 load_class 函数加载核心class

$RTR->_set_routing() 分析获取请求的 direct class method args

86行开始加载请求的控制器文件、检测类是否已经加载 方法是否存在  是否是私有方法 

出错解析出 router中的 404_override 控制器 文件存在就加载进来

128行 实例化 控制器(这个控制器也可能是上面404_override指定的控制器)

_remap优先执行

方法不存在 执行404_override 的控制器和方法(152 153重新new 对象CI要注意)

162行调用uri请求的控制器class 以及method args

call_user_func_array(array(&$CI, $method), array_slice($URI->rsegments, 2));

168行  $OUT->_display();     输出view数据到client端的browser

最后关闭数据库连接

Code Tips:

1、72行的函数返回的是CI_Controller的单例实例化对象

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

2、用到函数strncmp

int strncmp ( string $str1 , string $str2 , int $len ); 
比较字符串$str1 与$str2 第三个参数能指定比较长度 若为1 就是只比较两个字符串的第一个字符,= return 0、 > return 1 、< return -1

3、call_user_func_array(callback,args)

第一个参数是回调函数 应该是回调函数的名称,如果想用类的一个方法作为回调用这样的形式

array($class,$method);//$class是对象引用

0 0