Explorar el Código

Added support for loading additional data from original driver.
If original driver doesn't have metadata, then it automatically
falls back to trying to load a file.

Miha Vrhovnik hace 14 años
padre
commit
70ed10f294

+ 15 - 0
lib/Gedmo/Loggable/Mapping/Driver/Annotation.php

@@ -38,6 +38,10 @@ class Annotation implements AnnotationDriverInterface
      */
      */
     private $reader;
     private $reader;
 
 
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
@@ -94,4 +98,15 @@ class Annotation implements AnnotationDriverInterface
             }
             }
         }
         }
     }
     }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }

+ 1 - 2
lib/Gedmo/Loggable/Mapping/Driver/Yaml.php

@@ -46,8 +46,7 @@ class Yaml extends File implements Driver
      */
      */
     public function readExtendedMetadata(ClassMetadata $meta, array &$config)
     public function readExtendedMetadata(ClassMetadata $meta, array &$config)
     {
     {
-        $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
-        $mapping = $yaml[$meta->name];
+        $mapping = $this->_getMapping($meta->name);
 
 
         if (isset($mapping['gedmo'])) {
         if (isset($mapping['gedmo'])) {
             $classMapping = $mapping['gedmo'];
             $classMapping = $mapping['gedmo'];

+ 9 - 1
lib/Gedmo/Mapping/Driver.php

@@ -38,4 +38,12 @@ interface Driver
      * @return void
      * @return void
      */
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config);
     public function validateFullMetadata(ClassMetadata $meta, array $config);
-}
+
+    /**
+     * Passes in the original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver);
+}

+ 11 - 0
lib/Gedmo/Mapping/Driver/Chain.php

@@ -62,4 +62,15 @@ class Chain implements Driver
         }
         }
         throw new \Gedmo\Exception\UnexpectedValueException('Class ' . $meta->name . ' is not a valid entity or mapped super class.');
         throw new \Gedmo\Exception\UnexpectedValueException('Class ' . $meta->name . ' is not a valid entity or mapped super class.');
     }
     }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        //not needed here
+    }
 }
 }

+ 43 - 1
lib/Gedmo/Mapping/Driver/File.php

@@ -2,7 +2,8 @@
 
 
 namespace Gedmo\Mapping\Driver;
 namespace Gedmo\Mapping\Driver;
 
 
-use Gedmo\Mapping\Driver;
+use Gedmo\Mapping\Driver,
+    Doctrine\ORM\Mapping\Driver\AbstractFileDriver;
 
 
 /**
 /**
  * The mapping FileDriver abstract class, defines the
  * The mapping FileDriver abstract class, defines the
@@ -24,6 +25,11 @@ abstract class File implements Driver
      */
      */
     protected $_extension;
     protected $_extension;
 
 
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
+
     /**
     /**
      * List of paths for file search
      * List of paths for file search
      * @var array
      * @var array
@@ -81,4 +87,40 @@ abstract class File implements Driver
         }
         }
         throw new \Gedmo\Exception\UnexpectedValueException("No mapping file found named '$fileName' for class '$className'.");
         throw new \Gedmo\Exception\UnexpectedValueException("No mapping file found named '$fileName' for class '$className'.");
     }
     }
+
+    /**
+     * Tries to get a mapping for a given class
+     *
+     * @param  $className
+     * @return null|array|object
+     */
+    protected function _getMapping($className)
+    {
+        //try loading mapping from original driver first
+        $mapping = null;
+        if (!is_null($this->_originalDriver)) {
+            if ($this->_originalDriver instanceof AbstractFileDriver) {
+                $mapping = $this->_originalDriver->getElement($className);
+            }
+        }
+
+        //if no mapping found try to load mapping file again
+        if (is_null($mapping)) {
+            $yaml = $this->_loadMappingFile($this->_findMappingFile($className));
+            $mapping = $yaml[$className];
+        }
+
+        return $mapping;
+    }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }

+ 1 - 0
lib/Gedmo/Mapping/ExtensionMetadataFactory.php

@@ -150,6 +150,7 @@ final class ExtensionMetadataFactory
                 }
                 }
             }
             }
             $driver = new $driverClassName();
             $driver = new $driverClassName();
+            $driver->setOriginalDriver($omDriver);
             if ($driver instanceof FileDriver) {
             if ($driver instanceof FileDriver) {
                 $driver->setPaths($omDriver->getPaths());
                 $driver->setPaths($omDriver->getPaths());
             }
             }

+ 16 - 0
lib/Gedmo/Sluggable/Mapping/Driver/Annotation.php

@@ -48,6 +48,11 @@ class Annotation implements AnnotationDriverInterface
      */
      */
     private $reader;
     private $reader;
 
 
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
@@ -124,4 +129,15 @@ class Annotation implements AnnotationDriverInterface
         $mapping = $meta->getFieldMapping($field);
         $mapping = $meta->getFieldMapping($field);
         return $mapping && in_array($mapping['type'], $this->validTypes);
         return $mapping && in_array($mapping['type'], $this->validTypes);
     }
     }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }

+ 3 - 3
lib/Gedmo/Sluggable/Mapping/Driver/Yaml.php

@@ -49,9 +49,9 @@ class Yaml extends File implements Driver
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
-        $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
-        $mapping = $yaml[$meta->name];
+    public function readExtendedMetadata(ClassMetadata $meta, array &$config)
+    {
+        $mapping = $this->_getMapping($meta->name);
 
 
         if (isset($mapping['fields'])) {
         if (isset($mapping['fields'])) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {

+ 16 - 0
lib/Gedmo/Timestampable/Mapping/Driver/Annotation.php

@@ -45,6 +45,11 @@ class Annotation implements AnnotationDriverInterface
      */
      */
     private $reader;
     private $reader;
 
 
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
@@ -110,4 +115,15 @@ class Annotation implements AnnotationDriverInterface
         $mapping = $meta->getFieldMapping($field);
         $mapping = $meta->getFieldMapping($field);
         return $mapping && in_array($mapping['type'], $this->validTypes);
         return $mapping && in_array($mapping['type'], $this->validTypes);
     }
     }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }

+ 4 - 4
lib/Gedmo/Timestampable/Mapping/Driver/Yaml.php

@@ -47,10 +47,10 @@ class Yaml extends File implements Driver
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
-        $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
-        $mapping = $yaml[$meta->name];
-        
+    public function readExtendedMetadata(ClassMetadata $meta, array &$config)
+    {
+        $mapping = $this->_getMapping($meta->name);
+
         if (isset($mapping['fields'])) {
         if (isset($mapping['fields'])) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {
                 if (isset($fieldMapping['gedmo']['timestampable'])) {
                 if (isset($fieldMapping['gedmo']['timestampable'])) {

+ 16 - 0
lib/Gedmo/Translatable/Mapping/Driver/Annotation.php

@@ -49,6 +49,11 @@ class Annotation implements AnnotationDriverInterface
      */
      */
     private $reader;
     private $reader;
 
 
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
@@ -113,4 +118,15 @@ class Annotation implements AnnotationDriverInterface
             }
             }
         }
         }
     }
     }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }

+ 3 - 3
lib/Gedmo/Translatable/Mapping/Driver/Yaml.php

@@ -40,9 +40,9 @@ class Yaml extends File implements Driver
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
-        $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
-        $mapping = $yaml[$meta->name];
+    public function readExtendedMetadata(ClassMetadata $meta, array &$config)
+    {
+        $mapping = $this->_getMapping($meta->name);
 
 
         if (isset($mapping['gedmo'])) {
         if (isset($mapping['gedmo'])) {
             $classMapping = $mapping['gedmo'];
             $classMapping = $mapping['gedmo'];

+ 16 - 0
lib/Gedmo/Tree/Mapping/Driver/Annotation.php

@@ -83,6 +83,11 @@ class Annotation implements AnnotationDriverInterface
      */
      */
     private $reader;
     private $reader;
 
 
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
@@ -248,4 +253,15 @@ class Annotation implements AnnotationDriverInterface
             throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
             throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
         }
         }
     }
     }
+
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }

+ 3 - 3
lib/Gedmo/Tree/Mapping/Driver/Yaml.php

@@ -67,9 +67,9 @@ class Yaml extends File implements Driver
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */
-    public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
-        $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
-        $mapping = $yaml[$meta->name];
+    public function readExtendedMetadata(ClassMetadata $meta, array &$config)
+    {
+        $mapping = $this->_getMapping($meta->name);
 
 
         if (isset($mapping['gedmo'])) {
         if (isset($mapping['gedmo'])) {
             $classMapping = $mapping['gedmo'];
             $classMapping = $mapping['gedmo'];

+ 16 - 0
tests/Gedmo/Mapping/Mock/Extension/Encoder/Mapping/Driver/Annotation.php

@@ -10,6 +10,11 @@ use Doctrine\Common\Persistence\Mapping\ClassMetadata;
 
 
 class Annotation implements Driver
 class Annotation implements Driver
 {
 {
+    /**
+     * original driver if it is available
+     */
+    protected $_originalDriver = null;
+    
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     {
     {
         // in our case values are independant from each other
         // in our case values are independant from each other
@@ -56,4 +61,15 @@ class Annotation implements Driver
             }
             }
         }
         }
     }
     }
+    
+    /**
+     * Passes in the mapping read by original driver
+     *
+     * @param $driver
+     * @return void
+     */
+    public function setOriginalDriver($driver)
+    {
+        $this->_originalDriver = $driver;
+    }
 }
 }