From f9ebf6d14f70e35e4002fbf08cd7d1e158b99e20 Mon Sep 17 00:00:00 2001 From: Mathias Arlaud Date: Thu, 19 Jan 2023 11:19:28 +0100 Subject: [PATCH] use NormalizerInterface instead of ObjectNormalizer --- src/Maker/MakeSerializerNormalizer.php | 68 +++++++++++++++---- src/Resources/config/makers.xml | 1 + .../skeleton/serializer/Normalizer.tpl.php | 2 +- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/src/Maker/MakeSerializerNormalizer.php b/src/Maker/MakeSerializerNormalizer.php index 1299017858..b9b5810186 100644 --- a/src/Maker/MakeSerializerNormalizer.php +++ b/src/Maker/MakeSerializerNormalizer.php @@ -13,15 +13,16 @@ use Symfony\Bundle\MakerBundle\ConsoleStyle; use Symfony\Bundle\MakerBundle\DependencyBuilder; +use Symfony\Bundle\MakerBundle\FileManager; use Symfony\Bundle\MakerBundle\Generator; use Symfony\Bundle\MakerBundle\InputConfiguration; use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator; +use Symfony\Bundle\MakerBundle\Util\YamlSourceManipulator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface; -use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; /** @@ -29,6 +30,10 @@ */ final class MakeSerializerNormalizer extends AbstractMaker { + public function __construct(private FileManager $fileManager) + { + } + public static function getCommandName(): string { return 'make:serializer:normalizer'; @@ -49,33 +54,35 @@ public function configureCommand(Command $command, InputConfiguration $inputConf public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void { + $nextSteps = []; + $normalizerClassNameDetails = $generator->createClassNameDetails( $input->getArgument('name'), 'Serializer\\Normalizer\\', \Normalizer::class ); - $useStatements = new UseStatementGenerator([ - NormalizerInterface::class, - ObjectNormalizer::class, - CacheableSupportsMethodInterface::class, - ]); + $this->generateNormalizer($normalizerClassNameDetails->getFullName(), $generator); - $generator->generateClass( - $normalizerClassNameDetails->getFullName(), - 'serializer/Normalizer.tpl.php', - [ - 'use_statements' => $useStatements, - ] - ); + try { + $this->configureNormalizerService($normalizerClassNameDetails->getFullName(), $generator); + } catch (\Throwable) { + $nextSteps[] = "Your services.yaml could not be updated automatically. You'll need to inject the \$objectNormalizer argument to manually."; + } $generator->writeChanges(); $this->writeSuccessMessage($io); - $io->text([ - 'Next: Open your new serializer normalizer class and start customizing it.', + array_push( + $nextSteps, + 'Open your new serializer normalizer class and start customizing it.', 'Find the documentation at https://symfony.com/doc/current/serializer/custom_normalizer.html', + ); + + $io->text([ + 'Next:', + ...array_map(static fn (string $s): string => sprintf(' - %s', $s), $nextSteps), ]); } @@ -86,4 +93,35 @@ public function configureDependencies(DependencyBuilder $dependencies): void 'serializer' ); } + + private function generateNormalizer(string $className, Generator $generator): void + { + $useStatements = new UseStatementGenerator([ + NormalizerInterface::class, + CacheableSupportsMethodInterface::class, + ]); + + $generator->generateClass($className, 'serializer/Normalizer.tpl.php', [ + 'use_statements' => $useStatements, + ]); + } + + private function configureNormalizerService(string $className, Generator $generator): void + { + $servicesFilePath = 'config/services.yaml'; + + $manipulator = new YamlSourceManipulator($this->fileManager->getFileContents($servicesFilePath)); + $servicesData = $manipulator->getData(); + + if (!isset($servicesData['services'][$className])) { + $servicesData['services'][$className] = [ + 'arguments' => [ + '$objectNormalizer' => '@serializer.normalizer.object', + ], + ]; + } + + $manipulator->setData($servicesData); + $generator->dumpFile($servicesFilePath, $manipulator->getContents()); + } } diff --git a/src/Resources/config/makers.xml b/src/Resources/config/makers.xml index e3d785cb0a..74eb0ad19c 100644 --- a/src/Resources/config/makers.xml +++ b/src/Resources/config/makers.xml @@ -94,6 +94,7 @@ + diff --git a/src/Resources/skeleton/serializer/Normalizer.tpl.php b/src/Resources/skeleton/serializer/Normalizer.tpl.php index 4a36382827..af212e4af2 100644 --- a/src/Resources/skeleton/serializer/Normalizer.tpl.php +++ b/src/Resources/skeleton/serializer/Normalizer.tpl.php @@ -6,7 +6,7 @@ class implements NormalizerInterface, CacheableSupportsMethodInterface { - public function __construct(private ObjectNormalizer $normalizer) + public function __construct(private NormalizerInterface $objectNormalizer) { }