From f4738d97b78721196b81f2dcc5feda39739cbb95 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 28 Jul 2023 19:32:46 +0200 Subject: [PATCH] Add support for Application::setCatchErrors in symfony 6.4+, refs symfony/symfony#50420 --- src/Composer/Console/Application.php | 9 +++++++-- src/Composer/EventDispatcher/EventDispatcher.php | 3 +++ tests/Composer/Test/DocumentationTest.php | 3 +++ tests/Composer/Test/TestCase.php | 3 +++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 01a2497b7..da77636d1 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -87,6 +87,10 @@ class Application extends BaseApplication public function __construct(string $name = 'Composer', string $version = '') { + if (method_exists($this, 'setCatchErrors')) { + $this->setCatchErrors(true); + } + static $shutdownRegistered = false; if ($version === '') { $version = Composer::getVersion(); @@ -395,9 +399,10 @@ function_exists('php_uname') ? php_uname('s') . ' / ' . php_uname('r') : 'Unknow $this->hintCommonErrors($e, $output); - // symfony/console does not handle \Error subtypes so we have to renderThrowable ourselves + // symfony/console <6.4 does not handle \Error subtypes so we have to renderThrowable ourselves // instead of rethrowing those for consumption by the parent class - if (!$e instanceof \Exception) { + // can be removed when Composer supports PHP 8.1+ + if (!method_exists($this, 'setCatchErrors') && !$e instanceof \Exception) { if ($output instanceof ConsoleOutputInterface) { $this->renderThrowable($e, $output->getErrorOutput()); } else { diff --git a/src/Composer/EventDispatcher/EventDispatcher.php b/src/Composer/EventDispatcher/EventDispatcher.php index b564cf367..3326b8e31 100644 --- a/src/Composer/EventDispatcher/EventDispatcher.php +++ b/src/Composer/EventDispatcher/EventDispatcher.php @@ -277,6 +277,9 @@ protected function doDispatch(Event $event) $app = new Application(); $app->setCatchExceptions(false); + if (method_exists($app, 'setCatchErrors')) { + $app->setCatchErrors(false); + } $app->setAutoExit(false); $cmd = new $className($event->getName()); $app->add($cmd); diff --git a/tests/Composer/Test/DocumentationTest.php b/tests/Composer/Test/DocumentationTest.php index e7a49abde..d7e7ef856 100644 --- a/tests/Composer/Test/DocumentationTest.php +++ b/tests/Composer/Test/DocumentationTest.php @@ -53,6 +53,9 @@ public static function provideCommandCases(): \Generator { $application = new Application(); $application->setAutoExit(false); + if (method_exists($application, 'setCatchErrors')) { + $application->setCatchErrors(false); + } $application->setCatchExceptions(false); $description = new ApplicationDescription($application); diff --git a/tests/Composer/Test/TestCase.php b/tests/Composer/Test/TestCase.php index 6a2d7ce86..357fb9264 100644 --- a/tests/Composer/Test/TestCase.php +++ b/tests/Composer/Test/TestCase.php @@ -198,6 +198,9 @@ public function getApplicationTester(): ApplicationTester $application = new Application(); $application->setAutoExit(false); $application->setCatchExceptions(false); + if (method_exists($application, 'setCatchErrors')) { + $application->setCatchErrors(false); + } return new ApplicationTester($application); } -- GitLab