1
0
Fork 0

Decorate serializer service to avoid having to always pass context array

This commit is contained in:
Krzysztof Sikorski 2022-04-20 21:11:49 +02:00
parent 93a5ab6b4f
commit a733008cb4
Signed by: krzysztof-sikorski
GPG Key ID: 4EB564BD08FE8476
4 changed files with 51 additions and 9 deletions

View File

@ -3,7 +3,9 @@
declare(strict_types=1);
use App\Contract\Config\AppParameters;
use App\Service\Serializer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Serializer\SerializerInterface;
return static function (ContainerConfigurator $containerConfigurator) {
$containerConfigurator->import(
@ -41,4 +43,6 @@ return static function (ContainerConfigurator $containerConfigurator) {
resource: __DIR__ . '/../src/Controller/',
);
$prototypeConfigurator->tag(name: 'controller.service_arguments');
$servicesConfigurator->set(id: Serializer::class)->decorate(id: SerializerInterface::class);
};

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
namespace App\Command;
use App\Contract\Config\AppParameters;
use App\Contract\Config\AppSerializationGroups;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
@ -39,9 +39,12 @@ abstract class BaseCommand extends Command
mixed $value,
?string $serializationGroup = null
): void {
$context = AppParameters::SERIALIZER_DEFAULT_CONTEXT;
$context = [];
if (null !== $serializationGroup) {
$context[ObjectNormalizer::GROUPS][] = $serializationGroup;
$context[ObjectNormalizer::GROUPS] = [
AppSerializationGroups::DEFAULT,
$serializationGroup,
];
}
$serializedValue = $this->serializer->serialize(data: $value, format: JsonEncoder::FORMAT, context: $context);
$io->info(message: sprintf('%s: %s', $label, $serializedValue));

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace App\Controller;
use App\Contract\Config\AppParameters;
use App\Contract\Config\AppRoutes;
use App\DTO\NexusRawDataSubmissionResult;
use App\Service\NexusRawDataManager;
@ -71,11 +70,7 @@ final class SubmitJsonController
private function createJsonResponse(mixed $data, int $status): Response
{
$serializedData = $this->serializer->serialize(
data: $data,
format: JsonEncoder::FORMAT,
context: AppParameters::SERIALIZER_DEFAULT_CONTEXT,
);
$serializedData = $this->serializer->serialize(data: $data, format: JsonEncoder::FORMAT);
return new JsonResponse(data: $serializedData, status: $status, json: true);
}
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Contract\Config\AppParameters;
use Symfony\Component\Serializer\SerializerInterface;
use function array_merge;
/**
* Decorator service to work around the bug that original service seems to ignore default context defined in config
*/
class Serializer implements SerializerInterface
{
public function __construct(
private SerializerInterface $serializer,
) {
}
public function serialize(mixed $data, string $format, array $context = []): string
{
$context = $this->decorateContext(context: $context);
return $this->serializer->serialize(data: $data, format: $format, context: $context);
}
public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed
{
$context = $this->decorateContext(context: $context);
return $this->serializer->deserialize(data: $data, type: $type, format: $format, context: $context);
}
private function decorateContext(array $context): array
{
return array_merge(AppParameters::SERIALIZER_DEFAULT_CONTEXT, $context);
}
}