Sfoglia il codice sorgente

[all] made proper validation of metadata in case of composite keys

gediminasm 14 anni fa
parent
commit
aca550ee0d

+ 3 - 1
lib/Gedmo/Loggable/Mapping/Driver/Annotation.php

@@ -32,7 +32,9 @@ class Annotation implements Driver
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     {
-        
+        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+            throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
+        }
     }
 
     /**

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

@@ -33,7 +33,9 @@ class Yaml extends File implements Driver
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     {
-        
+        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+            throw new InvalidMappingException("Loggable does not support composite identifiers in class - {$meta->name}");
+        }
     }
 
     /**

+ 13 - 13
lib/Gedmo/Mapping/ExtensionMetadataFactory.php

@@ -8,7 +8,7 @@ use Doctrine\Common\Persistence\ObjectManager,
 /**
  * The extension metadata factory is responsible for extension driver
  * initialization and fully reading the extension metadata
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Mapping
  * @subpackage ExtensionMetadataFactory
@@ -22,23 +22,23 @@ class ExtensionMetadataFactory
      * @var Gedmo\Mapping\Driver
      */
     protected $driver;
-    
+
     /**
      * Object manager, entity or document
      * @var object
      */
     private $objectManager;
-    
+
     /**
      * Extension namespace
-     * 
+     *
      * @var string
      */
     private $extensionNamespace;
-    
+
     /**
      * Initializes extension driver
-     * 
+     *
      * @param ObjectManager $objectManager
      * @param string $extensionNamespace
      */
@@ -49,10 +49,10 @@ class ExtensionMetadataFactory
         $omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl();
         $this->driver = $this->getDriver($omDriver);
     }
-    
+
     /**
      * Reads extension metadata
-     * 
+     *
      * @param ClassMetadata $meta
      * @return array - the metatada configuration
      */
@@ -70,8 +70,8 @@ class ExtensionMetadataFactory
             }
         }
         $this->driver->readExtendedMetadata($meta, $config);
-        $this->driver->validateFullMetadata($meta, $config);
         if ($config) {
+            $this->driver->validateFullMetadata($meta, $config);
             // cache the metadata
             $cacheId = self::getCacheId($meta->name, $this->extensionNamespace);
             if ($cacheDriver = $this->objectManager->getMetadataFactory()->getCacheDriver()) {
@@ -80,10 +80,10 @@ class ExtensionMetadataFactory
         }
         return $config;
     }
-    
+
     /**
      * Get the cache id
-     * 
+     *
      * @param string $className
      * @param string $extensionNamespace
      * @return string
@@ -92,11 +92,11 @@ class ExtensionMetadataFactory
     {
         return $className . '\\$' . strtoupper(str_replace('\\', '_', $extensionNamespace)) . '_CLASSMETADATA';
     }
-    
+
     /**
      * Get the extended driver instance which will
      * read the metadata required by extension
-     * 
+     *
      * @param object $omDriver
      * @throws DriverException if driver was not found in extension
      * @return Gedmo\Mapping\Driver

+ 13 - 13
lib/Gedmo/Sluggable/Mapping/Driver/Annotation.php

@@ -12,7 +12,7 @@ use Gedmo\Mapping\Driver,
  * behavioral extension. Used for extraction of extended
  * metadata from Annotations specificaly for Sluggable
  * extension.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Sluggable.Mapping.Driver
  * @subpackage Annotation
@@ -25,32 +25,32 @@ class Annotation implements Driver
      * Annotation to mark field as sluggable and include it in slug building
      */
     const ANNOTATION_SLUGGABLE = 'Gedmo\Sluggable\Mapping\Sluggable';
-    
+
     /**
      * Annotation to identify field as one which holds the slug
      * together with slug options
      */
     const ANNOTATION_SLUG = 'Gedmo\Sluggable\Mapping\Slug';
-    
+
     /**
      * List of types which are valid for slug and sluggable fields
-     * 
+     *
      * @var array
      */
     private $validTypes = array(
         'string'
     );
-    
+
     /**
      * {@inheritDoc}
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     {
-        if ($config && !isset($config['fields'])) {
+        if (!isset($config['fields'])) {
             throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */
@@ -58,8 +58,8 @@ class Annotation implements Driver
         require_once __DIR__ . '/../Annotations.php';
         $reader = new AnnotationReader();
         $reader->setAnnotationNamespaceAlias('Gedmo\Sluggable\Mapping\\', 'gedmo');
-        
-        $class = $meta->getReflectionClass();        
+
+        $class = $meta->getReflectionClass();
         // property annotations
         foreach ($class->getProperties() as $property) {
             if ($meta->isMappedSuperclass && !$property->isPrivate() ||
@@ -87,11 +87,11 @@ class Annotation implements Driver
                 }
                 if (!$this->isValidField($meta, $field)) {
                     throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
-                } 
+                }
                 if (isset($config['slug'])) {
                     throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
                 }
-                
+
                 $config['slug'] = $field;
                 $config['style'] = $slug->style;
                 $config['updatable'] = $slug->updatable;
@@ -100,10 +100,10 @@ class Annotation implements Driver
             }
         }
     }
-    
+
     /**
      * Checks if $field type is valid as Sluggable field
-     * 
+     *
      * @param ClassMetadata $meta
      * @param string $field
      * @return boolean

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

@@ -12,7 +12,7 @@ use Gedmo\Mapping\Driver\File,
  * behavioral extension. Used for extraction of extended
  * metadata from yaml specificaly for Sluggable
  * extension.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Sluggable.Mapping.Driver
  * @subpackage Yaml
@@ -26,33 +26,33 @@ class Yaml extends File implements Driver
      * @var string
      */
     protected $_extension = '.dcm.yml';
-    
+
     /**
      * List of types which are valid for slug and sluggable fields
-     * 
+     *
      * @var array
      */
     private $validTypes = array(
         'string'
     );
-    
+
     /**
      * {@inheritDoc}
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     {
-        if ($config && !isset($config['fields'])) {
+        if (!isset($config['fields'])) {
             throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */
     public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
         $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
         $mapping = $yaml[$meta->name];
-        
+
         if (isset($mapping['fields'])) {
             foreach ($mapping['fields'] as $field => $fieldMapping) {
                 if (isset($fieldMapping['gedmo'])) {
@@ -65,29 +65,29 @@ class Yaml extends File implements Driver
                         $slug = $fieldMapping['gedmo']['slug'];
                         if (!$this->isValidField($meta, $field)) {
                             throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
-                        } 
+                        }
                         if (isset($config['slug'])) {
                             throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
                         }
-                        
+
                         $config['slug'] = $field;
-                        $config['style'] = isset($slug['style']) ? 
+                        $config['style'] = isset($slug['style']) ?
                             (string)$slug['style'] : 'default';
-                        
-                        $config['updatable'] = isset($slug['updatable']) ? 
+
+                        $config['updatable'] = isset($slug['updatable']) ?
                             (bool)$slug['updatable'] : true;
-                            
-                        $config['unique'] = isset($slug['unique']) ? 
+
+                        $config['unique'] = isset($slug['unique']) ?
                             (bool)$slug['unique'] : true;
-                            
-                        $config['separator'] = isset($slug['separator']) ? 
+
+                        $config['separator'] = isset($slug['separator']) ?
                             (string)$slug['separator'] : '-';
                     }
                 }
             }
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */
@@ -95,10 +95,10 @@ class Yaml extends File implements Driver
     {
         return \Symfony\Component\Yaml\Yaml::load($file);
     }
-    
+
     /**
      * Checks if $field type is valid as Sluggable field
-     * 
+     *
      * @param ClassMetadata $meta
      * @param string $field
      * @return boolean

+ 15 - 10
lib/Gedmo/Translatable/Mapping/Driver/Annotation.php

@@ -12,7 +12,7 @@ use Gedmo\Mapping\Driver,
  * behavioral extension. Used for extraction of extended
  * metadata from Annotations specificaly for Translatable
  * extension.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Translatable.Mapping.Driver
  * @subpackage Annotation
@@ -25,29 +25,34 @@ class Annotation implements Driver
      * Annotation to identity translation entity to be used for translation storage
      */
     const ANNOTATION_ENTITY_CLASS = 'Gedmo\Translatable\Mapping\TranslationEntity';
-    
+
     /**
-     * Annotation to identify field as translatable 
+     * Annotation to identify field as translatable
      */
     const ANNOTATION_TRANSLATABLE = 'Gedmo\Translatable\Mapping\Translatable';
-    
+
     /**
      * Annotation to identify field which can store used locale or language
      * alias is ANNOTATION_LANGUAGE
      */
     const ANNOTATION_LOCALE = 'Gedmo\Translatable\Mapping\Locale';
-    
+
     /**
      * Annotation to identify field which can store used locale or language
      * alias is ANNOTATION_LOCALE
      */
     const ANNOTATION_LANGUAGE = 'Gedmo\Translatable\Mapping\Language';
-    
+
     /**
      * {@inheritDoc}
      */
-    public function validateFullMetadata(ClassMetadata $meta, array $config) {}
-    
+    public function validateFullMetadata(ClassMetadata $meta, array $config)
+    {
+        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+            throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
+        }
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -55,7 +60,7 @@ class Annotation implements Driver
         require_once __DIR__ . '/../Annotations.php';
         $reader = new AnnotationReader();
         $reader->setAnnotationNamespaceAlias('Gedmo\Translatable\Mapping\\', 'gedmo');
-        
+
         $class = $meta->getReflectionClass();
         // class annotations
         $classAnnotations = $reader->getClassAnnotations($class);
@@ -66,7 +71,7 @@ class Annotation implements Driver
             }
             $config['translationClass'] = $annot->class;
         }
-        
+
         // property annotations
         foreach ($class->getProperties() as $property) {
             if ($meta->isMappedSuperclass && !$property->isPrivate() ||

+ 11 - 6
lib/Gedmo/Translatable/Mapping/Driver/Yaml.php

@@ -12,7 +12,7 @@ use Gedmo\Mapping\Driver\File,
  * behavioral extension. Used for extraction of extended
  * metadata from yaml specificaly for Translatable
  * extension.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Translatable.Mapping.Driver
  * @subpackage Yaml
@@ -26,19 +26,24 @@ class Yaml extends File implements Driver
      * @var string
      */
     protected $_extension = '.dcm.yml';
-    
+
     /**
      * {@inheritDoc}
      */
-    public function validateFullMetadata(ClassMetadata $meta, array $config) {}
-    
+    public function validateFullMetadata(ClassMetadata $meta, array $config)
+    {
+        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+            throw new InvalidMappingException("Translatable does not support composite identifiers in class - {$meta->name}");
+        }
+    }
+
     /**
      * {@inheritDoc}
      */
     public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
         $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
         $mapping = $yaml[$meta->name];
-        
+
         if (isset($mapping['gedmo'])) {
             $classMapping = $mapping['gedmo'];
             if (isset($classMapping['translation']['entity'])) {
@@ -65,7 +70,7 @@ class Yaml extends File implements Driver
             }
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */

+ 26 - 27
lib/Gedmo/Tree/Mapping/Driver/Annotation.php

@@ -12,7 +12,7 @@ use Gedmo\Mapping\Driver,
  * behavioral extension. Used for extraction of extended
  * metadata from Annotations specificaly for Tree
  * extension.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Tree.Mapping.Driver
  * @subpackage Annotation
@@ -25,45 +25,45 @@ class Annotation implements Driver
      * Annotation to define the tree type
      */
     const ANNOTATION_TREE = 'Gedmo\Tree\Mapping\Tree';
-    
+
     /**
      * Annotation to mark field as one which will store left value
      */
     const ANNOTATION_LEFT = 'Gedmo\Tree\Mapping\TreeLeft';
-    
+
     /**
      * Annotation to mark field as one which will store right value
      */
     const ANNOTATION_RIGHT = 'Gedmo\Tree\Mapping\TreeRight';
-    
+
     /**
      * Annotation to mark relative parent field
      */
     const ANNOTATION_PARENT = 'Gedmo\Tree\Mapping\TreeParent';
-    
+
     /**
      * Annotation to mark node level
      */
     const ANNOTATION_LEVEL = 'Gedmo\Tree\Mapping\TreeLevel';
-    
+
     /**
      * Annotation to mark field as tree root
      */
     const ANNOTATION_ROOT = 'Gedmo\Tree\Mapping\TreeRoot';
-    
+
     /**
      * Annotation to specify closure tree class
      */
     const ANNOTATION_CLOSURE = 'Gedmo\Tree\Mapping\TreeClosure';
-    
+
     /**
      * Annotation to mark field as child count
      */
     const ANNOTATION_CHILD_COUNT = 'Gedmo\Tree\Mapping\TreeChildCount';
-    
+
     /**
      * List of types which are valid for tree fields
-     * 
+     *
      * @var array
      */
     private $validTypes = array(
@@ -71,30 +71,33 @@ class Annotation implements Driver
         'smallint',
         'bigint'
     );
-    
+
     /**
      * List of tree strategies available
-     * 
+     *
      * @var array
      */
     private $strategies = array(
         'nested',
         'closure'
     );
-    
+
     /**
      * {@inheritDoc}
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config)
-    {        
+    {
         if (isset($config['strategy'])) {
+            if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+                throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
+            }
             $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
             $this->$method($meta, $config);
         } elseif ($config) {
             throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */
@@ -102,7 +105,7 @@ class Annotation implements Driver
         require_once __DIR__ . '/../Annotations.php';
         $reader = new AnnotationReader();
         $reader->setAnnotationNamespaceAlias('Gedmo\Tree\Mapping\\', 'gedmo');
-        
+
         $class = $meta->getReflectionClass();
         // class annotations
         $classAnnotations = $reader->getClassAnnotations($class);
@@ -120,7 +123,7 @@ class Annotation implements Driver
             }
             $config['closure'] = $annot->class;
         }
-        
+
         // property annotations
         foreach ($class->getProperties() as $property) {
             if ($meta->isMappedSuperclass && !$property->isPrivate() ||
@@ -194,10 +197,10 @@ class Annotation implements Driver
             }
         }
     }
-    
+
     /**
      * Checks if $field type is valid
-     * 
+     *
      * @param ClassMetadata $meta
      * @param string $field
      * @return boolean
@@ -207,10 +210,10 @@ class Annotation implements Driver
         $mapping = $meta->getFieldMapping($field);
         return $mapping && in_array($mapping['type'], $this->validTypes);
     }
-    
+
     /**
      * Validates metadata for nested type tree
-     * 
+     *
      * @param ClassMetadata $meta
      * @param array $config
      * @throws InvalidMappingException
@@ -232,10 +235,10 @@ class Annotation implements Driver
             throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
         }
     }
-    
+
     /**
      * Validates metadata for closure type tree
-     * 
+     *
      * @param ClassMetadata $meta
      * @param array $config
      * @throws InvalidMappingException
@@ -243,10 +246,6 @@ class Annotation implements Driver
      */
     private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
     {
-        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
-            throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
-        }
-        
         $missingFields = array();
         if (!isset($config['parent'])) {
             $missingFields[] = 'ancestor';

+ 18 - 19
lib/Gedmo/Tree/Mapping/Driver/Yaml.php

@@ -12,7 +12,7 @@ use Gedmo\Mapping\Driver\File,
  * behavioral extension. Used for extraction of extended
  * metadata from yaml specificaly for Tree
  * extension.
- * 
+ *
  * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  * @package Gedmo.Tree.Mapping.Driver
  * @subpackage Yaml
@@ -26,10 +26,10 @@ class Yaml extends File implements Driver
      * @var string
      */
     protected $_extension = '.dcm.yml';
-    
+
     /**
      * List of types which are valid for timestamp
-     * 
+     *
      * @var array
      */
     private $validTypes = array(
@@ -37,37 +37,40 @@ class Yaml extends File implements Driver
         'smallint',
         'bigint'
     );
-    
+
     /**
      * List of tree strategies available
-     * 
+     *
      * @var array
      */
     private $strategies = array(
         'nested',
         'closure'
     );
-    
+
     /**
      * {@inheritDoc}
      */
     public function validateFullMetadata(ClassMetadata $meta, array $config)
     {
         if (isset($config['strategy'])) {
+            if (is_array($meta->identifier) && count($meta->identifier) > 1) {
+                throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
+            }
             $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
             $this->$method($meta, $config);
         } elseif ($config) {
             throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */
     public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
         $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
         $mapping = $yaml[$meta->name];
-        
+
         if (isset($mapping['gedmo'])) {
             $classMapping = $mapping['gedmo'];
             if (isset($classMapping['tree']['type'])) {
@@ -130,7 +133,7 @@ class Yaml extends File implements Driver
             }
         }
     }
-    
+
     /**
      * {@inheritDoc}
      */
@@ -138,10 +141,10 @@ class Yaml extends File implements Driver
     {
         return \Symfony\Component\Yaml\Yaml::load($file);
     }
-    
+
     /**
      * Checks if $field type is valid
-     * 
+     *
      * @param ClassMetadata $meta
      * @param string $field
      * @return boolean
@@ -151,10 +154,10 @@ class Yaml extends File implements Driver
         $mapping = $meta->getFieldMapping($field);
         return $mapping && in_array($mapping['type'], $this->validTypes);
     }
-    
+
     /**
      * Validates metadata for nested type tree
-     * 
+     *
      * @param ClassMetadata $meta
      * @param array $config
      * @throws InvalidMappingException
@@ -176,10 +179,10 @@ class Yaml extends File implements Driver
             throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
         }
     }
-    
+
     /**
      * Validates metadata for closure type tree
-     * 
+     *
      * @param ClassMetadata $meta
      * @param array $config
      * @throws InvalidMappingException
@@ -187,10 +190,6 @@ class Yaml extends File implements Driver
      */
     private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
     {
-        if (is_array($meta->identifier) && count($meta->identifier) > 1) {
-            throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
-        }
-        
         $missingFields = array();
         if (!isset($config['parent'])) {
             $missingFields[] = 'ancestor';