10000 [Console] Allow to catch CommandNotFoundException · symfony/symfony@b21ce85 · GitHub
[go: up one dir, main page]

Skip to content

Commit b21ce85

Browse files
committed
[Console] Allow to catch CommandNotFoundException
1 parent 195e464 commit b21ce85

File tree

6 files changed

+27
-5
lines changed

6 files changed

+27
-5
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ public function run(InputInterface $input = null, OutputInterface $output = null
128128
$exception = new FatalThrowableError($e);
129129
}
130130

131-
if (null !== $this->runningCommand && null !== $e && null !== $this->dispatcher) {
132-
$event = new ConsoleErrorEvent($this->runningCommand, $input, $output, $e, $e->getCode());
131+
if (null !== $e && null !== $this->dispatcher) {
132+
$event = new ConsoleErrorEvent($input, $output, $e, $e->getCode(), $this->runningCommand);
133133
$this->dispatcher->dispatch(ConsoleEvents::ERROR, $event);
134134

135135
$e = $event->getError();

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ CHANGELOG
1010
with value optional explicitly passed empty
1111
* added console.error event to catch exceptions thrown by other listeners
1212
* deprecated console.exception event in favor of console.error
13+
* added ability to handle `CommandNotFoundException` through the
14+
`console.error` event
1315

1416
3.2.0
1517
------

src/Symfony/Component/Console/Event/ConsoleErrorEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConsoleErrorEvent extends ConsoleExceptionEvent
2727
private $error;
2828
private $handled = false;
2929

30-
public function __construct(Command $command, InputInterface $input, OutputInterface $output, $error, $exitCode)
30+
public function __construct(InputInterface $input, OutputInterface $output, $error, $exitCode, Command $command = null)
3131
{
3232
if (!$error instanceof \Throwable && !$error instanceof \Exception) {
3333
throw new InvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.', is_object($error) ? get_class($error) : gettype($error)));

src/Symfony/Component/Console/Event/ConsoleExceptionEvent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ConsoleExceptionEvent extends ConsoleEvent
2727
private $exception;
2828
private $exitCode;
2929

30-
public function __construct(Command $command, InputInterface $input, OutputInterface $output, \Exception $exception, $exitCode, $deprecation = true)
30+
public function __construct(Command $command = null, InputInterface $input, OutputInterface $output, \Exception $exception, $exitCode, $deprecation = true)
3131
{
3232
if ($deprecation) {
3333
@trigger_error(sprintf('The %s class is deprecated since version 3.3 and will be removed in 4.0. Use the ConsoleErrorEvent instead.', __CLASS__), E_USER_DEPRECATED);

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,26 @@ public function testRunAllowsErrorListenersToSilenceTheException()
10511051
$this->assertEquals(0, $tester->getStatusCode());
10521052
}
10531053

1054+
public function testConsoleErrorEventIsTriggeredOnCommandNotFound()
1055+
{
1056+
$dispatcher = new EventDispatcher();
1057+
$dispatcher->addListener('console.error', function (ConsoleErrorEvent $event) {
1058+
$this->assertNull($event->getCommand());
1059+
$this->assertInstanceOf(CommandNotFoundException::class, $event->getError());
1060+
$event->getOutput()->write('silenced command not found');
1061+
$event->markErrorAsHandled();
1062+
});
1063+
1064+
$application = new Application();
1065+
$application->setDispatcher($dispatcher);
1066+
$application->setAutoExit(false);
1067+
1068+
$tester = new ApplicationTester($application);
1069+
$tester->run(array('command' => 'unknown'));
1070+
$this->assertContains('silenced command not found', $tester->getDisplay());
1071+
$this->assertEquals(0, $tester->getStatusCode());
1072+
}
1073+
10541074
/**
10551075
* @group legacy
10561076
* @expectedDeprecation The "console.exception" event is deprecated since version 3.3 and will be removed in 4.0. Use the "console.error" event instead.

src/Symfony/Component/Console/Tests/EventListener/ExceptionListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private function getLogger()
111111

112112
private function getConsoleErrorEvent(\Exception $exception, InputInterface $input, $exitCode)
113113
{
114-
return new ConsoleErrorEvent(new Command('test:run'), $input, $this->getOutput(), $exception, $exitCode);
114+
return new ConsoleErrorEvent($input, $this->getOutput(), $exception, $exitCode, new Command('test:run'));
115115
}
116116

117117
private function getConsoleTerminateEvent(InputInterface $input, $exitCode)

0 commit comments

Comments
 (0)
0