partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 6cb8470937fc0a5cb5474774ba26041f71f69c2e
parent 5a990f72a0399131495ae6f8052c4a6967bfd8ca
Author: Felicitus <felicitus@felicitus.org>
Date:   Tue, 22 Sep 2015 19:49:52 +0200

Refactored the temporary file listener to work with the current Dunglas API

Diffstat:
Mapp/config/config.yml | 3+--
Msrc/PartKeepr/UploadedFileBundle/EventListener/TemporaryFileEventListener.php | 67+++++++++++++++++++++++++++++++++++++++++--------------------------
2 files changed, 42 insertions(+), 28 deletions(-)

diff --git a/app/config/config.yml b/app/config/config.yml @@ -163,7 +163,6 @@ services: tags: - { name: doctrine.event_listener, event: onFlush } - my_event_listener: class: PartKeepr\UploadedFileBundle\EventListener\TemporaryFileEventListener arguments: @@ -172,7 +171,7 @@ services: - "@annotation_reader" - "@property_accessor" - "@api.iri_converter" - tags: [ { name: "kernel.event_listener", event: "api.pre_update", method: "replaceTemporaryFile" }, { name: "kernel.event_listener", event: "api.pre_create", method: "replaceTemporaryFile" } ] + tags: [ { name: "kernel.event_listener", event: "kernel.view", method: "replaceTemporaryFile", priority: 100 } ] resource.distributor: parent: "api.resource" diff --git a/src/PartKeepr/UploadedFileBundle/EventListener/TemporaryFileEventListener.php b/src/PartKeepr/UploadedFileBundle/EventListener/TemporaryFileEventListener.php @@ -3,13 +3,13 @@ namespace PartKeepr\UploadedFileBundle\EventListener; use Doctrine\Common\Annotations\Reader; use Dunglas\ApiBundle\Api\IriConverterInterface; -use Dunglas\ApiBundle\Event\DataEvent; use PartKeepr\ImageBundle\Entity\Image; use PartKeepr\ImageBundle\Entity\TempImage; use PartKeepr\ImageBundle\Services\ImageService; use PartKeepr\UploadedFileBundle\Entity\TempUploadedFile; use PartKeepr\UploadedFileBundle\Entity\UploadedFile; use PartKeepr\UploadedFileBundle\Services\UploadedFileService; +use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; use Symfony\Component\PropertyAccess\PropertyAccessorInterface; class TemporaryFileEventListener @@ -59,11 +59,15 @@ class TemporaryFileEventListener * Automatically extracts the proper setters and getters from the metadata and instantiates the correct * UploadedFile child class. * - * @param DataEvent $event The event + * @param GetResponseForControllerResultEvent $event The event */ - public function replaceTemporaryFile(DataEvent $event) + public function replaceTemporaryFile(GetResponseForControllerResultEvent $event) { - $data = $event->getData(); + $data = $event->getControllerResult(); + + if (!is_object($data)) { + return; + } $classReflection = new \ReflectionClass($data); @@ -97,19 +101,7 @@ class TemporaryFileEventListener if ($item instanceof TempUploadedFile || $item instanceof TempImage) { $targetEntity = $manyToOneAnnotation->targetEntity; - /** - * @var $newFile UploadedFile - */ - $newFile = new $targetEntity(); - - $this->replaceFile($newFile, $item); - - $setterName = $this->getReferenceSetter($newFile, $data); - - if ($setterName !== false) { - $this->propertyAccessor->setValue($newFile, $setterName, $data); - } - + $newFile = $this->setReplacementFile($targetEntity, $item, $data); $collection[$key] = $newFile; } } @@ -123,15 +115,7 @@ class TemporaryFileEventListener if ($item instanceof TempUploadedFile || $item instanceof TempImage) { $targetEntity = $oneToOneAnnotation->targetEntity; - $newFile = new $targetEntity(); - - $this->replaceFile($newFile, $item); - - $setterName = $this->getReferenceSetter($newFile, $data); - - if ($setterName !== false) { - $this->propertyAccessor->setValue($newFile, $setterName, $data); - } + $newFile = $this->setReplacementFile($targetEntity, $item, $data); $this->propertyAccessor->setValue($data, $property->getName(), $newFile); } else { @@ -148,8 +132,39 @@ class TemporaryFileEventListener } } } + + $event->setControllerResult($data); + } + + /** + * Replaces the TemporaryUploadedFile or TempImage with the actual instance. Automatically sets the + * reference to the owning entity. + * + * @param string $targetEntity The entity to create + * @param TempUploadedFile|TempImage $source The source entity + * @param object $target The entity where to set the property + * + * @return object The newly created object instance + */ + protected function setReplacementFile($targetEntity, $source, $target) + { + /** + * @var $newFile UploadedFile + */ + $newFile = new $targetEntity(); + + $this->replaceFile($newFile, $source); + + $setterName = $this->getReferenceSetter($newFile, $target); + + if ($setterName !== false) { + $this->propertyAccessor->setValue($newFile, $setterName, $target); + } + + return $newFile; } + protected function replaceFile(UploadedFile $target, UploadedFile $source) { if ($target instanceof Image) {