@@ -172,65 +172,46 @@ final class CreateMediaObjectAction
172
172
Returning the plain file path on the filesystem where the file is stored is not useful for the client, which needs a
173
173
URL to work with.
174
174
175
- An [event subscriber](events .md#custom-event-listeners ) could be used to set the `contentUrl` property:
175
+ A [normalizer](serialization .md#normalization ) could be used to set the `contentUrl` property:
176
176
177
177
` ` ` php
178
178
<?php
179
- // api/src/EventSubscriber/ResolveMediaObjectContentUrlSubscriber .php
179
+ // api/src/Serializer/MediaObjectNormalizer .php
180
180
181
- namespace App\E ventSubscriber ;
181
+ namespace App\S erializer ;
182
182
183
- use ApiPlatform\C ore\E ventListener\E ventPriorities;
184
- use ApiPlatform\C ore\Util\Requ estAttributesExtractor;
185
183
use App\E ntity\M ediaObject;
186
- use Symfony\C omponent\E ventDispatcher\E ventSubscriberInterface;
187
- use Symfony\C omponent\H ttpFoundation\R esponse;
188
- use Symfony\C omponent\H ttpKernel\E vent\V iewEvent;
189
- use Symfony\C omponent\H ttpKernel\K ernelEvents;
184
+ use Symfony\C omponent\S erializer\N ormalizer\C ontextAwareNormalizerInterface;
185
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerAwareInterface;
186
+ use Symfony\C omponent\S erializer\N ormalizer\N ormalizerAwareTrait;
190
187
use Vich\UploaderB undle\S torage\S torageInterface;
191
188
192
- final class ResolveMediaObjectContentUrlSubscriber implements EventSubscriberInterface
189
+ final class MediaObjectNormalizer implements ContextAwareNormalizerInterface
193
190
{
194
- private $storage ;
191
+ use NormalizerAwareTrait ;
195
192
196
- public function __construct(StorageInterface $storage)
197
- {
198
- $this->storage = $storage;
199
- }
193
+ private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';
200
194
201
- public static function getSubscribedEvents(): array
195
+ public function __construct(private StorageInterface $storage)
202
196
{
203
- return [
204
- KernelEvents::VIEW => ['onPreSerialize', EventPriorities::PRE_SERIALIZE],
205
- ];
206
197
}
207
198
208
- public function onPreSerialize(ViewEvent $event ): void
199
+ public function normalize($object, ?string $format = null, array $context = [] ): array|string|int|float|bool| \A rrayObject|null
209
200
{
210
- $controllerResult = $event->getControllerResult();
211
- $request = $event->getRequest();
201
+ $context[self::ALREADY_CALLED] = true;
212
202
213
- if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
214
- return;
215
- }
216
-
217
- if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !\i s_a($attributes['resource_class'], MediaObject::class, true)) {
218
- return;
219
- }
203
+ $object->contentUrl = $this->storage->resolveUri($object, 'file');
220
204
221
- $mediaObjects = $controllerResult;
205
+ return $this->normalizer->normalize($object, $format, $context);
206
+ }
222
207
223
- if (!is_iterable($mediaObjects)) {
224
- $mediaObjects = [$mediaObjects];
208
+ public function supportsNormalization($data, ?string $format = null, array $context = []): bool
209
+ {
210
+ if (isset($context[self::ALREADY_CALLED])) {
211
+ return false;
225
212
}
226
213
227
- foreach ($mediaObjects as $mediaObject) {
228
- if (!$mediaObject instanceof MediaObject) {
229
- continue;
230
- }
231
-
232
- $mediaObject->contentUrl = $this->storage->resolveUri($mediaObject, 'file');
233
- }
214
+ return $data instanceof MediaObject;
234
215
}
235
216
}
236
217
` ` `
0 commit comments