Skip to content

Commit 53f1dd1

Browse files
committed
Move Calling other commands to a guide article
1 parent 038a045 commit 53f1dd1

File tree

3 files changed

+59
-68
lines changed

3 files changed

+59
-68
lines changed

components/console.rst

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -192,68 +192,6 @@ method::
192192
You can also test a whole console application by using
193193
:class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`.
194194

195-
.. _calling-existing-command:
196-
197-
Calling an Existing Command
198-
---------------------------
199-
200-
If a command depends on another one being run before it, instead of asking the
201-
user to remember the order of execution, you can call it directly yourself.
202-
This is also useful if you want to create a "meta" command that just runs a
203-
bunch of other commands (for instance, all commands that need to be run when
204-
the project's code has changed on the production servers: clearing the cache,
205-
generating Doctrine2 proxies, dumping Assetic assets, ...).
206-
207-
Calling a command from another one is straightforward::
208-
209-
protected function execute(InputInterface $input, OutputInterface $output)
210-
{
211-
$command = $this->getApplication()->find('demo:greet');
212-
213-
$arguments = array(
214-
'command' => 'demo:greet',
215-
'name' => 'Fabien',
216-
'--yell' => true,
217-
);
218-
219-
$greetInput = new ArrayInput($arguments);
220-
$returnCode = $command->run($greetInput, $output);
221-
222-
// ...
223-
}
224-
225-
First, you :method:`Symfony\\Component\\Console\\Application::find` the
226-
command you want to execute by passing the command name. Then, you need to create
227-
a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments
228-
and options you want to pass to the command.
229-
230-
Eventually, calling the ``run()`` method actually executes the command and
231-
returns the returned code from the command (return value from command's
232-
``execute()`` method).
233-
234-
.. tip::
235-
236-
If you want to suppress the output of the executed command, pass a
237-
:class:`Symfony\\Component\\Console\\Output\\NullOutput` as the second
238-
argument to ``$command->run()``.
239-
240-
.. caution::
241-
242-
Note that all the commands will run in the same process and some of Symfony's
243-
built-in commands may not work well this way. For instance, the ``cache:clear``
244-
and ``cache:warmup`` commands change some class definitions, so running
245-
something after them is likely to break.
246-
247-
.. note::
248-
249-
Most of the time, calling a command from code that is not executed on the
250-
command line is not a good idea for several reasons. First, the command's
251-
output is optimized for the console. But more important, you can think of
252-
a command as being like a controller; it should use the model to do
253-
something and display feedback to the user. So, instead of calling a
254-
command from the Web, refactor your code and move the logic to a new
255-
class.
256-
257195
Learn More
258196
----------
259197

console.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,6 @@ command:
220220
This method is executed after ``interact()`` and ``initialize()``.
221221
It contains the logic you want the command to execute.
222222
223-
Invoking other Commands
224-
-----------------------
225-
226-
See :ref:`calling-existing-command` if you need to implement a command that runs
227-
other dependent commands.
228-
229223
Testing Commands
230224
----------------
231225

console/calling_commands.rst

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
How to Call Other Commands
2+
==========================
3+
4+
If a command depends on another one being run before it, instead of asking the
5+
user to remember the order of execution, you can call it directly yourself.
6+
This is also useful if you want to create a "meta" command that just runs a
7+
bunch of other commands (for instance, all commands that need to be run when
8+
the project's code has changed on the production servers: clearing the cache,
9+
generating Doctrine2 proxies, dumping Assetic assets, ...).
10+
11+
Calling a command from another one is straightforward::
12+
13+
protected function execute(InputInterface $input, OutputInterface $output)
14+
{
15+
$command = $this->getApplication()->find('demo:greet');
16+
17+
$arguments = array(
18+
'command' => 'demo:greet',
19+
'name' => 'Fabien',
20+
'--yell' => true,
21+
);
22+
23+
$greetInput = new ArrayInput($arguments);
24+
$returnCode = $command->run($greetInput, $output);
25+
26+
// ...
27+
}
28+
29+
First, you :method:`Symfony\\Component\\Console\\Application::find` the
30+
command you want to execute by passing the command name. Then, you need to create
31+
a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments
32+
and options you want to pass to the command.
33+
34+
Eventually, calling the ``run()`` method actually executes the command and
35+
returns the returned code from the command (return value from command's
36+
``execute()`` method).
37+
38+
.. tip::
39+
40+
If you want to suppress the output of the executed command, pass a
41+
:class:`Symfony\\Component\\Console\\Output\\NullOutput` as the second
42+
argument to ``$command->run()``.
43+
44+
.. caution::
45+
46+
Note that all the commands will run in the same process and some of Symfony's
47+
built-in commands may not work well this way. For instance, the ``cache:clear``
48+
and ``cache:warmup`` commands change some class definitions, so running
49+
something after them is likely to break.
50+
51+
.. note::
52+
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.

0 commit comments

Comments
 (0)