Permalink
Browse files

Added recursive array merge to Config for pre-5.3

  • Loading branch information...
dchill42 committed Sep 1, 2011
1 parent 2524ea2 commit b9ec0ba8d331b2a3df208cf4374f3ed19981f1ea
Showing with 50 additions and 3 deletions.
  1. +50 −3 system/core/Config.php
View
@@ -43,6 +43,14 @@ class CI_Config {
*/
protected $is_loaded = array();
+ /**
+ * Callback to array merging function
+ *
+ * @var callback
+ * @access protected
+ */
+ protected $merge_arrays;
+
/**
* List of paths to search when trying to load a config file
*
@@ -60,6 +68,9 @@ public function __construct()
$this->config =& get_config();
log_message('debug', 'Config Class Initialized');
+ // Determine array merge function
+ $this->merge_arrays = is_php('5.3') ? 'array_replace_recursive' : array($this, '_merge_arrays');
+
// Set the base_url automatically if none was provided
if ($this->config['base_url'] == '')
{
@@ -227,7 +238,7 @@ public function get_ext($_file, $_name, &$_extras)
}
// Merge config and unset local
- $_merged = array_replace_recursive($_merged, $$_name);
+ $_merged = call_user_func($this->merge_arrays, $_merged, &$$_name);
unset($$_name);
}
}
@@ -340,11 +351,10 @@ public function site_url($uri = '')
* Base URL
* Returns base_url [. uri_string]
*
- * @access public
* @param string $uri
* @return string
*/
- function base_url($uri = '')
+ public function base_url($uri = '')
{
return $this->slash_item('base_url').ltrim($this->_uri_string($uri),'/');
}
@@ -436,6 +446,43 @@ public function _assign_to_config($items = array())
}
}
}
+
+ // --------------------------------------------------------------------
+
+ /**
+ * Merge config arrays recursively
+ *
+ * This function recursively merges the values from a new array into an existing array.
+ * It is a substitute for array_replace_recursive() in PHP < 5.3; accepting only two arrays.
+ * The main (existing) array is copied as a parameter, modified with the contents of
+ * the new array (which is referenced), and returned.
+ *
+ * @access protected
+ * @param array main array
+ * @param array new array of values to merge in
+ * @return array merged array
+ */
+ protected function _merge_arrays(array $main, array &$new)
+ {
+ // Iterate values of new array
+ foreach ($new as $key => &$value)
+ {
+ // Check for sub-arrays to merge
+ if (is_array($value) && isset($main[$key]) && is_array($main[$key]))
+ {
+ // Merge new sub-array with main sub-array
+ $main[$key] = $this->_merge_arrays($main[$key], $value);
+ }
+ else
+ {
+ // Add/replace value in main array
+ $main[$key] = $value;
+ }
+ }
+
+ // Return merged array
+ return $main;
+ }
}
// END CI_Config class

2 comments on commit b9ec0ba

Good fix! Supporting under 5.3 is a pain but one of the biggest draws for people to use this framework. i.e: portable as f**k.

Owner

dchill42 replied Sep 1, 2011

Thanks! I'm happy to support older versions of PHP5 - I just missed that array_replace_recursive() was 5.3+. I just uploaded the last tweak (which I missed in this commit), that condenses the code inside the _merge_arrays() loop with a ternary operator.

Please sign in to comment.