Quellcode durchsuchen

[Form] Removed form.type.loader tag and according compiler pass to simplify configuration. Custom types that are not in the DIC can be instantiated manually and passed wherever a type is desired.

Bernhard Schussek vor 14 Jahren
Ursprung
Commit
a58cfab37d

+ 0 - 38
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddFormTypeLoadersPass.php

@@ -1,38 +0,0 @@
-<?php
-
-namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
-
-/*
- * 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.
- */
-
-use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
-use Symfony\Component\DependencyInjection\Reference;
-
-/**
- * Adds all services with the tag "form.type.loader" as argument
- * to the "form.type.loader.chain" service
- *
- * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
- */
-class AddFormTypeLoadersPass implements CompilerPassInterface
-{
-    public function process(ContainerBuilder $container)
-    {
-        if (!$container->hasDefinition('form.type.loader.chain')) {
-            return;
-        }
-
-        $definition = $container->getDefinition('form.type.loader.chain');
-
-        foreach ($container->findTaggedServiceIds('form.type.loader') as $serviceId => $tag) {
-            $definition->addMethodCall('addLoader', array(new Reference($serviceId)));
-        }
-    }
-}

+ 3 - 3
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddFormTypesPass.php

@@ -16,7 +16,7 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
 
 /**
  * Adds all services with the tag "form.type" as argument
- * to the "form.type.loader.tagged" service
+ * to the "form.type.loader" service
  *
  * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  */
@@ -24,7 +24,7 @@ class AddFormTypesPass implements CompilerPassInterface
 {
     public function process(ContainerBuilder $container)
     {
-        if (!$container->hasDefinition('form.type.loader.tagged')) {
+        if (!$container->hasDefinition('form.type.loader')) {
             return;
         }
 
@@ -41,6 +41,6 @@ class AddFormTypesPass implements CompilerPassInterface
             $types[$alias] = $serviceId;
         }
 
-        $container->getDefinition('form.type.loader.tagged')->setArgument(1, $types);
+        $container->getDefinition('form.type.loader')->setArgument(1, $types);
     }
 }

+ 0 - 2
src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

@@ -13,7 +13,6 @@ namespace Symfony\Bundle\FrameworkBundle;
 
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddFormTypesPass;
-use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddFormTypeLoadersPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddFormGuessersPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddFormRendererFactoriesPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TemplatingPass;
@@ -80,7 +79,6 @@ class FrameworkBundle extends Bundle
         $container->addCompilerPass(new TemplatingPass());
         $container->addCompilerPass(new AddConstraintValidatorsPass());
         $container->addCompilerPass(new AddFormTypesPass());
-        $container->addCompilerPass(new AddFormTypeLoadersPass());
         $container->addCompilerPass(new AddFormGuessersPass());
         $container->addCompilerPass(new AddFormRendererFactoriesPass());
         $container->addCompilerPass(new AddClassesToCachePass());

+ 3 - 19
src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml

@@ -6,9 +6,7 @@
 
     <parameters>
         <parameter key="form.factory.class">Symfony\Component\Form\FormFactory</parameter>
-        <parameter key="form.type.loader.chain.class">Symfony\Component\Form\Type\Loader\TypeLoaderChain</parameter>
-        <parameter key="form.type.loader.simple.class">Symfony\Component\Form\Type\Loader\SimpleTypeLoader</parameter>
-        <parameter key="form.type.loader.tagged.class">Symfony\Bundle\FrameworkBundle\Form\ContainerAwareTypeLoader</parameter>
+        <parameter key="form.type.loader.class">Symfony\Bundle\FrameworkBundle\Form\ContainerAwareTypeLoader</parameter>
         <parameter key="form.guesser.validator.class">Symfony\Component\Form\Type\Guesser\ValidatorTypeGuesser</parameter>
         <parameter key="form.csrf_provider.class">Symfony\Component\Form\CsrfProvider\SessionCsrfProvider</parameter>
         <parameter key="form.renderer.factory.loader.class">Symfony\Bundle\FrameworkBundle\Form\ContainerAwareRendererFactoryLoader</parameter>
@@ -33,7 +31,7 @@
 
         <!-- FormFactory -->
         <service id="form.factory" class="%form.factory.class%">
-            <argument type="service" id="form.type.loader.chain" />
+            <argument type="service" id="form.type.loader" />
             <argument type="service" id="form.renderer.factory.loader" />
             <!--
             All services with tag "form.guesser" are inserted here by 
@@ -103,17 +101,8 @@
             <argument>%file.temporary_storage.directory%</argument>
         </service>
         
-        <!-- TypeLoaderChain -->
-        <service id="form.type.loader.chain" class="%form.type.loader.chain.class%">
-            <!--
-            All services with tag "form.type.loader" are inserted here by 
-            AddFormTypeLoadersPass
-            -->
-        </service>
-        
         <!-- ContainerAwareTypeLoader -->
-        <service id="form.type.loader.tagged" class="%form.type.loader.tagged.class%">
-            <tag name="form.type.loader" />
+        <service id="form.type.loader" class="%form.type.loader.class%">
             <argument type="service" id="service_container" />
             <!--
             All services with tag "form.type" are inserted here by 
@@ -122,11 +111,6 @@
             <argument type="collection" />
         </service>
         
-        <!-- SimpleTypeLoader -->
-        <service id="form.type.loader.simple" class="%form.type.loader.simple.class%">
-            <tag name="form.type.loader" />
-        </service>
-
         <!-- FieldTypes -->
         <service id="form.type.field" class="Symfony\Component\Form\Type\FieldType">
             <tag name="form.type" alias="field" />

+ 10 - 3
src/Symfony/Component/Form/FormFactory.php

@@ -49,13 +49,20 @@ class FormFactory implements FormFactoryInterface
         $passedOptions = array_keys($options);
 
         // TESTME
-        if (null === $name && preg_match('/\w+$/', $type, $matches)) {
-            $name = $matches[0];
+        if (null === $name) {
+            $typeAsString = is_object($type) ? get_class($type) : $type;
+
+            if (preg_match('/\w+$/', $typeAsString, $matches)) {
+                $name = $matches[0];
+            }
         }
 
         while (null !== $type) {
             // TODO check if type exists
-            $type = $this->typeLoader->getType($type);
+            if (!$type instanceof FormTypeInterface) {
+                $type = $this->typeLoader->getType($type);
+            }
+
             array_unshift($types, $type);
             $defaultOptions = $type->getDefaultOptions($options);
             $options = array_merge($defaultOptions, $options);

+ 0 - 44
src/Symfony/Component/Form/Type/Loader/SimpleTypeLoader.php

@@ -1,44 +0,0 @@
-<?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\Type\FormTypeInterface;
-use Symfony\Component\Form\Exception\TypeLoaderException;
-
-class SimpleTypeLoader implements TypeLoaderInterface
-{
-    private $types = array();
-
-    public function getType($name)
-    {
-        if (!isset($this->types[$name])) {
-            if (!class_exists($name)) {
-                throw new TypeLoaderException(sprintf('The type class "%s" does not exist', $name));
-            }
-
-            $type = new $name();
-
-            if (!$type instanceof FormTypeInterface) {
-                throw new TypeLoaderException(sprintf('The type class "%s" must implement "Symfony\Component\Form\Type\FormTypeInterface"', $name));
-            }
-
-            $this->types[$name] = $type;
-        }
-
-        return $this->types[$name];
-    }
-
-    public function hasType($name)
-    {
-        return class_exists($name);
-    }
-}

+ 0 - 76
tests/Symfony/Tests/Component/Form/Type/Loader/SimpleTypeLoaderTest.php

@@ -1,76 +0,0 @@
-<?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\FormInterface;
-use Symfony\Component\Form\Type\FormTypeInterface;
-use Symfony\Component\Form\Type\Loader\SimpleTypeLoader;
-use Symfony\Component\Form\Renderer\FormRendererInterface;
-
-class SimpleTypeLoaderTest extends \PHPUnit_Framework_TestCase
-{
-    private $loader;
-
-    protected function setUp()
-    {
-        $this->loader = new SimpleTypeLoader();
-    }
-
-    public function testHasType()
-    {
-        $this->assertTrue($this->loader->hasType(__NAMESPACE__.'\TestType'));
-        $this->assertFalse($this->loader->hasType(__NAMESPACE__.'\FooType'));
-    }
-
-    public function testGetType()
-    {
-        $this->assertEquals(new TestType(), $this->loader->getType(__NAMESPACE__.'\TestType'));
-    }
-
-    /**
-     * @expectedException Symfony\Component\Form\Exception\TypeLoaderException
-     */
-    public function testGetInvalidTypeThrowsException()
-    {
-        $this->loader->getType(__NAMESPACE__.'\FooType');
-    }
-
-    /**
-     * @expectedException Symfony\Component\Form\Exception\TypeLoaderException
-     */
-    public function testGetClassThatDoesNotImplementInterface()
-    {
-        $this->loader->getType(__NAMESPACE__.'\InvalidType');
-    }
-}
-
-class InvalidType
-{
-}
-
-class TestType implements FormTypeInterface
-{
-    function configure(FormBuilder $builder, array $options) {}
-
-    function buildRenderer(FormRendererInterface $renderer, FormInterface $form) {}
-
-    function buildRendererBottomUp(FormRendererInterface $renderer, FormInterface $form) {}
-
-    function createBuilder(array $options) {}
-
-    function getDefaultOptions(array $options) {}
-
-    function getParent(array $options) {}
-
-    function getName() {}
-}