Pārlūkot izejas kodu

[Form] [DoctrineBridge] Move EntityType and DoctrineTypeLoader into Doctrine Bridge

Benjamin Eberlei 14 gadi atpakaļ
vecāks
revīzija
ee96ad0e81

+ 8 - 1
src/Symfony/Component/Form/ChoiceList/EntityChoiceList.php

@@ -9,19 +9,26 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\ChoiceList;
+namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
 
 use Symfony\Component\Form\PropertyPath;
 use Symfony\Component\Form\Exception\FormException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
+use Symfony\Component\Form\ChoiceList\DefaultChoiceList;
 use Doctrine\ORM\EntityManager;
 use Doctrine\ORM\QueryBuilder;
 use Doctrine\ORM\NoResultException;
 
 class EntityChoiceList extends DefaultChoiceList
 {
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
     private $em;
 
+    /**
+     * @var Doctrine\ORM\Mapping\ClassMetadata
+     */
     private $class;
 
     /**

+ 4 - 2
src/Symfony/Component/Form/DataTransformer/EntitiesToArrayTransformer.php

@@ -9,10 +9,12 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Bridge\Doctrine\Form\DataTransformer;
 
-use Symfony\Component\Form\ChoiceList\EntityChoiceList;
+use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
+use Symfony\Component\Form\DataTransformer\TransformationFailedException;
+use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
 use Doctrine\Common\Collections\Collection;
 use Doctrine\Common\Collections\ArrayCollection;
 

+ 4 - 2
src/Symfony/Component/Form/DataTransformer/EntityToIdTransformer.php

@@ -9,10 +9,12 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\DataTransformer;
+namespace Symfony\Bridge\Doctrine\Form\DataTransformer;
 
-use Symfony\Component\Form\ChoiceList\EntityChoiceList;
+use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
+use Symfony\Component\Form\DataTransformer\DataTransformerInterface;
+use Symfony\Component\Form\DataTransformer\TransformationFailedException;
 
 class EntityToIdTransformer implements DataTransformerInterface
 {

+ 38 - 0
src/Symfony/Bridge/Doctrine/Form/DoctrineTypeLoader.php

@@ -0,0 +1,38 @@
+<?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\Bridge\Doctrine\Form;
+
+use Symfony\Component\Form\Type\Loader\TypeLoaderInterface;
+use Doctrine\ORM\EntityManager;
+
+class DoctrineTypeLoader implements TypeLoaderInterface
+{
+    private $types;
+
+    public function __construct(EntityManager $em)
+    {
+        $this->types['entity'] = new EntityType($em);
+    }
+
+    public function getType($name)
+    {
+        return $this->types[$name];
+    }
+
+    public function hasType($name)
+    {
+        return isset($this->types[$name]);
+    }
+}
+
+
+

+ 6 - 5
src/Symfony/Component/Form/Type/EntityType.php

@@ -9,17 +9,18 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\Type;
+namespace Symfony\Bridge\Doctrine\Form;
 
 use Symfony\Component\Form\FormBuilder;
 use Symfony\Component\Form\FormFactoryInterface;
-use Symfony\Component\Form\ChoiceList\EntityChoiceList;
-use Symfony\Component\Form\EventListener\MergeCollectionListener;
-use Symfony\Component\Form\DataTransformer\EntitiesToArrayTransformer;
+use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
+use Symfony\Bridge\Doctrine\Form\EventListener\MergeCollectionListener;
+use Symfony\Bridge\Doctrine\Form\DataTransformer\EntitiesToArrayTransformer;
+use Symfony\Bridge\Doctrine\Form\DataTransformer\EntityToIdTransformer;
 use Symfony\Component\Form\DataTransformer\ArrayToChoicesTransformer;
-use Symfony\Component\Form\DataTransformer\EntityToIdTransformer;
 use Symfony\Component\Form\DataTransformer\ScalarToChoicesTransformer;
 use Symfony\Component\Form\DataTransformer\DataTransformerChain;
+use Symfony\Component\Form\Type\AbstractType;
 use Doctrine\ORM\EntityManager;
 
 class EntityType extends AbstractType

+ 1 - 1
src/Symfony/Component/Form/EventListener/MergeCollectionListener.php

@@ -9,7 +9,7 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Component\Form\EventListener;
+namespace Symfony\Bridge\Doctrine\Form\EventListener;
 
 use Symfony\Component\Form\Events;
 use Symfony\Component\Form\Event\FilterDataEvent;

+ 22 - 0
tests/Symfony/Tests/Bridge/Doctrine/Fixtures/CompositeIdentEntity.php

@@ -0,0 +1,22 @@
+<?php
+
+namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
+
+/** @Entity */
+class CompositeIdentEntity
+{
+    /** @Id @Column(type="integer") */
+    protected $id1;
+
+    /** @Id @Column(type="integer") */
+    protected $id2;
+
+    /** @Column(type="string") */
+    public $name;
+
+    public function __construct($id1, $id2, $name) {
+        $this->id1 = $id1;
+        $this->id2 = $id2;
+        $this->name = $name;
+    }
+}

+ 18 - 0
tests/Symfony/Tests/Bridge/Doctrine/Fixtures/SingleIdentEntity.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Symfony\Tests\Bridge\Doctrine\Form\Fixtures;
+
+/** @Entity */
+class SingleIdentEntity
+{
+    /** @Id @Column(type="integer") */
+    protected $id;
+
+    /** @Column(type="string") */
+    public $name;
+
+    public function __construct($id, $name) {
+        $this->id = $id;
+        $this->name = $name;
+    }
+}

+ 2 - 4
tests/Symfony/Tests/Component/Form/Type/DoctrineOrmTestCase.php

@@ -9,13 +9,11 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Tests\Component\Form\Type;
-
-require_once __DIR__.'/TestCase.php';
+namespace Symfony\Tests\Bridge\Doctrine\Form\Type;
 
 use Doctrine\ORM\EntityManager;
 
-abstract class DoctrineOrmTestCase extends TestCase
+abstract class DoctrineOrmTestCase extends \Symfony\Tests\Component\Form\Type\TestCase
 {
     protected function setUp()
     {

+ 8 - 5
tests/Symfony/Tests/Component/Form/Type/EntityTypeTest.php

@@ -9,23 +9,25 @@
  * file that was distributed with this source code.
  */
 
-namespace Symfony\Tests\Component\Form\Type;
+namespace Symfony\Tests\Bridge\Doctrine\Form\Type;
 
 require_once __DIR__.'/DoctrineOrmTestCase.php';
 require_once __DIR__.'/../Fixtures/SingleIdentEntity.php';
 require_once __DIR__.'/../Fixtures/CompositeIdentEntity.php';
 
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
-use Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity;
-use Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity;
+use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity;
+use Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity;
+use Symfony\Bridge\Doctrine\Form\DoctrineTypeLoader;
 use Doctrine\ORM\Tools\SchemaTool;
+use Doctrine\ORM\EntityManager;
 use Doctrine\Common\Collections\ArrayCollection;
 
 class EntityTypeTest extends DoctrineOrmTestCase
 {
-    const SINGLE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity';
+    const SINGLE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\SingleIdentEntity';
 
-    const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity';
+    const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Bridge\Doctrine\Form\Fixtures\CompositeIdentEntity';
 
     protected function setUp()
     {
@@ -36,6 +38,7 @@ class EntityTypeTest extends DoctrineOrmTestCase
         }
 
         $this->em = $this->createTestEntityManager();
+        $this->chainLoader->addLoader(new DoctrineTypeLoader($this->em));
 
         $schemaTool = new SchemaTool($this->em);
         $classes = array(