Pārlūkot izejas kodu

[Uploadable] Added ability to set a callback method after file is moved to path

comfortablynumb 13 gadi atpakaļ
vecāks
revīzija
cd788ac461

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

@@ -33,5 +33,8 @@ final class Uploadable extends Annotation
 
     /** @var string */
     public $pathMethod = '';
+
+    /** @var string */
+    public $callback = '';
 }
 

+ 1 - 0
lib/Gedmo/Uploadable/Mapping/Driver/Annotation.php

@@ -81,6 +81,7 @@ class Annotation implements AnnotationDriverInterface
             $config['filePathField'] = false;
             $config['fileSizeField'] = false;
             $config['fileInfoProperty'] = $annot->fileInfoProperty;
+            $config['callback'] = $annot->callback;
 
             foreach ($class->getProperties() as $prop) {
                 if ($this->reader->getPropertyAnnotation($prop, self::UPLOADABLE_FILE_MIME_TYPE)) {

+ 6 - 2
lib/Gedmo/Uploadable/Mapping/Driver/Xml.php

@@ -42,8 +42,12 @@ class Xml extends BaseXml
                     (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['path'] = $this->_isAttributeSet($xmlUploadable, 'path') ?
+                    $this->_getAttribute($xml->{'uploadable'}, 'path') : '';
+                $config['pathMethod'] = $this->_isAttributeSet($xmlUploadable, 'path-method') ?
+                    $this->_getAttribute($xml->{'uploadable'}, 'path-method') : '';
+                $config['callback'] = $this->_isAttributeSet($xmlUploadable, 'callback') ?
+                    $this->_getAttribute($xml->{'uploadable'}, 'callback') : '';
                 $config['fileInfoProperty'] = $this->_getAttribute($xml->{'uploadable'}, 'file-info-property');
                 $config['fileMimeTypeField'] = false;
                 $config['filePathField'] = false;

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

@@ -48,6 +48,7 @@ class Yaml extends File implements Driver
                     (bool) $uploadable['appendNumber'] : false;
                 $config['path'] = isset($uploadable['path']) ? $uploadable['path'] : '';
                 $config['pathMethod'] = isset($uploadable['pathMethod']) ? $uploadable['pathMethod'] : '';
+                $config['callback'] = isset($uploadable['callback']) ? $uploadable['callback'] : '';
                 $config['fileInfoProperty'] = isset($uploadable['fileInfoProperty']) ? $uploadable['fileInfoProperty'] : '';
                 $config['fileMimeTypeField'] = false;
                 $config['filePathField'] = false;

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

@@ -123,6 +123,15 @@ class Validator
             }
         }
 
+        if ($config['callback'] !== '') {
+            if (!$refl->hasMethod($config['callback'])) {
+                throw new InvalidMappingException(sprintf('Class "%s" doesn\'t have method "%s"!',
+                    $meta->name,
+                    $config['callback']
+                ));
+            }
+        }
+
         if ($config['fileMimeTypeField']) {
             self::validateFileMimeTypeField($meta, $config['fileMimeTypeField']);
         }

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

@@ -169,6 +169,14 @@ class UploadableListener extends MappedEventSubscriber
 
                 $info = $this->moveFile($f, $path, $config['allowOverwrite'], $config['appendNumber']);
                 $filePathField->setValue($object, $info['filePath']);
+
+                if ($config['callback'] !== '') {
+                    $callbackMethod = $refl->getMethod($config['callback']);
+                    $callbackMethod->setAccessible(true);
+
+                    $callbackMethod->invokeArgs($object, array($config));
+                }
+
                 $changes = array(
                     $config['filePathField'] => array($filePathField->getValue($object), $info['filePath'])
                 );

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

@@ -136,6 +136,7 @@ people to push their own additional attributes/elements into the same field elem
   <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="callback" type="xs:string" 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" />

+ 2 - 1
tests/Gedmo/Mapping/Driver/Xml/Mapping.Fixture.Xml.Uploadable.dcm.xml

@@ -26,7 +26,8 @@
             append-number="true"
             path="/my/path"
             path-method="getPath"
-            file-info-property="fileInfo" />
+            file-info-property="fileInfo"
+            callback="callbackMethod" />
 
     </entity>
 

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

@@ -9,6 +9,7 @@ Mapping\Fixture\Yaml\Uploadable:
       path: '/my/path'
       pathMethod: getPath
       fileInfoProperty: fileInfo
+      callback: callbackMethod
   id:
     id:
       type: integer

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

@@ -18,4 +18,8 @@ class Uploadable
     {
         return $this->path;
     }
+
+    public function callbackMethod()
+    {
+    }
 }

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

@@ -18,4 +18,8 @@ class Uploadable
     {
         return $this->path;
     }
+
+    public function callbackMethod()
+    {
+    }
 }

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

@@ -67,5 +67,6 @@ class UploadableMappingTest extends BaseTestCaseOM
         $this->assertEquals('mimeType', $config['fileMimeTypeField']);
         $this->assertEquals('path', $config['filePathField']);
         $this->assertEquals('size', $config['fileSizeField']);
+        $this->assertEquals('callbackMethod', $config['callback']);
     }
 }

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

@@ -67,5 +67,6 @@ class UploadableMappingTest extends BaseTestCaseOM
         $this->assertEquals('mimeType', $config['fileMimeTypeField']);
         $this->assertEquals('path', $config['filePathField']);
         $this->assertEquals('size', $config['fileSizeField']);
+        $this->assertEquals('callbackMethod', $config['callback']);
     }
 }

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

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 /**
  * @ORM\Entity
- * @Gedmo\Uploadable(allowOverwrite=true, fileInfoProperty="fileInfo", pathMethod="getPath")
+ * @Gedmo\Uploadable(allowOverwrite=true, fileInfoProperty="fileInfo", pathMethod="getPath", callback="callbackMethod")
  */
 class File
 {
@@ -38,6 +38,8 @@ class File
      */
     private $article;
 
+    public $callbackWasCalled = false;
+
 
     public function getId()
     {
@@ -84,6 +86,11 @@ class File
         return $this->fileInfo;
     }
 
+    public function callbackMethod()
+    {
+        $this->callbackWasCalled = true;
+    }
+
     public function getPath()
     {
         return __DIR__.'/../../../../temp/uploadable';

+ 13 - 0
tests/Gedmo/Uploadable/UploadableEntityTest.php

@@ -197,6 +197,19 @@ class UploadableEntityTest extends BaseTestCaseORM
         $this->assertEquals($this->destinationTestFile, $file->getFilePath());
     }
 
+    public function testCallbackIsCalledIfItsSetOnEntity()
+    {
+        $file = new File();
+        $fileInfo = $this->generateUploadedFile();
+
+        $file->setFileInfo($fileInfo);
+
+        $this->em->persist($file);
+        $this->em->flush();
+
+        $this->assertTrue($file->callbackWasCalled);
+    }
+
     private function generateUploadedFile($index = 'image', $file = false, array $info = array())
     {
         if (empty($info)) {