diff --git a/drush.php b/drush.php index b6cb544bea..cd00e708c8 100755 --- a/drush.php +++ b/drush.php @@ -134,7 +134,7 @@ // Preflight and run $preflight = new Preflight($environment); $di = new DependencyInjection(); -$di->desiredHandlers(['errorHandler', 'shutdownHandler']); +$di->desiredHandlers(['errorHandler']); $runtime = new Runtime($preflight, $di); $status_code = $runtime->run($_SERVER['argv']); diff --git a/src/Runtime/DependencyInjection.php b/src/Runtime/DependencyInjection.php index 65cb9e5219..4743dc359c 100644 --- a/src/Runtime/DependencyInjection.php +++ b/src/Runtime/DependencyInjection.php @@ -146,9 +146,8 @@ protected function addDrushServices($container, ClassLoader $loader, DrushDrupal ->addMethodCall('addSearchLocation', ['CommandFiles']) ->addMethodCall('setSearchPattern', ['#.*(Commands|CommandFile).php$#']); - // Error and Shutdown handlers + // Error handler Robo::addShared($container, 'errorHandler', 'Drush\Runtime\ErrorHandler'); - Robo::addShared($container, 'shutdownHandler', 'Drush\Runtime\ShutdownHandler'); // Add inflectors. @see \Drush\Boot\BaseBoot::inflect $container->inflector(SiteAliasManagerAwareInterface::class) diff --git a/src/Runtime/Runtime.php b/src/Runtime/Runtime.php index 288a9cff04..71c5454869 100644 --- a/src/Runtime/Runtime.php +++ b/src/Runtime/Runtime.php @@ -4,11 +4,11 @@ namespace Drush\Runtime; -use Symfony\Component\Console\Output\ConsoleOutput; use Drush\Application; use Drush\Commands\DrushCommands; use Drush\Drush; use Drush\Preflight\Preflight; +use Symfony\Component\Console\Output\ConsoleOutput; /** * Control the Drush runtime environment @@ -21,8 +21,9 @@ */ class Runtime { + + // Shutdown handling removed from Drush, but 3rd party commandfiles may add it back. const DRUSH_RUNTIME_COMPLETED_NAMESPACE = 'runtime.execution.completed'; - const DRUSH_RUNTIME_EXIT_CODE_NAMESPACE = 'runtime.exit_code'; public function __construct(protected Preflight $preflight, protected DependencyInjection $di) { @@ -109,40 +110,19 @@ protected function doRun($argv, $output): int // Bootstrap: bootstrap site to the level requested by the command (via a 'post-init' hook) $status = $application->run($input, $output); - // Placate the Drush shutdown handler. + // Placate the Drush shutdown handler which can be provided via custom commandfile. Runtime::setCompleted(); - Runtime::setExitCode($status); return $status; } /** * Mark the current request as having completed successfully. + * + * Shutdown handling removed from Drush, but 3rd party commandfiles may add it back. */ public static function setCompleted(): void { Drush::config()->set(self::DRUSH_RUNTIME_COMPLETED_NAMESPACE, true); } - - /** - * Mark the exit code for current request. - * - * @deprecated - * Was used by backend.inc - */ - public static function setExitCode(int $code): void - { - Drush::config()->set(self::DRUSH_RUNTIME_EXIT_CODE_NAMESPACE, $code); - } - - /** - * Get the exit code for current request. - * - * @deprecated - * Was used by backend.inc - */ - public static function exitCode() - { - return Drush::config()->get(self::DRUSH_RUNTIME_EXIT_CODE_NAMESPACE, 0); - } } diff --git a/src/Runtime/ShutdownHandler.php b/src/Runtime/ShutdownHandler.php deleted file mode 100644 index 8e0494ba4c..0000000000 --- a/src/Runtime/ShutdownHandler.php +++ /dev/null @@ -1,60 +0,0 @@ -get(Runtime::DRUSH_RUNTIME_COMPLETED_NAMESPACE)) { - Drush::logger()->warning('Drush command terminated abnormally.'); - // Make sure that we will return an error code when we exit, - // even if the code that got us here did not. - if (!Runtime::exitCode()) { - Runtime::setExitCode(DrushCommands::EXIT_FAILURE); - } - } - - // This way returnStatus() will always be the last shutdown function (unless other shutdown functions register shutdown functions...) - // and won't prevent other registered shutdown functions (IE from numerous cron methods) from running by calling exit() before they get a chance. - register_shutdown_function([$this, 'returnStatus']); - } - - /** - * @deprecated. This function will be removed in Drush 10. Throw an exception to indicate an error. - */ - public function returnStatus(): never - { - exit(Runtime::exitCode()); - } -} diff --git a/tests/functional/ShutdownAndErrorHandlerTest.php b/tests/functional/ShutdownAndErrorHandlerTest.php index e869fe67db..0bcba1b28d 100644 --- a/tests/functional/ShutdownAndErrorHandlerTest.php +++ b/tests/functional/ShutdownAndErrorHandlerTest.php @@ -20,10 +20,10 @@ class ShutdownAndErrorHandlerTest extends CommandUnishTestCase */ public function testShutdownFunctionAbruptExit() { - // Run some garbage php with a syntax error. - $this->drush(PhpCommands::EVAL, ['exit(0);'], [], null, null, DrushCommands::EXIT_FAILURE); - - $this->assertStringContainsString("Drush command terminated abnormally.", $this->getErrorOutput(), 'Error handler did not log a message.'); + // Run some garbage php with a syntax error and assert correct exit code. + $this->drush(PhpCommands::EVAL, ['exit(1);'], [], null, null, DrushCommands::EXIT_FAILURE); + // Placate phpunit. If above succeeds we are done here. + $this->addToAssertionCount(1); } /** @@ -46,9 +46,9 @@ public function testShutdownFunctionExitCodePassedThrough() public function testShutdownFunctionPHPError() { // Run some garbage php with a syntax error. - $this->drush(PhpCommands::EVAL, ['\Drush\Drush::setContainer("string is the wrong type to pass here");'], [], null, null, PHP_MAJOR_VERSION == 5 ? 255 : DrushCommands::EXIT_FAILURE); - - $this->assertStringContainsString("Drush command terminated abnormally.", $this->getErrorOutput(), 'Error handler did not log a message.'); + $this->drush(PhpCommands::EVAL, ['\Drush\Drush::setContainer("string is the wrong type to pass here");'], [], null, null, DrushCommands::EXIT_FAILURE); + // Placate phpunit. If above succeeds we are done here. + $this->addToAssertionCount(1); } /** @@ -58,12 +58,6 @@ public function testErrorHandler() { // Access a missing array element $this->drush(PhpCommands::EVAL, ['$a = []; print $a["b"];']); - - if (empty($this->logLevel()) && PHP_MAJOR_VERSION <= 7) { - $this->assertEquals('', $this->getErrorOutput(), 'Error handler did not suppress deprecated message.'); - } else { - $msg = PHP_MAJOR_VERSION >= 8 ? 'Undefined array key "b" PhpCommands.php' : 'Undefined index: b PhpCommands.php'; - $this->assertStringContainsString($msg, $this->getErrorOutput()); - } + $this->assertStringContainsString('Undefined array key "b" PhpCommands.php', $this->getErrorOutput()); } }