Explorar el Código

Refactored the file driver to use a FileLocator

Christophe Coevoet hace 13 años
padre
commit
9e58bb76e7
Se han modificado 3 ficheros con 26 adiciones y 36 borrados
  1. 1 1
      composer.json
  2. 11 27
      lib/Gedmo/Mapping/Driver/File.php
  3. 14 8
      lib/Gedmo/Mapping/ExtensionMetadataFactory.php

+ 1 - 1
composer.json

@@ -25,7 +25,7 @@
     ],
     "require": {
         "php": ">=5.3.2",
-        "doctrine/common": ">=2.1,<2.4-dev"
+        "doctrine/common": ">=2.2,<2.4-dev"
     },
     "suggest": {
         "doctrine/orm": ">=2.1",

+ 11 - 27
lib/Gedmo/Mapping/Driver/File.php

@@ -3,6 +3,7 @@
 namespace Gedmo\Mapping\Driver;
 
 use Doctrine\Common\Persistence\Mapping\Driver\FileDriver;
+use Doctrine\Common\Persistence\Mapping\Driver\FileLocator;
 use Gedmo\Mapping\Driver;
 
 /**
@@ -19,6 +20,11 @@ use Gedmo\Mapping\Driver;
  */
 abstract class File implements Driver
 {
+    /**
+     * @var FileLocator
+     */
+    protected $locator;
+
     /**
      * File extension, must be set in child class
      * @var string
@@ -30,11 +36,10 @@ abstract class File implements Driver
      */
     protected $_originalDriver = null;
 
-    /**
-     * List of paths for file search
-     * @var array
-     */
-    private $_paths = array();
+    public function setLocator(FileLocator $locator)
+    {
+        $this->locator = $locator;
+    }
 
     /**
      * Set the paths for file lookup
@@ -67,27 +72,6 @@ abstract class File implements Driver
      */
     abstract protected function _loadMappingFile($file);
 
-    /**
-     * Finds the mapping file for the class with the given name by searching
-     * through the configured paths.
-     *
-     * @param $className
-     * @return string The (absolute) file name.
-     * @throws RuntimeException if not found
-     */
-    protected function _findMappingFile($className)
-    {
-        $fileName = str_replace('\\', '.', $className) . $this->_extension;
-
-        // Check whether file exists
-        foreach ((array) $this->_paths as $path) {
-            if (file_exists($path . DIRECTORY_SEPARATOR . $fileName)) {
-                return $path . DIRECTORY_SEPARATOR . $fileName;
-            }
-        }
-        throw new \Gedmo\Exception\UnexpectedValueException("No mapping file found named '$fileName' for class '$className'.");
-    }
-
     /**
      * Tries to get a mapping for a given class
      *
@@ -106,7 +90,7 @@ abstract class File implements Driver
 
         //if no mapping found try to load mapping file again
         if (is_null($mapping)) {
-            $yaml = $this->_loadMappingFile($this->_findMappingFile($className));
+            $yaml = $this->_loadMappingFile($this->locator->findMappingFile($className));
             $mapping = $yaml[$className];
         }
 

+ 14 - 8
lib/Gedmo/Mapping/ExtensionMetadataFactory.php

@@ -2,7 +2,10 @@
 
 namespace Gedmo\Mapping;
 
+use Doctrine\Common\Persistence\Mapping\Driver\DefaultFileLocator;
+use Doctrine\Common\Persistence\Mapping\Driver\SymfonyFileLocator;
 use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
+use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
 use Doctrine\Common\Persistence\ObjectManager;
 use Gedmo\Mapping\Driver\File as FileDriver;
 use Gedmo\Mapping\Driver\AnnotationDriverInterface;
@@ -22,7 +25,7 @@ final class ExtensionMetadataFactory
 {
     /**
      * Extension driver
-     * @var Gedmo\Mapping\Driver
+     * @var \Gedmo\Mapping\Driver
      */
     private $driver;
 
@@ -126,23 +129,25 @@ final class ExtensionMetadataFactory
      *
      * @param object $omDriver
      * @throws DriverException if driver was not found in extension
-     * @return Gedmo\Mapping\Driver
+     * @return \Gedmo\Mapping\Driver
      */
     private function getDriver($omDriver)
     {
         $driver = null;
         $className = get_class($omDriver);
         $driverName = substr($className, strrpos($className, '\\') + 1);
-        if ($driverName == 'DriverChain') {
+        if ($omDriver instanceof MappingDriverChain || $driverName == 'DriverChain') {
             $driver = new Driver\Chain();
             foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
                 $driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
             }
         } else {
             $driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
+            $isSimplified = false;
             if (substr($driverName, 0, 10) === 'Simplified') {
                 // support for simplified file drivers
                 $driverName = substr($driverName, 10);
+                $isSimplified = true;
             }
             // create driver instance
             $driverClassName = $this->extensionNamespace . '\Mapping\Driver\\' . $driverName;
@@ -155,13 +160,14 @@ final class ExtensionMetadataFactory
             $driver = new $driverClassName();
             $driver->setOriginalDriver($omDriver);
             if ($driver instanceof FileDriver) {
+                /** @var $driver FileDriver */
                 if ($omDriver instanceof MappingDriver) {
-                    $driver->setPaths($omDriver->getLocator()->getPaths());
-                    $driver->setExtension($omDriver->getLocator()->getFileExtension());
+                    $driver->setLocator($omDriver->getLocator());
+                // BC for Doctrine 2.2
+                } elseif ($isSimplified) {
+                    $driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $omDriver->getFileExtension()));
                 } else {
-                    // BC for Doctrine 2.2
-                    $driver->setPaths($omDriver->getPaths());
-                    $driver->setExtension($omDriver->getFileExtension());
+                    $driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $omDriver->getFileExtension()));
                 }
             }
             if ($driver instanceof AnnotationDriverInterface) {