Skip to content

Commit b626ac9

Browse files
author
optior
authored
Replace event subscriber with normalizer for resolving file url of me… (#1339)
1 parent 9cd2293 commit b626ac9

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

core/file-upload.md

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -172,65 +172,46 @@ final class CreateMediaObjectAction
172172
Returning the plain file path on the filesystem where the file is stored is not useful for the client, which needs a
173173
URL to work with.
174174

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:
176176

177177
```php
178178
<?php
179-
// api/src/EventSubscriber/ResolveMediaObjectContentUrlSubscriber.php
179+
// api/src/Serializer/MediaObjectNormalizer.php
180180
181-
namespace App\EventSubscriber;
181+
namespace App\Serializer;
182182
183-
use ApiPlatform\Core\EventListener\EventPriorities;
184-
use ApiPlatform\Core\Util\RequestAttributesExtractor;
185183
use App\Entity\MediaObject;
186-
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
187-
use Symfony\Component\HttpFoundation\Response;
188-
use Symfony\Component\HttpKernel\Event\ViewEvent;
189-
use Symfony\Component\HttpKernel\KernelEvents;
184+
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
185+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
186+
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
190187
use Vich\UploaderBundle\Storage\StorageInterface;
191188
192-
final class ResolveMediaObjectContentUrlSubscriber implements EventSubscriberInterface
189+
final class MediaObjectNormalizer implements ContextAwareNormalizerInterface
193190
{
194-
private $storage;
191+
use NormalizerAwareTrait;
195192
196-
public function __construct(StorageInterface $storage)
197-
{
198-
$this->storage = $storage;
199-
}
193+
private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';
200194
201-
public static function getSubscribedEvents(): array
195+
public function __construct(private StorageInterface $storage)
202196
{
203-
return [
204-
KernelEvents::VIEW => ['onPreSerialize', EventPriorities::PRE_SERIALIZE],
205-
];
206197
}
207198
208-
public function onPreSerialize(ViewEvent $event): void
199+
public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
209200
{
210-
$controllerResult = $event->getControllerResult();
211-
$request = $event->getRequest();
201+
$context[self::ALREADY_CALLED] = true;
212202
213-
if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
214-
return;
215-
}
216-
217-
if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !\is_a($attributes['resource_class'], MediaObject::class, true)) {
218-
return;
219-
}
203+
$object->contentUrl = $this->storage->resolveUri($object, 'file');
220204
221-
$mediaObjects = $controllerResult;
205+
return $this->normalizer->normalize($object, $format, $context);
206+
}
222207
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;
225212
}
226213
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;
234215
}
235216
}
236217
```

0 commit comments

Comments
 (0)