Commits

Phil Sturgeon committed ed1e87b Merge
  • Participants
  • Parent commits 68da135, c4314a5

Comments (0)

Files changed (8)

application/config/config.php

File contents unchanged.
 
 /*
  *---------------------------------------------------------------
- * PHP ERROR REPORTING LEVEL
+ * APPLICATION ENVIRONMENT
  *---------------------------------------------------------------
  *
- * By default CI runs with error reporting set to ALL.  For security
- * reasons you are encouraged to change this to 0 when your site goes live.
- * For more info visit:  http://www.php.net/error_reporting
+ * You can load different configurations depending on your
+ * current environment. Setting the environment also influences
+ * things like logging and error reporting.
+ *
+ * This can be set to anything, but default usage is:
+ *
+ *     development
+ *     testing
+ *     production
+ *
+ * NOTE: If you change these, also change the error_reporting() code below
  *
  */
-	error_reporting(E_ALL);
+	define('ENVIRONMENT', 'development');
+/*
+ *---------------------------------------------------------------
+ * ERROR REPORTING
+ *---------------------------------------------------------------
+ *
+ * Different environments will require different levels of error reporting.
+ * By default development will show errors but testing and live will hide them.
+ */
+
+	switch (ENVIRONMENT)
+	{
+		case 'development':
+			error_reporting(E_ALL);
+		break;
+	
+		case 'testing':
+		case 'production':
+			error_reporting(0);
+		break;
+
+		default:
+			exit('The application environment is not set correctly.');
+	}
 
 /*
  *---------------------------------------------------------------
  * as this file.
  *
  */
-	$system_path = "system";
+	$system_path = 'system';
 
 /*
  *---------------------------------------------------------------
  * NO TRAILING SLASH!
  *
  */
-	$application_folder = "application";
+	$application_folder = 'application';
 
 /*
  * --------------------------------------------------------------------
 // END OF USER CONFIGURABLE SETTINGS.  DO NOT EDIT BELOW THIS LINE
 // --------------------------------------------------------------------
 
-
-
-
 /*
  * ---------------------------------------------------------------
  *  Resolve the system path for increased reliability

system/core/Common.php

 			return $_config[0];
 		}
 
+		$file_path = APPPATH.'config/'.ENVIRONMENT.'/config'.EXT;
+
 		// Fetch the config file
-		if ( ! file_exists(APPPATH.'config/config'.EXT))
+		if ( ! file_exists($file_path))
 		{
-			exit('The configuration file does not exist.');
+			$file_path = APPPATH.'config/config'.EXT;
+			
+			if ( ! file_exists($file_path))
+			{
+				exit('The configuration file does not exist.');
+			}
 		}
-		else
-		{
-			require(APPPATH.'config/config'.EXT);
-		}
+	
+		require($file_path);
 
 		// Does the $config array exist in the file?
 		if ( ! isset($config) OR ! is_array($config))

system/core/Config.php

 	 *
 	 * @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)
 		$loaded = FALSE;
 
 		foreach($this->_config_paths as $path)
-		{
-			$file_path = $path.'config/'.$file.EXT;
+		{			
+			$file_path = $path.'config/'.ENVIRONMENT.'/'.$file.EXT;
 
 			if (in_array($file_path, $this->is_loaded, TRUE))
 			{
 				continue;
 			}
 
-			if ( ! file_exists($path.'config/'.$file.EXT))
+			if ( ! file_exists($file_path))
 			{
-				continue;
+				log_message('debug', 'Config for '.ENVIRONMENT.' environment is not found. Trying global config.');
+				$file_path = $path.'config/'.$file.EXT;
+				
+				if ( ! file_exists($file_path))
+				{
+					continue;
+				}
 			}
-
+			
 			include($file_path);
 
 			if ( ! isset($config) OR ! is_array($config))
 			{
 				return FALSE;
 			}
-			show_error('The configuration file '.$file.EXT.' does not exist.');
+			show_error('The configuration file '.ENVIRONMENT.'/'.$file.EXT.' and '.$file.EXT.' do not exist.');
 		}
-
+		
 		return TRUE;
 	}
 

system/core/Loader.php

 				foreach ($config_component->_config_paths as $path)
 				{
 					// We test for both uppercase and lowercase, for servers that
-					// are case-sensitive with regard to file names
-					if (file_exists($path .'config/'.strtolower($class).EXT))
+					// are case-sensitive with regard to file names. Check for environment
+					// first, global next
+					if (file_exists($path .'config/'.ENVIRONMENT.'/'.strtolower($class).EXT))
+					{
+						include_once($path .'config/'.ENVIRONMENT.'/'.strtolower($class).EXT);
+						break;
+					}
+					elseif (file_exists($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).EXT))
+					{
+						include_once($path .'config/'.ENVIRONMENT.'/'.ucfirst(strtolower($class)).EXT);
+						break;
+					}
+					elseif (file_exists($path .'config/'.strtolower($class).EXT))
 					{
 						include_once($path .'config/'.strtolower($class).EXT);
 						break;

system/database/DB.php

 	// Load the DB config file if a DSN string wasn't passed
 	if (is_string($params) AND strpos($params, '://') === FALSE)
 	{
-		include(APPPATH.'config/database'.EXT);
+		
+		$file_path = APPPATH.'config/'.ENVIRONMENT.'/database'.EXT;
+		
+		if ( ! file_exists($file_path))
+		{
+			log_message('debug', 'Database config for '.ENVIRONMENT.' environment is not found. Trying global config.');
+			$file_path = APPPATH.'config/database'.EXT;
+			
+			if ( ! file_exists($file_path))
+			{
+				continue;
+			}
+		}
+		
+		include($file_path);
 
 		if ( ! isset($db) OR count($db) == 0)
 		{

user_guide/database/configuration.html

 <h1>Database Configuration</h1>
 
 <p>CodeIgniter has a config file that lets you store your database connection values (username, password, database name, etc.).
-The config file is located at:</p>
-
-<p><kbd>application/config/database.php</kbd></p>
+The config file is located at <samp>application/config/database.php</samp>. You can also set database connection values for specific <a href="../libraries/config.html">environments</a> by placing <strong>database.php</strong> it the respective environment config folder.</p>
 
 <p>The config settings are stored in a multi-dimensional array with this prototype:</p>
 

user_guide/libraries/config.html

 
 <p>Where <var>item_name</var> is the $config array index you want to change, and <var>item_value</var> is its value.</p>
 
+<h2>Environments</h2>
+
+<p>You can set the environment of you application and load config items depending on the current environment. It also disables PHP from displaying errors in environments other than development. To set your environment, open <strong>index.php</strong>, located at the root and change the <var>ENVIRONMENT</var> constant. By default, there is support for a development, test and production environment.</p>
+	
+<code>
+define('<var>ENVIRONMENT</var>', '<var>development</var>');	
+</code>
+
+<p>To make a config file environment-aware, copy the file from <samp>application/config/</samp> to <samp>application/config/development/</samp>, depending on the environment the config file belongs to. You can place the following configuration files in environment folders:</p>
+	
+<ul>
+<li>Default config files</li>
+<li>Database config files</li>
+<li>Custom config files</li>
+</ul>
+
+<p><strong>Note:</strong> CodeIgniter always tries to load the config file for the current environment first. If the file does not exist, the global config file (i.e. <samp>application/config/</samp>) is loaded. This means you are not obligated to place <strong>all</strong> your config files (but rather the files that change per environment) in an environment folder.</p>
 
 <h2>Helper Functions</h2>