Просмотр исходного кода

ZF-6459: better recursive merging of options

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@15555 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 лет назад
Родитель
Сommit
bc855ec178

+ 24 - 1
library/Zend/Application.php

@@ -119,7 +119,7 @@ class Zend_Application
         $options = array_change_key_case($options, CASE_LOWER);
 
         if (!empty($options['config'])) {
-            $options = array_merge($options, $this->_loadConfig($options['config']));
+            $options = $this->mergeOptions($options, $this->_loadConfig($options['config']));
         }
         
         $this->_options = $options;
@@ -198,6 +198,29 @@ class Zend_Application
     }
 
     /**
+     * Merge options recursively
+     * 
+     * @param  array $array1 
+     * @param  mixed $array2 
+     * @return array
+     */
+    public function mergeOptions(array $array1, $array2 = null)
+    {
+        if (is_array($array2)) {
+            foreach ($array2 as $key => $val) {
+                if (is_array($array2[$key])) {
+                    $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
+                                  ? $this->mergeOptions($array1[$key], $array2[$key]) 
+                                  : $array2[$key];
+                } else {
+                    $array1[$key] = $val;
+                }
+            }
+        }
+        return $array1;
+    }
+
+    /**
      * Set PHP configuration settings
      * 
      * @param  array $settings 

+ 24 - 1
library/Zend/Application/Bootstrap/BootstrapAbstract.php

@@ -132,7 +132,7 @@ abstract class Zend_Application_Bootstrap_BootstrapAbstract
                 }
             }
         }
-        $this->_options = array_merge_recursive($this->_options, $options);
+        $this->_options = $this->mergeOptions($this->_options, $options);
         return $this;
     }
 
@@ -172,6 +172,29 @@ abstract class Zend_Application_Bootstrap_BootstrapAbstract
     }
 
     /**
+     * Merge options recursively
+     * 
+     * @param  array $array1 
+     * @param  mixed $array2 
+     * @return array
+     */
+    public function mergeOptions(array $array1, $array2 = null)
+    {
+        if (is_array($array2)) {
+            foreach ($array2 as $key => $val) {
+                if (is_array($array2[$key])) {
+                    $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
+                                  ? $this->mergeOptions($array1[$key], $array2[$key]) 
+                                  : $array2[$key];
+                } else {
+                    $array1[$key] = $val;
+                }
+            }
+        }
+        return $array1;
+    }
+
+    /**
      * Get class resources (as resource/method pairs)
      * 
      * Uses get_class_methods() by default, reflection on prior to 5.2.6,

+ 24 - 1
library/Zend/Application/Resource/ResourceAbstract.php

@@ -97,7 +97,7 @@ abstract class Zend_Application_Resource_ResourceAbstract implements Zend_Applic
             }
         }
         
-        $this->_options = array_merge_recursive($this->_options, $options);
+        $this->_options = $this->mergeOptions($this->_options, $options);
 
         return $this;
     }
@@ -113,6 +113,29 @@ abstract class Zend_Application_Resource_ResourceAbstract implements Zend_Applic
     }
 
     /**
+     * Merge options recursively
+     * 
+     * @param  array $array1 
+     * @param  mixed $array2 
+     * @return array
+     */
+    public function mergeOptions(array $array1, $array2 = null)
+    {
+        if (is_array($array2)) {
+            foreach ($array2 as $key => $val) {
+                if (is_array($array2[$key])) {
+                    $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
+                                  ? $this->mergeOptions($array1[$key], $array2[$key]) 
+                                  : $array2[$key];
+                } else {
+                    $array1[$key] = $val;
+                }
+            }
+        }
+        return $array1;
+    }
+
+    /**
      * Set the bootstrap to which the resource is attached
      * 
      * @param  Zend_Application_Bootstrap_Bootstrapper $bootstrap 

+ 1 - 1
tests/Zend/Application/Bootstrap/BootstrapAbstractTest.php

@@ -158,7 +158,7 @@ class Zend_Application_Bootstrap_BootstrapAbstractTest extends PHPUnit_Framework
             ),
         );
         $bootstrap->setOptions($options2);
-        $expected = array_merge_recursive($options, $options2);
+        $expected = $bootstrap->mergeOptions($options, $options2);
         $test     = $bootstrap->getOptions();
         $this->assertEquals($expected, $test);
     }

+ 2 - 1
tests/Zend/Application/Resource/ResourceAbstractTest.php

@@ -120,7 +120,8 @@ class Zend_Application_Resource_ResourceAbstractTest extends PHPUnit_Framework_T
         $options3  = array(
             'foo' => 'BAR',
         );
-        $expected = array_merge_recursive($options1, $options2, $options3);
+        $expected = $resource->mergeOptions($options1, $options2);
+        $expected = $resource->mergeOptions($expected, $options3);
         $resource->setOptions($options1)
                  ->setOptions($options2)
                  ->setOptions($options3);