From a32df74779895148ffc766aae2b7ed11b62ca3d2 Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Thu, 16 Dec 2021 12:12:05 +0100 Subject: [PATCH] Leverage PHP 8 attributes to configure Command --- src/Command/AddUserCommand.php | 10 +++++----- src/Command/DeleteUserCommand.php | 8 +++++--- src/Command/ListUsersCommand.php | 10 ++++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Command/AddUserCommand.php b/src/Command/AddUserCommand.php index d9de00f41..7004dfda6 100644 --- a/src/Command/AddUserCommand.php +++ b/src/Command/AddUserCommand.php @@ -15,6 +15,7 @@ use App\Repository\UserRepository; use App\Utils\Validator; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputArgument; @@ -46,12 +47,12 @@ * @author Javier Eguiluz * @author Yonel Ceruto */ +#[AsCommand( + name: 'app:add-user', + description: 'Creates users and stores them in the database' +)] class AddUserCommand extends Command { - // to make your command lazily loaded, configure the $defaultName static property, - // so it will be instantiated only when the command is actually called. - protected static $defaultName = 'app:add-user'; - private SymfonyStyle $io; public function __construct( @@ -69,7 +70,6 @@ public function __construct( protected function configure(): void { $this - ->setDescription('Creates users and stores them in the database') ->setHelp($this->getCommandHelp()) // commands can optionally define arguments and/or options (mandatory and optional) // see https://symfony.com/doc/current/components/console/console_arguments.html diff --git a/src/Command/DeleteUserCommand.php b/src/Command/DeleteUserCommand.php index cf4060cb8..ba9bec096 100644 --- a/src/Command/DeleteUserCommand.php +++ b/src/Command/DeleteUserCommand.php @@ -15,6 +15,7 @@ use App\Repository\UserRepository; use App\Utils\Validator; use Doctrine\ORM\EntityManagerInterface; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputArgument; @@ -37,10 +38,12 @@ * * @author Oleg Voronkovich */ +#[AsCommand( + name: 'app:delete-user', + description: 'Deletes users from the database' +)] class DeleteUserCommand extends Command { - protected static $defaultName = 'app:delete-user'; - private SymfonyStyle $io; public function __construct( @@ -57,7 +60,6 @@ public function __construct( protected function configure(): void { $this - ->setDescription('Deletes users from the database') ->addArgument('username', InputArgument::REQUIRED, 'The username of an existing user') ->setHelp(<<<'HELP' The %command.name% command deletes users from the database: diff --git a/src/Command/ListUsersCommand.php b/src/Command/ListUsersCommand.php index b2369feba..febc67387 100644 --- a/src/Command/ListUsersCommand.php +++ b/src/Command/ListUsersCommand.php @@ -13,6 +13,7 @@ use App\Entity\User; use App\Repository\UserRepository; +use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -37,11 +38,13 @@ * * @author Javier Eguiluz */ +#[AsCommand( + name: 'app:list-users', + description: 'Lists all the existing users', + aliases: ['app:users'] +)] class ListUsersCommand extends Command { - // a good practice is to use the 'app:' prefix to group all your custom application commands - protected static $defaultName = 'app:list-users'; - public function __construct( private MailerInterface $mailer, private string $emailSender, @@ -56,7 +59,6 @@ public function __construct( protected function configure(): void { $this - ->setDescription('Lists all the existing users') ->setHelp(<<<'HELP' The %command.name% command lists all the users registered in the application: