瀏覽代碼

[DependencyInjection] append new elements for prototype nodes without a key attribute

Johannes M. Schmitt 14 年之前
父節點
當前提交
c7ef8d98d6

+ 6 - 0
src/Symfony/Component/DependencyInjection/Configuration/ArrayNode.php

@@ -318,6 +318,12 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
         }
 
         foreach ($rightSide as $k => $v) {
+            // prototype, and key is irrelevant, so simply append the element
+            if (null !== $this->prototype && null === $this->keyAttribute) {
+                $leftSide[] = $v;
+                continue;
+            }
+
             // no conflict
             if (!array_key_exists($k, $leftSide)) {
                 if (!$this->allowNewKeys) {

+ 27 - 5
tests/Symfony/Tests/Component/DependencyInjection/Configuration/MergeTest.php

@@ -15,9 +15,7 @@ class MergeTest extends \PHPUnit_Framework_TestCase
         $tree = $tb
             ->root('root', 'array')
                 ->node('foo', 'scalar')
-                    ->merge()
-                        ->denyOverwrite()
-                    ->end()
+                    ->cannotBeOverwritten()
                 ->end()
             ->end()
             ->buildTree()
@@ -42,12 +40,12 @@ class MergeTest extends \PHPUnit_Framework_TestCase
                 ->node('foo', 'scalar')->end()
                 ->node('bar', 'scalar')->end()
                 ->node('unsettable', 'array')
-                    ->merge()->allowUnset()->end()
+                    ->canBeUnset()
                     ->node('foo', 'scalar')->end()
                     ->node('bar', 'scalar')->end()
                 ->end()
                 ->node('unsetted', 'array')
-                    ->merge()->allowUnset()->end()
+                    ->canBeUnset()
                     ->prototype('scalar')->end()
                 ->end()
             ->end()
@@ -145,4 +143,28 @@ class MergeTest extends \PHPUnit_Framework_TestCase
             )
         ), $tree->merge($a, $b));
     }
+
+    public function testPrototypeWithoutAKeyAttribute()
+    {
+        $tb = new TreeBuilder();
+
+        $tree = $tb
+            ->root('config', 'array')
+                ->node('append_elements', 'array')
+                    ->prototype('scalar')->end()
+                ->end()
+            ->end()
+            ->buildTree()
+        ;
+
+        $a = array(
+            'append_elements' => array('a', 'b'),
+        );
+
+        $b = array(
+            'append_elements' => array('c', 'd'),
+        );
+
+        $this->assertEquals(array('append_elements' => array('a', 'b', 'c', 'd')), $tree->merge($a, $b));
+    }
 }