Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove shutdown handling from Drush #5840

Open
wants to merge 6 commits into
base: 12.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion drush.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);

Expand Down
3 changes: 1 addition & 2 deletions src/Runtime/DependencyInjection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 6 additions & 26 deletions src/Runtime/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
{
Expand Down Expand Up @@ -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);
}
}
60 changes: 0 additions & 60 deletions src/Runtime/ShutdownHandler.php

This file was deleted.

22 changes: 8 additions & 14 deletions tests/functional/ShutdownAndErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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());
}
}