SignalRegistry.php 3.8 KB

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