Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Bukashk0zzz committed Mar 9, 2016
1 parent baecb68 commit a0f97cc
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 105 deletions.
51 changes: 31 additions & 20 deletions Annotation/LiipImagineSerializableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ final class LiipImagineSerializableField implements Annotation
*/
private $virtualField;

/**
* @var array $options Options
*/
private $options;


/**
* Constructor
Expand All @@ -48,34 +53,24 @@ final class LiipImagineSerializableField implements Annotation
*/
public function __construct(array $options)
{
if (!array_key_exists('value', $options) && !array_key_exists('filter', $options)) {
$this->options = $options;

if (!array_key_exists('value', $this->options) && !array_key_exists('filter', $this->options)) {
throw new \LogicException(sprintf('Either "value" or "filter" option must be set.'));
}

if (array_key_exists('value', $options)) {
if (!is_string($options['value'])) {
throw new \InvalidArgumentException(sprintf('Option "value" must be a string.'));
}
if ($this->checkOption('value')) {
$this->setFilter($options['value']);
} elseif (array_key_exists('filter', $options)) {
if (!is_string($options['filter'])) {
throw new \InvalidArgumentException(sprintf('Option "filter" must be a string.'));
}
$this->setFilter($options['filter']);
} elseif ($this->checkOption('filter')) {
$this->setFilter($this->options['filter']);
}

if (array_key_exists('vichUploaderField', $options)) {
if (!is_string($options['vichUploaderField'])) {
throw new \InvalidArgumentException(sprintf('Option "vichUploaderField" must be a string.'));
}
$this->setVichUploaderField($options['vichUploaderField']);
if ($this->checkOption('vichUploaderField')) {
$this->setVichUploaderField($this->options['vichUploaderField']);
}

if (array_key_exists('virtualField', $options)) {
if (!is_string($options['virtualField'])) {
throw new \InvalidArgumentException(sprintf('Option "virtualField" must be a string.'));
}
$this->setVirtualField($options['virtualField']);
if ($this->checkOption('virtualField')) {
$this->setVirtualField($this->options['virtualField']);
}
}

Expand Down Expand Up @@ -135,4 +130,20 @@ public function setVirtualField($virtualField)

return $this;
}

/**
* @param $optionName
* @return bool
* @throws \Exception
*/
private function checkOption($optionName) {
if (array_key_exists($optionName, $this->options)) {
if (!is_string($this->options[$optionName])) {
throw new \InvalidArgumentException(sprintf('Option "'.$optionName.'" must be a string.'));
}
return true;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
use Vich\UploaderBundle\Storage\StorageInterface;

/**
* JmsPostSerializeListener
* JmsSerializeListener
*
* @author Denis Golubovskiy <bukashk0zzz@gmail.com>
*/
class JmsPostSerializeListener
class JmsSerializeListener
{
/**
* @var RequestContext $requestContext Request context
Expand Down Expand Up @@ -53,13 +53,18 @@ class JmsPostSerializeListener
private $config;

/**
* @var array $serializedObjects Serialized objects
* @var array $serializedObjects Pre Serialized objects
*/
private $serializedObjects = [];
private $preSerializedObjects = [];

/**
* @var array $postSerializedObjects Post Serialized objects
*/
private $postSerializedObjects = [];

/** @noinspection MoreThanThreeArgumentsInspection
*
* JmsPostSerializeListener constructor.
* JmsSerializeListener constructor.
* @param RequestContext $requestContext
* @param CachedReader $annotationReader
* @param CacheManager $cacheManager
Expand All @@ -81,22 +86,52 @@ public function __construct(
}

/**
* On post serialize
* On pre serialize
*
* @param ObjectEvent $event Event
*/
public function onPostSerialize(ObjectEvent $event)
public function onPreSerialize(ObjectEvent $event)
{
$object = $event->getObject();
$object = $this->getObject($event);
$objectUid = spl_object_hash($object);

if ($object instanceof Proxy
&& ! $object->__isInitialized()
) {
$object->__load();
if (in_array($objectUid, $this->preSerializedObjects, true)) {
return;
}

$classAnnotation = $this->annotationReader->getClassAnnotation(
new \ReflectionClass(ClassUtils::getClass($object)),
LiipImagineSerializableClass::class
);

if ($classAnnotation instanceof LiipImagineSerializableClass) {
$reflectionClass = ClassUtils::newReflectionClass(get_class($object));

foreach ($reflectionClass->getProperties() as $property) {
$liipImagineAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class);
$property->setAccessible(true);

if ($liipImagineAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object) && !$liipImagineAnnotation->getVirtualField()) {
$property->setValue($object, $this->serializeValue($liipImagineAnnotation, $object, $value));
}
}

$this->preSerializedObjects[$objectUid] = $objectUid;
}

}

/**
* On post serialize
*
* @param ObjectEvent $event Event
*/
public function onPostSerialize(ObjectEvent $event)
{
$object = $this->getObject($event);
$objectUid = spl_object_hash($object);
if (in_array($objectUid, $this->serializedObjects, true)) {

if (in_array($objectUid, $this->postSerializedObjects, true)) {
return;
}

Expand All @@ -110,33 +145,58 @@ public function onPostSerialize(ObjectEvent $event)

foreach ($reflectionClass->getProperties() as $property) {
$liipImagineAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class);
$property->setAccessible(true);

if ($liipImagineAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object)) {
if ($vichField = $liipImagineAnnotation->getVichUploaderField()) {
$value = $this->vichStorage->resolvePath($object, $vichField);
}

$uri = $this->cacheManager->getBrowserPath($value, $liipImagineAnnotation->getFilter());
if ($virtualField = $liipImagineAnnotation->getVirtualField()) {
$object->{$virtualField} = $uri;
if ($liipImagineAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object) && $virtualField = $liipImagineAnnotation->getVirtualField()) {
/** @noinspection PhpUndefinedMethodInspection */
$event->getVisitor()->addData($virtualField, $this->serializeValue($liipImagineAnnotation, $object, $value));

if ($vichField && array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize']) {
$originalImageUri = $this->vichStorage->resolveUri($object, $vichField);
if ($vichField = $liipImagineAnnotation->getVichUploaderField() && array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize']) {
$originalImageUri = $this->vichStorage->resolveUri($object, $vichField);

if (array_key_exists('includeHost', $this->config) && $this->config['includeHost']) {
$originalImageUri = $this->getHostUrl().$originalImageUri;
}
$property->setValue($object, $originalImageUri);
if (array_key_exists('includeHost', $this->config) && $this->config['includeHost']) {
$originalImageUri = $this->getHostUrl().$originalImageUri;
}
} else {
$property->setValue($object, $uri);
$property->setValue($object, $originalImageUri);
}
}
}

$this->serializedObjects[$objectUid] = $objectUid;
$this->postSerializedObjects[$objectUid] = $objectUid;
}

}

/**
* @param ObjectEvent $event Event
* @return mixed
*/
protected function getObject($event)
{
$object = $event->getObject();

if ($object instanceof Proxy
&& ! $object->__isInitialized()
) {
$object->__load();
}

return $object;
}

/** @noinspection GenericObjectTypeUsageInspection
* @param LiipImagineSerializableField $liipImagineAnnotation
* @param object $object Serialized object
* @param string $value Value of field
* @return string
*/
private function serializeValue(LiipImagineSerializableField $liipImagineAnnotation, $object, $value)
{
if ($vichField = $liipImagineAnnotation->getVichUploaderField()) {
$value = $this->vichStorage->resolveUri($object, $vichField);
}

return $this->cacheManager->getBrowserPath($value, $liipImagineAnnotation->getFilter());
}

/**
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ use Bukashk0zzz\LiipImagineSerializationBundle\Annotation as Bukashk0zzz;
```

Bundle provides two annotations which allow the serialization of url or `@Vich\UploadableField` fields in your entities.
At first you have to add `@Bukashk0zzz\LiipImagineSerializationClass` to the entity class which has image fields.
At first you have to add `@Bukashk0zzz\LiipImagineSerializableClass` to the entity class which has image fields.
Then you have to add `@Bukashk0zzz\LiipImagineSerializableField` annotation to the field you want to serialize.

Annotation `@Bukashk0zzz\LiipImagineSerializationClass` does not have any option.
Annotation `@Bukashk0zzz\LiipImagineSerializableClass` does not have any option.
Annotation `@Bukashk0zzz\LiipImagineSerializableField` has one required option *filter* which value should link to the LiipImagine filter .
It can be set like this `@Bukashk0zzz\LiipImagineSerializableField("photoFile")` or `@Bukashk0zzz\LiipImagineSerializableField(filter="photoFile")`.
Also there is another two not required options:
Expand Down Expand Up @@ -114,7 +114,7 @@ use Bukashk0zzz\LiipImagineSerializationBundle\Annotation as Bukashk0zzz;
* @ORM\Entity()
*
* @Vich\Uploadable
* @Bukashk0zzz\LiipImagineSerializationClass
* @Bukashk0zzz\LiipImagineSerializableClass
*/
class User
{
Expand Down
3 changes: 2 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
bukashk0zzz_liip_imagine_serialization.listener.class: Bukashk0zzz\LiipImagineSerializationBundle\EventListener\JmsPostSerializeListener
bukashk0zzz_liip_imagine_serialization.listener.class: Bukashk0zzz\LiipImagineSerializationBundle\EventListener\JmsSerializeListener

services:
bukashk0zzz_liip_imagine_serialization.listener:
Expand All @@ -10,4 +10,5 @@ services:
- "@liip_imagine.cache.manager"
- "@vich_uploader.storage"
tags:
- { name: jms_serializer.event_listener, event: serializer.pre_serialize, method: onPreSerialize }
- { name: jms_serializer.event_listener, event: serializer.post_serialize, method: onPostSerialize }

0 comments on commit a0f97cc

Please sign in to comment.