35
35
use Symfony \Bundle \SecurityBundle \SecurityBundle ;
36
36
use Symfony \Bundle \TwigBundle \TwigBundle ;
37
37
use Symfony \Component \Console \Command \Command ;
38
+ use Symfony \Component \Console \Input \InputArgument ;
38
39
use Symfony \Component \Console \Input \InputInterface ;
40
+ use Symfony \Component \Console \Input \InputOption ;
39
41
use Symfony \Component \HttpFoundation \Response ;
40
42
use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
41
43
use Symfony \Component \Routing \Attribute \Route ;
@@ -57,11 +59,11 @@ final class MakeFormLogin extends AbstractMaker
57
59
58
60
private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml ' ;
59
61
private YamlSourceManipulator $ ysm ;
60
- private string $ controllerName ;
61
62
private string $ firewallToUpdate ;
62
63
private string $ userClass ;
63
64
private string $ userNameField ;
64
- private bool $ willLogout ;
65
+ /** @var ?array<string, mixed> */
66
+ private ?array $ securityData = null ;
65
67
66
68
public function __construct (
67
69
private FileManager $ fileManager ,
@@ -77,9 +79,12 @@ public static function getCommandName(): string
77
79
78
80
public function configureCommand (Command $ command , InputConfiguration $ inputConfig ): void
79
81
{
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 ' ));
81
85
82
86
$ this ->configureCommandWithTestsOption ($ command );
87
+ $ inputConfig ->setArgumentAsNonInteractive ('controllerName ' );
83
88
}
84
89
85
90
public static function getCommandDescription (): string
@@ -111,38 +116,44 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
111
116
throw new RuntimeCommandException (\sprintf ('The file "%s" does not exist. PHP & XML configuration formats are currently not supported. ' , self ::SECURITY_CONFIG_PATH ));
112
117
}
113
118
114
- $ this ->ysm = new YamlSourceManipulator ($ this ->fileManager ->getFileContents (self ::SECURITY_CONFIG_PATH ));
115
- $ securityData = $ this ->ysm ->getData ();
119
+ $ securityData = $ this ->getSecurityData ();
116
120
117
121
if (!isset ($ securityData ['security ' ]['providers ' ]) || !$ securityData ['security ' ]['providers ' ]) {
118
122
throw new RuntimeCommandException ('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml". ' );
119
123
}
120
124
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
+ }
126
133
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
+ }
132
137
133
138
$ this ->interactSetGenerateTests ($ input , $ io );
134
139
}
135
140
136
141
public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
137
142
{
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
+
138
149
$ useStatements = new UseStatementGenerator ([
139
150
AbstractController::class,
140
151
Response::class,
141
152
Route::class,
142
153
AuthenticationUtils::class,
143
154
]);
144
155
145
- $ controllerNameDetails = $ generator ->createClassNameDetails ($ this -> controllerName , 'Controller \\' , 'Controller ' );
156
+ $ controllerNameDetails = $ generator ->createClassNameDetails ($ input -> getArgument ( ' controllerName ' ) , 'Controller \\' , 'Controller ' );
146
157
$ templatePath = strtolower ($ controllerNameDetails ->getRelativeNameWithoutSuffix ());
147
158
148
159
$ controllerPath = $ generator ->generateController (
@@ -155,7 +166,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
155
166
]
156
167
);
157
168
158
- if ($ this -> willLogout ) {
169
+ if ($ input -> getOption ( ' will-logout ' ) ) {
159
170
$ manipulator = new ClassSourceManipulator ($ generator ->getFileContentsForPendingOperation ($ controllerPath ));
160
171
161
172
$ this ->securityControllerBuilder ->addLogoutMethod ($ manipulator );
@@ -167,26 +178,26 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
167
178
\sprintf ('%s/login.html.twig ' , $ templatePath ),
168
179
'security/formLogin/login_form.tpl.php ' ,
169
180
[
170
- 'logout_setup ' => $ this -> willLogout ,
181
+ 'logout_setup ' => $ input -> getOption ( ' will-logout ' ) ,
171
182
'username_label ' => Str::asHumanWords ($ this ->userNameField ),
172
183
'username_is_email ' => false !== stripos ($ this ->userNameField , 'email ' ),
173
184
]
174
185
);
175
186
176
187
$ securityData = $ this ->securityConfigUpdater ->updateForFormLogin ($ this ->ysm ->getContents (), $ this ->firewallToUpdate , 'app_login ' , 'app_login ' );
177
188
178
- if ($ this -> willLogout ) {
189
+ if ($ input -> getOption ( ' will-logout ' ) ) {
179
190
$ securityData = $ this ->securityConfigUpdater ->updateForLogout ($ securityData , $ this ->firewallToUpdate );
180
191
}
181
192
182
- if ($ this -> shouldGenerateTests ( )) {
193
+ if ($ input -> getOption ( ' with-tests ' )) {
183
194
$ userClassNameDetails = $ generator ->createClassNameDetails (
184
195
'\\' .$ this ->userClass ,
185
196
'Entity \\'
186
197
);
187
198
188
199
$ testClassDetails = $ generator ->createClassNameDetails (
189
- ' LoginControllerTest ' ,
200
+ $ controllerNameDetails -> getShortName (). ' Test ' ,
190
201
'Test \\' ,
191
202
);
192
203
@@ -223,4 +234,17 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
223
234
\sprintf ('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs. ' , $ templatePath ),
224
235
]);
225
236
}
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
+ }
226
250
}
0 commit comments