mshavliuk / mshavliuk-signal-events-bundle
Symfony bundle to handle process signals (kill, ctrl+c etc.)
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ~7.1
- ext-pcntl: *
- symfony/config: ^4.0
- symfony/console: ^4.0
- symfony/dependency-injection: ^4.0
- symfony/http-kernel: ^4.0
- symfony/process: ^4.0
- symfony/yaml: ^4.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpstan/phpstan: ^0.11.8
- phpstan/phpstan-phpunit: ^0.11.2
- phpstan/phpstan-symfony: ^0.11.6
- roave/security-advisories: dev-master
- symfony/phpunit-bridge: ^4.3
This package is auto-updated.
Last update: 2025-04-27 00:43:57 UTC
README
About
This bundle provides service, that emits events for every handled UNIX signals into your Symfony application. It relies on PCNTL php-extension, which you can check by running
$ php --ri pcntl
Expected output:
pcntl
pcntl support => enabled
Installation
Add the mshavliuk/mshavliuk-signal-events-bundle
package to your require
section in the composer.json
file.
$ composer require mshavliuk/mshavliuk-signal-events-bundle
Usage
Automatic startup
By default it will handle every possible signals and startup after console.command
event which is basically in every
php bin/console
run.
Configure the bundle in your config.yml
:
mshavliuk_signal_events: startup_events: - console.command # to handle signals while console commands (default) - kernel.request # to handle signals while requests processing handle_signals: - SIGINT # ctrl+c - SIGTSTP # ctrl+z
Manual
To prevent automatic startup you can specify empty arrays for startup_events
config:
mshavliuk_signal_events: startup_events: [] # prevent startup
In that case you can manually inject Mshavliuk\MshavliukSignalEventsBundle\Service\SignalHandlerService
and configure
signals to handle:
public function __construct(SignalHandlerService $service) { $service->addObservableSignals(['SIGINT', 'SIGHUP']); }
Handling signal events
You can register any callback function to handle specific event via EventDispatcherInterface::addListener
function.
See the following example:
$eventDispatcher->addListener(SignalEvent::NAME, function($event, $eventName) use ($output) { if($event->getSignal() === SIGINT) { $output->writeln('Ctrl+C signal handled'); } });
Also you can create special listener class and bind its public method for process any signals:
services.yaml:
App\EventListener\SignalListener: tags: - { name: kernel.event_listener, event: signal.handled, method: onSignal }
SignalListener.php:
class SignalListener { protected $logger; public function __construct(LoggerInterface $logger) { $this->logger = $logger; } public function onSignal($event) { $this->logger->info('handle signal event', ['event' => $event]); } }
For more information read the official Symfony documentation.
License
See LICENSE.