Skip to content

Commit

Permalink
[Console] Added a cookbook entry on invoking other commands
Browse files Browse the repository at this point in the history
  • Loading branch information
kix authored and weaverryan committed Jul 7, 2015
1 parent eb1ee92 commit 57938a5
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cookbook/console/console_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,40 @@ before translating contents::
However for other services the solution might be more complex. For more details,
see :doc:`/cookbook/service_container/scopes`.

Invoking Other Commands
-----------------------

If you need to implement a command that runs other dependent commands, you can fetch
these commands using the :class:`Symfony\\Component\\Console\\Application <Symfony\\Component\\Console\\Application>`'s ``find`` method.

Also note that you'll have to pass :class:`Symfony\\Component\\Console\\Input\\InputInterface` and :class:`Symfony\\Component\\Console\\Output\\OutputInterface`
as arguments to the command's ``execute`` method. This can be easily implemented
with :class:`Symfony\\Component\\Console\\Input\\ArrayInput`::

protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('some:command');
$command->execute(
new ArrayInput(array(
'foo' => 'foo',
'--bar' => 'foobar',
)),
$output
);
}

.. tip::

If you want to suppress the output of the executed command, pass a :class:`Symfony\\Component\\Console\\Output\\NullOutput`
as the second argument to ``$command->execute()``.

.. caution::

Note that all these commands will run in the same process, and some of Symfony's
built-in commands may not work well this way. For instance, ``cache:clear`` and ``cache:warmup``
commands change some class definitions, so running something
after them is likely to break.

Testing Commands
----------------

Expand Down

0 comments on commit 57938a5

Please sign in to comment.