瀏覽代碼

[Uploadable] Some refactor and small tweaks

comfortablynumb 13 年之前
父節點
當前提交
5a764af4ae

+ 7 - 20
lib/Gedmo/Uploadable/Mapping/Driver/Annotation.php

@@ -107,32 +107,19 @@ class Annotation implements AnnotationDriverInterface
                 }
                 }
             }
             }
 
 
-            if (!$config['filePathField']) {
-                throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFilePath field.',
-                    $class->getName()
-                ));
-            }
-
-            if (!$config['fileInfoField']) {
-                throw new InvalidMappingException(sprintf('Class "%s" must have an UploadableFileInfo field.',
-                    $class->getName()
-                ));
-            }
-
             foreach ($class->getMethods() as $method) {
             foreach ($class->getMethods() as $method) {
                 if ($this->reader->getMethodAnnotation($method, self::UPLOADABLE_PATH)) {
                 if ($this->reader->getMethodAnnotation($method, self::UPLOADABLE_PATH)) {
                     $config['pathMethod'] = $method->getName();
                     $config['pathMethod'] = $method->getName();
                 }
                 }
             }
             }
 
 
-            if ($config['path'] && $config['pathMethod'] === '') {
-                $msg = 'You need to define the path in the %s annotation, or add a method with %s annotation.';
+            Validator::validateConfiguration($meta, $config);
+        }
+
+        /*
+        // Code in case we need to identify entities which are not Uploadables, but have associations
+        // with other Uploadable entities
 
 
-                throw new InvalidMappingException(sprintf($msg,
-                    self::UPLOADABLE,
-                    self::UPLOADABLE_PATH
-                ));
-            }
         } else {
         } else {
             // We need to check if this class has a relation with Uploadable entities
             // We need to check if this class has a relation with Uploadable entities
             $associations = $meta->getAssociationMappings();
             $associations = $meta->getAssociationMappings();
@@ -153,7 +140,7 @@ class Annotation implements AnnotationDriverInterface
                     );
                     );
                 }
                 }
             }
             }
-        }
+        }*/
 
 
         $this->validateFullMetadata($meta, $config);
         $this->validateFullMetadata($meta, $config);
     }
     }

+ 40 - 0
lib/Gedmo/Uploadable/Mapping/Driver/Xml.php

@@ -36,6 +36,46 @@ class Xml extends BaseXml
 
 
         if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
         if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
             // TODO
             // TODO
+
+            /*
+             if (isset($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['fileMimeTypeField'] = false;
+                $config['filePathField'] = false;
+                $config['fileSizeField'] = false;
+
+                if (isset($xmlDoctrine->field)) {
+                    foreach ($xmlDoctrine->field as $mapping) {
+                        $mappingDoctrine = $mapping;
+                        $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
+
+                        $field = $this->_getAttribute($mappingDoctrine, 'name');
+
+                        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);
+            }
+            */
         }
         }
     }
     }
 }
 }

+ 37 - 0
lib/Gedmo/Uploadable/Mapping/Validator.php

@@ -4,6 +4,8 @@ namespace Gedmo\Uploadable\Mapping;
 
 
 use Doctrine\ORM\Mapping\ClassMetadataInfo;
 use Doctrine\ORM\Mapping\ClassMetadataInfo;
 use Gedmo\Exception\InvalidMappingException;
 use Gedmo\Exception\InvalidMappingException;
+use Gedmo\Exception\UploadableCantWriteException;
+use Doctrine\ORM\Mapping\ClassMetadata;
 
 
 /**
 /**
  * This class is used to validate mapping information
  * This class is used to validate mapping information
@@ -79,4 +81,39 @@ class Validator
             ));
             ));
         }
         }
     }
     }
+
+    public static function validatePath($path)
+    {
+        if (!is_dir($path) || !is_writable($path)) {
+            throw new UploadableCantWriteException(sprintf('Directory "%s" does not exist or is not writable',
+                $path
+            ));
+        }
+    }
+
+    public static function validateConfiguration(ClassMetadata $meta, array $config)
+    {
+        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.',
+                $meta->name
+            ));
+        }
+
+        if ($config['path'] === '' && $config['pathMethod'] === '') {
+            $msg = 'You need to define the path in the %s annotation, or add a method with %s annotation.';
+
+            throw new InvalidMappingException(sprintf($msg,
+                self::UPLOADABLE,
+                self::UPLOADABLE_PATH
+            ));
+        } else if ($config['path'] !== '') {
+            Validator::validatePath($config['path']);
+        }
+    }
 }
 }

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

@@ -17,7 +17,8 @@ use Doctrine\Common\Persistence\ObjectManager,
     Gedmo\Exception\UploadableNoFileException,
     Gedmo\Exception\UploadableNoFileException,
     Gedmo\Exception\UploadableNoTmpDirException,
     Gedmo\Exception\UploadableNoTmpDirException,
     Gedmo\Exception\UploadableUploadException,
     Gedmo\Exception\UploadableUploadException,
-    Gedmo\Exception\UploadableFileAlreadyExistsException;
+    Gedmo\Exception\UploadableFileAlreadyExistsException,
+    Gedmo\Uploadable\Mapping\Validator;
 
 
 /**
 /**
  * Uploadable listener
  * Uploadable listener
@@ -117,6 +118,16 @@ class UploadableListener extends MappedEventSubscriber
             $pathMethod = $refl->getMethod($config['pathMethod']);
             $pathMethod = $refl->getMethod($config['pathMethod']);
             $pathMethod->setAccessible(true);
             $pathMethod->setAccessible(true);
             $path = $pathMethod->invoke($object);
             $path = $pathMethod->invoke($object);
+
+            if (is_string($path) && $path !== '') {
+                Validator::validatePath($path);
+            } else {
+                $msg = 'The method which returns the file path in class "%s" must return a valid path.';
+
+                throw new \RuntimeException(sprintf($msg,
+                    $meta->name
+                ));
+            }
         }
         }
 
 
         $path = substr($path, strlen($path) - 1) === '/' ? substr($path, 0, strlen($path) - 2) : $path;
         $path = substr($path, strlen($path) - 1) === '/' ? substr($path, 0, strlen($path) - 2) : $path;

+ 34 - 0
tests/Gedmo/Mapping/Driver/Xml/Mapping.Fixture.Xml.Uploadable.dcm.xml

@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
+                  xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping">
+
+    <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">
+            <gedmo:uploadable-file-mime-type />
+        </field>
+
+        <field name="size" column="size" type="decimal">
+            <gedmo:uploadable-file-size />
+        </field>
+
+        <field name="path" column="path" type="string">
+            <gedmo:uploadable-file-path />
+        </field>
+
+        <gedmo:uploadable allow-overwrite="true" append-number="true" path="my/path" />
+
+    </entity>
+
+</doctrine-mapping>

+ 18 - 12
tests/Gedmo/Uploadable/UploadableEntityTest.php

@@ -55,21 +55,16 @@ class UploadableEntityTest extends BaseTestCaseORM
         $this->testFileSize = 4;
         $this->testFileSize = 4;
         $this->testFileMimeType = 'text/plain';
         $this->testFileMimeType = 'text/plain';
 
 
-        if (is_file($this->destinationTestFile)) {
-            unlink($this->destinationTestFile);
-        }
-
-        if (is_file($this->destinationTestFile2)) {
-            unlink($this->destinationTestFile2);
-        }
-
-        if (is_dir($this->destinationTestDir)) {
-            rmdir($this->destinationTestDir);
-        }
+        $this->clearFilesAndDirectories();
 
 
         mkdir($this->destinationTestDir);
         mkdir($this->destinationTestDir);
     }
     }
 
 
+    public function tearDown()
+    {
+        $this->clearFilesAndDirectories();
+    }
+
     public function testUploadableEntity()
     public function testUploadableEntity()
     {
     {
         // INSERTION of an Uploadable Entity
         // INSERTION of an Uploadable Entity
@@ -195,7 +190,18 @@ class UploadableEntityTest extends BaseTestCaseORM
         );
         );
     }
     }
 
 
-    private function populate()
+    private function clearFilesAndDirectories()
     {
     {
+        if (is_file($this->destinationTestFile)) {
+            unlink($this->destinationTestFile);
+        }
+
+        if (is_file($this->destinationTestFile2)) {
+            unlink($this->destinationTestFile2);
+        }
+
+        if (is_dir($this->destinationTestDir)) {
+            rmdir($this->destinationTestDir);
+        }
     }
     }
 }
 }