Переглянути джерело

[Uploadable] Added ability to set default path on listener.

comfortablynumb 13 роки тому
батько
коміт
0391ddca13

+ 1 - 1
lib/Gedmo/Exception/UploadableCantWriteException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableCantWriteException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadableDirectoryNotFoundException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableDirectoryNotFoundException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 20 - 0
lib/Gedmo/Exception/UploadableException.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Gedmo\Exception;
+
+use Gedmo\Exception;
+
+/**
+ * UploadableException
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Exception
+ * @subpackage UploadableException
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class UploadableException
+    extends RuntimeException
+    implements Exception
+{}

+ 1 - 1
lib/Gedmo/Exception/UploadableExtensionException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableExtensionException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadableFileAlreadyExistsException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableFileAlreadyExistsException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadableFormSizeException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableFormSizeException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadableIniSizeException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableIniSizeException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadableNoFileException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableNoFileException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 20 - 0
lib/Gedmo/Exception/UploadableNoPathDefinedException.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Gedmo\Exception;
+
+use Gedmo\Exception;
+
+/**
+ * UploadableNoPathDefinedException
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Exception
+ * @subpackage UploadableNoPathDefinedException
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+class UploadableNoPathDefinedException
+    extends UploadableException
+    implements Exception
+{}

+ 1 - 1
lib/Gedmo/Exception/UploadableNoTmpDirException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableNoTmpDirException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadablePartialException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadablePartialException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 1
lib/Gedmo/Exception/UploadableUploadException.php

@@ -15,6 +15,6 @@ use Gedmo\Exception;
  * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  */
 class UploadableUploadException
-    extends RuntimeException
+    extends UploadableException
     implements Exception
 {}

+ 1 - 8
lib/Gedmo/Uploadable/Mapping/Validator.php

@@ -114,14 +114,7 @@ class Validator
             }
         }
 
-        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['pathMethod'] !== '') {
+        if ($config['pathMethod'] !== '') {
             if (!$refl->hasMethod($config['pathMethod'])) {
                 throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
                     $meta->name,

+ 44 - 8
lib/Gedmo/Uploadable/UploadableListener.php

@@ -18,6 +18,7 @@ use Doctrine\Common\Persistence\ObjectManager,
     Gedmo\Exception\UploadableNoTmpDirException,
     Gedmo\Exception\UploadableUploadException,
     Gedmo\Exception\UploadableFileAlreadyExistsException,
+    Gedmo\Exception\UploadableNoPathDefinedException,
     Gedmo\Uploadable\Mapping\Validator;
 
 /**
@@ -34,6 +35,9 @@ class UploadableListener extends MappedEventSubscriber
 {
     const ACTION_INSERT = 'INSERT';
     const ACTION_UPDATE = 'UPDATE';
+
+    private $defaultPath;
+
     /**
      * {@inheritdoc}
      */
@@ -115,16 +119,26 @@ class UploadableListener extends MappedEventSubscriber
         $path = $config['path'];
 
         if ($path === '') {
-            $pathMethod = $refl->getMethod($config['pathMethod']);
-            $pathMethod->setAccessible(true);
-            $path = $pathMethod->invoke($object);
-
-            if (is_string($path) && $path !== '') {
-                Validator::validatePath($path);
+            if ($config['pathMethod'] !== '') {
+                $pathMethod = $refl->getMethod($config['pathMethod']);
+                $pathMethod->setAccessible(true);
+                $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
+                    ));
+                }
+            } else if ($this->getDefaultPath() !== null) {
+                $path = $this->getDefaultPath();
             } else {
-                $msg = 'The method which returns the file path in class "%s" must return a valid path.';
+                $msg = 'You have to define the path to save files either in the listener, or in the class "%s"';
 
-                throw new \RuntimeException(sprintf($msg,
+                throw new UploadableNoPathDefinedException(sprintf($msg,
                     $meta->name
                 ));
             }
@@ -345,6 +359,28 @@ class UploadableListener extends MappedEventSubscriber
         $this->loadMetadataForObjectClass($ea->getObjectManager(), $eventArgs->getClassMetadata());
     }
 
+    /**
+     * Sets the default path
+     *
+     * @param string
+     *
+     * @return void
+     */
+    public function setDefaultPath($path)
+    {
+        $this->defaultPath = $path;
+    }
+
+    /**
+     * Returns default path
+     *
+     * @return string
+     */
+    public function getDefaultPath()
+    {
+        return $this->defaultPath;
+    }
+
     /**
      * {@inheritDoc}
      */

+ 55 - 0
tests/Gedmo/Uploadable/Fixture/Entity/FileWithoutPath.php

@@ -0,0 +1,55 @@
+<?php
+
+namespace Uploadable\Fixture\Entity;
+
+use Gedmo\Mapping\Annotation as Gedmo;
+use Doctrine\ORM\Mapping as ORM;
+use Doctrine\Common\Collections\ArrayCollection;
+
+/**
+ * @ORM\Entity
+ * @Gedmo\Uploadable(fileInfoProperty="fileInfo")
+ */
+class FileWithoutPath
+{
+    /**
+     * @ORM\Column(name="id", type="integer")
+     * @ORM\Id
+     * @ORM\GeneratedValue(strategy="IDENTITY")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column(name="path", type="string", nullable=true)
+     * @Gedmo\UploadableFilePath
+     */
+    private $filePath;
+
+    private $fileInfo;
+
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    public function setFilePath($filePath)
+    {
+        $this->filePath = $filePath;
+    }
+
+    public function getFilePath()
+    {
+        return $this->filePath;
+    }
+
+    public function setFileInfo(array $fileInfo)
+    {
+        $this->fileInfo = $fileInfo;
+    }
+
+    public function getFileInfo()
+    {
+        return $this->fileInfo;
+    }
+}

+ 36 - 4
tests/Gedmo/Uploadable/UploadableEntityTest.php

@@ -2,12 +2,13 @@
 
 namespace Gedmo\Uploadable;
 
-use Tool\BaseTestCaseORM;
-use Doctrine\Common\EventManager;
-use Doctrine\Common\Util\Debug,
+use Tool\BaseTestCaseORM,
+    Doctrine\Common\EventManager,
+    Doctrine\Common\Util\Debug,
     Uploadable\Fixture\Entity\Image,
     Uploadable\Fixture\Entity\Article,
     Uploadable\Fixture\Entity\File,
+    Uploadable\Fixture\Entity\FileWithoutPath,
     Gedmo\Uploadable\Stub\UploadableListenerStub;
 
 /**
@@ -24,6 +25,7 @@ class UploadableEntityTest extends BaseTestCaseORM
     const IMAGE_CLASS = 'Uploadable\Fixture\Entity\Image';
     const ARTICLE_CLASS = 'Uploadable\Fixture\Entity\Article';
     const FILE_CLASS = 'Uploadable\Fixture\Entity\File';
+    const FILE_WITHOUT_PATH_CLASS = 'Uploadable\Fixture\Entity\FileWithoutPath';
 
     private $listener;
     private $testFile;
@@ -166,6 +168,35 @@ class UploadableEntityTest extends BaseTestCaseORM
         $this->assertEquals($file3Path, $files[2]->getFilePath());
     }
 
+    /**
+     * @expectedException Gedmo\Exception\UploadableNoPathDefinedException
+     */
+    public function testNoPathDefinedOnEntityOrListenerThrowsException()
+    {
+        $file = new FileWithoutPath();
+
+        $this->em->persist($file);
+        $this->em->flush();
+    }
+
+    public function testNoPathDefinedOnEntityButDefinedOnListenerUsesDefaultPath()
+    {
+        // We set the default path on the listener
+        $this->listener->setDefaultPath($this->destinationTestDir);
+
+        $file = new FileWithoutPath();
+        $fileInfo = $this->generateUploadedFile();
+
+        $file->setFileInfo($fileInfo);
+
+        $this->em->persist($file);
+        $this->em->flush();
+
+        $this->em->refresh($file);
+
+        $this->assertEquals($this->destinationTestFile, $file->getFilePath());
+    }
+
     private function generateUploadedFile($index = 'image', $file = false, array $info = array())
     {
         if (empty($info)) {
@@ -186,7 +217,8 @@ class UploadableEntityTest extends BaseTestCaseORM
         return array(
             self::IMAGE_CLASS,
             self::ARTICLE_CLASS,
-            self::FILE_CLASS
+            self::FILE_CLASS,
+            self::FILE_WITHOUT_PATH_CLASS
         );
     }