Skip to content

Commit

Permalink
use NormalizerInterface instead of ObjectNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Aug 22, 2023
1 parent 79c710b commit f9ebf6d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 16 deletions.
68 changes: 53 additions & 15 deletions src/Maker/MakeSerializerNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@

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;

/**
* @author Grégoire Pineau <lyrixx@lyrixx.info>
*/
final class MakeSerializerNormalizer extends AbstractMaker
{
public function __construct(private FileManager $fileManager)
{
}

public static function getCommandName(): string
{
return 'make:serializer:normalizer';
Expand All @@ -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 <info>services.yaml</> could not be updated automatically. You'll need to inject the <info>\$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 <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</>',
);

$io->text([
'Next:',
...array_map(static fn (string $s): string => sprintf(' - %s', $s), $nextSteps),
]);
}

Expand All @@ -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());
}
}
1 change: 1 addition & 0 deletions src/Resources/config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
</service>

<service id="maker.maker.make_serializer_normalizer" class="Symfony\Bundle\MakerBundle\Maker\MakeSerializerNormalizer">
<argument type="service" id="maker.file_manager" />
<tag name="maker.command" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/skeleton/serializer/Normalizer.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class <?= $class_name ?> implements NormalizerInterface, CacheableSupportsMethodInterface
{
public function __construct(private ObjectNormalizer $normalizer)
public function __construct(private NormalizerInterface $objectNormalizer)
{
}

Expand Down

0 comments on commit f9ebf6d

Please sign in to comment.