Browse Source

Add shallow merge ability to InstanceConfigTrait::config()

ADmad 11 years ago
parent
commit
628cd0ef6d
2 changed files with 30 additions and 4 deletions
  1. 10 4
      src/Core/InstanceConfigTrait.php
  2. 20 0
      tests/TestCase/Core/InstanceConfigTraitTest.php

+ 10 - 4
src/Core/InstanceConfigTrait.php

@@ -68,11 +68,12 @@ trait InstanceConfigTrait
      *
      * @param string|array|null $key The key to get/set, or a complete array of configs.
      * @param mixed|null $value The value to set.
-     * @param bool $merge Whether to merge or overwrite existing config, defaults to true.
+     * @param bool|string $merge 'deep' to merge recursively, 'shallow' for simple merge,
+     *   false to overwrite, defaults to 'deep'.
      * @return mixed Config value being read, or the object itself on write operations.
      * @throws \Cake\Core\Exception\Exception When trying to set a key that is invalid.
      */
-    public function config($key = null, $value = null, $merge = true)
+    public function config($key = null, $value = null, $merge = 'deep')
     {
         if (!$this->_configInitialized) {
             $this->_config = $this->_defaultConfig;
@@ -123,7 +124,8 @@ trait InstanceConfigTrait
      *
      * @param string|array $key Key to write to.
      * @param mixed $value Value to write.
-     * @param bool $merge Whether to merge or overwrite value.
+     * @param bool|string $merge 'deep' to merge recursively, 'shallow' for simple merge,
+     *   false to overwrite, defaults to false.
      * @return void
      * @throws \Cake\Core\Exception\Exception if attempting to clobber existing config
      */
@@ -139,7 +141,11 @@ trait InstanceConfigTrait
             } else {
                 $update = [$key => $value];
             }
-            $this->_config = Hash::merge($this->_config, Hash::expand($update));
+            if ($merge === 'shallow') {
+                $this->_config = array_merge($this->_config, Hash::expand($update));
+            } else {
+                $this->_config = Hash::merge($this->_config, Hash::expand($update));
+            }
             return;
         }
 

+ 20 - 0
tests/TestCase/Core/InstanceConfigTraitTest.php

@@ -268,6 +268,26 @@ class InstanceConfigTraitTest extends TestCase
     }
 
     /**
+     * test shallow merge
+     *
+     * @return void
+     */
+    public function testShallowMerge()
+    {
+        $this->object->config(['a' => ['new_nested' => true], 'new' => 'bar'], null, 'shallow');
+
+        $this->assertSame(
+            [
+                'some' => 'string',
+                'a' => ['new_nested' => true],
+                'new' => 'bar'
+            ],
+            $this->object->config(),
+            'When merging a scalar property will be overwritten with an array'
+        );
+    }
+
+    /**
      * testSetClobber
      *
      * @expectedException \Exception