15
15
use App \Repository \UserRepository ;
16
16
use App \Utils \Validator ;
17
17
use Doctrine \ORM \EntityManagerInterface ;
18
+ use Symfony \Component \Console \Attribute \Argument ;
18
19
use Symfony \Component \Console \Attribute \AsCommand ;
20
+ use Symfony \Component \Console \Attribute \Option ;
19
21
use Symfony \Component \Console \Command \Command ;
20
22
use Symfony \Component \Console \Exception \RuntimeException ;
21
- use Symfony \Component \Console \Input \InputArgument ;
22
23
use Symfony \Component \Console \Input \InputInterface ;
23
- use Symfony \Component \Console \Input \InputOption ;
24
24
use Symfony \Component \Console \Output \OutputInterface ;
25
25
use Symfony \Component \Console \Style \SymfonyStyle ;
26
26
use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
49
49
*/
50
50
#[AsCommand(
51
51
name: 'app:add-user ' ,
52
- description: 'Creates users and stores them in the database '
52
+ description: 'Creates users and stores them in the database ' ,
53
+ help: HELP ,
53
54
)]
54
55
final class AddUserCommand extends Command
55
56
{
@@ -64,23 +65,9 @@ public function __construct(
64
65
parent ::__construct ();
65
66
}
66
67
67
- protected function configure (): void
68
- {
69
- $ this
70
- ->setHelp ($ this ->getCommandHelp ())
71
- // commands can optionally define arguments and/or options (mandatory and optional)
72
- // see https://symfony.com/doc/current/components/console/console_arguments.html
73
- ->addArgument ('username ' , InputArgument::OPTIONAL , 'The username of the new user ' )
74
- ->addArgument ('password ' , InputArgument::OPTIONAL , 'The plain password of the new user ' )
75
- ->addArgument ('email ' , InputArgument::OPTIONAL , 'The email of the new user ' )
76
- ->addArgument ('full-name ' , InputArgument::OPTIONAL , 'The full name of the new user ' )
77
- ->addOption ('admin ' , null , InputOption::VALUE_NONE , 'If set, the user is created as an administrator ' )
78
- ;
79
- }
80
-
81
68
/**
82
- * This optional method is the first one executed for a command after configure()
83
- * and is useful to initialize properties based on the input arguments and options.
69
+ * This optional method is the first one executed for a command and is useful
70
+ * to initialize properties based on the input arguments and options.
84
71
*/
85
72
protected function initialize (InputInterface $ input , OutputInterface $ output ): void
86
73
{
@@ -91,9 +78,9 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
91
78
}
92
79
93
80
/**
94
- * This method is executed after initialize() and before execute (). Its purpose
95
- * is to check if some of the options/arguments are missing and interactively
96
- * ask the user for those values.
81
+ * This method is executed after initialize() and before __invoke (). Its purpose
82
+ * is to check if some options/arguments are missing and interactively ask the user
83
+ * for those values.
97
84
*
98
85
* This method is completely optional. If you are developing an internal console
99
86
* command, you probably should not implement this method because it requires
@@ -161,26 +148,21 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
161
148
/**
162
149
* This method is executed after interact() and initialize(). It usually
163
150
* contains the logic to execute to complete this command task.
151
+ *
152
+ * Commands can optionally define arguments and/or options (mandatory and optional)
153
+ *
154
+ * @see https://symfony.com/doc/current/components/console/console_arguments.html
164
155
*/
165
- protected function execute (InputInterface $ input , OutputInterface $ output ): int
166
- {
156
+ public function __invoke (
157
+ #[Argument('The username of the new user ' )] string $ username ,
158
+ #[Argument('The plain password of the new user ' , 'password ' )] string $ plainPassword ,
159
+ #[Argument('The email of the new user ' )] string $ email ,
160
+ #[Argument('The full name of the new user ' )] string $ fullName ,
161
+ #[Option('If set, the user is created as an administrator ' , 'admin ' )] bool $ isAdmin = false ,
162
+ ): int {
167
163
$ stopwatch = new Stopwatch ();
168
164
$ stopwatch ->start ('add-user-command ' );
169
165
170
- /** @var string $username */
171
- $ username = $ input ->getArgument ('username ' );
172
-
173
- /** @var string $plainPassword */
174
- $ plainPassword = $ input ->getArgument ('password ' );
175
-
176
- /** @var string $email */
177
- $ email = $ input ->getArgument ('email ' );
178
-
179
- /** @var string $fullName */
180
- $ fullName = $ input ->getArgument ('full-name ' );
181
-
182
- $ isAdmin = $ input ->getOption ('admin ' );
183
-
184
166
// make sure to validate the user data is correct
185
167
$ this ->validateUserData ($ username , $ plainPassword , $ email , $ fullName );
186
168
@@ -202,7 +184,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
202
184
203
185
$ event = $ stopwatch ->stop ('add-user-command ' );
204
186
205
- if ($ output ->isVerbose ()) {
187
+ if ($ this -> io ->isVerbose ()) {
206
188
$ this ->io ->comment (\sprintf ('New user database id: %d / Elapsed time: %.2f ms / Consumed memory: %.2f MB ' , $ user ->getId (), $ event ->getDuration (), $ event ->getMemory () / (1024 ** 2 )));
207
189
}
208
190
@@ -230,35 +212,32 @@ private function validateUserData(string $username, string $plainPassword, strin
230
212
throw new RuntimeException (\sprintf ('There is already a user registered with the "%s" email. ' , $ email ));
231
213
}
232
214
}
215
+ }
233
216
234
- /**
235
- * The command help is usually included in the configure() method, but when
236
- * it's too long, it's better to define a separate method to maintain the
237
- * code readability.
238
- */
239
- private function getCommandHelp (): string
240
- {
241
- return <<<'HELP'
242
- The <info>%command.name%</info> command creates new users and saves them in the database:
217
+ /**
218
+ * The command help is usually included in the #[AsCommand] attribute, but when
219
+ * it's too long, it's better to define a separate constant to maintain the
220
+ * code readability.
221
+ */
222
+ const HELP = <<<'HELP'
223
+ The <info>%command.name%</info> command creates new users and saves them in the database:
243
224
244
- <info>php %command.full_name%</info> <comment>username password email</comment>
225
+ <info>php %command.full_name%</info> <comment>username password email</comment>
245
226
246
- By default the command creates regular users. To create administrator users,
247
- add the <comment>--admin</comment> option:
227
+ By default the command creates regular users. To create administrator users,
228
+ add the <comment>--admin</comment> option:
248
229
249
- <info>php %command.full_name%</info> username password email <comment>--admin</comment>
230
+ <info>php %command.full_name%</info> username password email <comment>--admin</comment>
250
231
251
- If you omit any of the three required arguments, the command will ask you to
252
- provide the missing values:
232
+ If you omit any of the three required arguments, the command will ask you to
233
+ provide the missing values:
253
234
254
- # command will ask you for the email
255
- <info>php %command.full_name%</info> <comment>username password</comment>
235
+ # command will ask you for the email
236
+ <info>php %command.full_name%</info> <comment>username password</comment>
256
237
257
- # command will ask you for the email and password
258
- <info>php %command.full_name%</info> <comment>username</comment>
238
+ # command will ask you for the email and password
239
+ <info>php %command.full_name%</info> <comment>username</comment>
259
240
260
- # command will ask you for all arguments
261
- <info>php %command.full_name%</info>
262
- HELP;
263
- }
264
- }
241
+ # command will ask you for all arguments
242
+ <info>php %command.full_name%</info>
243
+ HELP;
0 commit comments