@@ -110,23 +110,19 @@ completion (by default, by pressing the Tab key).
|
110 | 110 | Creating a Command
|
111 | 111 | ------------------
|
112 | 112 |
|
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:: |
116 | 114 |
|
117 | 115 | // src/Command/CreateUserCommand.php
|
118 | 116 | namespace App\Command;
|
119 | 117 |
|
120 | 118 | use Symfony\Component\Console\Attribute\AsCommand;
|
121 | 119 | use Symfony\Component\Console\Command\Command;
|
122 |
| -use Symfony\Component\Console\Input\InputInterface; |
123 |
| -use Symfony\Component\Console\Output\OutputInterface; |
124 | 120 |
|
125 | 121 | // the name of the command is what users type after "php bin/console"
|
126 | 122 | #[AsCommand(name: 'app:create-user')]
|
127 |
| -class CreateUserCommand extends Command |
| 123 | +class CreateUserCommand |
128 | 124 | {
|
129 |
| -protected function execute(InputInterface $input, OutputInterface $output): int |
| 125 | +public function __invoke(): int |
130 | 126 | {
|
131 | 127 | // ... put here the code to create the user
|
132 | 128 |
|
@@ -147,6 +143,10 @@ want a command to create a user::
|
147 | 143 | }
|
148 | 144 | }
|
149 | 145 |
|
| 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 | + |
150 | 150 | Configuring the Command
|
151 | 151 | ~~~~~~~~~~~~~~~~~~~~~~~
|
152 | 152 |
|
@@ -156,18 +156,16 @@ You can optionally define a description, help message and the
|
156 | 156 |
|
157 | 157 | // src/Command/CreateUserCommand.php
|
158 | 158 |
|
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 |
161 | 165 | {
|
162 |
| -// ... |
163 |
| -protected function configure(): void |
| 166 | +public function __invoke(): int |
164 | 167 | {
|
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 | +// ... |
171 | 169 | }
|
172 | 170 | }
|
173 | 171 |
|
|
0 commit comments