Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 42c2a79

Browse files
committedMay 1, 2025·
Document invokable command
1 parent 3ae4fec commit 42c2a79

19 files changed

+124
-158
lines changed
 

‎components/console/changing_default_command.rst

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,14 @@ name to the ``setDefaultCommand()`` method::
99

1010
use Symfony\Component\Console\Attribute\AsCommand;
1111
use Symfony\Component\Console\Command\Command;
12-
use Symfony\Component\Console\Input\InputInterface;
13-
use Symfony\Component\Console\Output\OutputInterface;
12+
use Symfony\Component\Console\Style\SymfonyStyle;
1413

15-
#[AsCommand(name: 'hello:world')]
14+
#[AsCommand(name: 'hello:world', description: 'Outputs "Hello World"')]
1615
class HelloWorldCommand extends Command
1716
{
18-
protected function configure(): void
17+
public function __invoke(SymfonyStyle $io): int
1918
{
20-
$this->setDescription('Outputs "Hello World"');
21-
}
22-
23-
protected function execute(InputInterface $input, OutputInterface $output): int
24-
{
25-
$output->writeln('Hello World');
19+
$io->writeln('Hello World');
2620

2721
return Command::SUCCESS;
2822
}

‎components/console/events.rst

+11-15
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,32 @@ method::
209209
for these constants to be available.
210210

211211
If you use the Console component inside a Symfony application, commands can
212-
handle signals themselves. To do so, implement the
213-
:class:`Symfony\\Component\\Console\\Command\\SignalableCommandInterface` and subscribe to one or more signals::
212+
handle signals themselves. To do so, subscribe to :class:`Symfony\\Component\\Console\\Event\\ConsoleSignalEvent` event::
214213

215-
// src/Command/SomeCommand.php
214+
// src/Command/MyCommand.php
216215
namespace App\Command;
217216

218-
use Symfony\Component\Console\Command\Command;
219-
use Symfony\Component\Console\Command\SignalableCommandInterface;
217+
use Symfony\Component\Console\Attribute\AsCommand;
218+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
220219

221-
class SomeCommand extends Command implements SignalableCommandInterface
220+
#[AsCommand(name: 'app:my-command')]
221+
class MyCommand
222222
{
223223
// ...
224224

225-
public function getSubscribedSignals(): array
225+
#[AsEventListener(ConsoleSignalEvent::class)]
226+
public function handleSignal(ConsoleSignalEvent $event): void
226227
{
227-
// return here any of the constants defined by PCNTL extension
228-
return [\SIGINT, \SIGTERM];
229-
}
230-
231-
public function handleSignal(int $signal): int|false
232-
{
233-
if (\SIGINT === $signal) {
228+
// set here any of the constants defined by PCNTL extension
229+
if (in_array($event->getHandlingSignal(), [\SIGINT, \SIGTERM], true)) {
234230
// ...
235231
}
236232

237233
// ...
238234

239235
// return an integer to set the exit code, or
240236
// false to continue normal execution
241-
return 0;
237+
$event->setExitCode(0);
242238
}
243239
}
244240

‎components/console/helpers/cursor.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ of the output:
1313
// src/Command/MyCommand.php
1414
namespace App\Command;
1515
16-
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Attribute\AsCommand;
1717
use Symfony\Component\Console\Cursor;
18-
use Symfony\Component\Console\Input\InputInterface;
1918
use Symfony\Component\Console\Output\OutputInterface;
2019
21-
class MyCommand extends Command
20+
#[AsCommand(name: 'my-command')]
21+
class MyCommand
2222
{
2323
// ...
2424
25-
public function execute(InputInterface $input, OutputInterface $output): int
25+
public function __invoke(OutputInterface $output): int
2626
{
2727
// ...
2828

‎components/console/helpers/questionhelper.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ Suppose you want to confirm an action before actually executing it. Add
2727
the following to your command::
2828

2929
// ...
30+
use Symfony\Component\Console\Attribute\AsCommand;
3031
use Symfony\Component\Console\Command\Command;
3132
use Symfony\Component\Console\Input\InputInterface;
3233
use Symfony\Component\Console\Output\OutputInterface;
3334
use Symfony\Component\Console\Question\ConfirmationQuestion;
3435

35-
class YourCommand extends Command
36+
#[AsCommand(name: 'app:my-command')]
37+
class MyCommand
3638
{
37-
// ...
38-
39-
public function execute(InputInterface $input, OutputInterface $output): int
39+
public function __invoke(InputInterface $input, OutputInterface $output): int
4040
{
4141
$helper = $this->getHelper('question');
4242
$question = new ConfirmationQuestion('Continue with this action?', false);

‎components/console/helpers/table.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ When building a console application it may be useful to display tabular data:
2222
To display a table, use :class:`Symfony\\Component\\Console\\Helper\\Table`,
2323
set the headers, set the rows and then render the table::
2424

25+
use Symfony\Component\Console\Attribute\AsCommand;
2526
use Symfony\Component\Console\Command\Command;
2627
use Symfony\Component\Console\Helper\Table;
27-
use Symfony\Component\Console\Input\InputInterface;
2828
use Symfony\Component\Console\Output\OutputInterface;
2929
// ...
3030

31-
class SomeCommand extends Command
31+
#[AsCommand(name: 'app:my-command')]
32+
class MyCommand
3233
{
33-
public function execute(InputInterface $input, OutputInterface $output): int
34+
public function __invoke(OutputInterface $output): int
3435
{
3536
$table = new Table($output);
3637
$table
@@ -445,9 +446,10 @@ The only requirement to append rows is that the table must be rendered inside a
445446
use Symfony\Component\Console\Helper\Table;
446447
// ...
447448

448-
class SomeCommand extends Command
449+
#[AsCommand(name: 'app:my-command')]
450+
class MyCommand
449451
{
450-
public function execute(InputInterface $input, OutputInterface $output): int
452+
public function __invoke(OutputInterface $output): int
451453
{
452454
$section = $output->section();
453455
$table = new Table($section);

‎components/console/helpers/tree.rst

+3-8
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,17 @@ inside your console command::
2626
namespace App\Command;
2727

2828
use Symfony\Component\Console\Attribute\AsCommand;
29-
use Symfony\Component\Console\Command\Command;
3029
use Symfony\Component\Console\Helper\TreeHelper;
3130
use Symfony\Component\Console\Helper\TreeNode;
32-
use Symfony\Component\Console\Input\InputInterface;
33-
use Symfony\Component\Console\Output\OutputInterface;
3431
use Symfony\Component\Console\Style\SymfonyStyle;
3532

36-
#[AsCommand(name: 'app:some-command', description: '...')]
37-
class SomeCommand extends Command
33+
#[AsCommand(name: 'app:my-command', description: '...')]
34+
class MyCommand
3835
{
3936
// ...
4037

41-
protected function execute(InputInterface $input, OutputInterface $output): int
38+
protected function execute(SymfonyStyle $io): int
4239
{
43-
$io = new SymfonyStyle($input, $output);
44-
4540
$node = TreeNode::fromValues([
4641
'config/',
4742
'public/',

‎components/console/logger.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,16 @@ You can rely on the logger to use this dependency inside a command::
3434
use Acme\MyDependency;
3535
use Symfony\Component\Console\Attribute\AsCommand;
3636
use Symfony\Component\Console\Command\Command;
37-
use Symfony\Component\Console\Input\InputInterface;
3837
use Symfony\Component\Console\Logger\ConsoleLogger;
3938
use Symfony\Component\Console\Output\OutputInterface;
4039

4140
#[AsCommand(
4241
name: 'my:command',
4342
description: 'Use an external dependency requiring a PSR-3 logger'
4443
)]
45-
class MyCommand extends Command
44+
class MyCommand
4645
{
47-
protected function execute(InputInterface $input, OutputInterface $output): int
46+
public function __invoke(OutputInterface $output): int
4847
{
4948
$logger = new ConsoleLogger($output);
5049

‎components/console/single_command_tool.rst

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,18 @@ it is possible to remove this need by declaring a single command application::
99
<?php
1010
require __DIR__.'/vendor/autoload.php';
1111

12-
use Symfony\Component\Console\Input\InputArgument;
13-
use Symfony\Component\Console\Input\InputInterface;
14-
use Symfony\Component\Console\Input\InputOption;
12+
use Symfony\Component\Console\Attribute\Argument;
13+
use Symfony\Component\Console\Attribute\Option;
1514
use Symfony\Component\Console\Output\OutputInterface;
1615
use Symfony\Component\Console\SingleCommandApplication;
1716

1817
(new SingleCommandApplication())
1918
->setName('My Super Command') // Optional
2019
->setVersion('1.0.0') // Optional
21-
->addArgument('foo', InputArgument::OPTIONAL, 'The directory')
22-
->addOption('bar', null, InputOption::VALUE_REQUIRED)
23-
->setCode(function (InputInterface $input, OutputInterface $output): int {
20+
->setCode(function (OutputInterface $output, #[Argument] ?string $foo, #[Option] string $bar = ''): int {
2421
// output arguments and options
22+
23+
return 0;
2524
})
2625
->run();
2726

‎components/process.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,14 @@ However, if you run the command via the Symfony ``Process`` class, PHP will use
430430
the settings defined in the ``php.ini`` file. You can solve this issue by using
431431
the :class:`Symfony\\Component\\Process\\PhpSubprocess` class to run the command::
432432

433+
use Symfony\Component\Console\Attribute\AsCommand;
434+
use Symfony\Component\Console\Style\SymfonyStyle;
433435
use Symfony\Component\Process\Process;
434436

435-
class MyCommand extends Command
437+
#[AsCommand(name: 'app:my-command')]
438+
class MyCommand
436439
{
437-
protected function execute(InputInterface $input, OutputInterface $output): int
440+
public function __invoke(SymfonyStyle $io): int
438441
{
439442
// the memory_limit (and any other config option) of this command is
440443
// the one defined in php.ini instead of the new values (optionally)
@@ -444,6 +447,8 @@ the :class:`Symfony\\Component\\Process\\PhpSubprocess` class to run the command
444447
// the memory_limit (and any other config option) of this command takes
445448
// into account the values (optionally) passed via the '-d' command option
446449
$childProcess = new PhpSubprocess(['bin/console', 'cache:pool:prune']);
450+
451+
return 0;
447452
}
448453
}
449454

‎console.rst

+15-17
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,19 @@ completion (by default, by pressing the Tab key).
110110
Creating a Command
111111
------------------
112112

113-
Commands are defined in classes extending
114-
:class:`Symfony\\Component\\Console\\Command\\Command`. For example, you may
115-
want a command to create a user::
113+
Commands are defined in classes, for example, you may want a command to create a user::
116114

117115
// src/Command/CreateUserCommand.php
118116
namespace App\Command;
119117

120118
use Symfony\Component\Console\Attribute\AsCommand;
121119
use Symfony\Component\Console\Command\Command;
122-
use Symfony\Component\Console\Input\InputInterface;
123-
use Symfony\Component\Console\Output\OutputInterface;
124120

125121
// the name of the command is what users type after "php bin/console"
126122
#[AsCommand(name: 'app:create-user')]
127-
class CreateUserCommand extends Command
123+
class CreateUserCommand
128124
{
129-
protected function execute(InputInterface $input, OutputInterface $output): int
125+
public function __invoke(): int
130126
{
131127
// ... put here the code to create the user
132128

@@ -147,6 +143,10 @@ want a command to create a user::
147143
}
148144
}
149145

146+
Additionally, can extend the :class:`Symfony\\Component\\Console\\Command\\Command` class to
147+
leverage advanced features like lifecycle hooks: :method:`Symfony\\Component\\Console\\Command\\Command::initialize`,
148+
:method:`Symfony\\Component\\Console\\Command\\Command::interact`, and built-in helpers.
149+
150150
Configuring the Command
151151
~~~~~~~~~~~~~~~~~~~~~~~
152152

@@ -156,18 +156,16 @@ You can optionally define a description, help message and the
156156

157157
// src/Command/CreateUserCommand.php
158158

159-
// ...
160-
class CreateUserCommand extends Command
159+
#[AsCommand(
160+
name: 'app:create-user'),
161+
description: 'Creates a new user.'), // the command description shown when running "php bin/console list"
162+
help: 'This command allows you to create a user...', // the command help shown when running the command with the "--help" option
163+
)]
164+
class CreateUserCommand
161165
{
162-
// ...
163-
protected function configure(): void
166+
public function __invoke(): int
164167
{
165-
$this
166-
// the command description shown when running "php bin/console list"
167-
->setDescription('Creates a new user.')
168-
// the command help shown when running the command with the "--help" option
169-
->setHelp('This command allows you to create a user...')
170-
;
168+
// ...
171169
}
172170
}
173171

‎console/calling_commands.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ the returned code from the command (return value from command ``execute()``
1818
method)::
1919

2020
// ...
21+
use Symfony\Component\Console\Attribute\AsCommand;
2122
use Symfony\Component\Console\Command;
2223
use Symfony\Component\Console\Input\ArrayInput;
23-
use Symfony\Component\Console\Input\InputInterface;
2424
use Symfony\Component\Console\Output\OutputInterface;
2525

26-
class CreateUserCommand extends Command
26+
#[AsCommand(name: 'app:create-user')]
27+
class CreateUserCommand
2728
{
28-
// ...
29-
30-
protected function execute(InputInterface $input, OutputInterface $output): int
29+
public function __invoke(OutputInterface $output): int
3130
{
3231
$greetInput = new ArrayInput([
3332
// the command name is passed as first argument

‎console/commands_as_services.rst

+4-15
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,16 @@ For example, suppose you want to log something from within your command::
1616

1717
use Psr\Log\LoggerInterface;
1818
use Symfony\Component\Console\Attribute\AsCommand;
19-
use Symfony\Component\Console\Command\Command;
20-
use Symfony\Component\Console\Input\InputInterface;
21-
use Symfony\Component\Console\Output\OutputInterface;
2219

23-
#[AsCommand(name: 'app:sunshine')]
24-
class SunshineCommand extends Command
20+
#[AsCommand(name: 'app:sunshine', description: 'Good morning!')]
21+
class SunshineCommand
2522
{
2623
public function __construct(
2724
private LoggerInterface $logger,
2825
) {
29-
// you *must* call the parent constructor
30-
parent::__construct();
31-
}
32-
33-
protected function configure(): void
34-
{
35-
$this
36-
->setDescription('Good morning!');
3726
}
3827

39-
protected function execute(InputInterface $input, OutputInterface $output): int
28+
public function __invoke(): int
4029
{
4130
$this->logger->info('Waking up the sun');
4231
// ...
@@ -70,7 +59,7 @@ To make your command lazily loaded, either define its name using the PHP
7059
// ...
7160

7261
#[AsCommand(name: 'app:sunshine')]
73-
class SunshineCommand extends Command
62+
class SunshineCommand
7463
{
7564
// ...
7665
}

‎console/hide_commands.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ the ``hidden`` property of the ``AsCommand`` attribute::
1515
namespace App\Command;
1616

1717
use Symfony\Component\Console\Attribute\AsCommand;
18-
use Symfony\Component\Console\Command\Command;
1918

2019
#[AsCommand(name: 'app:legacy', hidden: true)]
21-
class LegacyCommand extends Command
20+
class LegacyCommand
2221
{
2322
// ...
2423
}

‎console/lockable_trait.rst

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@ that adds two convenient methods to lock and release commands::
1313
// ...
1414
use Symfony\Component\Console\Command\Command;
1515
use Symfony\Component\Console\Command\LockableTrait;
16-
use Symfony\Component\Console\Input\InputInterface;
17-
use Symfony\Component\Console\Output\OutputInterface;
16+
use Symfony\Component\Console\Style\SymfonyStyle;
1817

19-
class UpdateContentsCommand extends Command
18+
#[AsCommand(name: 'contents:update')]
19+
class UpdateContentsCommand
2020
{
2121
use LockableTrait;
2222

23-
// ...
24-
25-
protected function execute(InputInterface $input, OutputInterface $output): int
23+
public function __invoke(SymfonyStyle $io): int
2624
{
2725
if (!$this->lock()) {
28-
$output->writeln('The command is already running in another process.');
26+
$io->writeln('The command is already running in another process.');
2927

3028
return Command::SUCCESS;
3129
}
@@ -52,7 +50,8 @@ a ``$lockFactory`` property with your own lock factory::
5250
use Symfony\Component\Console\Command\LockableTrait;
5351
use Symfony\Component\Lock\LockFactory;
5452

55-
class UpdateContentsCommand extends Command
53+
#[AsCommand(name: 'contents:update')]
54+
class UpdateContentsCommand
5655
{
5756
use LockableTrait;
5857

‎console/style.rst

+21-32
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ questions to the user involves a lot of repetitive code.
77

88
Consider for example the code used to display the title of the following command::
99

10-
// src/Command/GreetCommand.php
10+
// src/Command/MyCommand.php
1111
namespace App\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

17-
class GreetCommand extends Command
18+
#[AsCommand(name: 'app:my-command')]
19+
class MyCommand
1820
{
19-
// ...
20-
21-
protected function execute(InputInterface $input, OutputInterface $output): int
21+
public function __invoke(InputInterface $input, OutputInterface $output): int
2222
{
2323
$output->writeln([
2424
'<info>Lorem Ipsum Dolor Sit Amet</>',
@@ -42,26 +42,22 @@ which allow to create *semantic* commands and forget about their styling.
4242
Basic Usage
4343
-----------
4444

45-
In your command, instantiate the :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle`
46-
class and pass the ``$input`` and ``$output`` variables as its arguments. Then,
47-
you can start using any of its helpers, such as ``title()``, which displays the
48-
title of the command::
45+
In your `__invoke` method, type the :class:`Symfony\\Component\\Console\\Style\\SymfonyStyle`
46+
argument. Then, you can start using any of its helpers, such as ``title()``, which
47+
displays the title of the command::
4948

50-
// src/Command/GreetCommand.php
49+
// src/Command/MyCommand.php
5150
namespace App\Command;
5251

52+
use Symfony\Component\Console\Attribute\AsCommand;
5353
use Symfony\Component\Console\Command\Command;
54-
use Symfony\Component\Console\Input\InputInterface;
55-
use Symfony\Component\Console\Output\OutputInterface;
5654
use Symfony\Component\Console\Style\SymfonyStyle;
5755

58-
class GreetCommand extends Command
56+
#[AsCommand(name: 'app:my-command')]
57+
class MyCommand
5958
{
60-
// ...
61-
62-
protected function execute(InputInterface $input, OutputInterface $output): int
59+
public function __invoke(SymfonyStyle $io): int
6360
{
64-
$io = new SymfonyStyle($input, $output);
6561
$io->title('Lorem Ipsum Dolor Sit Amet');
6662

6763
// ...
@@ -448,19 +444,17 @@ long they are. This is done to enable clickable URLs in terminals that support t
448444

449445
If you prefer to wrap all contents, including URLs, use this method::
450446

451-
// src/Command/GreetCommand.php
447+
// src/Command/MyCommand.php
452448
namespace App\Command;
453449

454450
// ...
455451
use Symfony\Component\Console\Style\SymfonyStyle;
456452

457-
class GreetCommand extends Command
453+
#[AsCommand(name: 'app:my-command')]
454+
class MyCommand
458455
{
459-
// ...
460-
461-
protected function execute(InputInterface $input, OutputInterface $output): int
456+
public function __invoke(SymfonyStyle $io): int
462457
{
463-
$io = new SymfonyStyle($input, $output);
464458
$io->getOutputWrapper()->setAllowCutUrls(true);
465459

466460
// ...
@@ -487,24 +481,19 @@ Then, instantiate this custom class instead of the default ``SymfonyStyle`` in
487481
your commands. Thanks to the ``StyleInterface`` you won't need to change the code
488482
of your commands to change their appearance::
489483

490-
// src/Command/GreetCommand.php
484+
// src/Command/MyCommand.php
491485
namespace App\Console;
492486

493487
use App\Console\CustomStyle;
494488
use Symfony\Component\Console\Command\Command;
495489
use Symfony\Component\Console\Input\InputInterface;
496490
use Symfony\Component\Console\Output\OutputInterface;
497491

498-
class GreetCommand extends Command
492+
#[AsCommand(name: 'app:my-command')]
493+
class MyCommand
499494
{
500-
// ...
501-
502-
protected function execute(InputInterface $input, OutputInterface $output): int
495+
public function __invoke(InputInterface $input, OutputInterface $output): int
503496
{
504-
// Before
505-
$io = new SymfonyStyle($input, $output);
506-
507-
// After
508497
$io = new CustomStyle($input, $output);
509498

510499
// ...

‎console/verbosity.rst

+7-6
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,22 @@ It is possible to print a message in a command for only a specific verbosity
4949
level. For example::
5050

5151
// ...
52+
use Symfony\Component\Console\Attribute\Argument;
53+
use Symfony\Component\Console\Attribute\AsCommand;
5254
use Symfony\Component\Console\Command\Command;
5355
use Symfony\Component\Console\Input\InputInterface;
5456
use Symfony\Component\Console\Output\OutputInterface;
5557

56-
class CreateUserCommand extends Command
58+
#[AsCommand(name: 'app:create-user')]
59+
class CreateUserCommand
5760
{
58-
// ...
59-
60-
public function execute(InputInterface $input, OutputInterface $output): int
61+
public function __invoke(OutputInterface $output, #[Argument] string $username, #[Argument] string $password): int
6162
{
6263
$user = new User(...);
6364

6465
$output->writeln([
65-
'Username: '.$input->getArgument('username'),
66-
'Password: '.$input->getArgument('password'),
66+
'Username: '.$username,
67+
'Password: '.$password,
6768
]);
6869

6970
// available methods: ->isSilent(), ->isQuiet(), ->isVerbose(), ->isVeryVerbose(), ->isDebug()

‎logging/monolog_console.rst

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ The example above could then be rewritten as::
3838
namespace App\Command;
3939

4040
use Psr\Log\LoggerInterface;
41+
use Symfony\Component\Console\Attribute\AsCommand;
4142
use Symfony\Component\Console\Command\Command;
42-
use Symfony\Component\Console\Input\InputInterface;
43-
use Symfony\Component\Console\Output\OutputInterface;
43+
use Symfony\Component\Console\Style\SymfonyStyle;
4444

45-
class YourCommand extends Command
45+
#[AsCommand(name: 'app:my-command')]
46+
class MyCommand
4647
{
4748
public function __construct(
4849
private LoggerInterface $logger,
4950
) {
50-
parent::__construct();
5151
}
5252

53-
protected function execute(InputInterface $input, OutputInterface $output): int
53+
public function __invoke(SymfonyStyle $io): int
5454
{
5555
$this->logger->debug('Some info');
5656
$this->logger->notice('Some more info');

‎routing.rst

+9-9
Original file line numberDiff line numberDiff line change
@@ -2484,23 +2484,23 @@ The solution is to configure the ``default_uri`` option to define the
24842484
24852485
Now you'll get the expected results when generating URLs in your commands::
24862486

2487-
// src/Command/SomeCommand.php
2487+
// src/Command/MyCommand.php
24882488
namespace App\Command;
24892489

2490-
use Symfony\Component\Console\Command\Command;
2491-
use Symfony\Component\Console\Input\InputInterface;
2492-
use Symfony\Component\Console\Output\OutputInterface;
2490+
use Symfony\Component\Console\Attribute\AsCommand;
2491+
use Symfony\Component\Console\Style\SymfonyStyle;
24932492
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
24942493
// ...
24952494

2496-
class SomeCommand extends Command
2495+
#[AsCommand(name: 'app:my-command')]
2496+
class MyCommand
24972497
{
2498-
public function __construct(private UrlGeneratorInterface $urlGenerator)
2499-
{
2500-
parent::__construct();
2498+
public function __construct(
2499+
private UrlGeneratorInterface $urlGenerator,
2500+
) {
25012501
}
25022502

2503-
protected function execute(InputInterface $input, OutputInterface $output): int
2503+
public function __invoke(SymfonyStyle $io): int
25042504
{
25052505
// generate a URL with no route arguments
25062506
$signUpPage = $this->urlGenerator->generate('sign_up');

‎scheduler.rst

+4-2
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,8 @@ The attribute takes more parameters to customize the trigger::
478478
// when applying this attribute to a Symfony console command, you can pass
479479
// arguments and options to the command using the 'arguments' option:
480480
#[AsCronTask('0 0 * * *', arguments: 'some_argument --some-option --another-option=some_value')]
481-
class MyCommand extends Command
481+
#[AsCommand(name: 'app:my-command')]
482+
class MyCommand
482483

483484
.. _scheduler-attributes-periodic-task:
484485

@@ -527,7 +528,8 @@ The ``#[AsPeriodicTask]`` attribute takes many parameters to customize the trigg
527528
// when applying this attribute to a Symfony console command, you can pass
528529
// arguments and options to the command using the 'arguments' option:
529530
#[AsPeriodicTask(frequency: '1 day', arguments: 'some_argument --some-option --another-option=some_value')]
530-
class MyCommand extends Command
531+
#[AsCommand(name: 'app:my-command')]
532+
class MyCommand
531533

532534
Managing Scheduled Messages
533535
---------------------------

0 commit comments

Comments
 (0)
Please sign in to comment.