1515use App \Repository \UserRepository ;
1616use App \Utils \Validator ;
1717use Doctrine \ORM \EntityManagerInterface ;
18+ use Symfony \Component \Console \Attribute \Argument ;
1819use Symfony \Component \Console \Attribute \AsCommand ;
20+ use Symfony \Component \Console \Attribute \Option ;
1921use Symfony \Component \Console \Command \Command ;
2022use Symfony \Component \Console \Exception \RuntimeException ;
21- use Symfony \Component \Console \Input \InputArgument ;
2223use Symfony \Component \Console \Input \InputInterface ;
23- use Symfony \Component \Console \Input \InputOption ;
2424use Symfony \Component \Console \Output \OutputInterface ;
2525use Symfony \Component \Console \Style \SymfonyStyle ;
2626use Symfony \Component \PasswordHasher \Hasher \UserPasswordHasherInterface ;
4949 */
5050#[AsCommand(
5151 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: self ::HELP ,
5354)]
5455final class AddUserCommand extends Command
5556{
@@ -64,23 +65,9 @@ public function __construct(
6465 parent ::__construct ();
6566 }
6667
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-
8168 /**
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.
8471 */
8572 protected function initialize (InputInterface $ input , OutputInterface $ output ): void
8673 {
@@ -91,9 +78,9 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
9178 }
9279
9380 /**
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.
9784 *
9885 * This method is completely optional. If you are developing an internal console
9986 * command, you probably should not implement this method because it requires
@@ -161,26 +148,21 @@ protected function interact(InputInterface $input, OutputInterface $output): voi
161148 /**
162149 * This method is executed after interact() and initialize(). It usually
163150 * 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/console/input.html
164155 */
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 {
167163 $ stopwatch = new Stopwatch ();
168164 $ stopwatch ->start ('add-user-command ' );
169165
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-
184166 // make sure to validate the user data is correct
185167 $ this ->validateUserData ($ username , $ plainPassword , $ email , $ fullName );
186168
@@ -202,7 +184,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
202184
203185 $ event = $ stopwatch ->stop ('add-user-command ' );
204186
205- if ($ output ->isVerbose ()) {
187+ if ($ this -> io ->isVerbose ()) {
206188 $ 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 )));
207189 }
208190
@@ -232,33 +214,30 @@ private function validateUserData(string $username, string $plainPassword, strin
232214 }
233215
234216 /**
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
217+ * The command help is usually included in the #[AsCommand] attribute , but when
218+ * it's too long, it's better to define a separate constant to maintain the
237219 * code readability.
238220 */
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:
221+ public const HELP = <<<'HELP'
222+ The <info>%command.name%</info> command creates new users and saves them in the database:
243223
244- <info>php %command.full_name%</info> <comment>username password email</comment>
224+ <info>php %command.full_name%</info> <comment>username password email</comment>
245225
246- By default the command creates regular users. To create administrator users,
247- add the <comment>--admin</comment> option:
226+ By default the command creates regular users. To create administrator users,
227+ add the <comment>--admin</comment> option:
248228
249- <info>php %command.full_name%</info> username password email <comment>--admin</comment>
229+ <info>php %command.full_name%</info> username password email <comment>--admin</comment>
250230
251- If you omit any of the three required arguments, the command will ask you to
252- provide the missing values:
231+ If you omit any of the three required arguments, the command will ask you to
232+ provide the missing values:
253233
254- # command will ask you for the email
255- <info>php %command.full_name%</info> <comment>username password</comment>
234+ # command will ask you for the email
235+ <info>php %command.full_name%</info> <comment>username password</comment>
256236
257- # command will ask you for the email and password
258- <info>php %command.full_name%</info> <comment>username</comment>
237+ # command will ask you for the email and password
238+ <info>php %command.full_name%</info> <comment>username</comment>
259239
260- # command will ask you for all arguments
261- <info>php %command.full_name%</info>
262- HELP;
263- }
240+ # command will ask you for all arguments
241+ <info>php %command.full_name%</info>
242+ HELP;
264243}
0 commit comments