1313
1414use Symfony \Bundle \MakerBundle \ConsoleStyle ;
1515use Symfony \Bundle \MakerBundle \DependencyBuilder ;
16+ use Symfony \Bundle \MakerBundle \FileManager ;
1617use Symfony \Bundle \MakerBundle \Generator ;
1718use Symfony \Bundle \MakerBundle \InputConfiguration ;
1819use Symfony \Bundle \MakerBundle \Util \UseStatementGenerator ;
20+ use Symfony \Bundle \MakerBundle \Util \YamlSourceManipulator ;
1921use Symfony \Component \Console \Command \Command ;
2022use Symfony \Component \Console \Input \InputArgument ;
2123use Symfony \Component \Console \Input \InputInterface ;
2224use Symfony \Component \Serializer \Normalizer \CacheableSupportsMethodInterface ;
2325use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
24- use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
2526use Symfony \Component \Serializer \Serializer ;
2627
2728/**
2829 * @author Grégoire Pineau <[email protected] > 2930 */
3031final class MakeSerializerNormalizer extends AbstractMaker
3132{
33+ public function __construct (private FileManager $ fileManager )
34+ {
35+ }
36+
3237 public static function getCommandName (): string
3338 {
3439 return 'make:serializer:normalizer ' ;
@@ -49,33 +54,35 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4954
5055 public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
5156 {
57+ $ nextSteps = [];
58+
5259 $ normalizerClassNameDetails = $ generator ->createClassNameDetails (
5360 $ input ->getArgument ('name ' ),
5461 'Serializer \\Normalizer \\' ,
5562 \Normalizer::class
5663 );
5764
58- $ useStatements = new UseStatementGenerator ([
59- NormalizerInterface::class,
60- ObjectNormalizer::class,
61- CacheableSupportsMethodInterface::class,
62- ]);
65+ $ this ->generateNormalizer ($ normalizerClassNameDetails ->getFullName (), $ generator );
6366
64- $ generator ->generateClass (
65- $ normalizerClassNameDetails ->getFullName (),
66- 'serializer/Normalizer.tpl.php ' ,
67- [
68- 'use_statements ' => $ useStatements ,
69- ]
70- );
67+ try {
68+ $ this ->configureNormalizerService ($ normalizerClassNameDetails ->getFullName (), $ generator );
69+ } catch (\Throwable ) {
70+ $ nextSteps [] = "Your <info>services.yaml</> could not be updated automatically. You'll need to inject the <info> \$objectNormalizer</> argument to manually. " ;
71+ }
7172
7273 $ generator ->writeChanges ();
7374
7475 $ this ->writeSuccessMessage ($ io );
7576
76- $ io ->text ([
77- 'Next: Open your new serializer normalizer class and start customizing it. ' ,
77+ array_push (
78+ $ nextSteps ,
79+ 'Open your new serializer normalizer class and start customizing it. ' ,
7880 'Find the documentation at <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</> ' ,
81+ );
82+
83+ $ io ->text ([
84+ 'Next: ' ,
85+ ...array_map (static fn (string $ s ): string => sprintf (' - %s ' , $ s ), $ nextSteps ),
7986 ]);
8087 }
8188
@@ -86,4 +93,35 @@ public function configureDependencies(DependencyBuilder $dependencies): void
8693 'serializer '
8794 );
8895 }
96+
97+ private function generateNormalizer (string $ className , Generator $ generator ): void
98+ {
99+ $ useStatements = new UseStatementGenerator ([
100+ NormalizerInterface::class,
101+ CacheableSupportsMethodInterface::class,
102+ ]);
103+
104+ $ generator ->generateClass ($ className , 'serializer/Normalizer.tpl.php ' , [
105+ 'use_statements ' => $ useStatements ,
106+ ]);
107+ }
108+
109+ private function configureNormalizerService (string $ className , Generator $ generator ): void
110+ {
111+ $ servicesFilePath = 'config/services.yaml ' ;
112+
113+ $ manipulator = new YamlSourceManipulator ($ this ->fileManager ->getFileContents ($ servicesFilePath ));
114+ $ servicesData = $ manipulator ->getData ();
115+
116+ if (!isset ($ servicesData ['services ' ][$ className ])) {
117+ $ servicesData ['services ' ][$ className ] = [
118+ 'arguments ' => [
119+ '$objectNormalizer ' => '@serializer.normalizer.object ' ,
120+ ],
121+ ];
122+ }
123+
124+ $ manipulator ->setData ($ servicesData );
125+ $ generator ->dumpFile ($ servicesFilePath , $ manipulator ->getContents ());
126+ }
89127}
0 commit comments