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'] = $value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$filters = $liipAnnotation->getFilter(); |
112
|
|
|
if (is_array($filters)) { |
113
|
|
|
foreach ($filters as $filter) { |
114
|
|
|
$result[$filter] = $this->cacheManager->getBrowserPath($value, $filter); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$filtered = $this->cacheManager->getBrowserPath($value, $filters); |
121
|
|
|
if (count($result) !== 0) { |
122
|
|
|
$result[$filters] = $filtered; |
123
|
|
|
|
124
|
|
|
return $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $filtered; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get host url (scheme, host, port) |
132
|
|
|
* |
133
|
|
|
* @return string Host url |
134
|
|
|
*/ |
135
|
|
|
protected function getHostUrl() |
136
|
|
|
{ |
137
|
|
|
$url = $this->requestContext->getScheme().'://'.$this->requestContext->getHost(); |
138
|
|
|
|
139
|
|
|
if ($this->requestContext->getScheme() === 'http' && $this->requestContext->getHttpPort() && $this->requestContext->getHttpPort() !== 80) { |
140
|
|
|
$url .= ':'.$this->requestContext->getHttpPort(); |
141
|
|
|
} elseif ($this->requestContext->getScheme() === 'https' && $this->requestContext->getHttpsPort() && $this->requestContext->getHttpsPort() !== 443) { |
142
|
|
|
$url .= ':'.$this->requestContext->getHttpsPort(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $url; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|