13
13
14
14
use Symfony \Bundle \MakerBundle \ConsoleStyle ;
15
15
use Symfony \Bundle \MakerBundle \DependencyBuilder ;
16
+ use Symfony \Bundle \MakerBundle \FileManager ;
16
17
use Symfony \Bundle \MakerBundle \Generator ;
17
18
use Symfony \Bundle \MakerBundle \InputConfiguration ;
18
19
use Symfony \Bundle \MakerBundle \Util \UseStatementGenerator ;
20
+ use Symfony \Bundle \MakerBundle \Util \YamlSourceManipulator ;
19
21
use Symfony \Component \Console \Command \Command ;
20
22
use Symfony \Component \Console \Input \InputArgument ;
21
23
use Symfony \Component \Console \Input \InputInterface ;
22
24
use Symfony \Component \Serializer \Normalizer \CacheableSupportsMethodInterface ;
23
25
use Symfony \Component \Serializer \Normalizer \NormalizerInterface ;
24
- use Symfony \Component \Serializer \Normalizer \ObjectNormalizer ;
25
26
use Symfony \Component \Serializer \Serializer ;
26
27
27
28
/**
28
29
* @author Grégoire Pineau <lyrixx@lyrixx.info>
29
30
*/
30
31
final class MakeSerializerNormalizer extends AbstractMaker
31
32
{
33
+ public function __construct (private FileManager $ fileManager )
34
+ {
35
+ }
36
+
32
37
public static function getCommandName (): string
33
38
{
34
39
return 'make:serializer:normalizer ' ;
@@ -49,33 +54,35 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
49
54
50
55
public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
51
56
{
57
+ $ nextSteps = [];
58
+
52
59
$ normalizerClassNameDetails = $ generator ->createClassNameDetails (
53
60
$ input ->getArgument ('name ' ),
54
61
'Serializer \\Normalizer \\' ,
55
62
\Normalizer::class
56
63
);
57
64
58
- $ useStatements = new UseStatementGenerator ([
59
- NormalizerInterface::class,
60
- ObjectNormalizer::class,
61
- CacheableSupportsMethodInterface::class,
62
- ]);
65
+ $ this ->generateNormalizer ($ normalizerClassNameDetails ->getFullName (), $ generator );
63
66
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
+ }
71
72
72
73
$ generator ->writeChanges ();
73
74
74
75
$ this ->writeSuccessMessage ($ io );
75
76
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. ' ,
78
80
'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 ),
79
86
]);
80
87
}
81
88
@@ -86,4 +93,35 @@ public function configureDependencies(DependencyBuilder $dependencies): void
86
93
'serializer '
87
94
);
88
95
}
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
+ }
89
127
}
0 commit comments