3535use Symfony \Bundle \SecurityBundle \SecurityBundle ;
3636use Symfony \Bundle \TwigBundle \TwigBundle ;
3737use Symfony \Component \Console \Command \Command ;
38+ use Symfony \Component \Console \Input \InputArgument ;
3839use Symfony \Component \Console \Input \InputInterface ;
40+ use Symfony \Component \Console \Input \InputOption ;
3941use Symfony \Component \HttpFoundation \Response ;
4042use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
4143use Symfony \Component \Routing \Attribute \Route ;
@@ -57,11 +59,11 @@ final class MakeFormLogin extends AbstractMaker
5759
5860 private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml ' ;
5961 private YamlSourceManipulator $ ysm ;
60- private string $ controllerName ;
6162 private string $ firewallToUpdate ;
6263 private string $ userClass ;
6364 private string $ userNameField ;
64- private bool $ willLogout ;
65+ /** @var ?array<string, mixed> */
66+ private ?array $ securityData = null ;
6567
6668 public function __construct (
6769 private FileManager $ fileManager ,
@@ -77,9 +79,12 @@ public static function getCommandName(): string
7779
7880 public function configureCommand (Command $ command , InputConfiguration $ inputConfig ): void
7981 {
80- $ command ->setHelp (file_get_contents (\dirname (__DIR__ , 2 ).'/Resources/help/security/MakeFormLogin.txt ' ));
82+ $ command ->addArgument ('controllerName ' , InputArgument::OPTIONAL , 'The class name of the Controller (e.g. <fg=yellow>SecurityController</>) ' )
83+ ->addOption ('will-logout ' , null , InputOption::VALUE_NONE , 'Will generate a \'/logout \' URL? ' )
84+ ->setHelp (file_get_contents (\dirname (__DIR__ , 2 ).'/Resources/help/security/MakeFormLogin.txt ' ));
8185
8286 $ this ->configureCommandWithTestsOption ($ command );
87+ $ inputConfig ->setArgumentAsNonInteractive ('controllerName ' );
8388 }
8489
8590 public static function getCommandDescription (): string
@@ -111,38 +116,44 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
111116 throw new RuntimeCommandException (\sprintf ('The file "%s" does not exist. PHP & XML configuration formats are currently not supported. ' , self ::SECURITY_CONFIG_PATH ));
112117 }
113118
114- $ this ->ysm = new YamlSourceManipulator ($ this ->fileManager ->getFileContents (self ::SECURITY_CONFIG_PATH ));
115- $ securityData = $ this ->ysm ->getData ();
119+ $ securityData = $ this ->getSecurityData ();
116120
117121 if (!isset ($ securityData ['security ' ]['providers ' ]) || !$ securityData ['security ' ]['providers ' ]) {
118122 throw new RuntimeCommandException ('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml". ' );
119123 }
120124
121- $ this ->controllerName = $ io ->ask (
122- 'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>) ' ,
123- 'SecurityController ' ,
124- Validator::validateClassName (...)
125- );
125+ if (null === $ input ->getArgument ('controllerName ' )) {
126+ $ input ->setArgument (
127+ 'controllerName ' , $ io ->ask (
128+ 'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>) ' ,
129+ 'SecurityController ' ,
130+ Validator::validateClassName (...)
131+ ));
132+ }
126133
127- $ securityHelper = new InteractiveSecurityHelper ();
128- $ this ->firewallToUpdate = $ securityHelper ->guessFirewallName ($ io , $ securityData );
129- $ this ->userClass = $ securityHelper ->guessUserClass ($ io , $ securityData ['security ' ]['providers ' ]);
130- $ this ->userNameField = $ securityHelper ->guessUserNameField ($ io , $ this ->userClass , $ securityData ['security ' ]['providers ' ]);
131- $ this ->willLogout = $ io ->confirm ('Do you want to generate a \'/logout \' URL? ' );
134+ if (false === $ input ->getOption ('will-logout ' )) {
135+ $ input ->setOption ('will-logout ' , $ io ->confirm ('Do you want to generate a \'/logout \' URL? ' ));
136+ }
132137
133138 $ this ->interactSetGenerateTests ($ input , $ io );
134139 }
135140
136141 public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
137142 {
143+ $ securityData = $ this ->getSecurityData ();
144+ $ securityHelper = new InteractiveSecurityHelper ();
145+ $ this ->firewallToUpdate = $ securityHelper ->guessFirewallName ($ io , $ securityData );
146+ $ this ->userClass = $ securityHelper ->guessUserClass ($ io , $ securityData ['security ' ]['providers ' ]);
147+ $ this ->userNameField = $ securityHelper ->guessUserNameField ($ io , $ this ->userClass , $ securityData ['security ' ]['providers ' ]);
148+
138149 $ useStatements = new UseStatementGenerator ([
139150 AbstractController::class,
140151 Response::class,
141152 Route::class,
142153 AuthenticationUtils::class,
143154 ]);
144155
145- $ controllerNameDetails = $ generator ->createClassNameDetails ($ this -> controllerName , 'Controller \\' , 'Controller ' );
156+ $ controllerNameDetails = $ generator ->createClassNameDetails ($ input -> getArgument ( ' controllerName ' ) , 'Controller \\' , 'Controller ' );
146157 $ templatePath = strtolower ($ controllerNameDetails ->getRelativeNameWithoutSuffix ());
147158
148159 $ controllerPath = $ generator ->generateController (
@@ -155,7 +166,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
155166 ]
156167 );
157168
158- if ($ this -> willLogout ) {
169+ if ($ input -> getOption ( ' will-logout ' ) ) {
159170 $ manipulator = new ClassSourceManipulator ($ generator ->getFileContentsForPendingOperation ($ controllerPath ));
160171
161172 $ this ->securityControllerBuilder ->addLogoutMethod ($ manipulator );
@@ -167,26 +178,26 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
167178 \sprintf ('%s/login.html.twig ' , $ templatePath ),
168179 'security/formLogin/login_form.tpl.php ' ,
169180 [
170- 'logout_setup ' => $ this -> willLogout ,
181+ 'logout_setup ' => $ input -> getOption ( ' will-logout ' ) ,
171182 'username_label ' => Str::asHumanWords ($ this ->userNameField ),
172183 'username_is_email ' => false !== stripos ($ this ->userNameField , 'email ' ),
173184 ]
174185 );
175186
176187 $ securityData = $ this ->securityConfigUpdater ->updateForFormLogin ($ this ->ysm ->getContents (), $ this ->firewallToUpdate , 'app_login ' , 'app_login ' );
177188
178- if ($ this -> willLogout ) {
189+ if ($ input -> getOption ( ' will-logout ' ) ) {
179190 $ securityData = $ this ->securityConfigUpdater ->updateForLogout ($ securityData , $ this ->firewallToUpdate );
180191 }
181192
182- if ($ this -> shouldGenerateTests ( )) {
193+ if ($ input -> getOption ( ' with-tests ' )) {
183194 $ userClassNameDetails = $ generator ->createClassNameDetails (
184195 '\\' .$ this ->userClass ,
185196 'Entity \\'
186197 );
187198
188199 $ testClassDetails = $ generator ->createClassNameDetails (
189- ' LoginControllerTest ' ,
200+ $ controllerNameDetails -> getShortName (). ' Test ' ,
190201 'Test \\' ,
191202 );
192203
@@ -223,4 +234,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
223234 \sprintf ('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs. ' , $ templatePath ),
224235 ]);
225236 }
237+
238+ /**
239+ * @return array<string, mixed> $items
240+ */
241+ private function getSecurityData (): array
242+ {
243+ if (null === $ this ->securityData ) {
244+ $ this ->ysm = new YamlSourceManipulator ($ this ->fileManager ->getFileContents (self ::SECURITY_CONFIG_PATH ));
245+ $ this ->securityData = $ this ->ysm ->getData ();
246+ }
247+
248+ return $ this ->securityData ;
249+ }
226250}
0 commit comments