|
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\Annotations\CachedReader; |
|
16
|
|
|
use Doctrine\Common\Util\ClassUtils; |
|
17
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
18
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
19
|
|
|
use Doctrine\Common\Persistence\Proxy; |
|
20
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
21
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* JmsSerializeListener |
|
25
|
|
|
* |
|
26
|
|
|
* @author Denis Golubovskiy <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class JmsSerializeListener |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var RequestContext $requestContext Request context |
|
32
|
|
|
*/ |
|
33
|
|
|
private $requestContext; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var CachedReader $annotationReader Cached annotation reader |
|
37
|
|
|
*/ |
|
38
|
|
|
private $annotationReader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var CacheManager $cacheManager LiipImagineBundle Cache Manager |
|
42
|
|
|
*/ |
|
43
|
|
|
private $cacheManager; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var StorageInterface $storage Vich storage |
|
47
|
|
|
*/ |
|
48
|
|
|
private $vichStorage; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var array $config Bundle config |
|
52
|
|
|
*/ |
|
53
|
|
|
private $config; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var array $serializedObjects Pre Serialized objects |
|
57
|
|
|
*/ |
|
58
|
|
|
private $preSerializedObjects = []; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var array $postSerializedObjects Post Serialized objects |
|
62
|
|
|
*/ |
|
63
|
|
|
private $postSerializedObjects = []; |
|
64
|
|
|
|
|
65
|
|
|
/** @noinspection MoreThanThreeArgumentsInspection |
|
66
|
|
|
* |
|
67
|
|
|
* JmsSerializeListener constructor. |
|
68
|
|
|
* @param RequestContext $requestContext |
|
69
|
|
|
* @param CachedReader $annotationReader |
|
70
|
|
|
* @param CacheManager $cacheManager |
|
71
|
|
|
* @param StorageInterface $vichStorage |
|
72
|
|
|
* @param array $config |
|
73
|
|
|
*/ |
|
74
|
|
|
public function __construct( |
|
75
|
|
|
RequestContext $requestContext, |
|
76
|
|
|
CachedReader $annotationReader, |
|
77
|
|
|
CacheManager $cacheManager, |
|
78
|
|
|
StorageInterface $vichStorage, |
|
79
|
|
|
array $config |
|
80
|
|
|
) { |
|
81
|
|
|
$this->requestContext = $requestContext; |
|
82
|
|
|
$this->annotationReader = $annotationReader; |
|
83
|
|
|
$this->cacheManager = $cacheManager; |
|
84
|
|
|
$this->vichStorage = $vichStorage; |
|
85
|
|
|
$this->config = $config; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* On pre serialize |
|
90
|
|
|
* |
|
91
|
|
|
* @param ObjectEvent $event Event |
|
92
|
|
|
*/ |
|
93
|
|
|
public function onPreSerialize(ObjectEvent $event) |
|
94
|
|
|
{ |
|
95
|
|
|
$object = $this->getObject($event); |
|
96
|
|
|
$objectUid = spl_object_hash($object); |
|
97
|
|
|
|
|
98
|
|
|
if (in_array($objectUid, $this->preSerializedObjects, true)) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$classAnnotation = $this->annotationReader->getClassAnnotation( |
|
103
|
|
|
new \ReflectionClass(ClassUtils::getClass($object)), |
|
104
|
|
|
LiipImagineSerializableClass::class |
|
105
|
|
|
); |
|
106
|
|
|
|
|
107
|
|
|
if ($classAnnotation instanceof LiipImagineSerializableClass) { |
|
108
|
|
|
$reflectionClass = ClassUtils::newReflectionClass(get_class($object)); |
|
109
|
|
|
|
|
110
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
|
111
|
|
|
$liipImagineAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class); |
|
112
|
|
|
$property->setAccessible(true); |
|
113
|
|
|
|
|
114
|
|
|
if ($liipImagineAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object) && !$liipImagineAnnotation->getVirtualField()) { |
|
|
|
|
|
|
115
|
|
|
$property->setValue($object, $this->serializeValue($liipImagineAnnotation, $object, $value)); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$this->preSerializedObjects[$objectUid] = $objectUid; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* On post serialize |
|
126
|
|
|
* |
|
127
|
|
|
* @param ObjectEvent $event Event |
|
128
|
|
|
*/ |
|
129
|
|
|
public function onPostSerialize(ObjectEvent $event) |
|
130
|
|
|
{ |
|
131
|
|
|
$object = $this->getObject($event); |
|
132
|
|
|
$objectUid = spl_object_hash($object); |
|
133
|
|
|
|
|
134
|
|
|
if (in_array($objectUid, $this->postSerializedObjects, true)) { |
|
135
|
|
|
return; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$classAnnotation = $this->annotationReader->getClassAnnotation( |
|
139
|
|
|
new \ReflectionClass(ClassUtils::getClass($object)), |
|
140
|
|
|
LiipImagineSerializableClass::class |
|
141
|
|
|
); |
|
142
|
|
|
|
|
143
|
|
|
if ($classAnnotation instanceof LiipImagineSerializableClass) { |
|
144
|
|
|
$reflectionClass = ClassUtils::newReflectionClass(get_class($object)); |
|
145
|
|
|
|
|
146
|
|
|
foreach ($reflectionClass->getProperties() as $property) { |
|
147
|
|
|
$liipImagineAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class); |
|
148
|
|
|
$property->setAccessible(true); |
|
149
|
|
|
|
|
150
|
|
|
if ($liipImagineAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object) && $virtualField = $liipImagineAnnotation->getVirtualField()) { |
|
|
|
|
|
|
151
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
152
|
|
|
$event->getVisitor()->addData($virtualField, $this->serializeValue($liipImagineAnnotation, $object, $value)); |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
if ($vichField = $liipImagineAnnotation->getVichUploaderField() && array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize']) { |
|
|
|
|
|
|
155
|
|
|
$originalImageUri = $this->vichStorage->resolveUri($object, $vichField); |
|
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
if (array_key_exists('includeHost', $this->config) && $this->config['includeHost']) { |
|
158
|
|
|
$originalImageUri = $this->getHostUrl().$originalImageUri; |
|
159
|
|
|
} |
|
160
|
|
|
$property->setValue($object, $originalImageUri); |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->postSerializedObjects[$objectUid] = $objectUid; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param ObjectEvent $event Event |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getObject($event) |
|
175
|
|
|
{ |
|
176
|
|
|
$object = $event->getObject(); |
|
177
|
|
|
|
|
178
|
|
|
if ($object instanceof Proxy |
|
179
|
|
|
&& ! $object->__isInitialized() |
|
180
|
|
|
) { |
|
181
|
|
|
$object->__load(); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
return $object; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** @noinspection GenericObjectTypeUsageInspection |
|
188
|
|
|
* @param LiipImagineSerializableField $liipImagineAnnotation |
|
189
|
|
|
* @param object $object Serialized object |
|
190
|
|
|
* @param string $value Value of field |
|
191
|
|
|
* @return string |
|
192
|
|
|
*/ |
|
193
|
|
|
private function serializeValue(LiipImagineSerializableField $liipImagineAnnotation, $object, $value) |
|
194
|
|
|
{ |
|
195
|
|
|
if ($vichField = $liipImagineAnnotation->getVichUploaderField()) { |
|
196
|
|
|
$value = $this->vichStorage->resolveUri($object, $vichField); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return $this->cacheManager->getBrowserPath($value, $liipImagineAnnotation->getFilter()); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get host url (scheme, host, port) |
|
204
|
|
|
* |
|
205
|
|
|
* @return string Host url |
|
206
|
|
|
*/ |
|
207
|
|
|
private function getHostUrl() |
|
208
|
|
|
{ |
|
209
|
|
|
$url = $this->requestContext->getScheme().'://'.$this->requestContext->getHost(); |
|
210
|
|
|
|
|
211
|
|
|
if ($this->requestContext->getScheme() === 'http' && $this->requestContext->getHttpPort() && $this->requestContext->getHttpPort() !== 80) { |
|
212
|
|
|
$url .= ':'.$this->requestContext->getHttpPort(); |
|
213
|
|
|
} elseif ($this->requestContext->getScheme() === 'https' && $this->requestContext->getHttpsPort() && $this->requestContext->getHttpsPort() !== 443) { |
|
214
|
|
|
$url .= ':'.$this->requestContext->getHttpsPort(); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $url; |
|
218
|
|
|
} |
|
219
|
|
|
} |
|
220
|
|
|
|