瀏覽代碼

[Uploadable] Added uploadablePreFileProcess and uploadablePostFileProcess events

comfortablynumb 12 年之前
父節點
當前提交
dc19251991

+ 138 - 0
lib/Gedmo/Uploadable/Event/UploadableBaseEventArgs.php

@@ -0,0 +1,138 @@
+<?php
+
+namespace Gedmo\Uploadable\Event;
+
+use Doctrine\Common\EventArgs;
+use Doctrine\ORM\EntityManager;
+use Gedmo\Uploadable\FileInfo\FileInfoInterface;
+use Gedmo\Uploadable\UploadableListener;
+
+/**
+ * Abstract Base Event to be extended by Uploadable events
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Uploadable.Event
+ * @subpackage UploadableBaseEventArgs
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+abstract class UploadableBaseEventArgs extends EventArgs
+{
+    /**
+     * The instance of the Uploadable listener that fired this event
+     *
+     * @var \Gedmo\Uploadable\UploadableListener
+     */
+    private $uploadableListener;
+
+    /**
+     * @var \Doctrine\ORM\EntityManager
+     */
+    private $em;
+
+    /**
+     * The Uploadable entity
+     *
+     * @var object $entity
+     */
+    private $entity;
+
+    /**
+     * The configuration of the Uploadable extension for this entity class
+     *
+     * @var array $extensionConfiguration
+     */
+    private $extensionConfiguration;
+
+    /**
+     * @var \Gedmo\Uploadable\FileInfo\FileInfoInterface
+     */
+    private $fileInfo;
+
+    /**
+     * @var string $action - Is the file being created, updated or removed?
+     *                       This value can be: CREATE, UPDATE or DELETE.
+     */
+    private $action;
+
+
+    /**
+     * @param \Gedmo\Uploadable\UploadableListener $listener
+     * @param \Doctrine\ORM\EntityManager $em
+     * @param array $config
+     * @param \Gedmo\Uploadable\FileInfo\FileInfoInterface $fileInfo
+     * @param $entity
+     * @param $action
+     */
+    public function __construct(UploadableListener $listener, EntityManager $em, array $config, FileInfoInterface $fileInfo, $entity, $action)
+    {
+        $this->uploadableListener = $listener;
+        $this->em = $em;
+        $this->config = $config;
+        $this->fileInfo = $fileInfo;
+        $this->entity = $entity;
+        $this->action = $action;
+    }
+
+    /**
+     * Retrieve the associated listener
+     *
+     * @return \Gedmo\Uploadable\UploadableListener
+     */
+    public function getListener()
+    {
+        return $this->uploadableListener;
+    }
+
+    /**
+     * Retrieve associated EntityManager
+     *
+     * @return \Doctrine\ORM\EntityManager
+     */
+    public function getEntityManager()
+    {
+        return $this->em;
+    }
+
+    /**
+     * Retrieve associated Entity
+     *
+     * @return object
+     */
+    public function getEntity()
+    {
+        return $this->entity;
+    }
+
+    /**
+     * Retrieve associated Uploadable extension configuration
+     *
+     * @return array
+     */
+    public function getExtensionConfiguration()
+    {
+        return $this->extensionConfiguration;
+    }
+
+    /**
+     * Retrieve the FileInfo associated with this entity.
+     *
+     * @return \Gedmo\Uploadable\FileInfo\FileInfoInterface
+     */
+    public function getFileInfo()
+    {
+        return $this->fileInfo;
+    }
+
+    /**
+     * Retrieve the action being performed to the entity: CREATE, UPDATE or DELETE
+     *
+     * @return string
+     */
+    public function getAction()
+    {
+        return $this->action;
+    }
+}

+ 18 - 0
lib/Gedmo/Uploadable/Event/UploadablePostFileProcessEventArgs.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Gedmo\Uploadable\Event;
+
+/**
+ * Post File Process Event for the Uploadable extension
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Uploadable.Event
+ * @subpackage UploadablePostFileProcessEventArgs
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+class UploadablePostFileProcessEventArgs extends UploadableBaseEventArgs
+{
+}

+ 18 - 0
lib/Gedmo/Uploadable/Event/UploadablePreFileProcessEventArgs.php

@@ -0,0 +1,18 @@
+<?php
+
+namespace Gedmo\Uploadable\Event;
+
+/**
+ * Pre File Process Event for the Uploadable extension
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Uploadable.Event
+ * @subpackage UploadablePreFileProcessEventArgs
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+class UploadablePreFileProcessEventArgs extends UploadableBaseEventArgs
+{
+}

+ 35 - 0
lib/Gedmo/Uploadable/Events.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Gedmo\Uploadable;
+
+/**
+ * Container for all Gedmo Uploadable events
+ *
+ * @author Gustavo Falco <comfortablynumb84@gmail.com>
+ * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
+ * @package Gedmo.Uploadable
+ * @subpackage Events
+ * @link http://www.gediminasm.org
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+final class Events
+{
+    private function __construct() {}
+    /**
+     * The uploadablePreFileProcess event occurs before a file is processed inside
+     * the Uploadable listener. This means it happens before the file is validated and moved
+     * to the configured path.
+     *
+     * @var string
+     */
+    const uploadablePreFileProcess = 'uploadablePreFileProcess';
+    /**
+     * The uploadablePostFileProcess event occurs after a file is processed inside
+     * the Uploadable listener. This means it happens after the file is validated and moved
+     * to the configured path.
+     *
+     * @var string
+     */
+    const uploadablePostFileProcess = 'uploadablePostFileProcess';
+}

+ 52 - 25
lib/Gedmo/Uploadable/UploadableListener.php

@@ -28,7 +28,10 @@ use Doctrine\Common\Persistence\ObjectManager,
     Gedmo\Uploadable\MimeType\MimeTypeGuesser,
     Gedmo\Uploadable\MimeType\MimeTypeGuesser,
     Gedmo\Uploadable\MimeType\MimeTypeGuesserInterface,
     Gedmo\Uploadable\MimeType\MimeTypeGuesserInterface,
     Gedmo\Uploadable\MimeType\MimeTypesExtensionsMap,
     Gedmo\Uploadable\MimeType\MimeTypesExtensionsMap,
-    Doctrine\Common\NotifyPropertyChanged;
+    Doctrine\Common\NotifyPropertyChanged,
+    Gedmo\Uploadable\Events,
+    Gedmo\Uploadable\Event\UploadablePreFileProcessEventArgs,
+    Gedmo\Uploadable\Event\UploadablePostFileProcessEventArgs;
 
 
 /**
 /**
  * Uploadable listener
  * Uploadable listener
@@ -134,9 +137,7 @@ class UploadableListener extends MappedEventSubscriber
      * Handle file-uploading depending on the action
      * Handle file-uploading depending on the action
      * being done with objects
      * being done with objects
      *
      *
-     * @param EventArgs
-     *
-     * @return void
+     * @param \Doctrine\Common\EventArgs $args
      */
      */
     public function onFlush(EventArgs $args)
     public function onFlush(EventArgs $args)
     {
     {
@@ -162,9 +163,7 @@ class UploadableListener extends MappedEventSubscriber
     /**
     /**
      * Handle removal of files
      * Handle removal of files
      *
      *
-     * @param EventArgs
-     *
-     * @return void
+     * @param \Doctrine\Common\EventArgs $args
      */
      */
     public function postFlush(EventArgs $args)
     public function postFlush(EventArgs $args)
     {
     {
@@ -209,6 +208,18 @@ class UploadableListener extends MappedEventSubscriber
 
 
         $refl = $meta->getReflectionClass();
         $refl = $meta->getReflectionClass();
         $fileInfo = $this->fileInfoObjects[$oid]['fileInfo'];
         $fileInfo = $this->fileInfoObjects[$oid]['fileInfo'];
+        $evm = $om->getEventManager();
+
+        if ($evm->hasListeners(Events::uploadablePreFileProcess)) {
+            $evm->dispatchEvent(Events::uploadablePreFileProcess, new UploadablePreFileProcessEventArgs(
+                $this,
+                $om,
+                $config,
+                $fileInfo,
+                $object,
+                $action
+            ));
+        }
 
 
         // Validations
         // Validations
         if ($config['maxSize'] > 0 && $fileInfo->getSize() > $config['maxSize']) {
         if ($config['maxSize'] > 0 && $fileInfo->getSize() > $config['maxSize']) {
@@ -347,17 +358,27 @@ class UploadableListener extends MappedEventSubscriber
             $uow->scheduleExtraUpdate($object, $changes);
             $uow->scheduleExtraUpdate($object, $changes);
         }
         }
 
 
+        if ($evm->hasListeners(Events::uploadablePostFileProcess)) {
+            $evm->dispatchEvent(Events::uploadablePostFileProcess, new UploadablePostFileProcessEventArgs(
+                $this,
+                $om,
+                $config,
+                $fileInfo,
+                $object,
+                $action
+            ));
+        }
+
         unset($this->fileInfoObjects[$oid]);
         unset($this->fileInfoObjects[$oid]);
     }
     }
 
 
     /**
     /**
      * Returns the path of the entity's file
      * Returns the path of the entity's file
      *
      *
-     * @param ClassMetadata
-     * @param array - Configuration
-     * @param object - Entity
-     *
-     * @return string
+     * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $meta
+     * @param array $config
+     * @param $object
+     * @return mixed
      */
      */
     public function getFilePath(ClassMetadata $meta, array $config, $object)
     public function getFilePath(ClassMetadata $meta, array $config, $object)
     {
     {
@@ -388,14 +409,21 @@ class UploadableListener extends MappedEventSubscriber
     /**
     /**
      * Moves the file to the specified path
      * Moves the file to the specified path
      *
      *
-
-     * @param FileInfoInterface
-     * @param string - Path
-     * @param mixed - String of class that implements FilenameGeneratorInterface, or false
-     * @param bool - Overwrite if file already exists?
-     * @param bool - Append a number if file already exists?
-     *
-     * @return array - Information about the moved file
+     * @param FileInfo\FileInfoInterface $fileInfo
+     * @param $path
+     * @param bool $filenameGeneratorClass
+     * @param bool $overwrite
+     * @param bool $appendNumber
+     * @return array
+     * @throws \Gedmo\Exception\UploadableUploadException
+     * @throws \Gedmo\Exception\UploadableNoFileException
+     * @throws \Gedmo\Exception\UploadableExtensionException
+     * @throws \Gedmo\Exception\UploadableIniSizeException
+     * @throws \Gedmo\Exception\UploadableFormSizeException
+     * @throws \Gedmo\Exception\UploadableFileAlreadyExistsException
+     * @throws \Gedmo\Exception\UploadablePartialException
+     * @throws \Gedmo\Exception\UploadableNoTmpDirException
+     * @throws \Gedmo\Exception\UploadableCantWriteException
      */
      */
     public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorClass = false, $overwrite = false, $appendNumber = false)
     public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorClass = false, $overwrite = false, $appendNumber = false)
     {
     {
@@ -517,9 +545,7 @@ class UploadableListener extends MappedEventSubscriber
     /**
     /**
      * Maps additional metadata
      * Maps additional metadata
      *
      *
-     * @param EventArgs
-     *
-     * @return void
+     * @param \Doctrine\Common\EventArgs $eventArgs
      */
      */
     public function loadClassMetadata(EventArgs $eventArgs)
     public function loadClassMetadata(EventArgs $eventArgs)
     {
     {
@@ -574,8 +600,9 @@ class UploadableListener extends MappedEventSubscriber
     /**
     /**
      * Adds a FileInfoInterface object for the given entity
      * Adds a FileInfoInterface object for the given entity
      *
      *
-     * @param object - $entity
-     * @param FileInfo\FileInfoInterface $fileInfo
+     * @param $entity
+     * @param $fileInfo
+     * @throws \RuntimeException
      */
      */
     public function addEntityFileInfo($entity, $fileInfo)
     public function addEntityFileInfo($entity, $fileInfo)
     {
     {