Browse Source

[Form] Passing FormFactory to FormBuilder in its constructor

Bernhard Schussek 14 năm trước cách đây
mục cha
commit
e3db366116

+ 2 - 8
src/Symfony/Component/Form/FormBuilder.php

@@ -56,20 +56,14 @@ class FormBuilder
 
     private $emptyData = '';
 
-    public function __construct($name, EventDispatcherInterface $dispatcher, $dataClass = null)
+    public function __construct($name, FormFactoryInterface $factory, EventDispatcherInterface $dispatcher, $dataClass = null)
     {
         $this->name = $name;
+        $this->factory = $factory;
         $this->dispatcher = $dispatcher;
         $this->dataClass = $dataClass;
     }
 
-    public function setFormFactory(FormFactoryInterface $factory)
-    {
-        $this->factory = $factory;
-
-        return $this;
-    }
-
     public function getFormFactory()
     {
         return $this->factory;

+ 1 - 2
src/Symfony/Component/Form/FormFactory.php

@@ -73,13 +73,12 @@ class FormFactory implements FormFactoryInterface
         }
 
         for ($i = 0, $l = count($types); $i < $l && !$builder; ++$i) {
-            $builder = $types[$i]->createBuilder($name, $options);
+            $builder = $types[$i]->createBuilder($name, $this, $options);
         }
 
         // TODO check if instance exists
 
         $builder->setTypes($types);
-        $builder->setFormFactory($this);
 
         foreach ($types as $type) {
             $type->buildForm($builder, $options);

+ 2 - 2
src/Symfony/Component/Form/Type/AbstractType.php

@@ -12,8 +12,8 @@
 namespace Symfony\Component\Form\Type;
 
 use Symfony\Component\Form\FormBuilder;
-use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\Form\FormView;
 
 abstract class AbstractType implements FormTypeInterface
@@ -30,7 +30,7 @@ abstract class AbstractType implements FormTypeInterface
     {
     }
 
-    public function createBuilder($name, array $options)
+    public function createBuilder($name, FormFactoryInterface $factory, array $options)
     {
         return null;
     }

+ 3 - 2
src/Symfony/Component/Form/Type/FieldType.php

@@ -14,6 +14,7 @@ namespace Symfony\Component\Form\Type;
 use Symfony\Component\Form\Util\PropertyPath;
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\Form\FormView;
 use Symfony\Component\Form\EventListener\TrimListener;
 use Symfony\Component\Form\Validator\DefaultValidator;
@@ -124,9 +125,9 @@ class FieldType extends AbstractType
         return $defaultOptions;
     }
 
-    public function createBuilder($name, array $options)
+    public function createBuilder($name, FormFactoryInterface $factory, array $options)
     {
-        return new FormBuilder($name, new EventDispatcher(), $options['data_class']);
+        return new FormBuilder($name, $factory, new EventDispatcher(), $options['data_class']);
     }
 
     public function getParent(array $options)

+ 2 - 1
src/Symfony/Component/Form/Type/FormTypeInterface.php

@@ -13,6 +13,7 @@ namespace Symfony\Component\Form\Type;
 
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormFactoryInterface;
 use Symfony\Component\Form\FormView;
 
 interface FormTypeInterface
@@ -23,7 +24,7 @@ interface FormTypeInterface
 
     function buildViewBottomUp(FormView $view, FormInterface $form);
 
-    function createBuilder($name, array $options);
+    function createBuilder($name, FormFactoryInterface $factory, array $options);
 
     function getDefaultOptions(array $options);
 

+ 8 - 9
tests/Symfony/Tests/Component/Form/FormBuilderTest.php

@@ -21,12 +21,15 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
 {
     private $dispatcher;
 
+    private $factory;
+
     private $builder;
 
     public function setUp()
     {
         $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
-        $this->builder = new FormBuilder('name', $this->dispatcher);
+        $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
+        $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher);
     }
 
     /**
@@ -54,7 +57,7 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
 
     public function testAddWithGuessFluent()
     {
-        $this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
+        $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
         $builder = $this->builder->add('foo');
         $this->assertSame($builder, $this->builder);
     }
@@ -110,12 +113,10 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
         $expectedName = 'foo';
         $expectedOptions = array('bar' => 'baz');
 
-        $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
-        $factory->expects($this->once())
+        $this->factory->expects($this->once())
                 ->method('createBuilder')
                 ->with($this->equalTo($expectedType), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
                 ->will($this->returnValue($this->getFormBuilder()));
-        $this->builder->setFormFactory($factory);
 
         $this->builder->add($expectedName, $expectedType, $expectedOptions);
         $builder = $this->builder->get($expectedName);
@@ -128,14 +129,12 @@ class FormBuilderTest extends \PHPUnit_Framework_TestCase
         $expectedName = 'foo';
         $expectedOptions = array('bar' => 'baz');
 
-        $factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
-        $factory->expects($this->once())
+        $this->factory->expects($this->once())
                 ->method('createBuilderForProperty')
                 ->with($this->equalTo('stdClass'), $this->equalTo($expectedName), $this->equalTo($expectedOptions))
                 ->will($this->returnValue($this->getFormBuilder()));
 
-        $this->builder = new FormBuilder('name', $this->dispatcher, 'stdClass');
-        $this->builder->setFormFactory($factory);
+        $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher, 'stdClass');
         $this->builder->add($expectedName, null, $expectedOptions);
         $builder = $this->builder->get($expectedName);
 

+ 4 - 1
tests/Symfony/Tests/Component/Form/FormTest.php

@@ -29,6 +29,8 @@ class FormTest extends \PHPUnit_Framework_TestCase
 {
     private $dispatcher;
 
+    private $factory;
+
     private $builder;
 
     private $form;
@@ -36,6 +38,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
     protected function setUp()
     {
         $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+        $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
         $this->form = $this->getBuilder()->getForm();
     }
 
@@ -863,7 +866,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
 
     protected function getBuilder($name = 'name', EventDispatcherInterface $dispatcher = null)
     {
-        return new FormBuilder($name, $dispatcher ?: $this->dispatcher);
+        return new FormBuilder($name, $this->factory, $dispatcher ?: $this->dispatcher);
     }
 
     protected function getMockForm($name = 'name')

+ 1 - 1
tests/Symfony/Tests/Component/Form/Type/TestCase.php

@@ -52,7 +52,7 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
 
         $this->factory = new FormFactory($this->typeLoader);
 
-        $this->builder = new FormBuilder('name', $this->dispatcher);
+        $this->builder = new FormBuilder('name', $this->factory, $this->dispatcher);
     }
 
     protected function getTypeLoaders()

+ 4 - 1
tests/Symfony/Tests/Component/Form/Validator/DelegatingValidatorTest.php

@@ -23,6 +23,8 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
 {
     private $dispatcher;
 
+    private $factory;
+
     private $builder;
 
     private $delegate;
@@ -36,6 +38,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
     protected function setUp()
     {
         $this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+        $this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
         $this->delegate = $this->getMock('Symfony\Component\Validator\ValidatorInterface');
         $this->validator = new DelegatingValidator($this->delegate);
         $this->message = 'Message';
@@ -71,7 +74,7 @@ class DelegatingValidatorTest extends \PHPUnit_Framework_TestCase
 
     protected function getBuilder($name = 'name', $propertyPath = null)
     {
-        $builder = new FormBuilder($name, $this->dispatcher);
+        $builder = new FormBuilder($name, $this->factory, $this->dispatcher);
         $builder->setAttribute('property_path', new PropertyPath($propertyPath ?: $name));
         $builder->setAttribute('error_mapping', array());