@@ -110,23 +110,19 @@ completion (by default, by pressing the Tab key).
110110Creating 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+
150150Configuring 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
0 commit comments