Browse Source

[Form] Removed FormFactory::addGuessers() and moved guessers to constructor arg instead

Bernhard Schussek 14 years ago
parent
commit
7b412cc762

+ 6 - 4
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddFormGuessersPass.php

@@ -16,8 +16,8 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
 use Symfony\Component\DependencyInjection\Reference;
 
 /**
- * Adds all services with the tag "form.guesser" as calls to the "addGuesser"
- * method of the "form.factory" service
+ * Adds all services with the tag "form.guesser" as constructor argument of the
+ * "form.factory" service
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  */
@@ -29,10 +29,12 @@ class AddFormGuessersPass implements CompilerPassInterface
             return;
         }
 
-        $definition = $container->getDefinition('form.factory');
+        $guessers = array();
 
         foreach ($container->findTaggedServiceIds('form.guesser') as $serviceId => $tag) {
-            $definition->addMethodCall('addGuesser', array(new Reference($serviceId)));
+            $guessers[] = new Reference($serviceId);
         }
+
+        $container->getDefinition('form.factory')->setArgument(1, $guessers);
     }
 }

+ 5 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

@@ -31,6 +31,11 @@
         <!-- FormFactory -->
         <service id="form.factory" class="%form.factory.class%">
             <argument type="service" id="form.type.loader.chain" />
+            <!--
+            All services with tag "form.guesser" are inserted here by 
+            AddFormGuessersPass
+            -->
+            <argument type="collection" />
         </service>
 
         <!-- ValidatorTypeGuesser -->

+ 8 - 6
src/Symfony/Component/Form/FormFactory.php

@@ -15,6 +15,7 @@ use Symfony\Component\Form\Type\FormTypeInterface;
 use Symfony\Component\Form\Type\Loader\TypeLoaderInterface;
 use Symfony\Component\Form\Type\Guesser\TypeGuesserInterface;
 use Symfony\Component\Form\Type\Guesser\Guess;
+use Symfony\Component\Form\Exception\UnexpectedTypeException;
 
 class FormFactory implements FormFactoryInterface
 {
@@ -22,14 +23,15 @@ class FormFactory implements FormFactoryInterface
 
     private $guessers = array();
 
-    public function __construct(TypeLoaderInterface $typeLoader)
+    public function __construct(TypeLoaderInterface $typeLoader, array $guessers = array())
     {
+        foreach ($guessers as $guesser) {
+            if (!$guesser instanceof TypeGuesserInterface) {
+                throw new UnexpectedTypeException($guesser, 'Symfony\Component\Form\Type\Guesser\TypeGuesserInterface');
+            }
+        }
         $this->typeLoader = $typeLoader;
-    }
-
-    public function addGuesser(TypeGuesserInterface $guesser)
-    {
-        $this->guessers[] = $guesser;
+        $this->guessers = $guessers;
     }
 
     public function createBuilder($type, $name = null, array $options = array())

+ 7 - 15
tests/Symfony/Tests/Component/Form/FormFactoryTest.php

@@ -50,9 +50,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
                 Guess::HIGH_CONFIDENCE
             )));
 
-        $factory = $this->createMockFactory(array('createBuilder'));
-        $factory->addGuesser($guesser1);
-        $factory->addGuesser($guesser2);
+        $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
 
         $factory->expects($this->once())
             ->method('createBuilder')
@@ -72,8 +70,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
                 ->with('Application\Author', 'firstName')
                 ->will($this->returnValue(null));
 
-        $factory = $this->createMockFactory(array('createBuilder'));
-        $factory->addGuesser($guesser);
+        $factory = $this->createMockFactory(array('createBuilder'), array($guesser));
 
         $factory->expects($this->once())
             ->method('createBuilder')
@@ -97,8 +94,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
                     Guess::MEDIUM_CONFIDENCE
                 )));
 
-        $factory = $this->createMockFactory(array('createBuilder'));
-        $factory->addGuesser($guesser);
+        $factory = $this->createMockFactory(array('createBuilder'), array($guesser));
 
         $factory->expects($this->once())
             ->method('createBuilder')
@@ -134,9 +130,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
                     Guess::HIGH_CONFIDENCE
                 )));
 
-        $factory = $this->createMockFactory(array('createBuilder'));
-        $factory->addGuesser($guesser1);
-        $factory->addGuesser($guesser2);
+        $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
 
         $factory->expects($this->once())
             ->method('createBuilder')
@@ -171,9 +165,7 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
                     Guess::HIGH_CONFIDENCE
                 )));
 
-        $factory = $this->createMockFactory(array('createBuilder'));
-        $factory->addGuesser($guesser1);
-        $factory->addGuesser($guesser2);
+        $factory = $this->createMockFactory(array('createBuilder'), array($guesser1, $guesser2));
 
         $factory->expects($this->once())
             ->method('createBuilder')
@@ -188,11 +180,11 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals('builderInstance', $builder);
     }
 
-    private function createMockFactory(array $methods = array())
+    private function createMockFactory(array $methods = array(), array $guessers = array())
     {
         return $this->getMockBuilder('Symfony\Component\Form\FormFactory')
             ->setMethods($methods)
-            ->setConstructorArgs(array($this->typeLoader))
+            ->setConstructorArgs(array($this->typeLoader, $guessers))
             ->getMock();
     }
 }