Skip to content

Replace event subscriber with normalizer for resolving file url of me… #1339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from Apr 8, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 21 additions & 39 deletions core/file-upload.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,67 +172,49 @@ final class CreateMediaObjectAction
Returning the plain file path on the filesystem where the file is stored is not useful for the client, which needs a
URL to work with.

An [event subscriber](events.md#custom-event-listeners) could be used to set the `contentUrl` property:
A [normalizer](serialization.md#normalization) could be used to set the `contentUrl` property:

```php
<?php
// api/src/EventSubscriber/ResolveMediaObjectContentUrlSubscriber.php
// api/src/Serializer/MediaObjectNormalizer.php

namespace App\EventSubscriber;
namespace App\Serializer;

use ApiPlatform\Core\EventListener\EventPriorities;
use ApiPlatform\Core\Util\RequestAttributesExtractor;
use App\Entity\MediaObject;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
use Vich\UploaderBundle\Storage\StorageInterface;

final class ResolveMediaObjectContentUrlSubscriber implements EventSubscriberInterface
final class MediaObjectNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
private $storage;
use NormalizerAwareTrait;

public function __construct(StorageInterface $storage)
{
$this->storage = $storage;
}
private const ALREADY_CALLED = 'MEDIA_OBJECT_NORMALIZER_ALREADY_CALLED';

public static function getSubscribedEvents(): array
public function __construct(private StorageInterface $storage)
{
return [
KernelEvents::VIEW => ['onPreSerialize', EventPriorities::PRE_SERIALIZE],
];
}

public function onPreSerialize(ViewEvent $event): void
public function normalize($object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
{
$controllerResult = $event->getControllerResult();
$request = $event->getRequest();
$context[self::ALREADY_CALLED] = true;

if ($controllerResult instanceof Response || !$request->attributes->getBoolean('_api_respond', true)) {
return;
}

if (!($attributes = RequestAttributesExtractor::extractAttributes($request)) || !\is_a($attributes['resource_class'], MediaObject::class, true)) {
return;
}
$object->contentUrl = $this->storage->resolveUri($object, 'file');

$mediaObjects = $controllerResult;
return $this->normalizer->normalize($object, $format, $context);
}

if (!is_iterable($mediaObjects)) {
$mediaObjects = [$mediaObjects];
public function supportsNormalization($data, ?string $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}

foreach ($mediaObjects as $mediaObject) {
if (!$mediaObject instanceof MediaObject) {
continue;
}

$mediaObject->contentUrl = $this->storage->resolveUri($mediaObject, 'file');
}
return $data instanceof MediaObject;
}
}

```

## Making a Request to the `/media_objects` Endpoint
Expand Down