ci源码分析--libraries/config
<?phpif ( ! defined('BASEPATH')) exit('不允许直接访问');
class Config {
var $config = array();
var $is_loaded = array();
function Config()
{
//自动加载配置文件 APPPATH/config/config.php
$this->config =& get_config();
log_message('debug', "配置类初始化成功");
}
//手工加载配置文件
//参数1:配置文件名,参数2:是否把配置单独存入一个数组(为了分开多个配置文件),参数3:加载不成功,是否显示错误
function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
{
//默认加载config.php
$file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
//如果已经载入了配置文件,直接返回
if (in_array($file, $this->is_loaded, TRUE))
{
return TRUE;
}
//检查配置文件是否存在
if ( ! file_exists(APPPATH.'config/'.$file.EXT))
{
//如果加载不成功不显示错误
if ($fail_gracefully === TRUE)
{
return FALSE;
}
//显示错误
show_error('配置文件不存在.');
}
//载入配置文件
include(APPPATH.'config/'.$file.EXT);
//判断配置文件格式是否正确
if ( ! isset($config) OR ! is_array($config))
{
//如果加载不成功不显示错误
if ($fail_gracefully === TRUE)
{
return FALSE;
}
//显示错误
show_error('你的配置文件格式不正确.');
}
//读出配置文件到指定数组
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;
//释放变量
unset($config);
log_message('debug', '载入配置文件: config/'.$file.EXT);
return TRUE;
}
//获取单一配置元素
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;
}
//设置一项元素
function set_item($item, $value)
{
$this->config[$item] = $value;
}
//得到你网站的URL
function site_url($uri = '')
{
if (is_array($uri))
{
$uri = implode('/', $uri);
}
if ($uri == '')
{
return $this->_slash_item('base_url').$this->item('index_page');
}
else
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return $this->_slash_item('base_url').$this->_slash_item('index_page').preg_replace("|^/*(.+?)/*$|", "\\1", $uri).$suffix;
}
}
//得到你系统的URL
function system_url()
{
$x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
return $this->_slash_item('base_url').end($x).'/';
}
//私有函数,用于取出配置文件项目并格式化
function _slash_item($item)
{
if ( ! isset($this->config[$item]))
{
return FALSE;
}
$pref = $this->config[$item];
if ($pref != '' && substr($pref, -1) != '/')
{
$pref .= '/';
}
return $pref;
}
}
?>
辛苦了。。呵呵。。最好再做一个汇总,以方便大家查看 就是做个索引贴呗,等我都弄完了就做 留个影子 能告诉我~
preg_replace("|/*(.+?)/*$|", "\1", BASEPATH)
它替换了什么么?为什么我觉得他什么都没替换? 顶顶,正在看CI源码,帮且很大
页:
[1]