Sfoglia il codice sorgente

adds setArgument to Definition

Johannes M. Schmitt 14 anni fa
parent
commit
84fa4b50db

+ 19 - 0
src/Symfony/Component/DependencyInjection/Definition.php

@@ -145,6 +145,25 @@ class Definition
         return $this;
     }
 
+    /**
+     * Sets a specific argument
+     *
+     * @param integer $index
+     * @param mixed $argument
+     *
+     * @return Definition The current instance
+     */
+    public function setArgument($index, $argument)
+    {
+        if ($index < 0 || $index > count($this->arguments) - 1) {
+            throw new \OutOfBoundsException(sprintf('The index "%d" is not in the range [0, %d].', $index, count($this->arguments) - 1));
+        }
+
+        $this->arguments[$index] = $argument;
+
+        return $this;
+    }
+
     /**
      * Gets the arguments to pass to the service constructor/factory method.
      *

+ 21 - 0
tests/Symfony/Tests/Component/DependencyInjection/DefinitionTest.php

@@ -167,4 +167,25 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
             'bar' => array(array('bar' => 'bar')),
         ), '->getTags() returns all tags');
     }
+
+    /**
+     * @covers Symfony\Component\DependencyInjection\Definition::setArgument
+     */
+    public function testSetArgument()
+    {
+        $def = new Definition('stdClass');
+
+        $def->addArgument('foo');
+        $this->assertSame(array('foo'), $def->getArguments());
+
+        $this->assertSame($def, $def->setArgument(0, 'moo'));
+        $this->assertSame(array('moo'), $def->getArguments());
+
+        $def->addArgument('moo');
+        $def
+            ->setArgument(0, 'foo')
+            ->setArgument(1, 'bar')
+        ;
+        $this->assertSame(array('foo', 'bar'), $def->getArguments());
+    }
 }