1717use Symfony \Bundle \MakerBundle \Generator ;
1818use Symfony \Bundle \MakerBundle \InputConfiguration ;
1919use Symfony \Bundle \MakerBundle \Util \UseStatementGenerator ;
20- use Symfony \Bundle \MakerBundle \Util \YamlSourceManipulator ;
2120use Symfony \Component \Console \Command \Command ;
2221use Symfony \Component \Console \Input \InputArgument ;
2322use Symfony \Component \Console \Input \InputInterface ;
24- use Symfony \Component \Serializer \ Normalizer \ CacheableSupportsMethodInterface ;
23+ use Symfony \Component \DependencyInjection \ Attribute \ Autowire ;
2524use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
2625use Symfony \Component \Serializer \Serializer ;
2726
3029 */
3130final class MakeSerializerNormalizer extends AbstractMaker
3231{
33- public function __construct (private FileManager $ fileManager )
32+ public function __construct (private ? FileManager $ fileManager = null )
3433 {
34+ if (null !== $ this ->fileManager ) {
35+ @trigger_deprecation (
36+ 'symfony/maker-bundle ' ,
37+ '1.56.0 ' ,
38+ sprintf ('Initializing MakeSerializerNormalizer while providing an instance of "%s" is deprecated. The $fileManager param will be removed in a future version. ' , FileManager::class)
39+ );
40+ }
3541 }
3642
3743 public static function getCommandName (): string
@@ -54,35 +60,41 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
5460
5561 public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
5662 {
57- $ nextSteps = [];
58-
5963 $ normalizerClassNameDetails = $ generator ->createClassNameDetails (
6064 $ input ->getArgument ('name ' ),
6165 'Serializer \\Normalizer \\' ,
6266 \Normalizer::class
6367 );
6468
65- $ this ->generateNormalizer ($ normalizerClassNameDetails ->getFullName (), $ generator );
69+ $ useStatements = new UseStatementGenerator ([
70+ NormalizerInterface::class,
71+ Autowire::class,
72+ sprintf ('App\Entity\%s ' , str_replace ('Normalizer ' , '' , $ normalizerClassNameDetails ->getShortName ())),
73+ ]);
74+
75+ $ entityDetails = $ generator ->createClassNameDetails (
76+ str_replace ('Normalizer ' , '' , $ normalizerClassNameDetails ->getShortName ()),
77+ 'Entity \\' ,
78+ );
6679
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. " ;
80+ if ($ entityExists = class_exists ($ entityDetails ->getFullName ())) {
81+ $ useStatements ->addUseStatement ($ entityDetails ->getFullName ());
7182 }
7283
84+ $ generator ->generateClass ($ normalizerClassNameDetails ->getFullName (), 'serializer/Normalizer.tpl.php ' , [
85+ 'use_statements ' => $ useStatements ,
86+ 'entity_exists ' => $ entityExists ,
87+ 'entity_name ' => $ entityDetails ->getShortName (),
88+ ]);
89+
7390 $ generator ->writeChanges ();
7491
7592 $ this ->writeSuccessMessage ($ io );
7693
77- array_push (
78- $ nextSteps ,
79- 'Open your new serializer normalizer class and start customizing it. ' ,
80- 'Find the documentation at <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</> ' ,
81- );
82-
8394 $ io ->text ([
8495 'Next: ' ,
85- ...array_map (static fn (string $ s ): string => sprintf (' - %s ' , $ s ), $ nextSteps ),
96+ ' - Open your new serializer normalizer class and start customizing it. ' ,
97+ ' - Find the documentation at <fg=yellow>https://symfony.com/doc/current/serializer/custom_normalizer.html</> ' ,
8698 ]);
8799 }
88100
@@ -93,35 +105,4 @@ public function configureDependencies(DependencyBuilder $dependencies): void
93105 'serializer '
94106 );
95107 }
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- }
127108}
0 commit comments