Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Added a cookbook entry on invoking other commands #4493

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cookbook/console/console_command.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this caution but I find the phrase too long to be easily understood. Could we split it into two sentences?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javiereguiluz, looks better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it looks better to me. Thanks!

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