1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Bukashk0zzzLiipImagineSerializationBundle |
4
|
|
|
* |
5
|
|
|
* (c) Denis Golubovskiy <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Bukashk0zzz\LiipImagineSerializationBundle\EventListener; |
12
|
|
|
|
13
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation\LiipImagineSerializableClass; |
14
|
|
|
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation\LiipImagineSerializableField; |
15
|
|
|
use Doctrine\Common\Util\ClassUtils; |
16
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* JmsPreSerializeListener |
20
|
|
|
* |
21
|
|
|
* @author Denis Golubovskiy <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class JmsPreSerializeListener extends JmsSerializeListenerAbstract |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array $serializedObjects Pre Serialized objects |
27
|
|
|
*/ |
28
|
|
|
private $preSerializedObjects = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* On pre serialize |
32
|
|
|
* |
33
|
|
|
* @param ObjectEvent $event Event |
34
|
|
|
*/ |
35
|
|
|
public function onPreSerialize(ObjectEvent $event) |
36
|
|
|
{ |
37
|
|
|
$object = $this->getObject($event); |
38
|
|
|
$objectUid = spl_object_hash($object); |
39
|
|
|
|
40
|
|
|
if (in_array($objectUid, $this->preSerializedObjects, true)) { |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$classAnnotation = $this->annotationReader->getClassAnnotation( |
45
|
|
|
new \ReflectionClass(ClassUtils::getClass($object)), |
46
|
|
|
LiipImagineSerializableClass::class |
47
|
|
|
); |
48
|
|
|
|
49
|
|
|
if ($classAnnotation instanceof LiipImagineSerializableClass) { |
50
|
|
|
$reflectionClass = ClassUtils::newReflectionClass(get_class($object)); |
51
|
|
|
|
52
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
53
|
|
|
$liipAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class); |
54
|
|
|
$property->setAccessible(true); |
55
|
|
|
|
56
|
|
|
if ($liipAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object)) { |
57
|
|
|
$vichField = $liipAnnotation->getVichUploaderField(); |
58
|
|
|
|
59
|
|
|
if (!$liipAnnotation->getVirtualField()) { |
60
|
|
|
$property->setValue($object, $this->serializeValue($liipAnnotation, $object, $value)); |
61
|
|
|
} elseif ($vichField && array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize']) { |
62
|
|
|
$originalImageUri = $this->vichStorage->resolveUri($object, $vichField); |
63
|
|
|
|
64
|
|
|
if (array_key_exists('includeHost', $this->config) && $this->config['includeHost']) { |
65
|
|
|
$originalImageUri = $this->getHostUrl().$originalImageUri; |
66
|
|
|
} |
67
|
|
|
$property->setValue($object, $originalImageUri); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->preSerializedObjects[$objectUid] = $objectUid; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|