CodeIgniter 源码解读之 CodeIgniter.php(一)

来到 CodeIgniter.php

CodeIgniter.php 的代码突然就变多了,但没事,我们慢慢的一行行的细细品。首先,这个不是一个类文件,貌似是一大推定义和引入文件。

  1. 声明CI的框架版本号
const CI_VERSION = '3.1.11';
  1. 加载框架的常量定义文件 constants.php
	# 这里的ENVIRONMENT常量是index.php文件中定义的
	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');
	}
  1. 加载框架核心公共方法文件 common.php
# 这个文件前后定义了很多系统函数,后面再一起看
require_once(BASEPATH.'core/Common.php');
  1. 安全处理程序(解决5.4之前版本的全局变量获取隐患)
# 调用is_php函数判断当前php版本是否大于等于5.4,如果是则返回true,反之则返回false
# 至于 is_php 函数的具体实现请移步Common.php文件查看
if ( ! is_php('5.4'))
{
	# 关闭自动转义
	ini_set('magic_quotes_runtime', 0);
	
	# 细心的同学肯定发现上面 is_php 函数传入了 5.4 这个版本,这是为什么捏?
	# 这是因为从php 5.4版本开始,php.ini 文件中就删除了register_globals,所以如果php版本大于 5.4 就没有必要再往下执行了
	# register_globals的作用就是注册为全局变量,所以当register_globals=On的时候,
	# 传递过来的值会被直接的注册为全局变量直接使用,而register_globals=Off的时候,需要到特定的数组里去得到它
	# 打比方:$_GET['username'] 可以通过 $username 获取,这样会存在很多安全隐患。
	# 例如在验证用户时,如果你的代码写的不够严谨,会导致攻击都构造特殊值来饶过验证
	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'
		);
		# variables_order 是定义在 php.ini 文件中的 (EGPCS)
		$_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))
				{
					# 将这些全局数组全部清空,通过 $_GET , $_POST 这种方式获取
					$GLOBALS[$var] = NULL;
				}
			}
		}
	}
}
  1. 注册 CI 异常处理函数
# 设置用户自定义的错误处理函数
set_error_handler('_error_handler');
# 设置用户自定义的异常处理函数
set_exception_handler('_exception_handler');
# 注册一个会在php中止时执行的函数
register_shutdown_function('_shutdown_handler');
  1. 配置扩展类的前缀(默认是 MY_)
if ( ! empty($assign_to_config['subclass_prefix']))
{
	get_config(array('subclass_prefix' => $assign_to_config['subclass_prefix']));
}
  1. 加载 Composer 的 自动加载文件 autoload.php
if ($composer_autoload = config_item('composer_autoload'))
{
	if ($composer_autoload === TRUE)
	{
		file_exists(APPPATH.'vendor/autoload.php')
			? require_once(APPPATH.'vendor/autoload.php')
			: log_message('error', '$config[\'composer_autoload\'] is set to TRUE but '.APPPATH.'vendor/autoload.php was not found.');
	}
	elseif (file_exists($composer_autoload))
	{
		require_once($composer_autoload);
	}
	else
	{
		log_message('error', 'Could not find the specified $config[\'composer_autoload\'] path: '.$composer_autoload);
	}
}
  1. 注册程序计时器
$BM =& load_class('Benchmark', 'core');
$BM->mark('total_execution_time_start');
$BM->mark('loading_time:_base_classes_start');
  1. 实例化钩子类并判断是否存在钩子 pre_system
$EXT =& load_class('Hooks', 'core');
$EXT->call_hook('pre_system');
  1. 实例化框架核心 Config 类并判断$assign_to_config
$CFG =& load_class('Config', 'core');

// Do we have any manually set config items in the index.php file?
if (isset($assign_to_config) && is_array($assign_to_config))
{
	foreach ($assign_to_config as $key => $value)
	{
		$CFG->set_item($key, $value);
	}
}

要写的太多啦!!!下一篇继续一起看哦~~

发布了8 篇原创文章 · 获赞 0 · 访问量 169

猜你喜欢

转载自blog.csdn.net/weixin_43950095/article/details/104544095