Browse Source

[DoctrineBundle] made XML/YAML mapping drivers more BC with their Doctrine counterparts

The ultimate goal is to move back these mapping dirvers to the Doctrine project.
Fabien Potencier 14 years ago
parent
commit
41242dcc00

+ 7 - 3
src/Symfony/Bundle/DoctrineAbstractBundle/DependencyInjection/AbstractDoctrineExtension.php

@@ -193,9 +193,9 @@ abstract class AbstractDoctrineExtension extends Extension
                 $mappingDriverDef = $container->getDefinition($mappingService);
                 $args = $mappingDriverDef->getArguments();
                 if ($driverType == 'annotation') {
-                    $args[1] = array_merge($driverPaths, $args[1]);
+                    $args[1] = array_merge(array_values($driverPaths), $args[1]);
                 } else {
-                    $args[0] = array_merge($driverPaths, $args[0]);
+                    $args[0] = array_merge(array_values($driverPaths), $args[0]);
                 }
                 $mappingDriverDef->setArguments($args);
             } else if ($driverType == 'annotation') {
@@ -205,10 +205,14 @@ abstract class AbstractDoctrineExtension extends Extension
                 ));
             } else {
                 $mappingDriverDef = new Definition('%'.$this->getObjectManagerElementName('metadata.'.$driverType.'.class%'), array(
-                    $driverPaths
+                    array_values($driverPaths)
                 ));
             }
             $mappingDriverDef->setPublic(false);
+            if (false !== strpos($mappingDriverDef->getClass(), 'yml') || false !== strpos($mappingDriverDef->getClass(), 'xml')) {
+                $mappingDriverDef->addMethodCall('setNamespacePrefixes', array(array_flip($driverPaths)));
+                $mappingDriverDef->addMethodCall('setGlobalBasename', array('mapping'));
+            }
 
             $container->setDefinition($mappingService, $mappingDriverDef);
 

+ 46 - 8
src/Symfony/Bundle/DoctrineBundle/Mapping/Driver/XmlDriver.php

@@ -21,10 +21,31 @@ use Doctrine\ORM\Mapping\Driver\XmlDriver as BaseXmlDriver;
  */
 class XmlDriver extends BaseXmlDriver
 {
-    protected $_globalFile = 'mapping';
+    protected $_prefixes = array();
+    protected $_globalBasename;
     protected $_classCache;
     protected $_fileExtension = '.orm.xml';
 
+    public function setGlobalBasename($file)
+    {
+        $this->_globalBasename = $file;
+    }
+
+    public function getGlobalBasename()
+    {
+        return $this->_globalBasename;
+    }
+
+    public function setNamespacePrefixes($prefixes)
+    {
+        $this->_prefixes = $prefixes;
+    }
+
+    public function getNamespacePrefixes()
+    {
+        return $this->_prefixes;
+    }
+
     public function isTransient($className)
     {
         return !in_array($className, $this->getAllClassNames());
@@ -39,7 +60,7 @@ class XmlDriver extends BaseXmlDriver
         $classes = array();
 
         if ($this->_paths) {
-            foreach ((array) $this->_paths as $prefix => $path) {
+            foreach ((array) $this->_paths as $path) {
                 if (!is_dir($path)) {
                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
                 }
@@ -52,12 +73,16 @@ class XmlDriver extends BaseXmlDriver
                 foreach ($iterator as $file) {
                     $fileName = $file->getBasename($this->_fileExtension);
 
-                    if ($fileName == $file->getBasename() || $fileName == $this->_globalFile) {
+                    if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
                         continue;
                     }
 
                     // NOTE: All files found here means classes are not transient!
-                    $classes[] = $prefix.'\\'.str_replace('.', '\\', $fileName);
+                    if (isset($this->_prefixes[$path])) {
+                        $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
+                    } else {
+                        $classes[] = str_replace('.', '\\', $fileName);
+                    }
                 }
             }
         }
@@ -81,16 +106,29 @@ class XmlDriver extends BaseXmlDriver
     protected function initialize()
     {
         $this->_classCache = array();
-        foreach ($this->_paths as $path) {
-            if (file_exists($file = $path.'/'.$this->_globalFile.$this->_fileExtension)) {
-                $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
+        if (null !== $this->_globalBasename) {
+            foreach ($this->_paths as $path) {
+                if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
+                    $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
+                }
             }
         }
     }
 
     protected function _findMappingFile($className)
     {
-        foreach ($this->_paths as $prefix => $path) {
+        $defaultFileName = str_replace('\\', '.', $className) . $this->_fileExtension;
+        foreach ($this->_paths as $path) {
+            if (!isset($this->_prefixes[$path])) {
+                if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
+                    return $path . DIRECTORY_SEPARATOR . $defaultFileName;
+                }
+
+                continue;
+            }
+
+            $prefix = $this->_prefixes[$path];
+
             if (0 !== strpos($className, $prefix.'\\')) {
                 continue;
             }

+ 46 - 8
src/Symfony/Bundle/DoctrineBundle/Mapping/Driver/YamlDriver.php

@@ -21,10 +21,31 @@ use Doctrine\ORM\Mapping\Driver\YamlDriver as BaseYamlDriver;
  */
 class YamlDriver extends BaseYamlDriver
 {
-    protected $_globalFile = 'mapping';
+    protected $_prefixes = array();
+    protected $_globalBasename;
     protected $_classCache;
     protected $_fileExtension = '.orm.yml';
 
+    public function setGlobalBasename($file)
+    {
+        $this->_globalBasename = $file;
+    }
+
+    public function getGlobalBasename()
+    {
+        return $this->_globalBasename;
+    }
+
+    public function setNamespacePrefixes($prefixes)
+    {
+        $this->_prefixes = $prefixes;
+    }
+
+    public function getNamespacePrefixes()
+    {
+        return $this->_prefixes;
+    }
+
     public function isTransient($className)
     {
         return !in_array($className, $this->getAllClassNames());
@@ -39,7 +60,7 @@ class YamlDriver extends BaseYamlDriver
         $classes = array();
 
         if ($this->_paths) {
-            foreach ((array) $this->_paths as $prefix => $path) {
+            foreach ((array) $this->_paths as $path) {
                 if (!is_dir($path)) {
                     throw MappingException::fileMappingDriversRequireConfiguredDirectoryPath($path);
                 }
@@ -52,12 +73,16 @@ class YamlDriver extends BaseYamlDriver
                 foreach ($iterator as $file) {
                     $fileName = $file->getBasename($this->_fileExtension);
 
-                    if ($fileName == $file->getBasename() || $fileName == $this->_globalFile) {
+                    if ($fileName == $file->getBasename() || $fileName == $this->_globalBasename) {
                         continue;
                     }
 
                     // NOTE: All files found here means classes are not transient!
-                    $classes[] = $prefix.'\\'.str_replace('.', '\\', $fileName);
+                    if (isset($this->_prefixes[$path])) {
+                        $classes[] = $this->_prefixes[$path].'\\'.str_replace('.', '\\', $fileName);
+                    } else {
+                        $classes[] = str_replace('.', '\\', $fileName);
+                    }
                 }
             }
         }
@@ -81,16 +106,29 @@ class YamlDriver extends BaseYamlDriver
     protected function initialize()
     {
         $this->_classCache = array();
-        foreach ($this->_paths as $path) {
-            if (file_exists($file = $path.'/'.$this->_globalFile.$this->_fileExtension)) {
-                $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
+        if (null !== $this->_globalBasename) {
+            foreach ($this->_paths as $path) {
+                if (file_exists($file = $path.'/'.$this->_globalBasename.$this->_fileExtension)) {
+                    $this->_classCache = array_merge($this->_classCache, $this->_loadMappingFile($file));
+                }
             }
         }
     }
 
     protected function _findMappingFile($className)
     {
-        foreach ($this->_paths as $prefix => $path) {
+        $defaultFileName = str_replace('\\', '.', $className) . $this->_fileExtension;
+        foreach ($this->_paths as $path) {
+            if (!isset($this->_prefixes[$path])) {
+                if (file_exists($path . DIRECTORY_SEPARATOR . $defaultFileName)) {
+                    return $path . DIRECTORY_SEPARATOR . $defaultFileName;
+                }
+
+                continue;
+            }
+
+            $prefix = $this->_prefixes[$path];
+
             if (0 !== strpos($className, $prefix.'\\')) {
                 continue;
             }

+ 4 - 4
src/Symfony/Bundle/DoctrineBundle/Tests/DependencyInjection/AbstractDoctrineExtensionTest.php

@@ -556,12 +556,12 @@ abstract class AbstractDoctrineExtensionTest extends TestCase
 
         $ymlDef = $container->getDefinition('doctrine.orm.default_yml_metadata_driver');
         $this->assertDICConstructorArguments($ymlDef, array(
-            array('Fixtures\Bundles\YamlBundle\Entity' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
+            array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
         ));
 
         $xmlDef = $container->getDefinition('doctrine.orm.default_xml_metadata_driver');
         $this->assertDICConstructorArguments($xmlDef, array(
-            array('Fixtures\Bundles\XmlBundle' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
+            array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
         ));
     }
 
@@ -605,12 +605,12 @@ abstract class AbstractDoctrineExtensionTest extends TestCase
 
         $ymlDef = $container->getDefinition('doctrine.orm.em2_yml_metadata_driver');
         $this->assertDICConstructorArguments($ymlDef, array(
-            array('Fixtures\Bundles\YamlBundle\Entity' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
+            array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'YamlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
         ));
 
         $xmlDef = $container->getDefinition('doctrine.orm.em2_xml_metadata_driver');
         $this->assertDICConstructorArguments($xmlDef, array(
-            array('Fixtures\Bundles\XmlBundle' => __DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
+            array(__DIR__ .DIRECTORY_SEPARATOR.'Fixtures'.DIRECTORY_SEPARATOR.'Bundles'.DIRECTORY_SEPARATOR.'XmlBundle'.DIRECTORY_SEPARATOR.'Resources'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'doctrine')
         ));
     }
 

+ 4 - 1
src/Symfony/Bundle/DoctrineBundle/Tests/Mapping/Driver/XmlDriverTest.php

@@ -22,6 +22,9 @@ class XmlDriverTest extends AbstractDriverTest
 
     protected function getDriver(array $paths = array())
     {
-        return new XmlDriver($paths);
+        $driver = new XmlDriver(array_values($paths));
+        $driver->setNamespacePrefixes(array_flip($paths));
+
+        return $driver;
     }
 }

+ 4 - 1
src/Symfony/Bundle/DoctrineBundle/Tests/Mapping/Driver/YamlDriverTest.php

@@ -22,6 +22,9 @@ class YamlDriverTest extends AbstractDriverTest
 
     protected function getDriver(array $paths = array())
     {
-        return new YamlDriver($paths);
+        $driver = new YamlDriver(array_values($paths));
+        $driver->setNamespacePrefixes(array_flip($paths));
+
+        return $driver;
     }
 }