SignalRegistry.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $this->stack[] = $this->signalHandlers;
  70. $this->signalHandlers = [];
  71. }
  72. /**
  73. * Restores the previous handlers from the stack, making them active.
  74. *
  75. * This also restores the original OS-level signal handler if no
  76. * more handlers are registered for a signal that was just popped.
  77. *
  78. * @internal
  79. */
  80. public function popPreviousHandlers(): void
  81. {
  82. $popped = $this->signalHandlers;
  83. $this->signalHandlers = array_pop($this->stack) ?? [];
  84. // Restore OS handler if no more Symfony handlers for this signal
  85. foreach ($popped as $signal => $handlers) {
  86. if (!($this->signalHandlers[$signal] ?? false) && isset($this->originalHandlers[$signal])) {
  87. pcntl_signal($signal, $this->originalHandlers[$signal]);
  88. }
  89. }
  90. }
  91. /**
  92. * @internal
  93. */
  94. public function scheduleAlarm(int $seconds): void
  95. {
  96. pcntl_alarm($seconds);
  97. }
  98. }