SignalRegistry.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace IAWP_SCOPED\Symfony\Component\Console\SignalRegistry; |
| 12 | |
| 13 | /** @internal */ |
| 14 | final class SignalRegistry |
| 15 | { |
| 16 | private $signalHandlers = []; |
| 17 | public function __construct() |
| 18 | { |
| 19 | if (\function_exists('pcntl_async_signals')) { |
| 20 | \pcntl_async_signals(\true); |
| 21 | } |
| 22 | } |
| 23 | public function register(int $signal, callable $signalHandler) : void |
| 24 | { |
| 25 | if (!isset($this->signalHandlers[$signal])) { |
| 26 | $previousCallback = \pcntl_signal_get_handler($signal); |
| 27 | if (\is_callable($previousCallback)) { |
| 28 | $this->signalHandlers[$signal][] = $previousCallback; |
| 29 | } |
| 30 | } |
| 31 | $this->signalHandlers[$signal][] = $signalHandler; |
| 32 | \pcntl_signal($signal, [$this, 'handle']); |
| 33 | } |
| 34 | public static function isSupported() : bool |
| 35 | { |
| 36 | if (!\function_exists('pcntl_signal')) { |
| 37 | return \false; |
| 38 | } |
| 39 | if (\in_array('pcntl_signal', \explode(',', \ini_get('disable_functions')))) { |
| 40 | return \false; |
| 41 | } |
| 42 | return \true; |
| 43 | } |
| 44 | /** |
| 45 | * @internal |
| 46 | */ |
| 47 | public function handle(int $signal) : void |
| 48 | { |
| 49 | $count = \count($this->signalHandlers[$signal]); |
| 50 | foreach ($this->signalHandlers[$signal] as $i => $signalHandler) { |
| 51 | $hasNext = $i !== $count - 1; |
| 52 | $signalHandler($signal, $hasNext); |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 |