From f6563e64eedb7a3901cdf834a7651a8193c1bab2 Mon Sep 17 00:00:00 2001 From: Stepan Anchugov Date: Wed, 19 Nov 2014 14:54:58 +0500 Subject: [PATCH 1/3] [Console] Added a cookbook entry on invoking other commands --- cookbook/console/console_command.rst | 34 ++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/cookbook/console/console_command.rst b/cookbook/console/console_command.rst index b71b0792781..d138b8f3828 100644 --- a/cookbook/console/console_command.rst +++ b/cookbook/console/console_command.rst @@ -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 `'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 ---------------- From 5f370277eb9348cff4af663cceed3fa5934ae4ea Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Mon, 22 Jun 2015 17:38:34 +0200 Subject: [PATCH 2/3] Removed duplication and moved a caution message --- components/console/introduction.rst | 22 ++++++++++++++----- cookbook/console/console_command.rst | 32 ++-------------------------- 2 files changed, 19 insertions(+), 35 deletions(-) diff --git a/components/console/introduction.rst b/components/console/introduction.rst index d698af4a96c..079dbec8665 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -476,6 +476,8 @@ method:: You can also test a whole console application by using :class:`Symfony\\Component\\Console\\Tester\\ApplicationTester`. +.. _calling-existing-command: + Calling an Existing Command --------------------------- @@ -505,16 +507,26 @@ Calling a command from another one is straightforward:: } First, you :method:`Symfony\\Component\\Console\\Application::find` the -command you want to execute by passing the command name. - -Then, you need to create a new -:class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments and -options you want to pass to the command. +command you want to execute by passing the command name. Then, you need to create +a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the arguments +and options you want to pass to the command. Eventually, calling the ``run()`` method actually executes the command and returns the returned code from the command (return value from command's ``execute()`` method). +.. 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 the 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. + .. note:: Most of the time, calling a command from code that is not executed on the diff --git a/cookbook/console/console_command.rst b/cookbook/console/console_command.rst index d138b8f3828..aca8c3e33d1 100644 --- a/cookbook/console/console_command.rst +++ b/cookbook/console/console_command.rst @@ -149,36 +149,8 @@ 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 `'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. +See :ref:`calling-existing-command` if you need to implement a command that runs +other dependent commands. Testing Commands ---------------- From 2c8e6f37ec9c6bc66b78169d6f631e2895128696 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Jun 2015 10:10:08 +0200 Subject: [PATCH 3/3] Fixed typos --- components/console/introduction.rst | 13 +++++++------ cookbook/console/console_command.rst | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/components/console/introduction.rst b/components/console/introduction.rst index 079dbec8665..fbe41f25a41 100644 --- a/components/console/introduction.rst +++ b/components/console/introduction.rst @@ -517,15 +517,16 @@ returns the returned code from the command (return value from command's .. 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()``. + 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 the 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. + Note that all the commands will run in the same process and some of Symfony's + built-in commands may not work well this way. For instance, the ``cache:clear`` + and ``cache:warmup`` commands change some class definitions, so running + something after them is likely to break. .. note:: diff --git a/cookbook/console/console_command.rst b/cookbook/console/console_command.rst index aca8c3e33d1..b48a6c24523 100644 --- a/cookbook/console/console_command.rst +++ b/cookbook/console/console_command.rst @@ -146,7 +146,7 @@ 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 +Invoking other Commands ----------------------- See :ref:`calling-existing-command` if you need to implement a command that runs