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

Skip to content

Commit 0ff0123

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

File tree

6 files changed

+76
-9
lines changed

6 files changed

+76
-9
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 3 additions & 3 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();
@@ -891,7 +891,7 @@ protected function doRunCommand(Command $command, InputInterface $input, OutputI
891891
}
892892

893893
if (null !== $e) {
894-
$event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode(), false);
894+
$event = new ConsoleExceptionEvent($input, $output, $e, $e->getCode(), $command, false);
895895
$this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
896896

897897
if ($e !== $event->getException()) {

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

Lines changed: 2 additions & 2 deletions
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)));
@@ -37,7 +37,7 @@ public function __construct(Command $command, InputInterface $input, OutputInter
3737
if (!$error instanceof \Exception) {
3838
$exception = new FatalThrowableError($error);
3939
}
40-
parent::__construct($command, $input, $output, $exception, $exitCode, false);
40+
parent::__construct($input, $output, $exception, $exitCode, $command, false);
4141

4242
$this->error = $error;
4343
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\Component\Console\Event;
1313

1414
use Symfony\Component\Console\Command\Command;
15-
use Symfony\Component\Console\Input\InputInterface;
16-
use Symfony\Component\Console\Output\OutputInterface;
1715

1816
/**
1917
* Allows to handle exception thrown in a command.
@@ -27,12 +25,23 @@ class ConsoleExceptionEvent extends ConsoleEvent
2725
private $exception;
2826
private $exitCode;
2927

30-
public function __construct(Command $command, InputInterface $input, OutputInterface $output, \Exception $exception, $exitCode, $deprecation = true)
28+
public function __construct($input, $output, $exception, $exitCode, $command = null, $deprecation = true)
3129
{
3230
if ($deprecation) {
3331
@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);
3432
}
3533

34+
if ($input instanceof Command) {
35+
$tmpExitCode = $command;
36+
$command = $input;
37+
$input = $output;
38+
$output = $exception;
39+
$exception = $exitCode;
40+
$exitCode = $tmpExitCode;
41+
42+
@trigger_error(sprintf('Passing an instance of %s as first argument of %s() is deprecated since version 3.3. Pass it as 5th argument instead.', Command::class, __METHOD__), E_USER_DEPRECATED);
43+
}
44+
3645
parent::__construct($command, $input, $output);
3746

3847
$this->exception = $exception;

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.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\EventListener;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
17+
use Symfony\Component\Console\Input\ArrayInput;
18+
use Symfony\Component\Console\Output\NullOutput;
19+
20+
/**
21+
* @group legacy
22+
*/
23+
class ConsoleExceptionEventTest extends TestCase
24+
{
25+
/**
26+
* @expectedDeprecation The Symfony\Component\Console\Event\ConsoleExceptionEvent class is deprecated since version 3.3 and will be removed in 4.0. Use the ConsoleErrorEvent instead.
27+
* @expectedDeprecation Passing an instance of Symfony\Component\Console\Command\Command as first argument of Symfony\Component\Console\Event\ConsoleExceptionEvent::__construct() is deprecated since version 3.3. Pass it as 5th argument instead.
28+
*/
29+
public function testLegacyConstructor()
30+
{
31+
$event = new ConsoleExceptionEvent($command = new Command('foo'), $input = new ArrayInput(array()), $output = new NullOutput(), $exception = new \Exception(), $exitCode = 255);
32+
$this->assertSame($command, $event->getCommand());
33+
$this->assertSame($input, $event->getInput());
34+
$this->assertSame($output, $event->getOutput());
35+
$this->assertSame($exception, $event->getException());
36+
$this->assertSame($exitCode, $event->getExitCode());
37+
}
38+
}

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