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