29
29
use Symfony \Bundle \MakerBundle \Util \YamlSourceManipulator ;
30
30
use Symfony \Bundle \MakerBundle \Validator ;
31
31
use Symfony \Component \Console \Command \Command ;
32
- use Symfony \Component \Console \Input \InputArgument ;
33
32
use Symfony \Component \Console \Input \InputInterface ;
34
33
use Symfony \Component \Mailer \MailerInterface ;
35
34
use Symfony \Component \Yaml \Yaml ;
@@ -56,6 +55,14 @@ class MakeResetPassword extends AbstractMaker
56
55
private $ doctrineHelper ;
57
56
private $ entityClassGenerator ;
58
57
58
+ private $ fromEmailAddress ;
59
+ private $ fromEmailName ;
60
+ private $ controllerResetSuccessRedirect ;
61
+ private $ userClass ;
62
+ private $ emailPropertyName ;
63
+ private $ emailGetterMethodName ;
64
+ private $ passwordSetterMethodName ;
65
+
59
66
public function __construct (FileManager $ fileManager , DoctrineHelper $ doctrineHelper , EntityClassGenerator $ entityClassGenerator )
60
67
{
61
68
$ this ->fileManager = $ fileManager ;
@@ -73,14 +80,14 @@ public static function getCommandDescription(): string
73
80
return 'Create controller, entity, and repositories for use with symfonycasts/reset-password-bundle ' ;
74
81
}
75
82
76
- public function configureCommand (Command $ command , InputConfiguration $ inputConfig )
83
+ public function configureCommand (Command $ command , InputConfiguration $ inputConfig ): void
77
84
{
78
85
$ command
79
86
->setHelp (file_get_contents (__DIR__ .'/../Resources/help/MakeResetPassword.txt ' ))
80
87
;
81
88
}
82
89
83
- public function configureDependencies (DependencyBuilder $ dependencies )
90
+ public function configureDependencies (DependencyBuilder $ dependencies ): void
84
91
{
85
92
$ dependencies ->addClassDependency (SymfonyCastsResetPasswordBundle::class, 'symfonycasts/reset-password-bundle ' );
86
93
$ dependencies ->addClassDependency (MailerInterface::class, 'symfony/mailer ' );
@@ -98,21 +105,10 @@ public function configureDependencies(DependencyBuilder $dependencies)
98
105
}
99
106
}
100
107
101
- public function interact (InputInterface $ input , ConsoleStyle $ io , Command $ command )
108
+ public function interact (InputInterface $ input , ConsoleStyle $ io , Command $ command ): void
102
109
{
103
110
$ io ->title ('Let \'s make a password reset feature! ' );
104
111
105
- // initialize arguments & commands that are internal (i.e. meant only to be asked)
106
- $ command
107
- ->addArgument ('from-email-address ' , InputArgument::REQUIRED )
108
- ->addArgument ('from-email-name ' , InputArgument::REQUIRED )
109
- ->addArgument ('controller-reset-success-redirect ' , InputArgument::REQUIRED )
110
- ->addArgument ('user-class ' )
111
- ->addArgument ('email-property-name ' )
112
- ->addArgument ('email-getter ' )
113
- ->addArgument ('password-setter ' )
114
- ;
115
-
116
112
$ interactiveSecurityHelper = new InteractiveSecurityHelper ();
117
113
118
114
if (!$ this ->fileManager ->fileExists ($ path = 'config/packages/security.yaml ' )) {
@@ -123,62 +119,48 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
123
119
$ securityData = $ manipulator ->getData ();
124
120
$ providersData = $ securityData ['security ' ]['providers ' ] ?? [];
125
121
126
- $ input ->setArgument (
127
- 'user-class ' ,
128
- $ userClass = $ interactiveSecurityHelper ->guessUserClass (
129
- $ io ,
130
- $ providersData ,
131
- 'What is the User entity that should be used with the "forgotten password" feature? (e.g. <fg=yellow>App \\Entity \\User</>) '
132
- )
122
+ $ this ->userClass = $ interactiveSecurityHelper ->guessUserClass (
123
+ $ io ,
124
+ $ providersData ,
125
+ 'What is the User entity that should be used with the "forgotten password" feature? (e.g. <fg=yellow>App \\Entity \\User</>) '
133
126
);
134
127
135
- $ input ->setArgument (
136
- 'email-property-name ' ,
137
- $ interactiveSecurityHelper ->guessEmailField ($ io , $ userClass )
138
- );
139
- $ input ->setArgument (
140
- 'email-getter ' ,
141
- $ interactiveSecurityHelper ->guessEmailGetter ($ io , $ userClass , $ input ->getArgument ('email-property-name ' ))
142
- );
143
- $ input ->setArgument (
144
- 'password-setter ' ,
145
- $ interactiveSecurityHelper ->guessPasswordSetter ($ io , $ userClass )
146
- );
128
+ $ this ->emailPropertyName = $ interactiveSecurityHelper ->guessEmailField ($ io , $ this ->userClass );
129
+ $ this ->emailGetterMethodName = $ interactiveSecurityHelper ->guessEmailGetter ($ io , $ this ->userClass , $ this ->emailPropertyName );
130
+ $ this ->passwordSetterMethodName = $ interactiveSecurityHelper ->guessPasswordSetter ($ io , $ this ->userClass );
147
131
148
- $ io ->text (sprintf ('Implementing reset password for <info>%s</info> ' , $ userClass ));
132
+ $ io ->text (sprintf ('Implementing reset password for <info>%s</info> ' , $ this -> userClass ));
149
133
150
134
$ io ->section ('- ResetPasswordController - ' );
151
135
$ io ->text ('A named route is used for redirecting after a successful reset. Even a route that does not exist yet can be used here. ' );
152
- $ input ->setArgument ('controller-reset-success-redirect ' , $ io ->ask (
136
+
137
+ $ this ->controllerResetSuccessRedirect = $ io ->ask (
153
138
'What route should users be redirected to after their password has been successfully reset? ' ,
154
139
'app_home ' ,
155
140
[Validator::class, 'notBlank ' ]
156
- )
157
141
);
158
142
159
143
$ io ->section ('- Email - ' );
160
144
$ emailText [] = 'These are used to generate the email code. Don \'t worry, you can change them in the code later! ' ;
161
145
$ io ->text ($ emailText );
162
146
163
- $ input -> setArgument ( ' from-email-address ' , $ io ->ask (
147
+ $ this -> fromEmailAddress = $ io ->ask (
164
148
'What email address will be used to send reset confirmations? e.g. [email protected] ' ,
165
149
null ,
166
150
[Validator::class, 'validateEmailAddress ' ]
167
- )) ;
151
+ );
168
152
169
- $ input -> setArgument ( ' from-email-name ' , $ io ->ask (
153
+ $ this -> fromEmailName = $ io ->ask (
170
154
'What "name" should be associated with that email address? e.g. "Acme Mail Bot" ' ,
171
155
null ,
172
156
[Validator::class, 'notBlank ' ]
173
- )
174
157
);
175
158
}
176
159
177
- public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator )
160
+ public function generate (InputInterface $ input , ConsoleStyle $ io , Generator $ generator ): void
178
161
{
179
- $ userClass = $ input ->getArgument ('user-class ' );
180
162
$ userClassNameDetails = $ generator ->createClassNameDetails (
181
- '\\' .$ userClass ,
163
+ '\\' .$ this -> userClass ,
182
164
'Entity \\'
183
165
);
184
166
@@ -217,24 +199,24 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
217
199
'request_form_type_class_name ' => $ requestFormTypeClassNameDetails ->getShortName (),
218
200
'reset_form_type_full_class_name ' => $ changePasswordFormTypeClassNameDetails ->getFullName (),
219
201
'reset_form_type_class_name ' => $ changePasswordFormTypeClassNameDetails ->getShortName (),
220
- 'password_setter ' => $ input -> getArgument ( ' password-setter ' ) ,
221
- 'success_redirect_route ' => $ input -> getArgument ( ' controller-reset-success-redirect ' ) ,
222
- 'from_email ' => $ input -> getArgument ( ' from-email-address ' ) ,
223
- 'from_email_name ' => $ input -> getArgument ( ' from-email-name ' ) ,
224
- 'email_getter ' => $ input -> getArgument ( ' email-getter ' ) ,
225
- 'email_field ' => $ input -> getArgument ( ' email-property-name ' ) ,
202
+ 'password_setter ' => $ this -> passwordSetterMethodName ,
203
+ 'success_redirect_route ' => $ this -> controllerResetSuccessRedirect ,
204
+ 'from_email ' => $ this -> fromEmailAddress ,
205
+ 'from_email_name ' => $ this -> fromEmailName ,
206
+ 'email_getter ' => $ this -> emailGetterMethodName ,
207
+ 'email_field ' => $ this -> emailPropertyName ,
226
208
]
227
209
);
228
210
229
- $ this ->generateRequestEntity ($ generator , $ requestClassNameDetails , $ repositoryClassNameDetails, $ userClass );
211
+ $ this ->generateRequestEntity ($ generator , $ requestClassNameDetails , $ repositoryClassNameDetails );
230
212
231
213
$ this ->setBundleConfig ($ io , $ generator , $ repositoryClassNameDetails ->getFullName ());
232
214
233
215
$ generator ->generateClass (
234
216
$ requestFormTypeClassNameDetails ->getFullName (),
235
217
'resetPassword/ResetPasswordRequestFormType.tpl.php ' ,
236
218
[
237
- 'email_field ' => $ input -> getArgument ( ' email-property-name ' ) ,
219
+ 'email_field ' => $ this -> emailPropertyName ,
238
220
]
239
221
);
240
222
@@ -257,7 +239,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
257
239
'reset_password/request.html.twig ' ,
258
240
'resetPassword/twig_request.tpl.php ' ,
259
241
[
260
- 'email_field ' => $ input -> getArgument ( ' email-property-name ' ) ,
242
+ 'email_field ' => $ this -> emailPropertyName ,
261
243
]
262
244
);
263
245
@@ -272,7 +254,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
272
254
$ this ->successMessage ($ input , $ io , $ requestClassNameDetails ->getFullName ());
273
255
}
274
256
275
- private function setBundleConfig (ConsoleStyle $ io , Generator $ generator , string $ repositoryClassFullName )
257
+ private function setBundleConfig (ConsoleStyle $ io , Generator $ generator , string $ repositoryClassFullName ): void
276
258
{
277
259
$ configFileExists = $ this ->fileManager ->fileExists ($ path = 'config/packages/reset_password.yaml ' );
278
260
@@ -322,7 +304,7 @@ private function setBundleConfig(ConsoleStyle $io, Generator $generator, string
322
304
$ generator ->dumpFile ($ path , $ manipulator ->getContents ());
323
305
}
324
306
325
- private function successMessage (InputInterface $ input , ConsoleStyle $ io , string $ requestClassName )
307
+ private function successMessage (InputInterface $ input , ConsoleStyle $ io , string $ requestClassName ): void
326
308
{
327
309
$ closing [] = 'Next: ' ;
328
310
$ closing [] = sprintf (' 1) Run <fg=yellow>"php bin/console make:migration"</> to generate a migration for the new <fg=yellow>"%s"</> entity. ' , $ requestClassName );
@@ -337,7 +319,7 @@ private function successMessage(InputInterface $input, ConsoleStyle $io, string
337
319
$ io ->newLine ();
338
320
}
339
321
340
- private function generateRequestEntity (Generator $ generator , ClassNameDetails $ requestClassNameDetails , ClassNameDetails $ repositoryClassNameDetails, string $ userClass ): void
322
+ private function generateRequestEntity (Generator $ generator , ClassNameDetails $ requestClassNameDetails , ClassNameDetails $ repositoryClassNameDetails ): void
341
323
{
342
324
$ requestEntityPath = $ this ->entityClassGenerator ->generateEntityClass ($ requestClassNameDetails , false , false , false );
343
325
@@ -365,7 +347,7 @@ private function generateRequestEntity(Generator $generator, ClassNameDetails $r
365
347
366
348
$ manipulator ->addManyToOneRelation ((new RelationManyToOne ())
367
349
->setPropertyName ('user ' )
368
- ->setTargetClassName ($ userClass )
350
+ ->setTargetClassName ($ this -> userClass )
369
351
->setMapInverseRelation (false )
370
352
->setCustomReturnType ('object ' , false )
371
353
->avoidSetter ()
0 commit comments