Skip to content

Commit 270ebc7

Browse files
committed
Tweaked the Console article
1 parent fe05d1a commit 270ebc7

File tree

3 files changed

+65
-86
lines changed

3 files changed

+65
-86
lines changed

components/console.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ You can install the component in 2 different ways:
2222

2323
.. include:: /components/require_autoload.rst.inc
2424

25-
Creating an Application File
26-
----------------------------
25+
Creating a Console Application
26+
------------------------------
2727

28-
At first, you need to create a file to run commands from the console::
28+
First, you need to create a PHP script to define the console application::
2929

3030
#!/usr/bin/env php
3131
<?php
@@ -41,13 +41,14 @@ At first, you need to create a file to run commands from the console::
4141

4242
$application->run();
4343

44-
See the :doc:`/console` article for information about how to create commands.
45-
You can register the commands using
44+
Then, you can register the commands using
4645
:method:`Symfony\\Component\\Console\\Application::add`::
4746

4847
// ...
4948
$application->add(new GenerateAdminCommand());
5049

50+
See the :doc:`/console` article for information about how to create commands.
51+
5152
Learn More
5253
----------
5354

console.rst

Lines changed: 56 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,27 @@ Console Commands
55
================
66

77
The 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

1212
Creating 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)::
4541
Configuring 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

7564
Executing 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

10896
Now, 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:
164152
Getting 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`
170158
instead::
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

197179
Now, 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

201183
Command Lifecycle
202184
-----------------
@@ -230,21 +212,21 @@ useful one is the :class:`Symfony\\Component\\Console\\Tester\\CommandTester`
230212
class. It uses special input and output classes to ease testing without a real
231213
console::
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(),

console/calling_commands.rst

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ returns the returned code from the command (return value from command's
5050

5151
.. note::
5252

53-
Most of the time, calling a command from code that is not executed on the
54-
command line is not a good idea for several reasons. First, the command's
55-
output is optimized for the console. But more important, you can think of
56-
a command as being like a controller; it should use the model to do
57-
something and display feedback to the user. So, instead of calling a
58-
command from the Web, refactor your code and move the logic to a new
59-
class.
53+
Most of the times, calling a command from code that is not executed on the
54+
command line is not a good idea. The main reason is that the command's
55+
output is optimized for the console and not to be passed to other commands.

0 commit comments

Comments
 (0)