国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php框架 > codeigniter > CodeIgniter core/Config.php

CodeIgniter core/Config.php

来源:程序员人生   发布时间:2014-09-18 02:21:01 阅读次数:3495次
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package CodeIgniter
 * @author ExpressionEngine Dev Team
 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license http://codeigniter.com/user_guide/license.html
 * @link http://codeigniter.com
 * @since Version 1.0
 * @filesource
 */


// ------------------------------------------------------------------------
//配置类
/**
 * CodeIgniter Config Class
 *
 * This class contains functions that enable config files to be managed
 *
 * @package CodeIgniter
 * @subpackage Libraries
 * @category Libraries
 * @author ExpressionEngine Dev Team
 * @link http://codeigniter.com/user_guide/libraries/config.html
 */
class CI_Config {


/**
* List of all loaded config values
*
* @var array
*/
//配置文件中的参数
var $config = array();
/**
* List of all loaded config files
*
* @var array
*/
//加载的文件
var $is_loaded = array();
/**
* List of paths to search when trying to load a config file
*
* @var array
*/
//配置文件的路径
var $_config_paths = array(APPPATH);



/**
* Constructor
*
* Sets the $config data from the primary config.php file as a class variable
*
* @access   public
* @param   string the config file name
* @param   boolean  if configuration values should be loaded into their own section
* @param   boolean  true if errors should just return false, false if an error message should be displayed
* @return  boolean  if the file was successfully loaded or not
*/
//设置config中的base_url
function __construct()
{
//得到config下的config.php中的配置信息
$this->config =& get_config();
//日志记录
log_message('debug', "Config Class Initialized");


// Set the base_url automatically if none was provided
//在config/config.php里面有个配置项是base_url,它并不是必须配置项,
//如果没有配置,则系统就在这个地方,自己去它进行赋值。
if ($this->config['base_url'] == '')
{
//一般来说,如果通过http访问网站的话,这个值都会有的。
if (isset($_SERVER['HTTP_HOST']))
{
//判断是否通过https方式访问。
$base_url = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off' ? 'https' : 'http';
$base_url .= '://'. $_SERVER['HTTP_HOST'];
//去掉文件名部分。
$base_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}


else
{
//如果发现没有$_SERVER['HTTP_HOST'],则直接设置为localhost
$base_url = 'http://localhost/';
}
//设置 base_url
//保存到base_url中,以后像辅助函数uri_helper就可以通过base_url()调用出Config组件此值。
$this->set_item('base_url', $base_url);
}
}


// --------------------------------------------------------------------


/**
* Load Config File
*
* @access public
* @param string the config file name
* @param   boolean  if configuration values should be loaded into their own section
* @param   boolean  true if errors should just return false, false if an error message should be displayed
* @return boolean if the file was loaded correctly
*/
/**
 * Load Config File
 * 先解释一下load方法的参数,$file就是配置文件名。配置文件目录一般为应用目录(application)/config/下
 * 下面会有很多个针对不同方面配置的文件,而我们通过Config组件加载的配置信息都会保存在Config::$config这个
 * 属性里面,所以第二个参数$use_sections就是设置是否当前配置文件是否以独立一个数组的形式充当Config::$config
 * 的一个元素加入,如果为true,则$config是一个两层的数组,如果为false,则单纯将配置文件里面的配置信息合并。
 * 例如配置文件abc.php,如果为true,则会以$config['abc']['xxx']的形式保存,否则直接合并即会有
 * $config['xxx']。
 * 第三个参数只是设置要不要报错而已,如果为true,则只会返回false,如果为false则直接在函数执行时报错。
*/
//判断是否加载
function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
{
//接下来这一行代码是为了方便我们调用的时候既可以以xxx.php的形式传参,也可以只以xxx(无后缀)的形式。
//另外如果$file为空,则默认是加载config.php
$file = ($file == '') ? 'config' : str_replace('.php', '', $file);
$found = FALSE;
$loaded = FALSE;


//判断是否有自习写的配置文件
$check_locations = defined('ENVIRONMENT')
? array(ENVIRONMENT.'/'.$file, $file)
: array($file);

//加载配置文件
//这个$this->_config_paths默认只有应用目录application/
foreach ($this->_config_paths as $path)
{
//分别从某特定环境的配置目录和默认的配置目录里面寻找。
foreach ($check_locations as $location)
{
$file_path = $path.'config/'.$location.'.php';
//如果文件已经加载过,跳出最上层的foreach循环
//值为 TRUE 则 in_array() 函数还会检查 $file_path 的类型是否和 $_is_loaded中的相同。
if (in_array($file_path, $this->is_loaded, TRUE))
{
$loaded = TRUE;
continue 2;
}
//没有加载,文件存在,跳出第二个foreach循环
if (file_exists($file_path))
{
//如果是已经加载过了,那么在Config::$config里面理应当有,所以直接跳出最外层循环。
$found = TRUE;
break;
}
}
//$found是用于判断在此$path里面,遍历上面的$check_locations有没有找到
    //而$load则是用于判断两层遍历以后,最终有没有把配置文件加载进来。
if ($found === FALSE)
{
continue;
}
//引入文件
//配置文件就是在这个地方加载的
include($file_path);

//下面这句可以看出,我们在包含的配置文件里面必须要有名为$config的数组。
    //如果配置信息格式不合法,看情况($$fail_gracefully的作用)处理错误
if ( ! isset($config) OR ! is_array($config))
{
if ($fail_gracefully === TRUE)
{
return FALSE;
}
show_error('Your '.$file_path.' file does not appear to contain a valid configuration array.');
}
//是否加入自己的配置参数信息
//下面就是$use_sections的作用,根据它来规定当前加载的配置信息的保存形式。
if ($use_sections === TRUE)
{
if (isset($this->config[$file]))
{
$this->config[$file] = array_merge($this->config[$file], $config);
}
else
{
$this->config[$file] = $config;
}
}
else
{
$this->config = array_merge($this->config, $config);
}
//保存哪些文件已经加载过,下次再调用此load方法的时候,通过它来避免重复加载,减少不必要的操作。
$this->is_loaded[] = $file_path;

unset($config);


$loaded = TRUE;
log_message('debug', 'Config file loaded: '.$file_path);
break;
}
//找不到配置文件
//加载失败,按情况处理错误。
if ($loaded === FALSE)
{
if ($fail_gracefully === TRUE)
{
return FALSE;
}
show_error('The configuration file '.$file.'.php does not exist.');
}


return TRUE;
}


// --------------------------------------------------------------------


/**
* Fetch a config file item
*
*
* @access public
* @param string the config item name
* @param string the index name
* @param bool
* @return string
* 取得某一配置项的内容,如果知道上面Config::load($file, $use_sections, $fail_gracefully);方法
  * 中$use_sections的意义的话,那个下面的$index意义就很容易理解了。
*/
//取出配置项的值
function item($item, $index = '')
{
//取出系统默认的index.php配置项的值
if ($index == '')
{
if ( ! isset($this->config[$item]))
{
return FALSE;
}


$pref = $this->config[$item];
}
//取出用户增加的配置文件中的配置项的值
else
{
if ( ! isset($this->config[$index]))
{
return FALSE;
}


if ( ! isset($this->config[$index][$item]))
{
return FALSE;
}


$pref = $this->config[$index][$item];
}


return $pref;
}


// --------------------------------------------------------------------


/**
* Fetch a config file item - adds slash after item (if item is not empty)
*
* @access public
* @param string the config item name
* @param bool
* @return string
*/
//在config[$item]不为空的情况下,在最后面增加'/'
//此方法仅仅是对配置信息进行一些修剪处理而已。
function slash_item($item)
{
if ( ! isset($this->config[$item]))
{
return FALSE;
}
//如果此配置项仅仅是包含一些对配置无效的字符,则直接返回空。
if( trim($this->config[$item]) == '')
{
return '';
}
//保证以一条/结尾。
return rtrim($this->config[$item], '/').'/';
}


// --------------------------------------------------------------------


/**
* Site URL
* Returns base_url . index_page [. uri_string]
*
* @access public
* @param string the URI string
* @return string
*/
//返回用于访问的url值:127.0.0.1/ci/index.php/$url.item('url_suffix')
//我们经常通过url_helper的site_url获得我们在项目中想要的路径,其实真正执行的是Config::site_url()这个方法。
function site_url($uri = '')
{
//$uri参数实质可以是数组的
if ($uri == '')
{
return $this->slash_item('base_url').$this->item('index_page');
}
//根据当前的路由格式返回相应的uri_string
if ($this->item('enable_query_strings') == FALSE)
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
}
else
{
return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
}
}


// -------------------------------------------------------------


/**
* Base URL
* Returns base_url [. uri_string]
*
* @access public
* @param string $uri
* @return string
*/
//返回base_url
function base_url($uri = '')
{
return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
}


// -------------------------------------------------------------


/**
* Build URI string for use in Config::site_url() and Config::base_url()
*
* @access protected
* @param  $uri
* @return string
*/
 /**
  * 按当前规定路由格式,返回正确的uri_string.
  * 主要是如果当参数$uri是数组的时候,依此拼接到后面,有'/'连接
 */
protected function _uri_string($uri)
{
if ($this->item('enable_query_strings') == FALSE)
{
if (is_array($uri))
{
$uri = implode('/', $uri);
}
$uri = trim($uri, '/');
}
else
{
if (is_array($uri))
{
$i = 0;
$str = '';
foreach ($uri as $key => $val)
{
$prefix = ($i == 0) ? '' : '&';
$str .= $prefix.$key.'='.$val;
$i++;
}
$uri = $str;
}
}
   return $uri;
}


// --------------------------------------------------------------------


/**
* System URL
*
* @access public
* @return string
*/
  //拿到系统目录的路径而已。
   //正则部分是首先去掉BASEPATH中多余重复的“/”,然后再拆分为数组。最后通过end()函数来拿到系统目录名。
function system_url()
{
$x = explode("/", preg_replace("|/*(.+?)/*$|", "1", BASEPATH));
return $this->slash_item('base_url').end($x).'/';
}


// --------------------------------------------------------------------


/**
* Set a config file item
*
* @access public
* @param string the config item key
* @param string the config item value
* @return void
*/
//对配置文件中的配置项设置值
//也可以增加配置属性
function set_item($item, $value)
{
$this->config[$item] = $value;
}


// --------------------------------------------------------------------


/**
* Assign to Config
*
* This function is called by the front controller (CodeIgniter.php)
* after the Config class is instantiated.  It permits config items
* to be assigned or overriden by variables contained in the index.php file
*
* @access private
* @param array
* @return void
*/
/**
 * 下面这个方法在CodeIgniter.php中调用过,是为把在index.php里设置的配置信息交给Config组件。 
 * 实质也是通过上面的Config::set_item();方法设置。
 */
//覆盖config文件中的配置项的值
//私有方法,只能在类的内部访问。
function _assign_to_config($items = array())
{
if (is_array($items))
{
foreach ($items as $key => $val)
{
$this->set_item($key, $val);
}
}
}
}


// END CI_Config class


/* End of file Config.php */
/* Location: ./system/core/Config.php */
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生