Explorar el Código

Now pass the ModelManager to Guessers.

Romain Geissler hace 13 años
padre
commit
2f8ffb3a8a

+ 1 - 1
Builder/DatagridBuilder.php

@@ -85,7 +85,7 @@ class DatagridBuilder implements DatagridBuilderInterface
     public function addFilter(DatagridInterface $datagrid, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
     {
         if ($type == null) {
-            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName());
+            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
 
             $type = $guessType->getType();
 

+ 1 - 1
Builder/ListBuilder.php

@@ -53,7 +53,7 @@ class ListBuilder implements ListBuilderInterface
     public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
     {
         if ($type == null) {
-            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName());
+            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
             $fieldDescription->setType($guessType->getType());
         } else {
             $fieldDescription->setType($type);

+ 1 - 1
Builder/ShowBuilder.php

@@ -54,7 +54,7 @@ class ShowBuilder implements ShowBuilderInterface
     public function addField(FieldDescriptionCollection $list, $type = null, FieldDescriptionInterface $fieldDescription, AdminInterface $admin)
     {
         if ($type == null) {
-            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName());
+            $guessType = $this->guesser->guessType($admin->getClass(), $fieldDescription->getName(), $admin->getModelManager());
             $fieldDescription->setType($guessType->getType());
         } else {
             $fieldDescription->setType($type);

+ 36 - 0
Guesser/AbstractTypeGuesser.php

@@ -0,0 +1,36 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\DoctrineORMAdminBundle\Guesser;
+
+use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
+use Doctrine\ORM\Mapping\MappingException;
+use Sonata\AdminBundle\Model\ModelManagerInterface;
+
+abstract class AbstractTypeGuesser implements TypeGuesserInterface
+{
+    /**
+     * @param string $class
+     * @param string $property
+     * @return TypeGuess
+     */
+    abstract public function guessType($class, $property, ModelManagerInterface $modelManager);
+
+    protected function getParentMetadataForProperty($baseClass, $propertyFullName, $modelManager)
+    {
+        try {
+            return $modelManager->getParentMetadataForProperty($baseClass, $propertyFullName);
+        } catch (MappingException $e) {
+            // no metadata not found.
+            return null;
+        }
+    }
+}

+ 4 - 68
Guesser/FilterTypeGuesser.php

@@ -12,32 +12,21 @@
 namespace Sonata\DoctrineORMAdminBundle\Guesser;
 
 use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
-use Symfony\Bridge\Doctrine\RegistryInterface;
 use Symfony\Component\Form\Guess\Guess;
 use Symfony\Component\Form\Guess\TypeGuess;
 use Doctrine\ORM\Mapping\ClassMetadataInfo;
-use Doctrine\ORM\Mapping\MappingException;
+use Sonata\AdminBundle\Model\ModelManagerInterface;
 
-class FilterTypeGuesser implements TypeGuesserInterface
+class FilterTypeGuesser extends AbstractTypeGuesser
 {
-    protected $registry;
-
-    private $cache;
-
-    public function __construct(RegistryInterface $registry)
-    {
-        $this->registry = $registry;
-        $this->cache = array();
-    }
-
     /**
      * @param string $class
      * @param string $property
      * @return TypeGuess
      */
-    public function guessType($class, $property)
+    public function guessType($class, $property, ModelManagerInterface $modelManager)
     {
-        if (!$ret = $this->getParentMetadataForProperty($class, $property)) {
+        if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
             return false;
         }
 
@@ -112,57 +101,4 @@ class FilterTypeGuesser implements TypeGuesserInterface
                 return new TypeGuess('doctrine_orm_string', $options, Guess::LOW_CONFIDENCE);
         }
     }
-
-    protected function getMetadata($class)
-    {
-        if (array_key_exists($class, $this->cache)) {
-            return $this->cache[$class];
-        }
-
-        $this->cache[$class] = null;
-        foreach ($this->registry->getEntityManagers() as $em) {
-            try {
-                return $this->cache[$class] = $em->getClassMetadata($class);
-            } catch (MappingException $e) {
-                // not an entity or mapped super class
-            }
-        }
-    }
-
-    protected function getParentMetadataForProperty($baseClass, $propertyFullName)
-    {
-        $nameElements = explode('.', $propertyFullName);
-        $lastPropertyName = array_pop($nameElements);
-        $class = $baseClass;
-        $parentAssociationMappings = array();
-
-        foreach($nameElements as $nameElement){
-            if (!$metadata = $this->getMetadata($class)) {
-                return null;
-            }
-
-            $class = $metadata->associationMappings[$nameElement]['targetEntity'];
-
-            if (!$metadata->hasAssociation($nameElement)) {
-                return null;
-            }
-
-            $mapping = $metadata->getAssociationMapping($nameElement);
-
-            switch ($mapping['type']) {
-                case ClassMetadataInfo::ONE_TO_ONE:
-                case ClassMetadataInfo::ONE_TO_MANY:
-                case ClassMetadataInfo::MANY_TO_ONE:
-                case ClassMetadataInfo::MANY_TO_MANY:
-                    $parentAssociationMappings[] = $mapping;
-
-                    break;
-
-                default:
-                    return null;
-            }
-        }
-
-        return array($this->getMetadata($class), $lastPropertyName, $parentAssociationMappings);
-    }
 }

+ 8 - 34
Guesser/TypeGuesser.php

@@ -16,35 +16,25 @@ use Symfony\Bridge\Doctrine\RegistryInterface;
 use Symfony\Component\Form\Guess\Guess;
 use Symfony\Component\Form\Guess\TypeGuess;
 use Doctrine\ORM\Mapping\ClassMetadataInfo;
-use Doctrine\ORM\Mapping\MappingException;
+use Sonata\AdminBundle\Model\ModelManagerInterface;
 
-class TypeGuesser implements TypeGuesserInterface
+class TypeGuesser extends AbstractTypeGuesser
 {
-    protected $registry;
-
-    private $cache;
-
-    public function __construct(RegistryInterface $registry)
-    {
-        $this->registry = $registry;
-        $this->cache = array();
-    }
-
     /**
      * @param string $class
      * @param string $property
      * @return TypeGuess
      */
-    public function guessType($class, $property)
+    public function guessType($class, $property, ModelManagerInterface $modelManager)
     {
-        if (!$ret = $this->getMetadata($class)) {
+        if (!$ret = $this->getParentMetadataForProperty($class, $property, $modelManager)) {
             return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
         }
 
-        list($metadata, $name) = $ret;
+        list($metadata, $propertyName, $parentAssociationMappings) = $ret;
 
-        if ($metadata->hasAssociation($property)) {
-            $mapping = $metadata->getAssociationMapping($property);
+        if ($metadata->hasAssociation($propertyName)) {
+            $mapping = $metadata->getAssociationMapping($propertyName);
 
             switch ($mapping['type']) {
                 case ClassMetadataInfo::ONE_TO_MANY:
@@ -61,7 +51,7 @@ class TypeGuesser implements TypeGuesserInterface
             }
         }
 
-        switch ($metadata->getTypeOfField($property))
+        switch ($metadata->getTypeOfField($propertyName))
         {
             case 'array':
                 return new TypeGuess('array', array(), Guess::HIGH_CONFIDENCE);
@@ -90,20 +80,4 @@ class TypeGuesser implements TypeGuesserInterface
                 return new TypeGuess('text', array(), Guess::LOW_CONFIDENCE);
         }
     }
-
-    protected function getMetadata($class)
-    {
-        if (array_key_exists($class, $this->cache)) {
-            return $this->cache[$class];
-        }
-
-        $this->cache[$class] = null;
-        foreach ($this->registry->getEntityManagers() as $name => $em) {
-            try {
-                return $this->cache[$class] = array($em->getClassMetadata($class), $name);
-            } catch (MappingException $e) {
-                // not an entity or mapped super class
-            }
-        }
-    }
 }

+ 0 - 3
Resources/config/doctrine_orm.xml

@@ -25,7 +25,6 @@
         </service>
 
         <service id="sonata.admin.guesser.orm_list" class="Sonata\DoctrineORMAdminBundle\Guesser\TypeGuesser">
-            <argument type="service" id="doctrine" />
             <tag name="sonata.admin.guesser.orm_list" />
         </service>
 
@@ -40,7 +39,6 @@
         </service>
 
         <service id="sonata.admin.guesser.orm_show" class="Sonata\DoctrineORMAdminBundle\Guesser\TypeGuesser">
-            <argument type="service" id="doctrine" />
             <tag name="sonata.admin.guesser.orm_show" />
         </service>
 
@@ -56,7 +54,6 @@
         </service>
 
         <service id="sonata.admin.guesser.orm_datagrid" class="Sonata\DoctrineORMAdminBundle\Guesser\FilterTypeGuesser">
-            <argument type="service" id="doctrine" />
             <tag name="sonata.admin.guesser.orm_datagrid" />
         </service>