codeigniter源代码分析之配置类Config.php

Config.php 主要是对CI中的各种config进行的操作

CI中写的是对 APPPATH.'/config/' 这个文件夹里面的各种config文件 默认是config.php文件

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Config {
	var $config = array();
	var $is_loaded = array();
	var $_config_paths = array(APPPATH);
	
	function __construct()
	{
		$this->config =& get_config();//加载config配置数组
		log_message('debug', "Config Class Initialized");
		// 设置base_url
		if ($this->config['base_url'] == '')
		{
			if (isset($_SERVER['HTTP_HOST']))
			{
				// 这个是取得请求的scheme
				$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);
		}
	}
	// 可加载APPPATH/config/ 中的任意配置文件
	function load($file = '', $use_sections = FALSE, $fail_gracefully = FALSE)
	{
		// 要加载的配置文件名称 默认为config
		$file = ($file == '') ? 'config' : str_replace('.php', '', $file);
		$found = FALSE;//找到标志
		$loaded = FALSE;//加载过标志
		// 通过上面的file变量形成 check_locations 本地要检查的路径
		$check_locations = defined('ENVIRONMENT')?array(ENVIRONMENT.'/'.$file, $file):array($file);
		foreach ($this->_config_paths as $path) // _config_path = APPPATH
		{
			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);//加载文件
			// 加载的指定配置文件里面没有数组 $config
			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数组中
				$this->config = array_merge($this->config, $config);
			}

			$this->is_loaded[] = $file_path;//标志已加载文件
			unset($config);//clean 加载文件的config copy

			$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;
	}
	// 取得其中项
	function item($item, $index = '')
	{
		if ($index == '')
		{
			if ( ! isset($this->config[$item]))
			{
				return FALSE;//请求的config不存在
			}

			$pref = $this->config[$item];
		}
		else
		{
			// 带有 index 索引
			if ( ! isset($this->config[$index]))
			{
				return FALSE;
			}

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

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

		return $pref;
	}
	function slash_item($item)
	{
		if ( ! isset($this->config[$item]))
		{
			return FALSE;
		}
		if( trim($this->config[$item]) == '')
		{
			return '';
		}

		return rtrim($this->config[$item], '/').'/';
	}
	function site_url($uri = '')
	{
		if ($uri == '')
		{
			return $this->slash_item('base_url').$this->item('index_page');//base_url+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;
			// base_url+index_page+uri_string+suffix
		}
		else
		{
			return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
		}
	}
	function base_url($uri = '')
	{
		return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
	}
	// 根据参数构造URI String
	protected function _uri_string($uri)
	{
		if ($this->item('enable_query_strings') == FALSE)
		{
			// 不允许query类型的url
			if (is_array($uri))
			{
				$uri = implode('/', $uri);
			}
			$uri = trim($uri, '/');
		}
		else
		{
			if (is_array($uri))
			{
				// 可以拼接之后将字符串开头的'&'去掉
				// foreach($uri as $key=>$val)
				// {
				// 	$str.='&'.$key.'='.$val;
				// }
				// $uri = ltrim($str,'&');
				$i = 0;
				$str = '';
				foreach ($uri as $key => $val)
				{
					$prefix = ($i == 0) ? '' : '&';
					$str .= $prefix.$key.'='.$val; // &key=val
					$i++;
				}
				$uri = $str;
			}
		}
	    return $uri;
	}
	// 系统目录
	function system_url()
	{
		$x = explode("/", preg_replace("|/*(.+?)/*$|", "\\1", BASEPATH));
		return $this->slash_item('base_url').end($x).'/';
	}
	function set_item($item, $value)
	{
		$this->config[$item] = $value;
	}
	// 对配置项进行覆盖
	function _assign_to_config($items = array())
	{
		if (is_array($items))
		{
			foreach ($items as $key => $val)
			{
				$this->set_item($key, $val);
			}
		}
	}
}

个人觉得这个类设计的并不是太合理:

不过还是首先说下优点把:

load方法设计的不错  整个文件加载的过程处理 一个出现重名冲突时候数组的合并

然后很多关于url的方法觉得应该写在 URI 类里面的,然后在Config可以去调去他们写到conig静态变量里面


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值