Skip to content

Commit 2467876

Browse files
committed
[Console] Use AsCommand attribute in all commands
1 parent c3ec6ba commit 2467876

File tree

6 files changed

+25
-32
lines changed

6 files changed

+25
-32
lines changed

components/console/changing_default_command.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ name to the ``setDefaultCommand()`` method::
1010

1111
namespace Acme\Console\Command;
1212

13+
use Symfony\Component\Console\Attribute\AsCommand;
1314
use Symfony\Component\Console\Command\Command;
1415
use Symfony\Component\Console\Input\InputInterface;
1516
use Symfony\Component\Console\Output\OutputInterface;
1617

18+
#[AsCommand(name: 'hello:world')]
1719
class HelloWorldCommand extends Command
1820
{
19-
protected static $defaultName = 'hello:world';
20-
2121
protected function configure()
2222
{
2323
$this->setDescription('Outputs "Hello World"');

components/console/console_arguments.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,20 @@ Have a look at the following command that has three options::
1414

1515
namespace Acme\Console\Command;
1616

17+
use Symfony\Component\Console\Attribute\AsCommand;
1718
use Symfony\Component\Console\Command\Command;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputDefinition;
2021
use Symfony\Component\Console\Input\InputInterface;
2122
use Symfony\Component\Console\Input\InputOption;
2223
use Symfony\Component\Console\Output\OutputInterface;
2324

25+
#[AsCommand(name: 'demo:args', description: 'Describe args behaviors')]
2426
class DemoArgsCommand extends Command
2527
{
26-
protected static $defaultName = 'demo:args';
27-
2828
protected function configure()
2929
{
3030
$this
31-
->setDescription('Describe args behaviors')
3231
->setDefinition(
3332
new InputDefinition([
3433
new InputOption('foo', 'f'),

components/console/logger.rst

+5-11
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,18 @@ You can rely on the logger to use this dependency inside a command::
3737
namespace Acme\Console\Command;
3838

3939
use Acme\MyDependency;
40+
use Symfony\Component\Console\Attribute\AsCommand;
4041
use Symfony\Component\Console\Command\Command;
4142
use Symfony\Component\Console\Input\InputInterface;
4243
use Symfony\Component\Console\Logger\ConsoleLogger;
4344
use Symfony\Component\Console\Output\OutputInterface;
4445

46+
#[AsCommand(
47+
name: 'my:command',
48+
description: 'Use an external dependency requiring a PSR-3 logger'
49+
)]
4550
class MyCommand extends Command
4651
{
47-
protected static $defaultName = 'my:command';
48-
49-
protected function configure()
50-
{
51-
$this
52-
->setDescription(
53-
'Use an external dependency requiring a PSR-3 logger'
54-
)
55-
;
56-
}
57-
5852
protected function execute(InputInterface $input, OutputInterface $output)
5953
{
6054
$logger = new ConsoleLogger($output);

console.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ want a command to create a user::
2929
// src/Command/CreateUserCommand.php
3030
namespace App\Command;
3131

32+
use Symfony\Component\Console\Attribute\AsCommand;
3233
use Symfony\Component\Console\Command\Command;
3334
use Symfony\Component\Console\Input\InputInterface;
3435
use Symfony\Component\Console\Output\OutputInterface;
3536

37+
// the name of the command is what users type after "php bin/console"
38+
#[AsCommand(name: 'app:create-user')]
3639
class CreateUserCommand extends Command
3740
{
38-
// the name of the command (the part after "bin/console")
3941
protected static $defaultName = 'app:create-user';
4042

4143
protected function configure(): void

console/commands_as_services.rst

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ For example, suppose you want to log something from within your command::
1818
namespace App\Command;
1919

2020
use Psr\Log\LoggerInterface;
21+
use Symfony\Component\Console\Attribute\AsCommand;
2122
use Symfony\Component\Console\Command\Command;
2223
use Symfony\Component\Console\Input\InputInterface;
2324
use Symfony\Component\Console\Output\OutputInterface;
2425

26+
#[AsCommand(name: 'app:sunshine')]
2527
class SunshineCommand extends Command
2628
{
27-
protected static $defaultName = 'app:sunshine';
2829
private $logger;
2930

3031
public function __construct(LoggerInterface $logger)
@@ -68,12 +69,15 @@ command and start logging.
6869
Lazy Loading
6970
------------
7071

71-
To make your command lazily loaded, either define its ``$defaultName`` static property::
72+
To make your command lazily loaded, either define its name using the PHP
73+
``AsCommand`` attribute::
7274

75+
use Symfony\Component\Console\Attribute\AsCommand;
76+
// ...
77+
78+
#[AsCommand(name: 'app:sunshine')]
7379
class SunshineCommand extends Command
7480
{
75-
protected static $defaultName = 'app:sunshine';
76-
7781
// ...
7882
}
7983

console/hide_commands.rst

+5-11
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,19 @@ However, sometimes commands are not intended to be run by end-users; for
88
example, commands for the legacy parts of the application, commands exclusively
99
run through scheduled tasks, etc.
1010

11-
In those cases, you can define the command as **hidden** by setting the
12-
``setHidden()`` method to ``true`` in the command configuration::
11+
In those cases, you can define the command as **hidden** by setting to ``true``
12+
the ``hidden`` property of the ``AsCommand`` attribute::
1313

1414
// src/Command/LegacyCommand.php
1515
namespace App\Command;
1616

17+
use Symfony\Component\Console\Attribute\AsCommand;
1718
use Symfony\Component\Console\Command\Command;
1819

20+
#[AsCommand(name: 'app:legacy', hidden: true)]
1921
class LegacyCommand extends Command
2022
{
21-
protected static $defaultName = 'app:legacy';
22-
23-
protected function configure(): void
24-
{
25-
$this
26-
->setHidden(true)
27-
// ...
28-
;
29-
}
23+
// ...
3024
}
3125

3226
Hidden commands behave the same as normal commands but they are no longer displayed

0 commit comments

Comments
 (0)