多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php框架 > codeigniter > CodeIgniter core/Config.php

CodeIgniter core/Config.php

來源:程序員人生   發布時間:2014-09-18 02:21:01 閱讀次數:3964次
<?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 */
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: free性vido另类重口 | 午夜免费 | 欧美最猛黑人xxxx黑人猛交3p | 亚洲网站在线免费观看 | 自拍 欧美| 精品视频一区二区三区四区五区 | 欧美亚洲国产片在线观看 | 欧美极品videossex激情 | 国产丰满眼镜女在线观看 | 浴室边摸边脱边吃奶边做视频 | 色综合一本到久久亚洲91 | 久久久免费精品视频 | h网站国产 | 日本特黄特黄刺激大片免费 | 国产呦精品一区二区三区网站 | 亚洲一区二区三区不卡视频 | 欧美国产亚洲精品高清不卡 | 欧美视频精品 | 国产美女视频一区二区二三区 | 美国毛片一级视频在线aa | 国产中文字幕在线 | jiizzyou欧美18| 亚洲欧美卡通成人制服动漫 | 免费精品美女久久久久久久久久 | 波多野结衣在线播放视频 | 欧美激情免费a视频 | 福利视频一区二区微拍堂 | 视频在线视频免费观看 | 亚洲一区色图 | 欧美视频不卡一区二区三区 | 致命坏男人漫画登录页面免费漫画第三话 | 福利片在线观看免费高清 | 亚州精品视频 | 欧美18av| 国产丝袜一区二区三区在线观看 | 亚洲三级小视频 | 秋霞理论一级在线观看手机版 | 欧美一级特黄特黄毛片 | 在线a人片免费观看不卡 | 成人五月网| 欧美jizz18性欧美 |