| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\SignalRegistry; |
| 13 |
|
| 14 |
final class SignalRegistry |
| 15 |
{ |
| 16 |
/** |
| 17 |
* @var array<int, array<callable>> |
| 18 |
*/ |
| 19 |
private array $signalHandlers = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array<array<int, array<callable>>> |
| 23 |
*/ |
| 24 |
private array $stack = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array<int, callable|int|string> |
| 28 |
*/ |
| 29 |
private array $originalHandlers = []; |
| 30 |
|
| 31 |
public function __construct() |
| 32 |
{ |
| 33 |
if (\function_exists('pcntl_async_signals')) { |
| 34 |
pcntl_async_signals(true); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function register(int $signal, callable $signalHandler): void |
| 39 |
{ |
| 40 |
$previous = pcntl_signal_get_handler($signal); |
| 41 |
|
| 42 |
if (!isset($this->originalHandlers[$signal])) { |
| 43 |
$this->originalHandlers[$signal] = $previous; |
| 44 |
} |
| 45 |
|
| 46 |
if (!isset($this->signalHandlers[$signal])) { |
| 47 |
if (\is_callable($previous) && [$this, 'handle'] !== $previous) { |
| 48 |
$this->signalHandlers[$signal][] = $previous; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$this->signalHandlers[$signal][] = $signalHandler; |
| 53 |
|
| 54 |
pcntl_signal($signal, [$this, 'handle']); |
| 55 |
} |
| 56 |
|
| 57 |
public static function isSupported(): bool |
| 58 |
{ |
| 59 |
return \function_exists('pcntl_signal'); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @internal |
| 64 |
*/ |
| 65 |
public function handle(int $signal): void |
| 66 |
{ |
| 67 |
$count = \count($this->signalHandlers[$signal]); |
| 68 |
|
| 69 |
foreach ($this->signalHandlers[$signal] as $i => $signalHandler) { |
| 70 |
$hasNext = $i !== $count - 1; |
| 71 |
$signalHandler($signal, $hasNext); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Pushes the current active handlers onto the stack and clears the active list. |
| 77 |
* |
| 78 |
* This prepares the registry for a new set of handlers within a specific scope. |
| 79 |
* |
| 80 |
* @internal |
| 81 |
*/ |
| 82 |
public function pushCurrentHandlers(): void |
| 83 |
{ |
| 84 |
// Restore the original OS-level disposition while the active map is empty, |
| 85 |
// so signals are not routed through handle() without a registered handler. |
| 86 |
foreach ($this->signalHandlers as $signal => $handlers) { |
| 87 |
if (isset($this->originalHandlers[$signal])) { |
| 88 |
pcntl_signal($signal, $this->originalHandlers[$signal]); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$this->stack[] = $this->signalHandlers; |
| 93 |
$this->signalHandlers = []; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Restores the previous handlers from the stack, making them active. |
| 98 |
* |
| 99 |
* This also restores the original OS-level signal handler if no |
| 100 |
* more handlers are registered for a signal that was just popped. |
| 101 |
* |
| 102 |
* @internal |
| 103 |
*/ |
| 104 |
public function popPreviousHandlers(): void |
| 105 |
{ |
| 106 |
$popped = $this->signalHandlers; |
| 107 |
$previous = array_pop($this->stack) ?? []; |
| 108 |
|
| 109 |
// Expose a transitional superset so handle() never reads a missing key |
| 110 |
// if a signal lands while the OS-level handlers below are being swapped. |
| 111 |
$this->signalHandlers = $previous + $popped; |
| 112 |
|
| 113 |
// Reinstall the registry handler for signals owned by the restored scope. |
| 114 |
foreach ($previous as $signal => $handlers) { |
| 115 |
pcntl_signal($signal, [$this, 'handle']); |
| 116 |
} |
| 117 |
|
| 118 |
// Restore the original OS-level handler for signals no scope owns anymore. |
| 119 |
foreach ($popped as $signal => $handlers) { |
| 120 |
if (!isset($previous[$signal]) && isset($this->originalHandlers[$signal])) { |
| 121 |
pcntl_signal($signal, $this->originalHandlers[$signal]); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$this->signalHandlers = $previous; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @internal |
| 130 |
*/ |
| 131 |
public function scheduleAlarm(int $seconds): void |
| 132 |
{ |
| 133 |
pcntl_alarm($seconds); |
| 134 |
} |
| 135 |
} |