Skip to content

Commit 59d1811

Browse files
committed
feature #1289 Leverage PHP 8 attributes to configure Command (Antoine Makdessi)
This PR was merged into the main branch. Discussion ---------- Leverage PHP 8 attributes to configure Command I wanted to test PHP 8 install on my new machine, thus tried some features on this project as it now requires v8 I followed https://symfony.com/blog/new-in-symfony-5-3-lazy-command-description Feel free to close, but I think it could be useful to showcase new way on this Symfony demo project as it is kind off an entry point, also Controllers uses this feature Commits ------- a32df74 Leverage PHP 8 attributes to configure Command
2 parents 03daaeb + a32df74 commit 59d1811

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/Command/AddUserCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Repository\UserRepository;
1616
use App\Utils\Validator;
1717
use Doctrine\ORM\EntityManagerInterface;
18+
use Symfony\Component\Console\Attribute\AsCommand;
1819
use Symfony\Component\Console\Command\Command;
1920
use Symfony\Component\Console\Exception\RuntimeException;
2021
use Symfony\Component\Console\Input\InputArgument;
@@ -46,12 +47,12 @@
4647
* @author Javier Eguiluz <[email protected]>
4748
* @author Yonel Ceruto <[email protected]>
4849
*/
50+
#[AsCommand(
51+
name: 'app:add-user',
52+
description: 'Creates users and stores them in the database'
53+
)]
4954
class AddUserCommand extends Command
5055
{
51-
// to make your command lazily loaded, configure the $defaultName static property,
52-
// so it will be instantiated only when the command is actually called.
53-
protected static $defaultName = 'app:add-user';
54-
5556
private SymfonyStyle $io;
5657

5758
public function __construct(
@@ -69,7 +70,6 @@ public function __construct(
6970
protected function configure(): void
7071
{
7172
$this
72-
->setDescription('Creates users and stores them in the database')
7373
->setHelp($this->getCommandHelp())
7474
// commands can optionally define arguments and/or options (mandatory and optional)
7575
// see https://symfony.com/doc/current/components/console/console_arguments.html

src/Command/DeleteUserCommand.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Repository\UserRepository;
1616
use App\Utils\Validator;
1717
use Doctrine\ORM\EntityManagerInterface;
18+
use Symfony\Component\Console\Attribute\AsCommand;
1819
use Symfony\Component\Console\Command\Command;
1920
use Symfony\Component\Console\Exception\RuntimeException;
2021
use Symfony\Component\Console\Input\InputArgument;
@@ -37,10 +38,12 @@
3738
*
3839
* @author Oleg Voronkovich <[email protected]>
3940
*/
41+
#[AsCommand(
42+
name: 'app:delete-user',
43+
description: 'Deletes users from the database'
44+
)]
4045
class DeleteUserCommand extends Command
4146
{
42-
protected static $defaultName = 'app:delete-user';
43-
4447
private SymfonyStyle $io;
4548

4649
public function __construct(
@@ -57,7 +60,6 @@ public function __construct(
5760
protected function configure(): void
5861
{
5962
$this
60-
->setDescription('Deletes users from the database')
6163
->addArgument('username', InputArgument::REQUIRED, 'The username of an existing user')
6264
->setHelp(<<<'HELP'
6365
The <info>%command.name%</info> command deletes users from the database:

src/Command/ListUsersCommand.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use App\Entity\User;
1515
use App\Repository\UserRepository;
16+
use Symfony\Component\Console\Attribute\AsCommand;
1617
use Symfony\Component\Console\Command\Command;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Input\InputOption;
@@ -37,11 +38,13 @@
3738
*
3839
* @author Javier Eguiluz <[email protected]>
3940
*/
41+
#[AsCommand(
42+
name: 'app:list-users',
43+
description: 'Lists all the existing users',
44+
aliases: ['app:users']
45+
)]
4046
class ListUsersCommand extends Command
4147
{
42-
// a good practice is to use the 'app:' prefix to group all your custom application commands
43-
protected static $defaultName = 'app:list-users';
44-
4548
public function __construct(
4649
private MailerInterface $mailer,
4750
private string $emailSender,
@@ -56,7 +59,6 @@ public function __construct(
5659
protected function configure(): void
5760
{
5861
$this
59-
->setDescription('Lists all the existing users')
6062
->setHelp(<<<'HELP'
6163
The <info>%command.name%</info> command lists all the users registered in the application:
6264

0 commit comments

Comments
 (0)