Skip to content

[make:serializer:normalizer] Inject a NormalizerInterface instead of an ObjectNormalizer #1273

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 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions src/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private function realPath(string $absolutePath): string
}

$finalPath = implode('/', $finalParts);

// Normalize: // => /
// Normalize: /./ => /
return str_replace(['//', '/./'], '/', $finalPath);
Expand Down
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 <[email protected]>
*/
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