Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c82934f

Browse filesBrowse files
committed
minor #18741 [Console] Improve console events doc (HeahDude)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Console] Improve console events doc The docs currently state that running a command from another command does not dispatch events. This side effect can be avoided by using `$this->getApplication()->doRun()` and the command name as first argument of the input, instead of `$this->getApplication()->find(...)->run()`. Also, the events page is linked by the component doc, for better discoverability I added a short section to link it in the main page of `Console`. Commits ------- e0d1b35 [Console] Improve console events doc
2 parents f8e7cb2 + e0d1b35 commit c82934f
Copy full SHA for c82934f

File tree

3 files changed

+31
-16
lines changed
Filter options

3 files changed

+31
-16
lines changed

‎components/console/events.rst

Copy file name to clipboardExpand all lines: components/console/events.rst
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ the wheel, it uses the Symfony EventDispatcher component to do the work::
1717
.. caution::
1818

1919
Console events are only triggered by the main command being executed.
20-
Commands called by the main command will not trigger any event.
20+
Commands called by the main command will not trigger any event, unless
21+
run by the application itself, see :doc:`/console/calling_commands`.
2122

2223
The ``ConsoleEvents::COMMAND`` Event
2324
------------------------------------
@@ -171,10 +172,10 @@ Listeners receive a
171172
use Symfony\Component\Console\Event\ConsoleSignalEvent;
172173

173174
$dispatcher->addListener(ConsoleEvents::SIGNAL, function (ConsoleSignalEvent $event) {
174-
175+
175176
// gets the signal number
176177
$signal = $event->getHandlingSignal();
177-
178+
178179
if (\SIGINT === $signal) {
179180
echo "bye bye!";
180181
}

‎console.rst

Copy file name to clipboardExpand all lines: console.rst
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,12 @@ registers an :doc:`event subscriber </event_dispatcher>` to listen to the
585585
:ref:`ConsoleEvents::TERMINATE event <console-events-terminate>` and adds a log
586586
message whenever a command doesn't finish with the ``0`` `exit status`_.
587587

588+
Using Events And Handling Signals
589+
---------------------------------
590+
591+
When a command is running, many events are dispatched, one of them allows to
592+
react to signals, read more in :doc:`this section </components/console/events>`.
593+
588594
Learn More
589595
----------
590596

‎console/calling_commands.rst

Copy file name to clipboardExpand all lines: console/calling_commands.rst
+21-13Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ or if you want to create a "meta" command that runs a bunch of other commands
88
changed on the production servers: clearing the cache, generating Doctrine
99
proxies, dumping web assets, ...).
1010

11-
Use the :method:`Symfony\\Component\\Console\\Application::find` method to
12-
find the command you want to run by passing the command name. Then, create a
13-
new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the
14-
arguments and options you want to pass to the command.
11+
Use the :method:`Symfony\\Component\\Console\\Application::doRun`. Then, create
12+
a new :class:`Symfony\\Component\\Console\\Input\\ArrayInput` with the
13+
arguments and options you want to pass to the command. The command name must be
14+
the first argument.
1515

16-
Eventually, calling the ``run()`` method actually runs the command and returns
17-
the returned code from the command (return value from command's ``execute()``
16+
Eventually, calling the ``doRun()`` method actually runs the command and returns
17+
the returned code from the command (return value from command ``execute()``
1818
method)::
1919

2020
// ...
@@ -29,15 +29,14 @@ method)::
2929

3030
protected function execute(InputInterface $input, OutputInterface $output): int
3131
{
32-
$command = $this->getApplication()->find('demo:greet');
33-
34-
$arguments = [
32+
$greetInput = new ArrayInput([
33+
// the command name is passed as first argument
34+
'command' => 'demo:greet',
3535
'name' => 'Fabien',
3636
'--yell' => true,
37-
];
37+
]);
3838

39-
$greetInput = new ArrayInput($arguments);
40-
$returnCode = $command->run($greetInput, $output);
39+
$returnCode = $this->getApplication()->doRun($greetInput, $output);
4140

4241
// ...
4342
}
@@ -47,7 +46,16 @@ method)::
4746

4847
If you want to suppress the output of the executed command, pass a
4948
:class:`Symfony\\Component\\Console\\Output\\NullOutput` as the second
50-
argument to ``$command->run()``.
49+
argument to ``$application->doRun()``.
50+
51+
.. note::
52+
53+
Using ``doRun()`` instead of ``run()`` prevents autoexiting and allows to
54+
return the exit code instead.
55+
56+
Also, using ``$this->getApplication()->doRun()`` instead of
57+
``$this->getApplication()->find('demo:greet')->run()`` will allow proper
58+
events to be dispatched for that inner command as well.
5159

5260
.. caution::
5361

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.