Skip to content

Commit 57938a5

Browse files
kixweaverryan
authored andcommitted
[Console] Added a cookbook entry on invoking other commands
1 parent eb1ee92 commit 57938a5

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

cookbook/console/console_command.rst

+34
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,40 @@ before translating contents::
150150
However for other services the solution might be more complex. For more details,
151151
see :doc:`/cookbook/service_container/scopes`.
152152

153+
Invoking Other Commands
154+
-----------------------
155+
156+
If you need to implement a command that runs other dependent commands, you can fetch
157+
these commands using the :class:`Symfony\\Component\\Console\\Application <Symfony\\Component\\Console\\Application>`'s ``find`` method.
158+
159+
Also note that you'll have to pass :class:`Symfony\\Component\\Console\\Input\\InputInterface` and :class:`Symfony\\Component\\Console\\Output\\OutputInterface`
160+
as arguments to the command's ``execute`` method. This can be easily implemented
161+
with :class:`Symfony\\Component\\Console\\Input\\ArrayInput`::
162+
163+
protected function execute(InputInterface $input, OutputInterface $output)
164+
{
165+
$command = $this->getApplication()->find('some:command');
166+
$command->execute(
167+
new ArrayInput(array(
168+
'foo' => 'foo',
169+
'--bar' => 'foobar',
170+
)),
171+
$output
172+
);
173+
}
174+
175+
.. tip::
176+
177+
If you want to suppress the output of the executed command, pass a :class:`Symfony\\Component\\Console\\Output\\NullOutput`
178+
as the second argument to ``$command->execute()``.
179+
180+
.. caution::
181+
182+
Note that all these commands will run in the same process, and some of Symfony's
183+
built-in commands may not work well this way. For instance, ``cache:clear`` and ``cache:warmup``
184+
commands change some class definitions, so running something
185+
after them is likely to break.
186+
153187
Testing Commands
154188
----------------
155189

0 commit comments

Comments
 (0)