8000 [Console] Added a cookbook entry on invoking other commands · symfony/symfony-docs@f6563e6 · GitHub
[go: up one dir, main page]

Skip to content

Commit f6563e6

Browse files
committed
[Console] Added a cookbook entry on invoking other commands
1 parent 0c92fab commit f6563e6

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

cookbook/console/console_command.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,40 @@ before translating contents::
146146
However for other services the solution might be more complex. For more details,
147147
see :doc:`/cookbook/service_container/scopes`.
148148

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

0 commit comments

Comments
 (0)
0