Skip to content

Commit 26512a9

Browse files
committed
feat: handle Ctrl+C and Ctrl+Break in SignalTrait on Windows
1 parent 99f9f92 commit 26512a9

1 file changed

Lines changed: 103 additions & 2 deletions

File tree

‎system/CLI/SignalTrait.php‎

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,21 @@
1818
/**
1919
* Signal Trait
2020
*
21-
* Provides PCNTL signal handling capabilities for CLI commands.
22-
* Requires the PCNTL extension (Unix only).
21+
* Provides signal handling capabilities for CLI commands.
22+
*
23+
* On Unix it requires the PCNTL extension. On Windows, where PCNTL does not
24+
* exist, only Ctrl+C and Ctrl+Break are supported, through
25+
* `sapi_windows_set_ctrl_handler()`. Both are reported as SIGINT (2).
2326
*/
2427
trait SignalTrait
2528
{
29+
/**
30+
* The number of SIGINT on every platform that defines it. The SIG*
31+
* constants come from the PCNTL extension, which Windows does not have.
32+
* Ctrl+C and Ctrl+Break are reported to handlers as this value.
33+
*/
34+
private const INTERRUPT_SIGNAL = 2;
35+
2636
/**
2737
* Whether the process should continue running (false = termination requested).
2838
*/
@@ -47,6 +57,11 @@ trait SignalTrait
4757
*/
4858
private array $signalMethodMap = [];
4959

60+
/**
61+
* Whether the Windows console control handler is registered.
62+
*/
63+
private bool $windowsCtrlHandlerRegistered = false;
64+
5065
/**
5166
* Cached result of PCNTL extension availability.
5267
*/
@@ -86,9 +101,36 @@ protected function isPosixAvailable(): bool
86101
return self::$isPosixAvailable;
87102
}
88103

104+
/**
105+
* Check if the Windows console control handler is available.
106+
*
107+
* It is the Windows counterpart of PCNTL, and handles Ctrl+C and Ctrl+Break.
108+
*/
109+
protected function isWindowsCtrlHandlerAvailable(): bool
110+
{
111+
return is_windows() && function_exists('sapi_windows_set_ctrl_handler');
112+
}
113+
114+
/**
115+
* Add or remove the Windows console control handler.
116+
*
117+
* Wraps `sapi_windows_set_ctrl_handler()` so it can be replaced in tests,
118+
* as the function is only available on Windows.
119+
*
120+
* @param callable(int): void $handler
121+
*/
122+
protected function setWindowsCtrlHandler(callable $handler, bool $add): bool
123+
{
124+
return sapi_windows_set_ctrl_handler($handler, $add);
125+
}
126+
89127
/**
90128
* Register signal handlers.
91129
*
130+
* On Windows only Ctrl+C and Ctrl+Break can be handled. They are reported
131+
* as SIGINT (2), and the SIG* constants are not available there, so call it
132+
* without arguments or pass `2`. Any other signal is ignored.
133+
*
92134
* @param list<int> $signals List of signals to handle
93135
* @param array<int, string> $methodMap Optional signal-to-method mapping
94136
*/
@@ -97,6 +139,10 @@ protected function registerSignals(
97139
array $methodMap = [],
98140
): void {
99141
if (! $this->isPcntlAvailable()) {
142+
if ($this->isWindowsCtrlHandlerAvailable()) {
143+
$this->registerWindowsCtrlHandler($signals, $methodMap);
144+
}
145+
100146
return;
101147
}
102148

@@ -131,6 +177,46 @@ protected function registerSignals(
131177
}
132178
}
133179

180+
/**
181+
* Register the handler for Ctrl+C and Ctrl+Break on Windows.
182+
*
183+
* @param list<int> $signals List of signals to handle
184+
* @param array<int, string> $methodMap Optional signal-to-method mapping
185+
*/
186+
private function registerWindowsCtrlHandler(array $signals, array $methodMap): void
187+
{
188+
if ($signals !== [] && ! in_array(self::INTERRUPT_SIGNAL, $signals, true)) {
189+
return;
190+
}
191+
192+
$this->signalMethodMap = $methodMap;
193+
194+
if ($this->windowsCtrlHandlerRegistered) {
195+
return;
196+
}
197+
198+
if ($this->setWindowsCtrlHandler([$this, 'handleWindowsCtrlEvent'], true)) {
199+
$this->windowsCtrlHandlerRegistered = true;
200+
$this->registeredSignals[] = self::INTERRUPT_SIGNAL;
201+
} else {
202+
CLI::write(lang('CLI.signals.failedSignal', [$this->getSignalName(self::INTERRUPT_SIGNAL)]), 'red');
203+
}
204+
}
205+
206+
/**
207+
* Handle a Windows console control event (Ctrl+C or Ctrl+Break).
208+
*
209+
* Windows hands the handler no signal number, so the event is reported as SIGINT.
210+
* Like on Unix, the process is no longer stopped by the event, and the command
211+
* is expected to check `shouldTerminate()` and exit by itself.
212+
*/
213+
protected function handleWindowsCtrlEvent(int $event): void
214+
{
215+
$this->callCustomHandler(self::INTERRUPT_SIGNAL);
216+
217+
$this->running = false;
218+
}
219+
134220
/**
135221
* Handle incoming signals.
136222
*/
@@ -302,6 +388,11 @@ protected function mapSignal(int $signal, string $method): void
302388
*/
303389
protected function getSignalName(int $signal): string
304390
{
391+
// The SIG* constants come from PCNTL, which Windows does not have.
392+
if (! $this->isPcntlAvailable()) {
393+
return $signal === self::INTERRUPT_SIGNAL ? 'SIGINT' : "Signal {$signal}";
394+
}
395+
305396
return match ($signal) {
306397
SIGTERM => 'SIGTERM',
307398
SIGINT => 'SIGINT',
@@ -322,6 +413,16 @@ protected function getSignalName(int $signal): string
322413
*/
323414
protected function unregisterSignals(): void
324415
{
416+
if ($this->windowsCtrlHandlerRegistered) {
417+
$this->setWindowsCtrlHandler([$this, 'handleWindowsCtrlEvent'], false);
418+
419+
$this->windowsCtrlHandlerRegistered = false;
420+
$this->registeredSignals = [];
421+
$this->signalMethodMap = [];
422+
423+
return;
424+
}
425+
325426
if (! $this->isPcntlAvailable()) {
326427
return;
327428
}

0 commit comments

Comments
 (0)