1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Serializer; |
4
|
|
|
|
5
|
|
|
use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
9
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\Collection\Collection; |
10
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\ComponentLocation; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form; |
12
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Dynamic\AbstractDynamicPage; |
13
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\FileInterface; |
14
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Page; |
15
|
|
|
use Silverback\ApiComponentBundle\Entity\Layout\Layout; |
16
|
|
|
use Silverback\ApiComponentBundle\Factory\Entity\Content\Component\Form\FormViewFactory; |
17
|
|
|
use Silverback\ApiComponentBundle\Imagine\PathResolver; |
18
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
19
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
20
|
|
|
use Symfony\Component\Serializer\SerializerAwareInterface; |
21
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
22
|
|
|
|
23
|
|
|
final class ApiNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface |
24
|
|
|
{ |
25
|
|
|
private $decorated; |
26
|
|
|
private $imagineCacheManager; |
27
|
|
|
private $formViewFactory; |
28
|
|
|
private $pathResolver; |
29
|
|
|
private $em; |
30
|
|
|
private $projectDir; |
31
|
|
|
private $collectionDataProvider; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* FileNormalizer constructor. |
35
|
|
|
* @param NormalizerInterface $decorated |
36
|
|
|
* @param CacheManager $imagineCacheManager |
37
|
|
|
* @param FormViewFactory $formViewFactory |
38
|
|
|
* @param PathResolver $pathResolver |
39
|
|
|
* @param EntityManagerInterface $entityManager |
40
|
|
|
* @param ContextAwareCollectionDataProviderInterface $collectionDataProvider |
41
|
|
|
* @param string $projectDir |
42
|
|
|
*/ |
43
|
10 |
|
public function __construct( |
44
|
|
|
NormalizerInterface $decorated, |
45
|
|
|
CacheManager $imagineCacheManager, |
46
|
|
|
FormViewFactory $formViewFactory, |
47
|
|
|
PathResolver $pathResolver, |
48
|
|
|
EntityManagerInterface $entityManager, |
49
|
|
|
ContextAwareCollectionDataProviderInterface $collectionDataProvider, |
50
|
|
|
string $projectDir = '/' |
51
|
|
|
) { |
52
|
10 |
|
if (!$decorated instanceof DenormalizerInterface) { |
53
|
|
|
throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class)); |
54
|
|
|
} |
55
|
10 |
|
$this->decorated = $decorated; |
56
|
10 |
|
$this->imagineCacheManager = $imagineCacheManager; |
57
|
10 |
|
$this->formViewFactory = $formViewFactory; |
58
|
10 |
|
$this->pathResolver = $pathResolver; |
59
|
10 |
|
$this->em = $entityManager; |
60
|
10 |
|
$this->collectionDataProvider = $collectionDataProvider; |
61
|
10 |
|
$this->projectDir = $projectDir; |
62
|
10 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $data |
66
|
|
|
* @param string|null $format |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
5 |
|
public function supportsNormalization($data, $format = null): bool |
70
|
|
|
{ |
71
|
5 |
|
return $this->decorated->supportsNormalization($data, $format); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $object |
76
|
|
|
* @param string|null $format |
77
|
|
|
* @param array $context |
78
|
|
|
* @return array|bool|float|int|string |
79
|
|
|
* @throws \Symfony\Component\Serializer\Exception\LogicException |
80
|
|
|
* @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException |
81
|
|
|
* @throws \Symfony\Component\Serializer\Exception\CircularReferenceException |
82
|
|
|
* @throws \ApiPlatform\Core\Exception\ResourceClassNotSupportedException |
83
|
|
|
*/ |
84
|
6 |
|
public function normalize($object, $format = null, array $context = []) |
85
|
|
|
{ |
86
|
6 |
|
if (($object instanceof Page || $object instanceof AbstractDynamicPage) && !$object->getLayout()) { |
87
|
|
|
// Should we be using the ItemDataProvider (or detect data provider and use that, we already use a custom data provider for layouts) |
88
|
|
|
$object->setLayout($this->em->getRepository(Layout::class)->findOneBy(['default' => true])); |
89
|
|
|
} |
90
|
6 |
|
if ($object instanceof Collection) { |
91
|
|
|
// We should really find whatever the data provider is currently for the resource instead of just using the default |
92
|
|
|
$object->setCollection($this->collectionDataProvider->getCollection($object->getResource(), 'GET', $context)); |
93
|
|
|
} |
94
|
6 |
|
if ($object instanceof Form && !$object->getForm()) { |
95
|
1 |
|
$object->setForm($this->formViewFactory->create($object)); |
96
|
|
|
} |
97
|
6 |
|
$data = $this->decorated->normalize($object, $format, $context); |
98
|
|
|
|
99
|
6 |
|
if ($object instanceof FileInterface) { |
100
|
1 |
|
$data = array_merge($data, $this->getFileData($object)); |
101
|
|
|
} |
102
|
6 |
|
return $data; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \Silverback\ApiComponentBundle\Entity\Content\FileInterface $object |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
1 |
|
private function getFileData(FileInterface $object): array |
110
|
|
|
{ |
111
|
1 |
|
$data = []; |
112
|
1 |
|
$filePath = $object->getFilePath(); |
113
|
1 |
|
if ($filePath) { |
114
|
1 |
|
if (false !== \exif_imagetype($filePath)) { |
115
|
1 |
|
[$width, $height] = getimagesize($filePath); |
116
|
|
|
} else { |
117
|
|
|
$width = $height = 0; |
118
|
|
|
} |
119
|
1 |
|
$data['width'] = $width; |
120
|
1 |
|
$data['height'] = $height; |
121
|
|
|
|
122
|
1 |
|
$supported = $this->isImagineSupportedFile($filePath); |
123
|
1 |
|
foreach ($object::getImagineFilters() as $returnKey => $filter) { |
124
|
1 |
|
$data[$returnKey] = $supported ? parse_url( |
125
|
1 |
|
$this->imagineCacheManager->getBrowserPath($this->pathResolver->resolve($filePath), $filter), |
126
|
1 |
|
PHP_URL_PATH |
127
|
1 |
|
) : null; |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
$data['filePath'] = $this->getPublicPath($filePath); |
131
|
|
|
} |
132
|
1 |
|
return $data; |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
private function getPublicPath(string $filePath) { |
136
|
1 |
|
$publicPaths = [$this->projectDir, '/public/', '/web/']; |
137
|
1 |
|
foreach ($publicPaths as $path) { |
138
|
1 |
|
if (mb_strpos($filePath, $path) === 0 && $start = \strlen($path)) { |
139
|
1 |
|
$filePath = mb_substr($filePath, $start); |
140
|
|
|
} |
141
|
|
|
} |
142
|
1 |
|
return $filePath; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $data |
147
|
|
|
* @param string $type |
148
|
|
|
* @param string|null $format |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
5 |
|
public function supportsDenormalization($data, $type, $format = null): bool |
152
|
|
|
{ |
153
|
5 |
|
return $this->decorated->supportsDenormalization($data, $type, $format); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param mixed $data |
158
|
|
|
* @param string $class |
159
|
|
|
* @param string|null $format |
160
|
|
|
* @param array $context |
161
|
|
|
* @return object |
162
|
|
|
* @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException |
163
|
|
|
* @throws \Symfony\Component\Serializer\Exception\RuntimeException |
164
|
|
|
* @throws \Symfony\Component\Serializer\Exception\LogicException |
165
|
|
|
* @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException |
166
|
|
|
* @throws \Symfony\Component\Serializer\Exception\ExtraAttributesException |
167
|
|
|
* @throws \Symfony\Component\Serializer\Exception\BadMethodCallException |
168
|
|
|
* @throws \InvalidArgumentException |
169
|
|
|
*/ |
170
|
1 |
|
public function denormalize($data, $class, $format = null, array $context = []) |
171
|
|
|
{ |
172
|
1 |
|
$context['allow_extra_attributes'] = $class === Form::class; |
173
|
1 |
|
return $this->decorated->denormalize($data, $class, $format, $context); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param SerializerInterface $serializer |
178
|
|
|
*/ |
179
|
5 |
|
public function setSerializer(SerializerInterface $serializer) |
180
|
|
|
{ |
181
|
5 |
|
if ($this->decorated instanceof SerializerAwareInterface) { |
182
|
5 |
|
$this->decorated->setSerializer($serializer); |
183
|
|
|
} |
184
|
5 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param string $filePath |
188
|
|
|
* @return bool |
189
|
|
|
*/ |
190
|
3 |
|
public function isImagineSupportedFile(?string $filePath): bool |
191
|
|
|
{ |
192
|
3 |
|
if (!$filePath) { |
193
|
|
|
return false; |
194
|
|
|
} |
195
|
|
|
try { |
196
|
3 |
|
$imageType = \exif_imagetype($filePath); |
197
|
1 |
|
} catch (\Exception $e) { |
198
|
1 |
|
return false; |
199
|
|
|
} |
200
|
3 |
|
return \in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000, IMAGETYPE_PNG, IMAGETYPE_GIF], true); |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|