@@ -59,11 +59,11 @@ final class MakeFormLogin extends AbstractMaker
59
59
60
60
private const SECURITY_CONFIG_PATH = 'config/packages/security.yaml ' ;
61
61
private YamlSourceManipulator $ ysm ;
62
+ private string $ controllerName ;
62
63
private string $ firewallToUpdate ;
63
64
private string $ userClass ;
64
65
private string $ userNameField ;
65
- /** @var ?array<string, mixed> */
66
- private ?array $ securityData = null ;
66
+ private bool $ willLogout ;
67
67
68
68
public function __construct (
69
69
private FileManager $ fileManager ,
@@ -80,7 +80,7 @@ public static function getCommandName(): string
80
80
public function configureCommand (Command $ command , InputConfiguration $ inputConfig ): void
81
81
{
82
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? ' )
83
+ ->addOption ('will-logout ' , null , InputOption::VALUE_NEGATABLE , 'Will generate a \'/logout \' URL? ' )
84
84
->setHelp ($ this ->getHelpFileContents ('security/MakeFormLogin.txt ' ));
85
85
86
86
$ this ->configureCommandWithTestsOption ($ command );
@@ -116,44 +116,38 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
116
116
throw new RuntimeCommandException (\sprintf ('The file "%s" does not exist. PHP & XML configuration formats are currently not supported. ' , self ::SECURITY_CONFIG_PATH ));
117
117
}
118
118
119
- $ securityData = $ this ->getSecurityData ();
119
+ $ this ->ysm = new YamlSourceManipulator ($ this ->fileManager ->getFileContents (self ::SECURITY_CONFIG_PATH ));
120
+ $ securityData = $ this ->ysm ->getData ();
120
121
121
122
if (!isset ($ securityData ['security ' ]['providers ' ]) || !$ securityData ['security ' ]['providers ' ]) {
122
123
throw new RuntimeCommandException ('To generate a form login authentication, you must configure at least one entry under "providers" in "security.yaml". ' );
123
124
}
124
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
+ $ this ->controllerName = $ input ->getArgument ('controllerName ' ) ?? $ io ->ask (
127
+ 'Choose a name for the controller class (e.g. <fg=yellow>SecurityController</>) ' ,
128
+ 'SecurityController ' ,
129
+ Validator::validateClassName (...)
130
+ );
133
131
134
- if (false === $ input ->getOption ('will-logout ' )) {
135
- $ input ->setOption ('will-logout ' , $ io ->confirm ('Do you want to generate a \'/logout \' URL? ' ));
136
- }
132
+ $ securityHelper = new InteractiveSecurityHelper ();
133
+ $ this ->firewallToUpdate = $ securityHelper ->guessFirewallName ($ io , $ securityData );
134
+ $ this ->userClass = $ securityHelper ->guessUserClass ($ io , $ securityData ['security ' ]['providers ' ]);
135
+ $ this ->userNameField = $ securityHelper ->guessUserNameField ($ io , $ this ->userClass , $ securityData ['security ' ]['providers ' ]);
136
+ $ this ->willLogout = $ input ->getOption ('will-logout ' ) ?? $ io ->confirm ('Do you want to generate a \'/logout \' URL? ' );
137
137
138
138
$ this ->interactSetGenerateTests ($ input , $ io );
139
139
}
140
140
141
141
public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
142
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
-
149
143
$ useStatements = new UseStatementGenerator ([
150
144
AbstractController::class,
151
145
Response::class,
152
146
Route::class,
153
147
AuthenticationUtils::class,
154
148
]);
155
149
156
- $ controllerNameDetails = $ generator ->createClassNameDetails ($ input -> getArgument ( ' controllerName ' ) , 'Controller \\' , 'Controller ' );
150
+ $ controllerNameDetails = $ generator ->createClassNameDetails ($ this -> controllerName , 'Controller \\' , 'Controller ' );
157
151
$ templatePath = strtolower ($ controllerNameDetails ->getRelativeNameWithoutSuffix ());
158
152
159
153
$ controllerPath = $ generator ->generateController (
@@ -166,7 +160,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
166
160
]
167
161
);
168
162
169
- if ($ input -> getOption ( ' will-logout ' ) ) {
163
+ if ($ this -> willLogout ) {
170
164
$ manipulator = new ClassSourceManipulator ($ generator ->getFileContentsForPendingOperation ($ controllerPath ));
171
165
172
166
$ this ->securityControllerBuilder ->addLogoutMethod ($ manipulator );
@@ -178,19 +172,19 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
178
172
\sprintf ('%s/login.html.twig ' , $ templatePath ),
179
173
'security/formLogin/login_form.tpl.php ' ,
180
174
[
181
- 'logout_setup ' => $ input -> getOption ( ' will-logout ' ) ,
175
+ 'logout_setup ' => $ this -> willLogout ,
182
176
'username_label ' => Str::asHumanWords ($ this ->userNameField ),
183
177
'username_is_email ' => false !== stripos ($ this ->userNameField , 'email ' ),
184
178
]
185
179
);
186
180
187
181
$ securityData = $ this ->securityConfigUpdater ->updateForFormLogin ($ this ->ysm ->getContents (), $ this ->firewallToUpdate , 'app_login ' , 'app_login ' );
188
182
189
- if ($ input -> getOption ( ' will-logout ' ) ) {
183
+ if ($ this -> willLogout ) {
190
184
$ securityData = $ this ->securityConfigUpdater ->updateForLogout ($ securityData , $ this ->firewallToUpdate );
191
185
}
192
186
193
- if ($ input -> getOption ( ' with-tests ' )) {
187
+ if ($ this -> shouldGenerateTests ( )) {
194
188
$ userClassNameDetails = $ generator ->createClassNameDetails (
195
189
'\\' .$ this ->userClass ,
196
190
'Entity \\'
@@ -234,17 +228,4 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
234
228
\sprintf ('Next: Review and adapt the login template: <info>%s/login.html.twig</info> to suit your needs. ' , $ templatePath ),
235
229
]);
236
230
}
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
- }
250
231
}
0 commit comments