文件地址:./system/core/Config.php
主要作用:管理配置
1.成員屬性$config所有已加載配置的值的列表
2.成員屬性$is_loaded所有加載配置文件的列表
3.成員屬性$_config_paths當需要加載配置文件的時候搜索路徑的列表
4.__construct()構造方法程序會首先自動執行這個方法
它所做的內容主要有兩個a)獲取配置賦值給成員屬性$config
b)設置配置中的base_url
5.load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
加載配置文件
$file是你自定義的配置文件的文件名,這個文件名沒有 .php 的擴展名.?
$use_sections如果你需要加載多個自定義配置文件,一般情況下它們會被合并成一個數組。然而,如果在不同的配置文件中存在同名的索引,那么會發生沖突。為了避免這個問題,你可以把第二個參數設置為 TRUE ,這可以使每個配置文件的內容存儲在一個單獨的數組中,數組的索引就是配置文件的文件名。
$fail_gracefully允許屏蔽當配置文件不存在時產生的錯誤信息:
(0)過濾并設置$file 變量
(1)初始化$founf 為FALSE 用于判斷文件是否存在
(2)初始化$loaded為FALSE 用于判斷文件是否被加載
(3)檢測文件路徑如果有環境變量添加環境變量
(4)進入foreach遍歷文件路徑,并查找文件是否被加載和存在
(5)如果文件不存在跳出foreach循環
(6)加載文件
(7)判斷$config是否存在,這個$config 應該是加載的文件中定義的
(8)判斷$use_sections 如果為真則將使每個配置文件的內容存儲在一個單獨的數組中,數組的索引就是配置文件的文件名。
如果為假則所有配置文件會被合并成一個數組
(9)將文件添加進is_loaded數組中,并銷毀$config
(10)$loaded 設置為true log記錄 跳出循環
(11)如果loaded為false并且$fail_gracefully不等于true 顯示錯誤日志
6.item() 獲取一個配置項
$item 配置項的名字
$index如果配置是一個數組的時候,這一項是數組索引名字
(1)判斷index是否為空
(2)如果index為空,判斷$this->config[$item]是否存在,如果不存在返回,存在,賦值給$pref
(3)如果index不為空,判斷$this->config[$index]是否存在,判斷$this->config[$index][$item]是否存在,將$this->config[$index][$item]賦值給$pref
(4)返回$pref
7.slash_item()獲取一個配置項并添加/
$item配置項的名字
(1)判斷配置項是否存在不存在返回false
(2)判斷配置項是否為空如果是空返回''
(3)在配置值的后面添加/并返回。
8.site_url()該函數得到你網站的 URL,其中包含了你在 config 文件中設置的 "index" 的值。
$uriuri字符串就是訪問路徑所帶的參數
(1) 如果$uri = '' 返回由base_url和index_page組成的url
(2) 判斷$this->item('enable_query_strings')真假,并返回不同形式的地址。(這一項是在application/config/config.php文件中配置的。用來區分傳參方式,如果為false就是默認的傳參方式example.com/who/what/where/。如果為true就是 example.com/index.php?c=controller&m=function這樣的傳參方式。)
9.base_url()該函數返回站點的根 URL,可以在這個函數后拼接一個 URL 路徑,用以生成 CSS 或圖片文件的 URL。
10._uri_string() 構建uri串讓site_url(),base_url()兩個函數使用。
11.system_url()該函數得到 system 文件夾的 URL。
12.set_item() ?設置一個配置項
13._assign_to_config() 設置多個配置項(以數組的形式key是要設置的配置項的名字,value 是配置項的值)
源碼:
~~~
<?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
*/
function __construct()
{
$this->config =& get_config();
log_message('debug', "Config Class Initialized");
// Set the base_url automatically if none was provided 假如
if ($this->config['base_url'] == '')
{
if (isset($_SERVER['HTTP_HOST']))
{
$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
{
$base_url = 'http://localhost/';
}
$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
*/
function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
{
$file = ($file == '') ? 'config' : str_replace('.php', '', $file);
$found = FALSE;
$loaded = FALSE;
$check_locations = defined('ENVIRONMENT')
? array(ENVIRONMENT.'/'.$file, $file)
: array($file);
foreach ($this->_config_paths as $path)
{
foreach ($check_locations as $location)
{
$file_path = $path.'config/'.$location.'.php';
if (in_array($file_path, $this->is_loaded, TRUE))
{
$loaded = TRUE;
continue 2;
}
if (file_exists($file_path))
{
$found = TRUE;
break;
}
}
if ($found === FALSE)
{
continue;
}
include($file_path);
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.');
}
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);
}
$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
*/
function item($item, $index = '')
{
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
*/
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
*/
function site_url($uri = '')
{
if ($uri == '')
{
return $this->slash_item('base_url').$this->item('index_page');
}
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
*/
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
*/
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
*/
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
*/
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 */
~~~