Jelajahi Sumber

[Uploadable] Now the FileInfoInterface object is set in the listener instead of doing it in the entity

comfortablynumb 13 tahun lalu
induk
melakukan
7aa8cb3c2d

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

@@ -29,9 +29,6 @@ final class Uploadable extends Annotation
     /** @var string */
     /** @var string */
     public $path = '';
     public $path = '';
 
 
-    /** @var string @required */
-    public $fileInfoProperty;
-
     /** @var string */
     /** @var string */
     public $pathMethod = '';
     public $pathMethod = '';
 
 

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

@@ -80,7 +80,6 @@ class Annotation implements AnnotationDriverInterface
             $config['fileMimeTypeField'] = false;
             $config['fileMimeTypeField'] = false;
             $config['filePathField'] = false;
             $config['filePathField'] = false;
             $config['fileSizeField'] = false;
             $config['fileSizeField'] = false;
-            $config['fileInfoProperty'] = $annot->fileInfoProperty;
             $config['callback'] = $annot->callback;
             $config['callback'] = $annot->callback;
             $config['filenameGenerator'] = $annot->filenameGenerator;
             $config['filenameGenerator'] = $annot->filenameGenerator;
 
 

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

@@ -48,7 +48,6 @@ class Xml extends BaseXml
                     $this->_getAttribute($xml->{'uploadable'}, 'path-method') : '';
                     $this->_getAttribute($xml->{'uploadable'}, 'path-method') : '';
                 $config['callback'] = $this->_isAttributeSet($xmlUploadable, 'callback') ?
                 $config['callback'] = $this->_isAttributeSet($xmlUploadable, 'callback') ?
                     $this->_getAttribute($xml->{'uploadable'}, 'callback') : '';
                     $this->_getAttribute($xml->{'uploadable'}, 'callback') : '';
-                $config['fileInfoProperty'] = $this->_getAttribute($xml->{'uploadable'}, 'file-info-property');
                 $config['fileMimeTypeField'] = false;
                 $config['fileMimeTypeField'] = false;
                 $config['filePathField'] = false;
                 $config['filePathField'] = false;
                 $config['fileSizeField'] = false;
                 $config['fileSizeField'] = false;

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

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

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

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

+ 48 - 20
lib/Gedmo/Uploadable/UploadableListener.php

@@ -60,6 +60,14 @@ class UploadableListener extends MappedEventSubscriber
      */
      */
     private $pendingFileRemovals = array();
     private $pendingFileRemovals = array();
 
 
+    /**
+     * Array of FileInfoInterface objects. The index is the hash of the entity owner
+     * of the FileInfoInterface object.
+     *
+     * @var array
+     */
+    private $fileInfoObjects = array();
+
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
@@ -151,33 +159,18 @@ class UploadableListener extends MappedEventSubscriber
     public function processFile(UnitOfWork $uow, AdapterInterface $ea, ClassMetadata $meta, array $config, $object, $action)
     public function processFile(UnitOfWork $uow, AdapterInterface $ea, ClassMetadata $meta, array $config, $object, $action)
     {
     {
         $refl = $meta->getReflectionClass();
         $refl = $meta->getReflectionClass();
-        $fileInfoProp = $refl->getProperty($config['fileInfoProperty']);
-        $fileInfoProp->setAccessible(true);
-        $fileInfo = $fileInfoProp->getValue($object);
+        $oid = spl_object_hash($object);
 
 
-        if (!$fileInfo) {
+        if (!isset($this->fileInfoObjects[$oid])) {
             // Nothing to do
             // Nothing to do
 
 
             return;
             return;
         }
         }
 
 
-        $fileInfoClass = $this->getDefaultFileInfoClass();
-        $fileInfo = is_array($fileInfo) ? new $fileInfoClass($fileInfo) : $fileInfo;
-        $fileInfoProp->setValue($object, $fileInfo);
-
+        $fileInfo = $this->fileInfoObjects[$oid];
         $filePathField = $refl->getProperty($config['filePathField']);
         $filePathField = $refl->getProperty($config['filePathField']);
         $filePathField->setAccessible(true);
         $filePathField->setAccessible(true);
 
 
-        if (!($fileInfo instanceof FileInfoInterface)) {
-            $msg = 'Property "%s" for class "%s" must contain either an array with information ';
-            $msg .= 'about an uploaded or a FileInfoInterface instance. ';
-
-            throw new \RuntimeException(sprintf($msg,
-                $fileInfoProp->getName(),
-                $meta->name
-            ));
-        }
-
         $path = $config['path'];
         $path = $config['path'];
 
 
         if ($path === '') {
         if ($path === '') {
@@ -266,9 +259,9 @@ class UploadableListener extends MappedEventSubscriber
         }
         }
 
 
         $uow->scheduleExtraUpdate($object, $changes);
         $uow->scheduleExtraUpdate($object, $changes);
-
-        $oid = spl_object_hash($object);
         $ea->setOriginalObjectProperty($uow, $oid, $config['filePathField'], $info['filePath']);
         $ea->setOriginalObjectProperty($uow, $oid, $config['filePathField'], $info['filePath']);
+
+        unset($this->fileInfoObjects[$oid]);
     }
     }
 
 
     /**
     /**
@@ -508,6 +501,41 @@ class UploadableListener extends MappedEventSubscriber
         return $this->defaultFileInfoClass;
         return $this->defaultFileInfoClass;
     }
     }
 
 
+    /**
+     * Adds a FileInfoInterface object for the given entity
+     *
+     * @param object - $entity
+     * @param FileInfo\FileInfoInterface $fileInfo
+     */
+    public function addEntityFileInfo($entity, $fileInfo)
+    {
+        $fileInfoClass = $this->getDefaultFileInfoClass();
+        $fileInfo = is_array($fileInfo) ? new $fileInfoClass($fileInfo) : $fileInfo;
+
+        if (!is_object($fileInfo) || !($fileInfo instanceof FileInfoInterface)) {
+            $msg = 'You must pass an instance of FileInfoInterface or a valid array for entity of class "%s".';
+
+            throw new \RuntimeException(sprintf($msg,
+                get_class($entity)
+            ));
+        }
+
+        $this->fileInfoObjects[spl_object_hash($entity)] = $fileInfo;
+    }
+
+    public function getEntityFileInfo($entity)
+    {
+        $oid = spl_object_hash($entity);
+
+        if (!isset($this->fileInfoObjects[$oid])) {
+            throw new \RuntimeException(sprintf('There\'s no FileInfoInterface object for entity of class "%s".',
+                get_class($entity)
+            ));
+        }
+
+        return $this->fileInfoObjects[$oid];
+    }
+
     /**
     /**
      * {@inheritDoc}
      * {@inheritDoc}
      */
      */

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

@@ -138,7 +138,6 @@ people to push their own additional attributes/elements into the same field elem
     <xs:attribute name="append-number" 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="callback" type="xs:string" use="optional" />
     <xs:attribute name="path" 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" />
     <xs:attribute name="path-method" type="xs:string" use="optional" />
     <xs:attribute name="filename-generator" type="xs:string" use="optional" />
     <xs:attribute name="filename-generator" type="xs:string" use="optional" />
   </xs:complexType>
   </xs:complexType>

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

@@ -26,7 +26,6 @@
             append-number="true"
             append-number="true"
             path="/my/path"
             path="/my/path"
             path-method="getPath"
             path-method="getPath"
-            file-info-property="fileInfo"
             callback="callbackMethod"
             callback="callbackMethod"
             filename-generator="SHA1" />
             filename-generator="SHA1" />
 
 

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

@@ -8,7 +8,6 @@ Mapping\Fixture\Yaml\Uploadable:
       appendNumber: true
       appendNumber: true
       path: '/my/path'
       path: '/my/path'
       pathMethod: getPath
       pathMethod: getPath
-      fileInfoProperty: fileInfo
       callback: callbackMethod
       callback: callbackMethod
       filenameGenerator: SHA1
       filenameGenerator: SHA1
   id:
   id:

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

@@ -63,7 +63,6 @@ class UploadableMappingTest extends BaseTestCaseOM
         $this->assertTrue($config['appendNumber']);
         $this->assertTrue($config['appendNumber']);
         $this->assertEquals('/my/path', $config['path']);
         $this->assertEquals('/my/path', $config['path']);
         $this->assertEquals('getPath', $config['pathMethod']);
         $this->assertEquals('getPath', $config['pathMethod']);
-        $this->assertEquals('fileInfo', $config['fileInfoProperty']);
         $this->assertEquals('mimeType', $config['fileMimeTypeField']);
         $this->assertEquals('mimeType', $config['fileMimeTypeField']);
         $this->assertEquals('path', $config['filePathField']);
         $this->assertEquals('path', $config['filePathField']);
         $this->assertEquals('size', $config['fileSizeField']);
         $this->assertEquals('size', $config['fileSizeField']);

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

@@ -63,7 +63,6 @@ class UploadableMappingTest extends BaseTestCaseOM
         $this->assertTrue($config['appendNumber']);
         $this->assertTrue($config['appendNumber']);
         $this->assertEquals('/my/path', $config['path']);
         $this->assertEquals('/my/path', $config['path']);
         $this->assertEquals('getPath', $config['pathMethod']);
         $this->assertEquals('getPath', $config['pathMethod']);
-        $this->assertEquals('fileInfo', $config['fileInfoProperty']);
         $this->assertEquals('mimeType', $config['fileMimeTypeField']);
         $this->assertEquals('mimeType', $config['fileMimeTypeField']);
         $this->assertEquals('path', $config['filePathField']);
         $this->assertEquals('path', $config['filePathField']);
         $this->assertEquals('size', $config['fileSizeField']);
         $this->assertEquals('size', $config['fileSizeField']);

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

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 
 /**
 /**
  * @ORM\Entity
  * @ORM\Entity
- * @Gedmo\Uploadable(allowOverwrite=true, fileInfoProperty="fileInfo", pathMethod="getPath", callback="callbackMethod")
+ * @Gedmo\Uploadable(allowOverwrite=true, pathMethod="getPath", callback="callbackMethod")
  */
  */
 class File
 class File
 {
 {

+ 1 - 1
tests/Gedmo/Uploadable/Fixture/Entity/FileWithSha1Name.php

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 
 /**
 /**
  * @ORM\Entity
  * @ORM\Entity
- * @Gedmo\Uploadable(pathMethod="getPath", fileInfoProperty="fileInfo", filenameGenerator="SHA1")
+ * @Gedmo\Uploadable(pathMethod="getPath", filenameGenerator="SHA1")
  */
  */
 class FileWithSha1Name
 class FileWithSha1Name
 {
 {

+ 1 - 1
tests/Gedmo/Uploadable/Fixture/Entity/FileWithoutPath.php

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 
 /**
 /**
  * @ORM\Entity
  * @ORM\Entity
- * @Gedmo\Uploadable(fileInfoProperty="fileInfo")
+ * @Gedmo\Uploadable
  */
  */
 class FileWithoutPath
 class FileWithoutPath
 {
 {

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

@@ -8,7 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
 
 
 /**
 /**
  * @ORM\Entity
  * @ORM\Entity
- * @Gedmo\Uploadable(fileInfoProperty="fileInfo", pathMethod="getPath")
+ * @Gedmo\Uploadable(pathMethod="getPath")
  */
  */
 class Image
 class Image
 {
 {

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

@@ -90,7 +90,7 @@ class UploadableEntityTest extends BaseTestCaseORM
 
 
         $image2 = new Image();
         $image2 = new Image();
         $image2->setTitle('456');
         $image2->setTitle('456');
-        $image2->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($image2, $fileInfo);
 
 
         $this->em->persist($image2);
         $this->em->persist($image2);
         $this->em->flush();
         $this->em->flush();
@@ -110,7 +110,7 @@ class UploadableEntityTest extends BaseTestCaseORM
         $fileInfo['name'] = $this->testFilename2;
         $fileInfo['name'] = $this->testFilename2;
 
 
         // We use a FileInfoInterface instance here
         // We use a FileInfoInterface instance here
-        $image2->setFileInfo(new FileInfoArray($fileInfo));
+        $this->listener->addEntityFileInfo($image2, new FileInfoArray($fileInfo));
 
 
         // For now, we need to force the update changing one of the managed fields. If we don't do this,
         // For now, we need to force the update changing one of the managed fields. If we don't do this,
         // entity won't be marked for update
         // entity won't be marked for update
@@ -155,9 +155,9 @@ class UploadableEntityTest extends BaseTestCaseORM
         $fileInfo2 = $this->generateUploadedFile($filesArrayIndex);
         $fileInfo2 = $this->generateUploadedFile($filesArrayIndex);
         $fileInfo3 = $this->generateUploadedFile($filesArrayIndex);
         $fileInfo3 = $this->generateUploadedFile($filesArrayIndex);
 
 
-        $file1->setFileInfo($fileInfo);
-        $file2->setFileInfo($fileInfo2);
-        $file3->setFileInfo($fileInfo3);
+        $this->listener->addEntityFileInfo($file1, $fileInfo);
+        $this->listener->addEntityFileInfo($file2, $fileInfo2);
+        $this->listener->addEntityFileInfo($file3, $fileInfo3);
 
 
         $this->em->persist($article);
         $this->em->persist($article);
 
 
@@ -183,7 +183,7 @@ class UploadableEntityTest extends BaseTestCaseORM
 
 
         $fileInfo = $this->generateUploadedFile();
         $fileInfo = $this->generateUploadedFile();
 
 
-        $file->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($file, $fileInfo);
 
 
         $this->em->persist($file);
         $this->em->persist($file);
         $this->em->flush();
         $this->em->flush();
@@ -197,7 +197,7 @@ class UploadableEntityTest extends BaseTestCaseORM
         $file = new FileWithoutPath();
         $file = new FileWithoutPath();
         $fileInfo = $this->generateUploadedFile();
         $fileInfo = $this->generateUploadedFile();
 
 
-        $file->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($file, $fileInfo);
 
 
         $this->em->persist($file);
         $this->em->persist($file);
         $this->em->flush();
         $this->em->flush();
@@ -212,7 +212,7 @@ class UploadableEntityTest extends BaseTestCaseORM
         $file = new File();
         $file = new File();
         $fileInfo = $this->generateUploadedFile();
         $fileInfo = $this->generateUploadedFile();
 
 
-        $file->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($file, $fileInfo);
 
 
         $this->em->persist($file);
         $this->em->persist($file);
         $this->em->flush();
         $this->em->flush();
@@ -231,7 +231,7 @@ class UploadableEntityTest extends BaseTestCaseORM
         $fileInfo = $this->generateUploadedFile();
         $fileInfo = $this->generateUploadedFile();
         $fileInfo['error'] = $error;
         $fileInfo['error'] = $error;
 
 
-        $file->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($file, $fileInfo);
 
 
         $this->em->persist($file);
         $this->em->persist($file);
         $this->em->flush();
         $this->em->flush();
@@ -246,14 +246,10 @@ class UploadableEntityTest extends BaseTestCaseORM
         $file = new File();
         $file = new File();
         $fileInfo = $this->generateUploadedFile();
         $fileInfo = $this->generateUploadedFile();
 
 
-        $file->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($file, $fileInfo);
+        $fileInfo = $this->listener->getEntityFileInfo($file);
 
 
-        $this->em->persist($file);
-        $this->em->flush();
-
-        $this->em->refresh($file);
-
-        $this->assertInstanceOf($fileInfoStubClass, $file->getFileInfo());
+        $this->assertInstanceOf($fileInfoStubClass, $fileInfo);
     }
     }
 
 
     public function testFileWithFilenameGenerator()
     public function testFileWithFilenameGenerator()
@@ -261,7 +257,7 @@ class UploadableEntityTest extends BaseTestCaseORM
         $file = new FileWithSha1Name();
         $file = new FileWithSha1Name();
         $fileInfo = $this->generateUploadedFile();
         $fileInfo = $this->generateUploadedFile();
 
 
-        $file->setFileInfo($fileInfo);
+        $this->listener->addEntityFileInfo($file, $fileInfo);
 
 
         $this->em->persist($file);
         $this->em->persist($file);
         $this->em->flush();
         $this->em->flush();