Skip to content

Commit b56ae75

Browse files
committed
Merge remote-tracking branch 'sensio/2.7' into 2.7
* sensio/2.7: Add file extension to SOAP article Rename cache/ to http_cache/ Rewords Tweaked the Console article Fix build Remove all other duplication from components article Move Testing commands to the guide Move Console helper links to guide Move `Calling other commands` to a guide article Move Command Lifecycle to guide Rewrote Console Commands introduction
2 parents 5c5e3a0 + d02479e commit b56ae75

19 files changed

+394
-442
lines changed

components/console.rst

Lines changed: 13 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -22,260 +22,32 @@ You can install the component in 2 different ways:
2222

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

25-
Creating a basic Command
26-
------------------------
25+
Creating a Console Application
26+
------------------------------
2727

28-
To make a console command that greets you from the command line, create ``GreetCommand.php``
29-
and add the following to it::
30-
31-
namespace Acme\Console\Command;
32-
33-
use Symfony\Component\Console\Command\Command;
34-
use Symfony\Component\Console\Input\InputArgument;
35-
use Symfony\Component\Console\Input\InputInterface;
36-
use Symfony\Component\Console\Input\InputOption;
37-
use Symfony\Component\Console\Output\OutputInterface;
38-
39-
class GreetCommand extends Command
40-
{
41-
protected function configure()
42-
{
43-
$this
44-
->setName('demo:greet')
45-
->setDescription('Greet someone')
46-
->addArgument(
47-
'name',
48-
InputArgument::OPTIONAL,
49-
'Who do you want to greet?'
50-
)
51-
->addOption(
52-
'yell',
53-
null,
54-
InputOption::VALUE_NONE,
55-
'If set, the task will yell in uppercase letters'
56-
)
57-
;
58-
}
59-
60-
protected function execute(InputInterface $input, OutputInterface $output)
61-
{
62-
$name = $input->getArgument('name');
63-
if ($name) {
64-
$text = 'Hello '.$name;
65-
} else {
66-
$text = 'Hello';
67-
}
68-
69-
if ($input->getOption('yell')) {
70-
$text = strtoupper($text);
71-
}
72-
73-
$output->writeln($text);
74-
}
75-
}
76-
77-
You also need to create the file to run at the command line which creates
78-
an ``Application`` and adds commands to it::
28+
First, you need to create a PHP script to define the console application::
7929

8030
#!/usr/bin/env php
8131
<?php
8232
// application.php
8333

8434
require __DIR__.'/vendor/autoload.php';
8535

86-
use Acme\Console\Command\GreetCommand;
8736
use Symfony\Component\Console\Application;
8837

8938
$application = new Application();
90-
$application->add(new GreetCommand());
91-
$application->run();
92-
93-
Test the new console command by running the following
94-
95-
.. code-block:: bash
96-
97-
$ php application.php demo:greet Fabien
98-
99-
This will print the following to the command line:
100-
101-
.. code-block:: text
102-
103-
Hello Fabien
104-
105-
You can also use the ``--yell`` option to make everything uppercase:
106-
107-
.. code-block:: bash
108-
109-
$ php application.php demo:greet Fabien --yell
110-
111-
This prints::
112-
113-
HELLO FABIEN
114-
115-
Command Lifecycle
116-
~~~~~~~~~~~~~~~~~
117-
118-
Commands have three lifecycle methods:
119-
120-
:method:`Symfony\\Component\\Console\\Command\\Command::initialize` *(optional)*
121-
This method is executed before the ``interact()`` and the ``execute()``
122-
methods. Its main purpose is to initialize variables used in the rest of
123-
the command methods.
124-
125-
:method:`Symfony\\Component\\Console\\Command\\Command::interact` *(optional)*
126-
This method is executed after ``initialize()`` and before ``execute()``.
127-
Its purpose is to check if some of the options/arguments are missing
128-
and interactively ask the user for those values. This is the last place
129-
where you can ask for missing options/arguments. After this command,
130-
missing options/arguments will result in an error.
131-
132-
:method:`Symfony\\Component\\Console\\Command\\Command::execute` *(required)*
133-
This method is executed after ``interact()`` and ``initialize()``.
134-
It contains the logic you want the command to execute.
135-
136-
137-
138-
Console Helpers
139-
---------------
140-
141-
The console component also contains a set of "helpers" - different small
142-
tools capable of helping you with different tasks:
143-
144-
* :doc:`/components/console/helpers/questionhelper`: interactively ask the user for information
145-
* :doc:`/components/console/helpers/formatterhelper`: customize the output colorization
146-
* :doc:`/components/console/helpers/progressbar`: shows a progress bar
147-
* :doc:`/components/console/helpers/table`: displays tabular data as a table
148-
149-
.. _component-console-testing-commands:
15039

151-
Testing Commands
152-
----------------
40+
// ... register commands
15341

154-
Symfony provides several tools to help you test your commands. The most
155-
useful one is the :class:`Symfony\\Component\\Console\\Tester\\CommandTester`
156-
class. It uses special input and output classes to ease testing without a real
157-
console::
158-
159-
use Acme\Console\Command\GreetCommand;
160-
use Symfony\Component\Console\Application;
161-
use Symfony\Component\Console\Tester\CommandTester;
162-
163-
class ListCommandTest extends \PHPUnit_Framework_TestCase
164-
{
165-
public function testExecute()
166-
{
167-
$application = new Application();
168-
$application->add(new GreetCommand());
169-
170-
$command = $application->find('demo:greet');
171-
$commandTester = new CommandTester($command);
172-
$commandTester->execute(array('command' => $command->getName()));
173-
174-
$this->assertRegExp('/.../', $commandTester->getDisplay());
175-
176-
// ...
177-
}
178-
}
179-
180-
The :method:`Symfony\\Component\\Console\\Tester\\CommandTester::getDisplay`
181-
method returns what would have been displayed during a normal call from the
182-
console.
183-
184-
You can test sending arguments and options to the command by passing them
185-
as an array to the :method:`Symfony\\Component\\Console\\Tester\\CommandTester::execute`
186-
method::
187-
188-
use Acme\Console\Command\GreetCommand;
189-
use Symfony\Component\Console\Application;
190-
use Symfony\Component\Console\Tester\CommandTester;
191-
192-
class ListCommandTest extends \PHPUnit_Framework_TestCase
193-
{
194-
// ...
195-
196-
public function testNameIsOutput()
197-
{
198-
$application = new Application();
199-
$application->add(new GreetCommand());
200-
201-
$command = $application->find('demo:greet');
202-
$commandTester = new CommandTester($command);
203-
$commandTester->execute(array(
204-
'command' => $command->getName(),
205-
'name' => 'Fabien',
206-
'--iterations' => 5,
207-
));
208-
209-
$this->assertRegExp('/Fabien/', $commandTester->getDisplay());
210-
}
211-
}
212-
213-
.. tip::
214-
215-
You can also test a whole console application by using
216-
:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
217-
218-
.. _calling-existing-command:
219-
220-
Calling an Existing Command
221-
---------------------------
222-
223-
If a command depends on another one being run before it, instead of asking the
224-
user to remember the order of execution, you can call it directly yourself.
225-
This is also useful if you want to create a "meta" command that just runs a
226-
bunch of other commands (for instance, all commands that need to be run when
227-
the project's code has changed on the production servers: clearing the cache,
228-
generating Doctrine2 proxies, dumping Assetic assets, ...).
229-
230-
Calling a command from another one is straightforward::
231-
232-
protected function execute(InputInterface $input, OutputInterface $output)
233-
{
234-
$command = $this->getApplication()->find('demo:greet');
235-
236-
$arguments = array(
237-
'command' => 'demo:greet',
238-
'name' => 'Fabien',
239-
'--yell' => true,
240-
);
241-
242-
$greetInput = new ArrayInput($arguments);
243-
$returnCode = $command->run($greetInput, $output);
244-
245-
// ...
246-
}
247-
248-
First, you :method:`Symfony\\Component\\Console\\Application::find` the
249-
command you want to execute by passing the command name. Then, you need to create
250-
a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments
251-
and options you want to pass to the command.
252-
253-
Eventually, calling the ``run()`` method actually executes the command and
254-
returns the returned code from the command (return value from command's
255-
``execute()`` method).
256-
257-
.. tip::
258-
259-
If you want to suppress the output of the executed command, pass a
260-
:class:`Symfony\\Component\\Console\\Output\\NullOutput` as the second
261-
argument to ``$command->run()``.
262-
263-
.. caution::
42+
$application->run();
26443

265-
Note that all the commands will run in the same process and some of Symfony's
266-
built-in commands may not work well this way. For instance, the ``cache:clear``
267-
and ``cache:warmup`` commands change some class definitions, so running
268-
something after them is likely to break.
44+
Then, you can register the commands using
45+
:method:`Symfony\\Component\\Console\\Application::add`::
26946

270-
.. note::
47+
// ...
48+
$application->add(new GenerateAdminCommand());
27149

272-
Most of the time, calling a command from code that is not executed on the
273-
command line is not a good idea for several reasons. First, the command's
274-
output is optimized for the console. But more important, you can think of
275-
a command as being like a controller; it should use the model to do
276-
something and display feedback to the user. So, instead of calling a
277-
command from the Web, refactor your code and move the logic to a new
278-
class.
50+
See the :doc:`/console` article for information about how to create commands.
27951

28052
Learn More
28153
----------
@@ -284,7 +56,8 @@ Learn More
28456
:maxdepth: 1
28557
:glob:
28658

287-
console/*
288-
console/helpers/index
59+
/console
60+
/components/console/*
61+
/components/console/helpers/index
28962

29063
.. _Packagist: https://packagist.org/packages/symfony/console

components/console/helpers/dialoghelper.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,4 +288,4 @@ input stream.
288288
.. seealso::
289289

290290
You find more information about testing commands in the console component
291-
docs about :ref:`testing console commands <component-console-testing-commands>`.
291+
docs about :ref:`testing console commands <console-testing-commands>`.

0 commit comments

Comments
 (0)