Skip to content

Commit 77dad0f

Browse files
committed
feature #298 [Serializer] Add a 'make:serializer:normalizer' command (lyrixx, weaverryan)
This PR was merged into the 1.0-dev branch. Discussion ---------- [Serializer] Add a 'make:serializer:normalizer' command fixes #287 Commits ------- 3b46687 Updating docs 605db5d [Serializer] Add a 'make:serializer:normalizer' command
2 parents 7d95b8d + 3b46687 commit 77dad0f

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\MakerBundle\Maker;
13+
14+
use Symfony\Bundle\MakerBundle\ConsoleStyle;
15+
use Symfony\Bundle\MakerBundle\DependencyBuilder;
16+
use Symfony\Bundle\MakerBundle\Generator;
17+
use Symfony\Bundle\MakerBundle\InputConfiguration;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Input\InputArgument;
20+
use Symfony\Component\Console\Input\InputInterface;
21+
use Symfony\Component\Serializer\Serializer;
22+
23+
/**
24+
* @author Grégoire Pineau <[email protected]>
25+
*/
26+
final class MakeSerializerNormalizer extends AbstractMaker
27+
{
28+
public static function getCommandName(): string
29+
{
30+
return 'make:serializer:normalizer';
31+
}
32+
33+
public function configureCommand(Command $command, InputConfiguration $inputConf)
34+
{
35+
$command
36+
->setDescription('Creates a new serializer normalizer class')
37+
->addArgument('name', InputArgument::OPTIONAL, 'Choose a class name for your normalizer (e.g. <fg=yellow>UserNormalizer</>)')
38+
->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeSerializerNormalizer.txt'))
39+
;
40+
}
41+
42+
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
43+
{
44+
$normalizerClassNameDetails = $generator->createClassNameDetails(
45+
$input->getArgument('name'),
46+
'Serializer\\Normalizer\\',
47+
'Normalizer'
48+
);
49+
50+
$generator->generateClass(
51+
$normalizerClassNameDetails->getFullName(),
52+
'serializer/Normalizer.tpl.php'
53+
);
54+
55+
$generator->writeChanges();
56+
57+
$this->writeSuccessMessage($io);
58+
59+
$io->text([
60+
'Next: Open your new serializer normalizer class and start customizing it.',
61+
'Find the documentation at <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</>',
62+
]);
63+
}
64+
65+
public function configureDependencies(DependencyBuilder $dependencies)
66+
{
67+
$dependencies->addClassDependency(
68+
Serializer::class,
69+
'serializer'
70+
);
71+
}
72+
}

src/Resources/config/makers.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
<tag name="maker.command" />
5555
</service>
5656

57+
<service id="maker.maker.make_serializer_normalizer" class="Symfony\Bundle\MakerBundle\Maker\MakeSerializerNormalizer">
58+
<tag name="maker.command" />
59+
</service>
60+
5761
<service id="maker.maker.make_subscriber" class="Symfony\Bundle\MakerBundle\Maker\MakeSubscriber">
5862
<tag name="maker.command" />
5963
<argument type="service" id="maker.event_registry" />
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The <info>%command.name%</info> command generates a new serializer normalizer class.
2+
3+
<info>php %command.full_name% UserNormalizer</info>
4+
5+
If the argument is missing, the command will ask for the class name interactively.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $namespace; ?>;
4+
5+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
6+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
7+
8+
class <?= $class_name ?> implements NormalizerInterface
9+
{
10+
private $normalizer;
11+
12+
public function __construct(ObjectNormalizer $normalizer)
13+
{
14+
$this->normalizer = $normalizer;
15+
}
16+
17+
public function normalize($object, $format = null, array $context = array()): array
18+
{
19+
$data = $this->normalizer->normalize($object, $format, $context);
20+
21+
// Here: add, edit, or delete some data
22+
23+
return $data;
24+
}
25+
26+
public function supportsNormalization($data, $format = null): bool
27+
{
28+
return $data instanceof \App\Entity\BlogPost;
29+
}
30+
}

0 commit comments

Comments
 (0)