Bläddra i källkod

[Form] Introduce ArrayTypeLoader, Remove EntityManager and FormFactory interface from DefaultTypeLoader

Benjamin Eberlei 14 år sedan
förälder
incheckning
6bcc04905c

+ 46 - 0
src/Symfony/Component/Form/Type/Loader/ArrayTypeLoader.php

@@ -0,0 +1,46 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Type\Loader;
+
+use Symfony\Component\Form\FormFactoryInterface;
+use Symfony\Component\Form\Type;
+use Symfony\Component\Form\Type\FormTypeInterface;
+
+class ArrayTypeLoader implements TypeLoaderInterface
+{
+    /**
+     * @var array
+     */
+    private $types;
+
+    public function __construct(array $types)
+    {
+        foreach ($types as $type) {
+            $this->addType($type);
+        }
+    }
+
+    private function addType(FormTypeInterface $type)
+    {
+        $this->types[$type->getName()] = $type;
+    }
+
+    public function getType($name)
+    {
+        return $this->types[$name];
+    }
+
+    public function hasType($name)
+    {
+        return isset($this->types[$name]);
+    }
+}

+ 11 - 11
src/Symfony/Component/Form/Type/Loader/DefaultTypeLoader.php

@@ -24,10 +24,9 @@ class DefaultTypeLoader implements TypeLoaderInterface
 {
     private $types = array();
 
-    public function initialize(FormFactoryInterface $factory,
-            FormThemeFactoryInterface $themeFactory, $template,
-            CsrfProviderInterface $csrfProvider, ValidatorInterface $validator,
-            TemporaryStorage $storage, EntityManager $em = null)
+    public function __construct(
+            FormThemeFactoryInterface $themeFactory, $template = null, ValidatorInterface $validator = null,
+            CsrfProviderInterface $csrfProvider = null, TemporaryStorage $storage = null)
     {
         $this->addType(new Type\FieldType($validator, $themeFactory, $template));
         $this->addType(new Type\FormType());
@@ -36,10 +35,15 @@ class DefaultTypeLoader implements TypeLoaderInterface
         $this->addType(new Type\ChoiceType());
         $this->addType(new Type\CollectionType());
         $this->addType(new Type\CountryType());
-        $this->addType(new Type\CsrfType($csrfProvider));
+        if ($csrfProvider) {
+            // TODO Move to a Symfony\Bridge\FormSecurity
+            $this->addType(new Type\CsrfType($csrfProvider));
+        }
         $this->addType(new Type\DateType());
         $this->addType(new Type\DateTimeType());
-        $this->addType(new Type\FileType($storage));
+        if ($storage) {
+            $this->addType(new Type\FileType($storage));
+        }
         $this->addType(new Type\HiddenType());
         $this->addType(new Type\IntegerType());
         $this->addType(new Type\LanguageType());
@@ -55,13 +59,9 @@ class DefaultTypeLoader implements TypeLoaderInterface
         $this->addType(new Type\TimeType());
         $this->addType(new Type\TimezoneType());
         $this->addType(new Type\UrlType());
-
-        if (null !== $em) {
-            $this->addType(new Type\EntityType($em));
-        }
     }
 
-    public function addType(FormTypeInterface $type)
+    private function addType(FormTypeInterface $type)
     {
         $this->types[$type->getName()] = $type;
     }

+ 7 - 6
tests/Symfony/Tests/Component/Form/Renderer/Theme/AbstractThemeFunctionalTest.php

@@ -40,12 +40,13 @@ abstract class AbstractThemeFunctionalTest extends \PHPUnit_Framework_TestCase
         $storage = new \Symfony\Component\HttpFoundation\File\TemporaryStorage('foo', 1, \sys_get_temp_dir());
 
         // ok more than 2 lines, see DefaultFormFactory.php for proposed simplication
-        $typeLoader = new DefaultTypeLoader();
-        $this->factory = new FormFactory($typeLoader);
-        $typeLoader->initialize($this->factory, $themeFactory, $template, $csrfProvider, $validator , $storage);
-        // this is the relevant bit about your own forms:
-        $typeLoader->addType(new MyTestFormConfig());
-        $typeLoader->addType(new MyTestSubFormConfig());
+        $chainLoader = new \Symfony\Component\Form\Type\Loader\TypeLoaderChain();
+        $chainLoader->addLoader(new DefaultTypeLoader($themeFactory, $template, $validator, $csrfProvider , $storage));
+        $chainLoader->addLoader(new \Symfony\Component\Form\Type\Loader\ArrayTypeLoader(array(
+            new MyTestFormConfig(),
+            new MyTestSubFormConfig()
+        )));
+        $this->factory = new FormFactory($chainLoader);
     }
 
     public function testFullFormRendering()

+ 4 - 0
tests/Symfony/Tests/Component/Form/Type/EntityTypeTest.php

@@ -31,6 +31,10 @@ class EntityTypeTest extends DoctrineOrmTestCase
     {
         parent::setUp();
 
+        if (!$this->chainLoader->hasType('entity')) {
+            $this->markTestSkipped('No entity type.');
+        }
+
         $this->em = $this->createTestEntityManager();
 
         $schemaTool = new SchemaTool($this->em);

+ 43 - 0
tests/Symfony/Tests/Component/Form/Type/Loader/ArrayTypeLoaderTest.php

@@ -0,0 +1,43 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Tests\Component\Form\Type\Loader;
+
+use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\Type\FormTypeInterface;
+use Symfony\Component\Form\Type\Loader\ArrayTypeLoader;
+
+class ArrayTypeLoaderTest extends \PHPUnit_Framework_TestCase
+{
+    public function testHasType()
+    {
+        $type = $this->getMock('Symfony\Component\Form\Type\FormTypeInterface');
+        $type->expects($this->once())
+             ->method('getName')
+             ->will($this->returnValue('foo'));
+
+        $loader = new ArrayTypeLoader(array($type));
+        $this->assertTrue($loader->hasType('foo'));
+        $this->assertFalse($loader->hasType('bar'));
+    }
+
+    public function testGetType()
+    {
+        $type = $this->getMock('Symfony\Component\Form\Type\FormTypeInterface');
+        $type->expects($this->once())
+             ->method('getName')
+             ->will($this->returnValue('foo'));
+
+        $loader = new ArrayTypeLoader(array($type));
+        $this->assertSame($type, $loader->getType('foo'));
+        $this->assertSame($loader->getType('foo'), $loader->getType('foo'));
+    }
+}

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

@@ -34,6 +34,8 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
 
     protected $dispatcher;
 
+    protected $chainLoader;
+
     protected function setUp()
     {
         $this->themeFactory = $this->getMock('Symfony\Component\Form\Renderer\Theme\FormThemeFactoryInterface');
@@ -50,10 +52,11 @@ abstract class TestCase extends \PHPUnit_Framework_TestCase
         $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
             ->disableOriginalConstructor()
             ->getMock();
-        $loader = new DefaultTypeLoader();
-        $this->factory = new FormFactory($loader);
-        $loader->initialize($this->factory, $this->themeFactory, null, $this->csrfProvider,
-                $this->validator, $this->storage, $this->em);
+        $loader = new DefaultTypeLoader($this->themeFactory, null,
+                $this->validator, $this->csrfProvider, $this->storage);
+        $this->chainLoader = new \Symfony\Component\Form\Type\Loader\TypeLoaderChain();
+        $this->chainLoader->addLoader($loader);
+        $this->factory = new FormFactory($this->chainLoader);
 
         $this->builder = new FormBuilder($this->dispatcher);
     }