Skip to content

Commit dc86d70

Browse files
committed
docs: document SignalTrait support for Ctrl+C on Windows
1 parent 441eaeb commit dc86d70

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎docs/changelogs/v4.8.0.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ Release Date: Unreleased
223223
- Likewise, the `command()` function now also supports the above enhancements for command-line option parsing when using the function to run commands from code.
224224
- Added `make:request` generator command to scaffold [Form Request](../incoming/form_requests.md#form-requests) classes.
225225
- Added `key:rotate` command to demote the current `encryption.key` to `encryption.previousKeys` in **.env** and generate a new key. See [Rotating with the `key:rotate` Command](../libraries/encryption.md#rotating-with-the-keyrotate-command).
226+
- `SignalTrait` now handles Ctrl+C and Ctrl+Break on Windows, through `sapi_windows_set_ctrl_handler()`, so a command can stop gracefully there too. The events are reported as `SIGINT` (`2`). See [Windows](../cli/cli_signals.md#windows).
226227
- Added `AbstractCommand::callSilently()` to invoke another command with its output discarded, restoring the prior output afterwards. See [Calling Silently](../cli/cli_modern_commands.md#calling-silently).
227228
- The output side of the `CLI` class now lives in instance classes, with `CLI` delegating to them: `Output` (`print()`, `write()`, `error()`, `newLine()`, `deferNewLine()`, `beep()`, `clearScreen()`, `color()`, `wrap()`), `Terminal` (dimensions and color support), `ProgressBar`, `Table`, and `Color` (ANSI codes and `Color::width()`). `CLI::getOutput()` and `CLI::getTerminal()` return the shared instances. See [The Classes Behind the Facade](../cli/cli_library.md#the-classes-behind-the-facade).
228229
- Modern commands can write through `$this->getOutput()`, the same `Output` used by the static `CLI` methods. `AbstractCommand::run()` and `Commands::runCommand()` accept an `Output` to run against, which `Console` passes down and sub-commands inherit. See [Writing Output](../cli/cli_modern_commands.md#writing-output).

‎docs/cli/cli_signals.md‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ These signals are handled directly by the kernel and will terminate or suspend y
3636

3737
Signal handling requires:
3838

39-
- **Unix-based system** (Linux, macOS, BSD) - Windows is not supported
39+
- **Unix-based system** (Linux, macOS, BSD) - see [Windows](#windows) for Windows
4040
- **PCNTL extension** - for signal registration and handling
4141
- **POSIX extension** - required for pause/resume functionality (SIGTSTP/SIGCONT)
4242

4343
!!! note "Note"
4444
On systems without these extensions, the SignalTrait will gracefully degrade and disable signal handling.
4545

46+
### Windows
47+
48+
Windows has no PCNTL extension and no Unix signals. Since v4.8.0, `SignalTrait` handles the one thing Windows can deliver to a PHP script: the console control events produced by **Ctrl+C** and **Ctrl+Break**. They are handled through PHP's `sapi_windows_set_ctrl_handler()` and reported to your command as `SIGINT` (`2`), so `isRunning()`, `shouldTerminate()`, custom handlers, and `onInterruption()` work as they do on Unix.
49+
50+
- Call `registerSignals()` without arguments. The `SIG*` constants come from the PCNTL extension, which is not available on Windows. If you pass a list, it must contain `2`. Every other signal is ignored.
51+
- To map the event to your own method, use `2` as the key: `$this->registerSignals([], [2 => 'handleInterrupt']);`.
52+
- `withSignalsBlocked()` runs the operation, but cannot block anything. The event does not stop the process by itself once a handler is registered, so a critical section is still completed. Your handler runs when the event arrives.
53+
- Pause and resume (`SIGTSTP` / `SIGCONT`), and every other signal, do not exist on Windows.
54+
55+
!!! important "Important"
56+
Once a handler is registered, Ctrl+C and Ctrl+Break no longer end the process. Your command must check `shouldTerminate()` and return, as on Unix.
57+
4658
## Using the SignalTrait
4759

4860
The `SignalTrait` provides a comprehensive signal handling system for CLI commands. To use it, simply add the trait to your command class and register signals in your command's `run()` method:
@@ -491,7 +503,7 @@ $this->registerSignals(
491503
```
492504

493505
!!! note "Note"
494-
Requires the PCNTL extension. On Windows, signal handling is automatically disabled.
506+
Requires the PCNTL extension. On Windows, only Ctrl+C and Ctrl+Break are handled, as `SIGINT` (`2`). See [Windows](#windows).
495507

496508
#### isRunning()
497509

0 commit comments

Comments
 (0)