### Installation Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md Installs the spatie/laravel-signal-aware-command package using Composer. ```bash composer require spatie/laravel-signal-aware-command ``` -------------------------------- ### Handling SIGINT Signal on Command Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md An example of implementing the `onSigint` method within a command that extends `SignalAwareCommand` to handle the SIGINT signal. ```php use Spatie\SignalAwareCommand\SignalAwareCommand; class YourCommand extends SignalAwareCommand { protected $signature = 'your-command'; public function handle() { $this->info('Command started...'); sleep(100); } public function onSigint() { // will be executed when you stop the command $this->info('You stopped the command!'); } } ``` -------------------------------- ### Making Artisan Command Signal Aware Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md Shows the basic structure for making an Artisan command signal aware by extending the `SignalAwareCommand` class. ```php use Spatie\SignalAwareCommand\SignalAwareCommand; class YourCommand extends SignalAwareCommand { // your code } ``` -------------------------------- ### Running Tests Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md This command is used to execute the test suite for the package, ensuring that all functionalities are working as expected and to verify any changes made during development. ```bash composer test ``` -------------------------------- ### Basic Signal Handling in Artisan Command Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md Demonstrates how to extend `SignalAwareCommand` and implement an `onSigint` method to handle the SIGINT signal when the command is stopped. ```php use Spatie\SignalAwareCommand\SignalAwareCommand; class YourCommand extends SignalAwareCommand { protected $signature = 'your-command'; public function handle() { $this->info('Command started...'); sleep(100); } public function onSigint() { // will be executed when you stop the command $this->info('You stopped the command!'); } } ``` -------------------------------- ### Registering Signal Handling in a Command Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md This snippet demonstrates how to extend the `SignalAwareCommand` base class and define the `handlesSignals` property to specify which signals the command should listen for. The `handle` method contains the command's main logic, including a `sleep` call to keep the command running and responsive to signals. ```php use Spatie\SignalAwareCommand\SignalAwareCommand; class YourCommand extends SignalAwareCommand { protected $signature = 'your-command'; protected $handlesSignals = [SIGINT]; public function handle() { (new SomeOtherClass())->performSomeWork(); sleep(100); } } ``` -------------------------------- ### Registering Signals to Handle Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md Defines the `handlesSignals` property in a command extending `SignalAwareCommand` to specify which signals the command should listen for. ```php use Spatie\SignalAwareCommand\SignalAwareCommand; class YourCommand extends SignalAwareCommand { protected $signature = 'your-command'; protected $handlesSignals = [SIGINT]; public function handle() { (new SomeOtherClass())->performSomeWork(); sleep(100); } } ``` -------------------------------- ### Registering Signal Handler with Signal Facade Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md Uses the `Signal` facade to register a callback function that will be executed when a specific signal (SIGINT) is received by a command. ```php use Illuminate\Console\Command; use Spatie\SignalAwareCommand\Facades\Signal; class SomeOtherClass { public function performSomeWork() { Signal::handle(SIGINT, function(Command $commandThatReceivedSignal) { $commandThatReceivedSignal->info('Received the SIGINT signal!'); }) } } ``` -------------------------------- ### Listening for SignalReceived Event Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md This snippet shows how to listen for the `Spatie\SignalAwareCommand\Events\SignalReceived` event in any class. The event listener receives the `SignalReceived` event object, from which the signal number and name can be accessed. It also provides access to the command instance that triggered the event, allowing for interaction like displaying messages. ```php use Spatie\SignalAwareCommand\Events\SignalReceived; use Spatie\SignalAwareCommand\Signals; class SomeOtherClass { public function performSomeWork() { Event::listen(function(SignalReceived $event) { $signalNumber = $event->signal; $signalName = Signals::getSignalName($signalNumber); $event->command->info("Received the {$signalName} signal"); }); } } ``` -------------------------------- ### Clearing Signal Handlers Source: https://github.com/spatie/laravel-signal-aware-command/blob/main/README.md Demonstrates how to remove a previously registered signal handler for a specific signal using `Signal::clearHandlers()` and how to clear all handlers. ```php use Spatie\SignalAwareCommand\Facades\Signal; public function performSomeWork() { Signal::handle(SIGNINT, function() { // perform cleanup }); $this->doSomeWork(); // at this point doSomeWork was executed without any problems // running a cleanup isn't necessary anymore Signal::clearHandlers(SIGINT); } // To clear all handlers for all signals use Signal::clearHandlers(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.