CodeIgniter 核心代碼閱讀-配置文件Config.php

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();
		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);
		}
	}

	//加載配置文件
	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;
	}

	//獲取一個配置
	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 slash_item($item)
	{
		if ( ! isset($this->config[$item]))
		{
			return FALSE;
		}
		if( trim($this->config[$item]) == '')
		{
			return '';
		}

		return rtrim($this->config[$item], '/').'/';
	}

	//該函數得到你網站的 URL,其中包含了你在 config 文件中設置的 "index" 的值。
	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);
		}
	}

	//該函數返回站點的根 URL,可以在這個函數後拼接一個 URL 路徑,用以生成 CSS 或圖片文件的 URL。
        //以上兩個函數一般通過URL 輔助函數中相應的函數(site_url() 和 base_url())調用。
	function base_url($uri = '')
	{
		return $this->slash_item('base_url').ltrim($this->_uri_string($uri), '/');
	}

	//構造URI字符串,生成site_url和base_url
	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;
	}

	//系統url
	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;
	}

	//在CodeIgniter.php中調用的方法,將index.php中的配置設置到config數組中
        function _assign_to_config($items = array())
	{
		if (is_array($items))
		{
			foreach ($items as $key => $val)
			{
				$this->set_item($key, $val);
			}
		}
	}
默認情況下,CodeIgniter已經有一個主要的配置文件,位於application/config/config.php。如果你用文本編輯器打開你會看到配置項目被存儲在一個叫$config的數組裏。

您可以添加您自己的配置項目到這個文件裏,或者您更願意讓您自己的配置項目與原配置項目分開(assuming you even need config items),簡單的創建一個文件並保存到config這個文件夾裏就行了。

提示: 如果你想建立一個和主要配置文件一樣格式的配置文件,把你的配置項目建立在一個名爲$config的數組中,即使配置文件中有相同的數組名,CodeIgniter也能智能的管理這些文件而不會發生衝突.

你可以根據當前的開發/服務環境讀取不同的配置文件. ENVIRONMENT 常量在 index.php 定義, 在 處理多環境 一章有詳細的描述和說明.

一個特定環境的配置文件, 需要按此路徑創建或者複製一個文件: application/config/{ENVIRONMENT}/{FILENAME}.php

例如,要創建一個僅'production'環境的 config.php, 你應該:

創建文件夾:application/config/production/
複製現有的 config.php 到上面的文件夾
編輯 application/config/production/config.php 設定你在'production'環境所需的設置
當你設置 ENVIRONMENT 常量爲 'production', 你剛創建的僅'production'環境的 config.php 配置文件將被加載。

你可以放置以下配置文件到特定環境的文件夾:

默認的 CodeIgniter 配置文件
你自己的用戶配置文件
注意: CodeIgniter 會先嚐試加載當前環境的配置文件。 如果文件不存在,將加載全局配置文件,例如 (application/config/) 。 這意味着你沒有必要把 所有的配置文件放在特定環境的文件夾裏, − 僅僅把各環境不同的放進去就行了。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章