|
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\LiipImagineSerializableField; |
|
14
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
15
|
|
|
use JMS\Serializer\EventDispatcher\ObjectEvent; |
|
16
|
|
|
use Symfony\Component\Routing\RequestContext; |
|
17
|
|
|
use Doctrine\Common\Persistence\Proxy; |
|
18
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
19
|
|
|
use Vich\UploaderBundle\Storage\StorageInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* JmsSerializeListenerAbstract |
|
23
|
|
|
* |
|
24
|
|
|
* @author Denis Golubovskiy <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class JmsSerializeListenerAbstract |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var RequestContext $requestContext Request context |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $requestContext; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var CachedReader $annotationReader Cached annotation reader |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $annotationReader; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var CacheManager $cacheManager LiipImagineBundle Cache Manager |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $cacheManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var StorageInterface $storage Vich storage |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $vichStorage; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array $config Bundle config |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $config; |
|
52
|
|
|
|
|
53
|
|
|
/** @noinspection MoreThanThreeArgumentsInspection |
|
54
|
|
|
* |
|
55
|
|
|
* JmsSerializeListenerAbstract constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param RequestContext $requestContext |
|
58
|
|
|
* @param CachedReader $annotationReader |
|
59
|
|
|
* @param CacheManager $cacheManager |
|
60
|
|
|
* @param StorageInterface $vichStorage |
|
61
|
|
|
* @param array $config |
|
62
|
|
|
*/ |
|
63
|
|
|
public function __construct( |
|
64
|
|
|
RequestContext $requestContext, |
|
65
|
|
|
CachedReader $annotationReader, |
|
66
|
|
|
CacheManager $cacheManager, |
|
67
|
|
|
StorageInterface $vichStorage, |
|
68
|
|
|
array $config |
|
69
|
|
|
) { |
|
70
|
|
|
$this->requestContext = $requestContext; |
|
71
|
|
|
$this->annotationReader = $annotationReader; |
|
72
|
|
|
$this->cacheManager = $cacheManager; |
|
73
|
|
|
$this->vichStorage = $vichStorage; |
|
74
|
|
|
$this->config = $config; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param ObjectEvent $event Event |
|
79
|
|
|
* @return mixed |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getObject(ObjectEvent $event) |
|
82
|
|
|
{ |
|
83
|
|
|
$object = $event->getObject(); |
|
84
|
|
|
|
|
85
|
|
|
if ($object instanceof Proxy |
|
86
|
|
|
&& ! $object->__isInitialized() |
|
87
|
|
|
) { |
|
88
|
|
|
$object->__load(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $object; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** @noinspection GenericObjectTypeUsageInspection |
|
95
|
|
|
* @param LiipImagineSerializableField $liipAnnotation |
|
96
|
|
|
* @param object $object Serialized object |
|
97
|
|
|
* @param string $value Value of field |
|
98
|
|
|
* @return array|string |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function serializeValue(LiipImagineSerializableField $liipAnnotation, $object, $value) |
|
101
|
|
|
{ |
|
102
|
|
|
if ($vichField = $liipAnnotation->getVichUploaderField()) { |
|
103
|
|
|
$value = $this->vichStorage->resolveUri($object, $vichField); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$result = []; |
|
107
|
|
|
if (array_key_exists('includeOriginal', $this->config) && $this->config['includeOriginal']) { |
|
108
|
|
|
$result['original'] = (array_key_exists('includeHostForOriginal', $this->config) && $this->config['includeHostForOriginal'] && $liipAnnotation->getVichUploaderField()) |
|
109
|
|
|
? $this->getHostUrl().$value |
|
110
|
|
|
: $value; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$filters = $liipAnnotation->getFilter(); |
|
114
|
|
|
if (is_array($filters)) { |
|
115
|
|
|
/** @var array $filters */ |
|
116
|
|
|
foreach ($filters as $filter) { |
|
117
|
|
|
$result[$filter] = $this->cacheManager->getBrowserPath($value, $filter); |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
if (array_key_exists('includeHost', $this->config) && !$this->config['includeHost']) { |
|
|
|
|
|
|
120
|
|
|
$result[$filter] = $this->stripHostFromUrl($result[$filter]); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$filtered = $this->cacheManager->getBrowserPath($value, $filters); |
|
128
|
|
|
if (count($result) !== 0) { |
|
129
|
|
|
$result[$filters] = $filtered; |
|
130
|
|
|
|
|
131
|
|
View Code Duplication |
if (array_key_exists('includeHost', $this->config) && !$this->config['includeHost']) { |
|
|
|
|
|
|
132
|
|
|
$result[$filters] = $this->stripHostFromUrl($result[$filters]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $result; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $filtered; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get host url (scheme, host, port) |
|
143
|
|
|
* |
|
144
|
|
|
* @return string Host url |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getHostUrl() |
|
147
|
|
|
{ |
|
148
|
|
|
$url = $this->requestContext->getScheme().'://'.$this->requestContext->getHost(); |
|
149
|
|
|
|
|
150
|
|
|
if ($this->requestContext->getScheme() === 'http' && $this->requestContext->getHttpPort() && $this->requestContext->getHttpPort() !== 80) { |
|
151
|
|
|
$url .= ':'.$this->requestContext->getHttpPort(); |
|
152
|
|
|
} elseif ($this->requestContext->getScheme() === 'https' && $this->requestContext->getHttpsPort() && $this->requestContext->getHttpsPort() !== 443) { |
|
153
|
|
|
$url .= ':'.$this->requestContext->getHttpsPort(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $url; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Removes host and scheme (protocol) from passed url |
|
161
|
|
|
* |
|
162
|
|
|
* @param $url |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
private function stripHostFromUrl($url) |
|
166
|
|
|
{ |
|
167
|
|
|
$parts = parse_url($url); |
|
168
|
|
|
if (isset ($parts['path'])) { |
|
169
|
|
|
if (isset ($parts['query'])) { |
|
170
|
|
|
return $parts['path'] . '?' . $parts['query']; |
|
171
|
|
|
} else { |
|
172
|
|
|
return $parts['path']; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.