@@ -7,18 +7,18 @@ questions to the user involves a lot of repetitive code.
7
7
8
8
Consider for example the code used to display the title of the following command::
9
9
10
- // src/Command/GreetCommand .php
10
+ // src/Command/MyCommand .php
11
11
namespace App\Command;
12
12
13
+ use Symfony\Component\Console\Attribute\AsCommand;
13
14
use Symfony\Component\Console\Command\Command;
14
15
use Symfony\Component\Console\Input\InputInterface;
15
16
use Symfony\Component\Console\Output\OutputInterface;
16
17
17
- class GreetCommand extends Command
18
+ #[AsCommand(name: 'app:my-command')]
19
+ class MyCommand
18
20
{
19
- // ...
20
-
21
- protected function execute(InputInterface $input, OutputInterface $output): int
21
+ public function __invoke(InputInterface $input, OutputInterface $output): int
22
22
{
23
23
$output->writeln([
24
24
'<info>Lorem Ipsum Dolor Sit Amet</>',
@@ -42,26 +42,22 @@ which allow to create *semantic* commands and forget about their styling.
42
42
Basic Usage
43
43
-----------
44
44
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::
49
48
50
- // src/Command/GreetCommand .php
49
+ // src/Command/MyCommand .php
51
50
namespace App\Command;
52
51
52
+ use Symfony\Component\Console\Attribute\AsCommand;
53
53
use Symfony\Component\Console\Command\Command;
54
- use Symfony\Component\Console\Input\InputInterface;
55
- use Symfony\Component\Console\Output\OutputInterface;
56
54
use Symfony\Component\Console\Style\SymfonyStyle;
57
55
58
- class GreetCommand extends Command
56
+ #[AsCommand(name: 'app:my-command')]
57
+ class MyCommand
59
58
{
60
- // ...
61
-
62
- protected function execute(InputInterface $input, OutputInterface $output): int
59
+ public function __invoke(SymfonyStyle $io): int
63
60
{
64
- $io = new SymfonyStyle($input, $output);
65
61
$io->title('Lorem Ipsum Dolor Sit Amet');
66
62
67
63
// ...
@@ -448,19 +444,17 @@ long they are. This is done to enable clickable URLs in terminals that support t
448
444
449
445
If you prefer to wrap all contents, including URLs, use this method::
450
446
451
- // src/Command/GreetCommand .php
447
+ // src/Command/MyCommand .php
452
448
namespace App\Command;
453
449
454
450
// ...
455
451
use Symfony\Component\Console\Style\SymfonyStyle;
456
452
457
- class GreetCommand extends Command
453
+ #[AsCommand(name: 'app:my-command')]
454
+ class MyCommand
458
455
{
459
- // ...
460
-
461
- protected function execute(InputInterface $input, OutputInterface $output): int
456
+ public function __invoke(SymfonyStyle $io): int
462
457
{
463
- $io = new SymfonyStyle($input, $output);
464
458
$io->getOutputWrapper()->setAllowCutUrls(true);
465
459
466
460
// ...
@@ -487,24 +481,19 @@ Then, instantiate this custom class instead of the default ``SymfonyStyle`` in
487
481
your commands. Thanks to the ``StyleInterface `` you won't need to change the code
488
482
of your commands to change their appearance::
489
483
490
- // src/Command/GreetCommand .php
484
+ // src/Command/MyCommand .php
491
485
namespace App\Console;
492
486
493
487
use App\Console\CustomStyle;
494
488
use Symfony\Component\Console\Command\Command;
495
489
use Symfony\Component\Console\Input\InputInterface;
496
490
use Symfony\Component\Console\Output\OutputInterface;
497
491
498
- class GreetCommand extends Command
492
+ #[AsCommand(name: 'app:my-command')]
493
+ class MyCommand
499
494
{
500
- // ...
501
-
502
- protected function execute(InputInterface $input, OutputInterface $output): int
495
+ public function __invoke(InputInterface $input, OutputInterface $output): int
503
496
{
504
- // Before
505
- $io = new SymfonyStyle($input, $output);
506
-
507
- // After
508
497
$io = new CustomStyle($input, $output);
509
498
510
499
// ...
0 commit comments