Skip to content

[Console] Skip commands from ConsoleCommandEvent #9235

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
Aug 31, 2014
Merged
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,16 +892,20 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
$event = new ConsoleCommandEvent($command, $input, $output);
$this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);

try {
$exitCode = $command->run($input, $output);
} catch (\Exception $e) {
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
if ($event->commandShouldRun()) {
try {
$exitCode = $command->run($input, $output);
} catch (\Exception $e) {
$event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode());
$this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);

$event = new ConsoleExceptionEvent($command, $input, $output, $e, $event->getExitCode());
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $event->getExitCode());
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);

throw $event->getException();
throw $event->getException();
}
} else {
$exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
}

$event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
Expand Down
43 changes: 42 additions & 1 deletion src/Symfony/Component/Console/Event/ConsoleCommandEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,51 @@
namespace Symfony\Component\Console\Event;

/**
* Allows to do things before the command is executed.
* Allows to do things before the command is executed, like skipping the command or changing the input.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ConsoleCommandEvent extends ConsoleEvent
{
/**
* The return code for skipped commands, this will also be passed into the terminate event
*/
const RETURN_CODE_DISABLED = 113;
Copy link
Member

Choose a reason for hiding this comment

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

Why 113 ?

I think this constant should be defined in Application class because it is used in it and makes the return code of the application.

Copy link
Author

Choose a reason for hiding this comment

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

@GromNaN http://www.tldp.org/LDP/abs/html/exitcodes.html @fabpot pointed me to this document, and there the author recommends the return codes within the range 64-113, and I am a kind of max guy, so I chose the upper range limit.


/**
* Indicates if the command should be run or skipped
*
* @var bool
*/
private $commandShouldRun = true;

/**
* Disables the command, so it won't be run
*
* @return bool
*/
public function disableCommand()
{
return $this->commandShouldRun = false;
}

/**
* Enables the command
*
* @return bool
*/
public function enableCommand()
{
return $this->commandShouldRun = true;
}

/**
* Returns true if the command is runnable, false otherwise
*
* @return bool
*/
public function commandShouldRun()
Copy link
Contributor

Choose a reason for hiding this comment

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

should is the wrong term. if "should" says no, it doesn't mean it's not allowed to run. but that is what it is used for.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since the return code is named _DISABLED. maybe this method should be called commandIsDisabled()?

{
return $this->commandShouldRun;
}
}
30 changes: 26 additions & 4 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,22 @@ public function testRunDispatchesAllEventsWithException()
$this->assertContains('before.foo.after.caught.', $tester->getDisplay());
}

public function testRunWithDispatcherSkippingCommand()
{
$application = new Application();
$application->setDispatcher($this->getDispatcher(true));
$application->setAutoExit(false);

$application->register('foo')->setCode(function (InputInterface $input, OutputInterface $output) {
$output->write('foo.');
});

$tester = new ApplicationTester($application);
$exitCode = $tester->run(array('command' => 'foo'));
$this->assertContains('before.after.', $tester->getDisplay());
$this->assertEquals(ConsoleCommandEvent::RETURN_CODE_DISABLED, $exitCode);
}

public function testTerminalDimensions()
{
$application = new Application();
Expand All @@ -906,16 +922,22 @@ public function testTerminalDimensions()
$this->assertSame(array($width, 80), $application->getTerminalDimensions());
}

protected function getDispatcher()
protected function getDispatcher($skipCommand = false)
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) {
$dispatcher->addListener('console.command', function (ConsoleCommandEvent $event) use ($skipCommand) {
$event->getOutput()->write('before.');

if ($skipCommand) {
$event->disableCommand();
}
});
$dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) {
$dispatcher->addListener('console.terminate', function (ConsoleTerminateEvent $event) use ($skipCommand) {
$event->getOutput()->write('after.');

$event->setExitCode(128);
if (!$skipCommand) {
$event->setExitCode(113);
}
});
$dispatcher->addListener('console.exception', function (ConsoleExceptionEvent $event) {
$event->getOutput()->writeln('caught.');
Expand Down