1
0
Fork 0

Add handling of ctrl-C on windows for php 7.4+

pull/8948/head
Jordi Boggiano 2020-06-03 15:18:27 +02:00
parent 29761cf78d
commit 396ad87fd0
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
2 changed files with 31 additions and 5 deletions

View File

@ -367,6 +367,17 @@ EOT
});
}
}
// handler Ctrl+C for Windows on PHP 7.4+
if (function_exists('sapi_windows_set_ctrl_handler')) {
@mkdir($directory, 0777, true);
if ($realDir = realpath($directory)) {
sapi_windows_set_ctrl_handler(function () use ($realDir) {
$fs = new Filesystem();
$fs->removeDirectory($realDir);
exit(130);
});
}
}
// avoid displaying 9999999-dev as version if dev-master was selected
if ($package instanceof AliasPackage && $package->getPrettyVersion() === VersionParser::DEV_MASTER_ALIAS) {

View File

@ -195,10 +195,11 @@ class InstallationManager
}
};
// handler Ctrl+C for unix-like systems
$handleInterrupts = function_exists('pcntl_async_signals') && function_exists('pcntl_signal');
$handleInterruptsUnix = function_exists('pcntl_async_signals') && function_exists('pcntl_signal');
$handleInterruptsWindows = function_exists('sapi_windows_set_ctrl_handler');
$prevHandler = null;
if ($handleInterrupts) {
$windowsHandler = null;
if ($handleInterruptsUnix) {
pcntl_async_signals(true);
$prevHandler = pcntl_signal_get_handler(SIGINT);
pcntl_signal(SIGINT, function ($sig) use ($runCleanup, $prevHandler) {
@ -211,6 +212,14 @@ class InstallationManager
exit(130);
});
}
if ($handleInterruptsWindows) {
$windowsHandler = function () use ($runCleanup) {
$runCleanup();
exit(130);
};
sapi_windows_set_ctrl_handler($windowsHandler);
}
try {
foreach ($operations as $index => $operation) {
@ -317,16 +326,22 @@ class InstallationManager
} catch (\Exception $e) {
$runCleanup();
if ($handleInterrupts) {
if ($handleInterruptsUnix) {
pcntl_signal(SIGINT, $prevHandler);
}
if ($handleInterruptsWindows) {
sapi_windows_set_ctrl_handler($prevHandler, false);
}
throw $e;
}
if ($handleInterrupts) {
if ($handleInterruptsUnix) {
pcntl_signal(SIGINT, $prevHandler);
}
if ($handleInterruptsWindows) {
sapi_windows_set_ctrl_handler($prevHandler, false);
}
// do a last write so that we write the repository even if nothing changed
// as that can trigger an update of some files like InstalledVersions.php if