1717use Symfony \Bundle \MakerBundle \InputConfiguration ;
1818use Symfony \Bundle \MakerBundle \Str ;
1919use Symfony \Bundle \MakerBundle \Util \ClassSource \Model \ClassData ;
20+ use Symfony \Bundle \MakerBundle \Validator \TargetEnum ;
2021use Symfony \Component \Console \Command \Command ;
22+ use Symfony \Component \Console \Helper \QuestionHelper ;
2123use Symfony \Component \Console \Input \InputArgument ;
2224use Symfony \Component \Console \Input \InputInterface ;
25+ use Symfony \Component \Console \Input \InputOption ;
26+ use Symfony \Component \Console \Question \ChoiceQuestion ;
2327use Symfony \Component \Validator \Constraint ;
2428use Symfony \Component \Validator \ConstraintValidator ;
2529use Symfony \Component \Validator \Validation ;
@@ -45,10 +49,30 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4549 {
4650 $ command
4751 ->addArgument ('name ' , InputArgument::OPTIONAL , 'The name of the validator class (e.g. <fg=yellow>EnabledValidator</>) ' )
52+ ->addOption ('target ' , null , InputOption::VALUE_REQUIRED , 'The target of the constraint class ' )
4853 ->setHelp ($ this ->getHelpFileContents ('MakeValidator.txt ' ))
4954 ;
5055 }
5156
57+ public function interact (InputInterface $ input , ConsoleStyle $ io , Command $ command ): void
58+ {
59+ $ target = $ input ->getOption ('target ' );
60+ $ supportedTargets = TargetEnum::values ();
61+
62+ if (!$ target || \in_array ($ target , $ supportedTargets )) {
63+ return ;
64+ }
65+
66+ $ helper = new QuestionHelper ();
67+ $ question = new ChoiceQuestion (
68+ 'Target type ' ,
69+ $ supportedTargets
70+ );
71+
72+ $ target = $ helper ->ask ($ input , $ io ->getOutput (), $ question );
73+ $ input ->setOption ('target ' , $ target );
74+ }
75+
5276 /** @return void */
5377 public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator )
5478 {
@@ -77,6 +101,9 @@ class: \sprintf('Validator\\%s', Str::removeSuffix($validatorClassData->getClass
77101 $ generator ->generateClassFromClassData (
78102 $ constraintDataClass ,
79103 'validator/Constraint.tpl.php ' ,
104+ [
105+ 'target ' => $ input ->getOption ('target ' ),
106+ ]
80107 );
81108
82109 $ generator ->writeChanges ();
0 commit comments