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

Skip console commands from event listeners #4058

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

Merged
merged 1 commit into from
Nov 4, 2014
Merged
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
35 changes: 35 additions & 0 deletions 35 components/console/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ dispatched. Listeners receive a
$application = $command->getApplication();
});

Disable Commands inside Listeners
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 2.6
Disabling commands inside listeners was introduced in Symfony 2.6.

Using the
:method:`Symfony\\Component\\Console\\Event\\ConsoleCommandEvent::disableCommand`
method, you can disable a command inside a listener. The application
will then not execute the command but return the code `113` (defined in
``ConsoleCommandEvent::RETURN_CODE_DISABLED``), which is one of the
`reserved exit codes`_ for console commands to conform with the C/C++ standard.::

use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;

$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
// get the command to be executed
$command = $event->getCommand();

// ... check if the command can be executed

// disable the command, this will result in the command being skipped
// and code 113 being returned from the Application
$event->disableCommand();

// it is possible to enable the command in a later listener
if (!$event->commandShouldRun()) {
$event->enableCommand();
}
});

.. _`reserved exit codes`: http://www.tldp.org/LDP/abs/html/exitcodes.html

The ``ConsoleEvents::TERMINATE`` Event
--------------------------------------

Expand Down Expand Up @@ -118,3 +152,4 @@ Listeners receive a
// change the exception to another one
$event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
});

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