Przeglądaj źródła

[DoctrineBundle] Making mapping information more flexible to allow different metadata mapping files for each bundle

Jonathan H. Wage 15 lat temu
rodzic
commit
85ed6df2f0

+ 1 - 1
src/Symfony/Framework/DoctrineBundle/Bundle.php

@@ -28,7 +28,7 @@ class Bundle extends BaseBundle
 {
   public function buildContainer(ContainerInterface $container)
   {
-    Loader::registerExtension(new DoctrineExtension());
+    Loader::registerExtension(new DoctrineExtension($container));
 
     $metadataDirs = array();
     $entityDirs = array();

+ 49 - 8
src/Symfony/Framework/DoctrineBundle/DependencyInjection/DoctrineExtension.php

@@ -7,6 +7,7 @@ use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
 use Symfony\Components\DependencyInjection\BuilderConfiguration;
 use Symfony\Components\DependencyInjection\Definition;
 use Symfony\Components\DependencyInjection\Reference;
+use Symfony\Components\DependencyInjection\Container;
 
 /*
  * This file is part of the symfony framework.
@@ -33,6 +34,11 @@ class DoctrineExtension extends LoaderExtension
 
   protected $alias;
 
+  public function __construct(Container $container)
+  {
+    $this->container = $container;
+  }
+
   public function setAlias($alias)
   {
     $this->alias = $alias;
@@ -188,6 +194,29 @@ class DoctrineExtension extends LoaderExtension
         $configuration->setDefinition(sprintf('doctrine.orm.%s_cache', $driver), $clone);
       }
 
+      // configure metadata driver for each bundle based on the type of mapping files found
+      $mappingDriverDef = new Definition('Doctrine\ORM\Mapping\Driver\DriverChain');
+      $bundleEntityMappings = array();
+      $bundleDirs = $this->container->getParameter('kernel.bundle_dirs');
+      foreach ($this->container->getParameter('kernel.bundles') as $className)
+      {
+        $tmp = dirname(str_replace('\\', '/', $className));
+        $namespace = str_replace('/', '\\', dirname($tmp));
+        $class = basename($tmp);
+
+        if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config/doctrine/metadata'))
+        {
+          $type = $this->detectMappingType($dir);
+          $mappingDriverDef->addMethodCall('addDriver', array(
+              new Reference(sprintf('doctrine.orm.metadata_driver.%s', $type)),
+              $namespace.'\\'.$class.'\\Entities'
+            )
+          );
+        }
+      }
+
+      $configuration->setDefinition('doctrine.orm.metadata_driver', $mappingDriverDef);
+
       $methods = array(
         'setMetadataCacheImpl' => new Reference('doctrine.orm.metadata_cache'),
         'setQueryCacheImpl' => new Reference('doctrine.orm.query_cache'),
@@ -227,14 +256,6 @@ class DoctrineExtension extends LoaderExtension
       }
     }
 
-    $configuration->setAlias(
-      'doctrine.orm.metadata_driver',
-      sprintf(
-        'doctrine.orm.metadata_driver.%s',
-        $configuration->getParameter('doctrine.orm.metadata_driver')
-      )
-    );
-
     $configuration->setAlias(
       'doctrine.orm.cache',
       sprintf(
@@ -246,6 +267,26 @@ class DoctrineExtension extends LoaderExtension
     return $configuration;
   }
 
+  /**
+   * Detect the type of Doctrine 2 mapping files located in a given directory.
+   * Simply finds the first file in a directory and returns the extension. If no
+   * mapping files are found then the annotation type is returned.
+   *
+   * @param string $dir
+   * @return string $type
+   */
+  protected function detectMappingType($dir)
+  {
+    $files = glob($dir.'/*.*');
+    if (!$files)
+    {
+      return 'annotation';
+    }
+    $info = pathinfo($files[0]);
+
+    return $info['extension'];
+  }
+
   /**
    * Returns the base path for the XSD files.
    *

+ 0 - 1
src/Symfony/Framework/DoctrineBundle/Resources/config/orm.xml

@@ -5,7 +5,6 @@
     xsi:schemaLocation="http://www.symfony-project.org/schema/dic/services http://www.symfony-project.org/schema/dic/services/services-1.0.xsd">
 
   <parameters>
-    <parameter key="doctrine.orm.metadata_driver">xml</parameter>
     <parameter key="doctrine.orm.cache_driver">array</parameter>
     <parameter key="doctrine.orm.cache.memcache.host">localhost</parameter>
     <parameter key="doctrine.orm.cache.memcache.port">11211</parameter>