@@ -5,31 +5,27 @@ Console Commands
55================
66
77The Symfony framework provide lots of commands through the ``app/console `` file
8- (e.g. the common ``app/console cache:clear `` command). These commands are
9- created using the :doc: `Console component </components/console >`. This allows
10- you to add custom commands as well, for instance to manage admin users .
8+ (e.g. the well-known ``app/console cache:clear `` command). These commands are
9+ created with the :doc: `Console component </components/console >` and you can
10+ also use it to create your own commands .
1111
1212Creating a Command
1313------------------
1414
15- Each command will have its own command class that manages the logic. It serves
16- as a controller for the console, except that it doesn't work with the HTTP
17- Request/Response flow, but with Input/Output streams .
15+ Commands are defined in classes which must be created in the `` Command `` namespace
16+ of your bundle (e.g. `` AppBundle\Command ``) and their names must end with the
17+ `` Command `` suffix .
1818
19- Your command has to be in the ``Command `` namespace of your bundle (e.g.
20- ``AppBundle\Command ``) and the name has to end with ``Command ``.
19+ For example, a command called ``CreateUser `` must follow this structure::
2120
22- For instance, assume you create a command to generate new admin users (you'll
23- learn about the methods soon)::
24-
25- // src/AppBundle/Command/GenerateAdminCommand.php
21+ // src/AppBundle/Command/CreateUserCommand.php
2622 namespace AppBundle\Command;
2723
2824 use Symfony\Component\Console\Command\Command;
2925 use Symfony\Component\Console\Input\InputInterface;
3026 use Symfony\Component\Console\Output\OutputInterface;
3127
32- class GenerateAdminCommand extends Command
28+ class CreateUserCommand extends Command
3329 {
3430 protected function configure()
3531 {
@@ -45,76 +41,68 @@ learn about the methods soon)::
4541Configuring the Command
4642-----------------------
4743
48- First of all, you need to configure the name of the command in ``configure() ``.
49- Besides the name, you can configure things like the help message and
50- :doc: `input options and arguments </console/input >`.
51-
52- ::
44+ First of all, you must configure the name of the command in the ``configure() ``
45+ method. Then you can optionally define a help message and the
46+ :doc: `input options and arguments </console/input >`::
5347
5448 // ...
5549 protected function configure()
5650 {
5751 $this
5852 // the name of the command (the part after "app/console")
59- ->setName('app:generate-admin')
60-
61- // the shot description shown while running "php app/console list"
62- ->setDescription('Generates new admin users.')
53+ ->setName('app:create-users')
6354
64- // the help message shown when running the command with the
65- // "--help" option
66- ->setHelp(<<<EOT
67- This command allows you to generate admins.
55+ // the short description shown while running "php app/console list"
56+ ->setDescription('Creates new users.')
6857
69- ...
70- EOT
71- )
58+ // the full command description shown when running the command with
59+ // the "--help" option
60+ ->setHelp("This command allows you to create users..." )
7261 ;
7362 }
7463
7564Executing the Command
7665---------------------
7766
78- After configuring, you can execute the command in the terminal:
67+ After configuring the command , you can execute it in the terminal:
7968
8069.. code-block :: bash
8170
82- $ php app/console app:generate-admin
71+ $ php app/console app:create-users
8372
8473 As you might expect, this command will do nothing as you didn't write any logic
85- yet. When running the command, the ``execute() `` method will be executed. This
86- method has access to the input stream (e.g. options and arguments) and the
87- output stream (to write messages to the console)::
74+ yet. Add your own logic inside the ``execute() `` method, which has access to the
75+ input stream (e.g. options and arguments) and the output stream (to write
76+ messages to the console)::
8877
8978 // ...
9079 protected function execute(InputInterface $input, OutputInterface $output)
9180 {
92- // outputs multiple lines to the console
81+ // outputs multiple lines to the console (adding "\n" at the end of each line)
9382 $output->writeln([
94- 'Admin Generator ',
95- '=============== ',
83+ 'User Creator ',
84+ '============',
9685 '',
9786 ]);
9887
99- // output a single line
88+ // outputs a message followed by a "\n"
10089 $output->writeln('Whoa!');
10190
102- // output a message without moving to a new line (the message will
103- // apear on one line)
91+ // outputs a message without adding a "\n" at the end of the line
10492 $output->write('You are about to ');
105- $output->write('generate an admin user.');
93+ $output->write('create a user.');
10694 }
10795
10896Now, try executing the command:
10997
11098.. code-block :: bash
11199
112- $ php app/console app:generate-admin
113- Admin Generator
114- ===============
100+ $ php app/console app:create-user
101+ User Creator
102+ ============
115103
116104 Whoa!
117- You are about to generate an admin user.
105+ You are about to create a user.
118106
119107 Console Input
120108-------------
@@ -128,7 +116,7 @@ Use input options or arguments to pass information to the command::
128116 {
129117 $this
130118 // configure an argument
131- ->addArgument('username', InputArgument::REQUIRED, 'The username of the admin .')
119+ ->addArgument('username', InputArgument::REQUIRED, 'The username of the user .')
132120 // ...
133121 ;
134122 }
@@ -137,8 +125,8 @@ Use input options or arguments to pass information to the command::
137125 public function execute(InputInterface $input, OutputInterface $output)
138126 {
139127 $output->writeln([
140- 'Admin Generator ',
141- '=============== ',
128+ 'User Creator ',
129+ '============',
142130 '',
143131 ]);
144132
@@ -150,9 +138,9 @@ Now, you can pass the username to the command:
150138
151139.. code-block :: bash
152140
153- $ php app/console app:generate-admin Wouter
154- Admin Generator
155- ===============
141+ $ php app/console app:create-user Wouter
142+ User Creator
143+ ============
156144
157145 Username: Wouter
158146
@@ -164,15 +152,15 @@ Now, you can pass the username to the command:
164152Getting Services from the Service Container
165153-------------------------------------------
166154
167- To actually generate a new admin user, the command has to access some
168- :doc: `services </service_container >`. This can be done by extending
169- :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Command\\ ContainerAwareCommand `
155+ To actually create a new user, the command has to access to some
156+ :doc: `services </service_container >`. This can be done by making the command
157+ extend the :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Command\\ ContainerAwareCommand `
170158instead::
171159
172160 // ...
173161 use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
174162
175- class GenerateAdminCommand extends ContainerAwareCommand
163+ class CreateUserCommand extends ContainerAwareCommand
176164 {
177165 // ...
178166
@@ -181,22 +169,16 @@ instead::
181169 // ...
182170
183171 // access the container using getContainer()
184- $adminGenerator = $this->getContainer()->get('app.admin_generator');
185-
186- $generatedPassword = md5(uniqid());
187-
188- $output->writeln('Generated password: '.$generatedPassword);
189-
190- // for instance, generate an admin like this
191- $adminGenerator->generate($input->getArgument('username'), $generatedPassword);
172+ $userManager = $this->getContainer()->get('app.user_manager');
173+ $userManager->create($input->getArgument('username'));
192174
193- $output->writeln('Admin successfully generated!');
175+ $output->writeln('User successfully generated!');
194176 }
195177 }
196178
197179Now, once you created the required services and logic, the command will execute
198- the ``generate() `` method of the ``app.admin_generator `` service and the admin
199- will be created.
180+ the ``generate() `` method of the ``app.user_manager `` service and the user will
181+ be created.
200182
201183Command Lifecycle
202184-----------------
@@ -230,21 +212,21 @@ useful one is the :class:`Symfony\\Component\\Console\\Tester\\CommandTester`
230212class. It uses special input and output classes to ease testing without a real
231213console::
232214
233- // tests/AppBundle/Command/GenerateAdminCommandTest .php
215+ // tests/AppBundle/Command/CreateUserCommandTest .php
234216 namespace Tests\AppBundle\Command;
235217
236- use AppBundle\Command\GenerateAdminCommand ;
218+ use AppBundle\Command\CreateUserCommand ;
237219 use Symfony\Bundle\FrameworkBundle\Console\Application;
238220 use Symfony\Component\Console\Tester\CommandTester;
239221
240- class GenerateAdminCommandTest extends \PHPUnit_Framework_TestCase
222+ class CreateUserCommandTest extends \PHPUnit_Framework_TestCase
241223 {
242224 public function testExecute()
243225 {
244226 $application = new Application();
245- $application->add(new GenerateAdminCommand ());
227+ $application->add(new CreateUserCommand ());
246228
247- $command = $application->find('app:generate-admin ');
229+ $command = $application->find('app:create-user ');
248230 $commandTester = new CommandTester($command);
249231 $commandTester->execute(array(
250232 'command' => $command->getName(),
@@ -285,17 +267,17 @@ you can extend your test from
285267 use Symfony\Bundle\FrameworkBundle\Console\Application;
286268 use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
287269
288- class GenerateAdminCommandTest extends KernelTestCase
270+ class CreateUserCommandTest extends KernelTestCase
289271 {
290272 public function testExecute()
291273 {
292274 $kernel = $this->createKernel();
293275 $kernel->boot();
294276
295277 $application = new Application($kernel);
296- $application->add(new GenerateAdminCommand ());
278+ $application->add(new CreateUserCommand ());
297279
298- $command = $application->find('app:generate-admin ');
280+ $command = $application->find('app:create-user ');
299281 $commandTester = new CommandTester($command);
300282 $commandTester->execute(array(
301283 'command' => $command->getName(),
0 commit comments