Bläddra i källkod

[Uploadable] Added XML and YML drivers

comfortablynumb 13 år sedan
förälder
incheckning
4b6a214e9c

+ 0 - 2
lib/Gedmo/Mapping/Annotation/All.php

@@ -32,8 +32,6 @@ include __DIR__.'/TreeRight.php';
 include __DIR__.'/TreeRoot.php';
 include __DIR__.'/Versioned.php';
 include __DIR__.'/Uploadable.php';
-include __DIR__.'/UploadableFileInfo.php';
 include __DIR__.'/UploadableFileMimeType.php';
 include __DIR__.'/UploadableFilePath.php';
 include __DIR__.'/UploadableFileSize.php';
-include __DIR__.'/UploadablePath.php';

+ 6 - 0
lib/Gedmo/Mapping/Annotation/Uploadable.php

@@ -27,5 +27,11 @@ final class Uploadable extends Annotation
 
     /** @var string */
     public $path = '';
+
+    /** @var string @required */
+    public $fileInfoProperty;
+
+    /** @var string */
+    public $pathMethod = '';
 }
 

+ 0 - 23
lib/Gedmo/Mapping/Annotation/UploadableFileInfo.php

@@ -1,23 +0,0 @@
-<?php
-
-namespace Gedmo\Mapping\Annotation;
-
-use Doctrine\Common\Annotations\Annotation;
-
-/**
- * UploadableFileInfo Annotation for Uploadable behavioral extension
- *
- * @Annotation
- * @Target("PROPERTY")
- *
- * @author Gustavo Falco <comfortablynumb84@gmail.com>
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @package Gedmo.Mapping.Annotation
- * @subpackage UploadableFileInfo
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
-final class UploadableFileInfo extends Annotation
-{
-}
-

+ 0 - 23
lib/Gedmo/Mapping/Annotation/UploadablePath.php

@@ -1,23 +0,0 @@
-<?php
-
-namespace Gedmo\Mapping\Annotation;
-
-use Doctrine\Common\Annotations\Annotation;
-
-/**
- * UploadablePath Annotation for Uploadable behavioral extension
- *
- * @Annotation
- * @Target("METHOD")
- *
- * @author Gustavo Falco <comfortablynumb84@gmail.com>
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @package Gedmo.Mapping.Annotation
- * @subpackage Uploadable
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
-final class UploadablePath extends Annotation
-{
-}
-

+ 2 - 19
lib/Gedmo/Uploadable/Mapping/Driver/Annotation.php

@@ -29,8 +29,6 @@ class Annotation implements AnnotationDriverInterface
     const UPLOADABLE_FILE_MIME_TYPE = 'Gedmo\\Mapping\\Annotation\\UploadableFileMimeType';
     const UPLOADABLE_FILE_PATH = 'Gedmo\\Mapping\\Annotation\\UploadableFilePath';
     const UPLOADABLE_FILE_SIZE = 'Gedmo\\Mapping\\Annotation\\UploadableFileSize';
-    const UPLOADABLE_FILE_INFO = 'Gedmo\\Mapping\\Annotation\\UploadableFileInfo';
-    const UPLOADABLE_PATH = 'Gedmo\\Mapping\\Annotation\\UploadablePath';
 
     /**
      * Annotation reader instance
@@ -78,38 +76,23 @@ class Annotation implements AnnotationDriverInterface
             $config['allowOverwrite'] = $annot->allowOverwrite;
             $config['appendNumber'] = $annot->appendNumber;
             $config['path'] = $annot->path;
-            $config['pathMethod'] = '';
+            $config['pathMethod'] = $annot->pathMethod;
             $config['fileMimeTypeField'] = false;
             $config['filePathField'] = false;
             $config['fileSizeField'] = false;
+            $config['fileInfoProperty'] = $annot->fileInfoProperty;
 
             foreach ($class->getProperties() as $prop) {
                 if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {
                     $config['fileMimeTypeField'] = $prop->getName();
-
-                    Validator::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
                 }
 
                 if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_PATH)) {
                     $config['filePathField'] = $prop->getName();
-
-                    Validator::validateFilePathField($meta, $config['filePathField']);
                 }
 
                 if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_SIZE)) {
                     $config['fileSizeField'] = $prop->getName();
-
-                    Validator::validateFileSizeField($meta, $config['fileSizeField']);
-                }
-
-                if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_INFO)) {
-                    $config['fileInfoField'] = $prop->getName();
-                }
-            }
-
-            foreach ($class->getMethods() as $method) {
-                if ($this->reader->getMethodAnnotation($method, self::UPLOADABLE_PATH)) {
-                    $config['pathMethod'] = $method->getName();
                 }
             }
 

+ 9 - 17
lib/Gedmo/Uploadable/Mapping/Driver/Xml.php

@@ -35,15 +35,16 @@ class Xml extends BaseXml
         $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
 
         if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
-            // TODO
-
-            /*
-             if (isset($xml->uploadable)) {
+            if (isset($xml->uploadable)) {
+                $xmlUploadable = $xml->uploadable;
                 $config['uploadable'] = true;
-                $config['allowOverwrite'] = isset($xml->allowOverwrite) ? $xml->allowOverwrite : false;
-                $config['appendNumber'] = isset($xml->appendNumber) ? $xml->appendNumber: false;
-                $config['path'] = isset($xml->path) ? $xml->path: '';
-                $config['pathMethod'] = '';
+                $config['allowOverwrite'] = $this->_isAttributeSet($xmlUploadable, 'allow-overwrite') ?
+                    (bool) $this->_getAttribute($xmlUploadable, 'allow-overwrite') : false;
+                $config['appendNumber'] = $this->_isAttributeSet($xmlUploadable, 'append-number') ?
+                    (bool) $this->_getAttribute($xmlUploadable, 'append-number') : false;
+                $config['path'] = $this->_getAttribute($xml->{'uploadable'}, 'path');
+                $config['pathMethod'] = $this->_getAttribute($xml->{'uploadable'}, 'path-method');
+                $config['fileInfoProperty'] = $this->_getAttribute($xml->{'uploadable'}, 'file-info-property');
                 $config['fileMimeTypeField'] = false;
                 $config['filePathField'] = false;
                 $config['fileSizeField'] = false;
@@ -57,25 +58,16 @@ class Xml extends BaseXml
 
                         if (isset($mapping->{'uploadable-file-mime-type'})) {
                             $config['fileMimeTypeField'] = $field;
-
-                            Validator::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
                         } else if (isset($mapping->{'uploadable-file-size'})) {
                             $config['fileSizeField'] = $field;
-
-                            Validator::validateFileSizeField($meta, $config['fileSizeField']);
                         } else if (isset($mapping->{'uploadable-file-path'})) {
                             $config['filePathField'] = $field;
-
-                            Validator::validateFilePathField($meta, $config['filePathField']);
-                        } else if (isset($mapping->{'uploadable-file-info'})) {
-                            $config['fileInfoField'] = $field;
                         }
                     }
                 }
 
                 Validator::validateConfiguration($meta, $config);
             }
-            */
         }
     }
 }

+ 31 - 1
lib/Gedmo/Uploadable/Mapping/Driver/Yaml.php

@@ -38,7 +38,37 @@ class Yaml extends File implements Driver
         if (isset($mapping['gedmo'])) {
             $classMapping = $mapping['gedmo'];
 
-            // TODO
+            if (isset($classMapping['uploadable'])) {
+                $uploadable = $classMapping['uploadable'];
+
+                $config['uploadable'] = true;
+                $config['allowOverwrite'] = isset($uploadable['allowOverwrite']) ?
+                    (bool) $uploadable['allowOverwrite'] : false;
+                $config['appendNumber'] = isset($uploadable['appendNumber']) ?
+                    (bool) $uploadable['appendNumber'] : false;
+                $config['path'] = isset($uploadable['path']) ? $uploadable['path'] : '';
+                $config['pathMethod'] = isset($uploadable['pathMethod']) ? $uploadable['pathMethod'] : '';
+                $config['fileInfoProperty'] = isset($uploadable['fileInfoProperty']) ? $uploadable['fileInfoProperty'] : '';
+                $config['fileMimeTypeField'] = false;
+                $config['filePathField'] = false;
+                $config['fileSizeField'] = false;
+
+                if (isset($mapping['fields'])) {
+                    foreach ($mapping['fields'] as $field => $info) {
+                        if (isset($info['gedmo'])) {
+                            if ($info['gedmo'][0] === 'uploadableFileMimeType') {
+                                $config['fileMimeTypeField'] = $field;
+                            } else if ($info['gedmo'][0] === 'uploadableFileSize') {
+                                $config['fileSizeField'] = $field;
+                            } else if ($info['gedmo'][0] === 'uploadableFilePath') {
+                                $config['filePathField'] = $field;
+                            }
+                        }
+                    }
+                }
+
+                Validator::validateConfiguration($meta, $config);
+            }
         }
     }
 

+ 28 - 4
lib/Gedmo/Uploadable/Mapping/Validator.php

@@ -93,16 +93,25 @@ class Validator
 
     public static function validateConfiguration(ClassMetadata $meta, array $config)
     {
+        $refl = $meta->getReflectionClass();
+
         if (!$config['filePathField']) {
             throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFilePath field.',
                 $meta->name
             ));
         }
 
-        if (!$config['fileInfoField']) {
-            throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFileInfo field.',
+        if (!$config['fileInfoProperty']) {
+            throw new InvalidMappingException(sprintf('Class "%s" must define a "fileInfoProperty".',
                 $meta->name
             ));
+        } else {
+            if (!$refl->hasProperty($config['fileInfoProperty'])) {
+                throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have property "%s"!',
+                    $meta->name,
+                    $config['fileInfoProperty']
+                ));
+            }
         }
 
         if ($config['path'] === '' && $config['pathMethod'] === '') {
@@ -112,8 +121,23 @@ class Validator
                 self::UPLOADABLE,
                 self::UPLOADABLE_PATH
             ));
-        } else if ($config['path'] !== '') {
-            Validator::validatePath($config['path']);
+        } else if ($config['pathMethod'] !== '') {
+            if (!$refl->hasMethod($config['pathMethod'])) {
+                throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
+                    $meta->name,
+                    $config['pathMethod']
+                ));
+            }
+        }
+
+        if ($config['fileMimeTypeField']) {
+            self::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
         }
+
+        if ($config['fileSizeField']) {
+            self::validateFileSizeField($meta, $config['fileSizeField']);
+        }
+
+        self::validateFilePathField($meta, $config['filePathField']);
     }
 }

+ 1 - 1
lib/Gedmo/Uploadable/UploadableListener.php

@@ -106,7 +106,7 @@ class UploadableListener extends MappedEventSubscriber
     public function processFile(UnitOfWork $uow, AdapterInterface $ea, ClassMetadata $meta, array $config, $object, $action)
     {
         $refl = $meta->getReflectionClass();
-        $fileInfoProp = $refl->getProperty($config['fileInfoField']);
+        $fileInfoProp = $refl->getProperty($config['fileInfoProperty']);
         $fileInfoProp->setAccessible(true);
         $file = $fileInfoProp->getValue($object);
         $filePathField = $refl->getProperty($config['filePathField']);

+ 12 - 0
schemas/orm/doctrine-extensions-mapping-2-2.xsd

@@ -27,6 +27,7 @@ people to push their own additional attributes/elements into the same field elem
   <xs:element name="tree-closure" type="gedmo:tree-closure"/>
   <xs:element name="loggable" type="gedmo:loggable"/>
   <xs:element name="soft-deleteable" type="gedmo:soft-deleteable"/>
+  <xs:element name="uploadable" type="gedmo:uploadable"/>
   <!-- field -->
   <xs:element name="slug" type="gedmo:slug"/>
   <xs:element name="translatable" type="gedmo:translatable"/>
@@ -41,6 +42,9 @@ people to push their own additional attributes/elements into the same field elem
   <xs:element name="tree-lock-time" type="gedmo:emptyType"/>
   <xs:element name="sortable-group" type="gedmo:emptyType"/>
   <xs:element name="sortable-position" type="gedmo:emptyType"/>
+  <xs:element name="uploadable-file-mime-type" type="gedmo:emptyType"/>
+  <xs:element name="uploadable-file-path" type="gedmo:emptyType"/>
+  <xs:element name="uploadable-file-size" type="gedmo:emptyType"/>
 
   <xs:complexType name="translation">
     <xs:attribute name="entity" type="xs:string" use="optional" />
@@ -129,4 +133,12 @@ people to push their own additional attributes/elements into the same field elem
     </xs:restriction>
   </xs:simpleType>
 
+  <xs:complexType name="uploadable">
+    <xs:attribute name="allow-overwrite" type="xs:boolean" use="optional" />
+    <xs:attribute name="append-number" type="xs:boolean" use="optional" />
+    <xs:attribute name="path" type="xs:string" use="optional" />
+    <xs:attribute name="file-info-property" type="xs:string" use="required" />
+    <xs:attribute name="path-method" type="xs:string" use="optional" />
+  </xs:complexType>
+
 </xs:schema>

+ 7 - 8
tests/Gedmo/Mapping/Driver/Xml/Mapping.Fixture.Xml.Uploadable.dcm.xml

@@ -5,17 +5,11 @@
 
     <entity name="Mapping\Fixture\Xml\Uploadable" table="uploadables">
 
-        <indexes>
-            <index name="name_idx" columns="name"/>
-        </indexes>
-
         <id name="id" type="integer" column="id">
             <generator strategy="AUTO"/>
         </id>
 
-        <field name="name" type="string" length="128"/>
-
-        <field name="mime" column="mime" type="string">
+        <field name="mimeType" column="mime" type="string">
             <gedmo:uploadable-file-mime-type />
         </field>
 
@@ -27,7 +21,12 @@
             <gedmo:uploadable-file-path />
         </field>
 
-        <gedmo:uploadable allow-overwrite="true" append-number="true" path="my/path" />
+        <gedmo:uploadable
+            allow-overwrite="true"
+            append-number="true"
+            path="/my/path"
+            path-method="getPath"
+            file-info-property="fileInfo" />
 
     </entity>
 

+ 29 - 0
tests/Gedmo/Mapping/Driver/Yaml/Mapping.Fixture.Yaml.Uploadable.dcm.yml

@@ -0,0 +1,29 @@
+---
+Mapping\Fixture\Yaml\Uploadable:
+  type: entity
+  table: uploadables
+  gedmo:
+    uploadable:
+      allowOverwrite: true
+      appendNumber: true
+      path: '/my/path'
+      pathMethod: getPath
+      fileInfoProperty: fileInfo
+  id:
+    id:
+      type: integer
+      generator:
+        strategy: AUTO
+  fields:
+    path:
+      type: string
+      gedmo:
+        - uploadableFilePath
+    mimeType:
+      type: string
+      gedmo:
+        - uploadableFileMimeType
+    size:
+      type: decimal
+      gedmo:
+        - uploadableFileSize

+ 21 - 0
tests/Gedmo/Mapping/Fixture/Xml/Uploadable.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Mapping\Fixture\Xml;
+
+class Uploadable
+{
+    private $id;
+
+    private $mimeType;
+
+    private $fileInfo;
+
+    private $size;
+
+    private $path;
+
+    public function getPath()
+    {
+        return $this->path;
+    }
+}

+ 21 - 0
tests/Gedmo/Mapping/Fixture/Yaml/Uploadable.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace Mapping\Fixture\Yaml;
+
+class Uploadable
+{
+    private $id;
+
+    private $mimeType;
+
+    private $fileInfo;
+
+    private $size;
+
+    private $path;
+
+    public function getPath()
+    {
+        return $this->path;
+    }
+}

+ 71 - 0
tests/Gedmo/Mapping/UploadableMappingTest.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Gedmo\Mapping;
+
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
+use Doctrine\Common\EventManager;
+use Doctrine\ORM\Mapping\Driver\DriverChain;
+use Doctrine\ORM\Mapping\Driver\YamlDriver;
+use Gedmo\Uploadable\UploadableListener;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping tests for Uploadable extension
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Mapping
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class UploadableMappingTest extends BaseTestCaseOM
+{
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em;
+
+    /**
+     * @var Gedmo\SoftDeleteable\UploadableListener
+     */
+    private $listener;
+
+    public function setUp()
+    {
+        parent::setUp();
+        
+        $reader = new AnnotationReader();
+        $annotationDriver = new AnnotationDriver($reader);
+
+        $yamlDriver = new YamlDriver(__DIR__.'/Driver/Yaml');
+
+        $chain = new DriverChain;
+        $chain->addDriver($yamlDriver, 'Mapping\Fixture\Yaml');
+        $chain->addDriver($annotationDriver, 'Mapping\Fixture');
+
+        $this->listener = new UploadableListener();
+        $this->evm = new EventManager;
+        $this->evm->addEventSubscriber($this->listener);
+
+        $this->em = $this->getMockSqliteEntityManager(array(
+            'Mapping\Fixture\Yaml\Uploadable'
+        ), $chain);
+    }
+
+    public function testYamlMapping()
+    {
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Yaml\Uploadable');
+        $config = $this->listener->getConfiguration($this->em, $meta->name);
+        
+        $this->assertTrue($config['uploadable']);
+        $this->assertTrue($config['allowOverwrite']);
+        $this->assertTrue($config['appendNumber']);
+        $this->assertEquals('/my/path', $config['path']);
+        $this->assertEquals('getPath', $config['pathMethod']);
+        $this->assertEquals('fileInfo', $config['fileInfoProperty']);
+        $this->assertEquals('mimeType', $config['fileMimeTypeField']);
+        $this->assertEquals('path', $config['filePathField']);
+        $this->assertEquals('size', $config['fileSizeField']);
+    }
+}

+ 71 - 0
tests/Gedmo/Mapping/Xml/UploadableMappingTest.php

@@ -0,0 +1,71 @@
+<?php
+
+namespace Gedmo\Mapping\Xml;
+
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
+use Doctrine\Common\EventManager;
+use Doctrine\ORM\Mapping\Driver\DriverChain;
+use Doctrine\ORM\Mapping\Driver\XmlDriver;
+use Gedmo\Uploadable\UploadableListener;
+use Tool\BaseTestCaseOM;
+
+/**
+ * These are mapping tests for Uploadable extension
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Mapping
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class UploadableMappingTest extends BaseTestCaseOM
+{
+    /**
+     * @var Doctrine\ORM\EntityManager
+     */
+    private $em;
+
+    /**
+     * @var Gedmo\SoftDeleteable\UploadableListener
+     */
+    private $listener;
+
+    public function setUp()
+    {
+        parent::setUp();
+        
+        $reader = new AnnotationReader();
+        $annotationDriver = new AnnotationDriver($reader);
+
+        $xmlDriver = new XmlDriver(__DIR__.'/../Driver/Xml');
+
+        $chain = new DriverChain;
+        $chain->addDriver($xmlDriver, 'Mapping\Fixture\Xml');
+        $chain->addDriver($annotationDriver, 'Mapping\Fixture');
+
+        $this->listener = new UploadableListener;
+        $this->evm = new EventManager;
+        $this->evm->addEventSubscriber($this->listener);
+
+        $this->em = $this->getMockSqliteEntityManager(array(
+            'Mapping\Fixture\Xml\Uploadable'
+        ), $chain);
+    }
+
+    public function testMetadata()
+    {
+        $meta = $this->em->getClassMetadata('Mapping\Fixture\Xml\Uploadable');
+        $config = $this->listener->getConfiguration($this->em, $meta->name);
+
+        $this->assertTrue($config['uploadable']);
+        $this->assertTrue($config['allowOverwrite']);
+        $this->assertTrue($config['appendNumber']);
+        $this->assertEquals('/my/path', $config['path']);
+        $this->assertEquals('getPath', $config['pathMethod']);
+        $this->assertEquals('fileInfo', $config['fileInfoProperty']);
+        $this->assertEquals('mimeType', $config['fileMimeTypeField']);
+        $this->assertEquals('path', $config['filePathField']);
+        $this->assertEquals('size', $config['fileSizeField']);
+    }
+}

+ 1 - 7
tests/Gedmo/Uploadable/Fixture/Entity/File.php

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 /**
  * @ORM\Entity
- * @Gedmo\Uploadable(allowOverwrite=true)
+ * @Gedmo\Uploadable(allowOverwrite=true, fileInfoProperty="fileInfo", pathMethod="getPath")
  */
 class File
 {
@@ -30,9 +30,6 @@ class File
      */
     private $filePath;
 
-    /**
-     * @Gedmo\UploadableFileInfo
-     */
     private $fileInfo;
 
     /**
@@ -87,9 +84,6 @@ class File
         return $this->fileInfo;
     }
 
-    /**
-     * @Gedmo\UploadablePath
-     */
     public function getPath()
     {
         return __DIR__.'/../../../../temp/uploadable';

+ 1 - 7
tests/Gedmo/Uploadable/Fixture/Entity/Image.php

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 /**
  * @ORM\Entity
- * @Gedmo\Uploadable()
+ * @Gedmo\Uploadable(fileInfoProperty="fileInfo", pathMethod="getPath")
  */
 class Image
 {
@@ -30,9 +30,6 @@ class Image
      */
     private $filePath;
 
-    /**
-     * @Gedmo\UploadableFileInfo
-     */
     private $fileInfo;
 
 
@@ -71,9 +68,6 @@ class Image
         return $this->fileInfo;
     }
 
-    /**
-     * @Gedmo\UploadablePath
-     */
     public function getPath()
     {
         return __DIR__.'/../../../../temp/uploadable';