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 54495b2

Browse filesBrowse files
committed
feature #22181 [Console] Allow to catch CommandNotFoundException (chalasr)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Console] Allow to catch CommandNotFoundException | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #22144 (comment) | License | MIT | Doc PR | n/a Basically reverts #22144, making the command argument optional in console.error event instead, so that `CommandNotFoundException` can be handled as any other console error. Commits ------- b21ce85 [Console] Allow to catch CommandNotFoundException
2 parents b65ebc7 + b21ce85 commit 54495b2
Copy full SHA for 54495b2

File tree

6 files changed

+27
-5
lines changed
Filter options

6 files changed

+27
-5
lines changed

‎src/Symfony/Component/Console/Application.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Application.php
+2-2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/CHANGELOG.md
+2Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Event/ConsoleErrorEvent.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Event/ConsoleExceptionEvent.php
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/ApplicationTest.php
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,26 @@ public function testRunAllowsErrorListenersToSilenceTheException()
10661066
$this->assertEquals(0, $tester->getStatusCode());
10671067
}
10681068

1069+
public function testConsoleErrorEventIsTriggeredOnCommandNotFound()
1070+
{
1071+
$dispatcher = new EventDispatcher();
1072+
$dispatcher->addListener('console.error', function (ConsoleErrorEvent $event) {
1073+
$this->assertNull($event->getCommand());
1074+
$this->assertInstanceOf(CommandNotFoundException::class, $event->getError());
1075+
$event->getOutput()->write('silenced command not found');
1076+
$event->markErrorAsHandled();
1077+
});
1078+
1079+
$application = new Application();
1080+
$application->setDispatcher($dispatcher);
1081+
$application->setAutoExit(false);
1082+
1083+
$tester = new ApplicationTester($application);
1084+
$tester->run(array('command' => 'unknown'));
1085+
$this->assertContains('silenced command not found', $tester->getDisplay());
1086+
$this->assertEquals(0, $tester->getStatusCode());
1087+
}
1088+
10691089
/**
10701090
* @group legacy
10711091
* @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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Console/Tests/EventListener/ExceptionListenerTest.php
+1-1Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.