@@ -5,31 +5,27 @@ Console Commands
5
5
================
6
6
7
7
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 .
11
11
12
12
Creating a Command
13
13
------------------
14
14
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 .
18
18
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::
21
20
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
26
22
namespace AppBundle\Command;
27
23
28
24
use Symfony\Component\Console\Command\Command;
29
25
use Symfony\Component\Console\Input\InputInterface;
30
26
use Symfony\Component\Console\Output\OutputInterface;
31
27
32
- class GenerateAdminCommand extends Command
28
+ class CreateUserCommand extends Command
33
29
{
34
30
protected function configure()
35
31
{
@@ -45,76 +41,68 @@ learn about the methods soon)::
45
41
Configuring the Command
46
42
-----------------------
47
43
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 >`::
53
47
54
48
// ...
55
49
protected function configure()
56
50
{
57
51
$this
58
52
// 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')
63
54
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.')
68
57
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..." )
72
61
;
73
62
}
74
63
75
64
Executing the Command
76
65
---------------------
77
66
78
- After configuring, you can execute the command in the terminal:
67
+ After configuring the command , you can execute it in the terminal:
79
68
80
69
.. code-block :: bash
81
70
82
- $ php app/console app:generate-admin
71
+ $ php app/console app:create-users
83
72
84
73
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)::
88
77
89
78
// ...
90
79
protected function execute(InputInterface $input, OutputInterface $output)
91
80
{
92
- // outputs multiple lines to the console
81
+ // outputs multiple lines to the console (adding "\n" at the end of each line)
93
82
$output->writeln([
94
- 'Admin Generator ',
95
- '=============== ',
83
+ 'User Creator ',
84
+ '============',
96
85
'',
97
86
]);
98
87
99
- // output a single line
88
+ // outputs a message followed by a "\n"
100
89
$output->writeln('Whoa!');
101
90
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
104
92
$output->write('You are about to ');
105
- $output->write('generate an admin user.');
93
+ $output->write('create a user.');
106
94
}
107
95
108
96
Now, try executing the command:
109
97
110
98
.. code-block :: bash
111
99
112
- $ php app/console app:generate-admin
113
- Admin Generator
114
- ===============
100
+ $ php app/console app:create-user
101
+ User Creator
102
+ ============
115
103
116
104
Whoa!
117
- You are about to generate an admin user.
105
+ You are about to create a user.
118
106
119
107
Console Input
120
108
-------------
@@ -128,7 +116,7 @@ Use input options or arguments to pass information to the command::
128
116
{
129
117
$this
130
118
// configure an argument
131
- ->addArgument('username', InputArgument::REQUIRED, 'The username of the admin .')
119
+ ->addArgument('username', InputArgument::REQUIRED, 'The username of the user .')
132
120
// ...
133
121
;
134
122
}
@@ -137,8 +125,8 @@ Use input options or arguments to pass information to the command::
137
125
public function execute(InputInterface $input, OutputInterface $output)
138
126
{
139
127
$output->writeln([
140
- 'Admin Generator ',
141
- '=============== ',
128
+ 'User Creator ',
129
+ '============',
142
130
'',
143
131
]);
144
132
@@ -150,9 +138,9 @@ Now, you can pass the username to the command:
150
138
151
139
.. code-block :: bash
152
140
153
- $ php app/console app:generate-admin Wouter
154
- Admin Generator
155
- ===============
141
+ $ php app/console app:create-user Wouter
142
+ User Creator
143
+ ============
156
144
157
145
Username: Wouter
158
146
@@ -164,15 +152,15 @@ Now, you can pass the username to the command:
164
152
Getting Services from the Service Container
165
153
-------------------------------------------
166
154
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 `
170
158
instead::
171
159
172
160
// ...
173
161
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
174
162
175
- class GenerateAdminCommand extends ContainerAwareCommand
163
+ class CreateUserCommand extends ContainerAwareCommand
176
164
{
177
165
// ...
178
166
@@ -181,22 +169,16 @@ instead::
181
169
// ...
182
170
183
171
// 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'));
192
174
193
- $output->writeln('Admin successfully generated!');
175
+ $output->writeln('User successfully generated!');
194
176
}
195
177
}
196
178
197
179
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.
200
182
201
183
Command Lifecycle
202
184
-----------------
@@ -230,21 +212,21 @@ useful one is the :class:`Symfony\\Component\\Console\\Tester\\CommandTester`
230
212
class. It uses special input and output classes to ease testing without a real
231
213
console::
232
214
233
- // tests/AppBundle/Command/GenerateAdminCommandTest .php
215
+ // tests/AppBundle/Command/CreateUserCommandTest .php
234
216
namespace Tests\AppBundle\Command;
235
217
236
- use AppBundle\Command\GenerateAdminCommand ;
218
+ use AppBundle\Command\CreateUserCommand ;
237
219
use Symfony\Bundle\FrameworkBundle\Console\Application;
238
220
use Symfony\Component\Console\Tester\CommandTester;
239
221
240
- class GenerateAdminCommandTest extends \PHPUnit_Framework_TestCase
222
+ class CreateUserCommandTest extends \PHPUnit_Framework_TestCase
241
223
{
242
224
public function testExecute()
243
225
{
244
226
$application = new Application();
245
- $application->add(new GenerateAdminCommand ());
227
+ $application->add(new CreateUserCommand ());
246
228
247
- $command = $application->find('app:generate-admin ');
229
+ $command = $application->find('app:create-user ');
248
230
$commandTester = new CommandTester($command);
249
231
$commandTester->execute(array(
250
232
'command' => $command->getName(),
@@ -285,17 +267,17 @@ you can extend your test from
285
267
use Symfony\Bundle\FrameworkBundle\Console\Application;
286
268
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
287
269
288
- class GenerateAdminCommandTest extends KernelTestCase
270
+ class CreateUserCommandTest extends KernelTestCase
289
271
{
290
272
public function testExecute()
291
273
{
292
274
$kernel = $this->createKernel();
293
275
$kernel->boot();
294
276
295
277
$application = new Application($kernel);
296
- $application->add(new GenerateAdminCommand ());
278
+ $application->add(new CreateUserCommand ());
297
279
298
- $command = $application->find('app:generate-admin ');
280
+ $command = $application->find('app:create-user ');
299
281
$commandTester = new CommandTester($command);
300
282
$commandTester->execute(array(
301
283
'command' => $command->getName(),
0 commit comments