-
Notifications
You must be signed in to change notification settings - Fork 0
ConfigSectionsPatch
This is a patch for the config::load() behaviour that applies to 1.3.0 to 1.3.3 of Code Igniter:
Adds another parameter $use_sections to load() to load a config file into its own section. If false, it loads into the 'global' config values like normal. If true, it creates a new global value based on the name of the file, which is an array containing the contents of the config file. Default is to not use sections.
Example:
config/myconfig.php: [code] $config['foo'] = 'bar'; $config['log_path'] = '/some/path'; [/code]
Normal way: [code] $this->config->load('myconfig'); $value = $this->config->item('foo'); [/code]
With sections: [code] $this->config->load('myconfig', true); $cfg = $this->config->item('myconfig'); $value = $cfg['foo']; [/code]
The 3rd parameter, $fail_gracefully, controls if a CI error page is displayed if the file cannot be loaded or doesn't contain the config array (if false), or if it just returns true or false depending on if it was successful or not (true). Default is to display a CI error message and halt processing of the rest of the application.
[h3]The Patch:[/h3]
Here is the new load() function, for system/libraries/config.php:
[code] /** * 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 successfully loaded or not */ function load($file = '', $use_sections = false, $fail_gracefully = false) { $file = ($file == '') ? 'config' : str_replace(EXT, '', $file);
if (in_array($file, $this->is_loaded))
{
return true;
}
if (!file_exists(APPPATH.'config/'.$file.EXT)) {
if ($fail_gracefully)
{
return false;
}
show_error('The configuration file '.$file.EXT.' does not exist.');
}
include_once(APPPATH.'config/'.$file.EXT);
if ( ! isset($config) OR ! is_array($config))
{
if ($fail_gracefully)
{
return false;
}
show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.');
}
if ($use_sections)
{
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 loaded: config/'.$file.EXT);
return true;
}
// END load()
[/code]
Or if you prefer, a unified diff version (you can apply by saving it as 'config.patch', then running 'patch < config.patch' in the main directory (where system is a subdirectory):
[code] --- CodeIgniter_1.3.3/system/libraries/Config.php 2006-04-03 18:54:22.000000000 -0400 +++ system/libraries/Config.php 2006-05-12 15:46:13.000000000 -0400 @@ -53,30 +53,59 @@ * * @access public * @param string the config file name
-
* @return void
-
* @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 */
-
function load($file = '')
-
function load($file = '', $use_sections = false, $fail_gracefully = false) { $file = ($file == '') ? 'config' : str_replace(EXT, '', $file); if (in_array($file, $this->is_loaded)) {
-
return;
-
return true; }
-
if (!file_exists(APPPATH.'config/'.$file.EXT)) {
-
if ($fail_gracefully)
-
{
-
return false;
-
}
-
show_error('The configuration file '.$file.EXT.' does not exist.');
-
}
-
include_once(APPPATH.'config/'.$file.EXT); if ( ! isset($config) OR ! is_array($config)) {
-
if ($fail_gracefully)
-
{
-
return false;
-
} show_error('Your '.$file.EXT.' file does not appear to contain a valid configuration array.'); }
-
$this->config = array_merge($this->config, $config);
-
if ($use_sections)
-
{
-
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 loaded: config/'.$file.EXT);
-
return true; } // END load()
[/code]