|
| 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 | + |
| 12 | +namespace Symfony\Component\Console\Helper; |
| 13 | + |
| 14 | +/** |
| 15 | + * TerminalInputHelper stops Ctrl-C and similar signals from leaving the terminal in |
| 16 | + * an unusable state if its settings have been modified when reading user input. |
| 17 | + * This can be an issue on non-Windows platforms. |
| 18 | + * |
| 19 | + * Usage: |
| 20 | + * |
| 21 | + * $inputHelper = new TerminalInputHelper($inputStream); |
| 22 | + * |
| 23 | + * ...change terminal settings |
| 24 | + * |
| 25 | + * // Wait for input before all input reads |
| 26 | + * $inputHelper->waitForInput(); |
| 27 | + * |
| 28 | + * ...read input |
| 29 | + * |
| 30 | + * // Call finish to restore terminal settings and signal handlers |
| 31 | + * $inputHelper->finish() |
| 32 | + * |
| 33 | + * @internal |
| 34 | + */ |
| 35 | +final class TerminalInputHelper |
| 36 | +{ |
| 37 | + /** @var resource */ |
| 38 | + private $inputStream; |
| 39 | + private bool $isStdin; |
| 40 | + private string $initialState; |
| 41 | + private int $signalToKill = 0; |
| 42 | + private array $signalHandlers = []; |
| 43 | + private array $targetSignals = []; |
| 44 | + |
| 45 | + /** |
| 46 | + * @param resource $inputStream |
| 47 | + * |
| 48 | + * @throws \RuntimeException If unable to read terminal settings |
| 49 | + */ |
| 50 | + public function __construct($inputStream) |
| 51 | + { |
| 52 | + if (!\is_string($state = shell_exec('stty -g'))) { |
| 53 | + throw new \RuntimeException('Unable to read the terminal settings.'); |
| 54 | + } |
| 55 | + $this->inputStream = $inputStream; |
| 56 | + $this->initialState = $state; |
| 57 | + $this->isStdin = 'php://stdin' === stream_get_meta_data($inputStream)['uri']; |
| 58 | + $this->createSignalHandlers(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Waits for input and terminates if sent a default signal. |
| 63 | + */ |
| 64 | + public function waitForInput(): void |
| 65 | + { |
| 66 | + if ($this->isStdin) { |
| 67 | + $r = [$this->inputStream]; |
| 68 | + $w = []; |
| 69 | + |
| 70 | + // Allow signal handlers to run, either before Enter is pressed |
| 71 | + // when icanon is enabled, or a single character is entered when |
| 72 | + // icanon is disabled |
| 73 | + while (0 === @stream_select($r, $w, $w, 0, 100)) { |
| 74 | + $r = [$this->inputStream]; |
| 75 | + } |
| 76 | + } |
| 77 | + $this->checkForKillSignal(); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Restores terminal state and signal handlers. |
| 82 | + */ |
| 83 | + public function finish(): void |
| 84 | + { |
| 85 | + // Safeguard in case an unhandled kill signal exists |
| 86 | + $this->checkForKillSignal(); |
| 87 | + shell_exec('stty '.$this->initialState); |
| 88 | + $this->signalToKill = 0; |
| 89 | + |
| 90 | + foreach ($this->signalHandlers as $signal => $originalHandler) { |
| 91 | + pcntl_signal($signal, $originalHandler); |
| 92 | + } |
| 93 | + $this->signalHandlers = []; |
| 94 | + $this->targetSignals = []; |
| 95 | + } |
| 96 | + |
| 97 | + private function createSignalHandlers(): void |
| 98 | + { |
| 99 | + if (!\function_exists('pcntl_async_signals') || !\function_exists('pcntl_signal')) { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + pcntl_async_signals(true); |
| 104 | + $this->targetSignals = [\SIGINT, \SIGQUIT, \SIGTERM]; |
| 105 | + |
| 106 | + foreach ($this->targetSignals as $signal) { |
| 107 | + $this->signalHandlers[$signal] = pcntl_signal_get_handler($signal); |
| 108 | + |
| 109 | + pcntl_signal($signal, function ($signal) { |
| 110 | + // Save current state, then restore to initial state |
| 111 | + $currentState = shell_exec('stty -g'); |
| 112 | + shell_exec('stty '.$this->initialState); |
| 113 | + $originalHandler = $this->signalHandlers[$signal]; |
| 114 | + |
| 115 | + if (\is_callable($originalHandler)) { |
| 116 | + $originalHandler($signal); |
| 117 | + // Handler did not exit, so restore to current state |
| 118 | + shell_exec('stty '.$currentState); |
| 119 | + |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + // Not a callable, so SIG_DFL or SIG_IGN |
| 124 | + if (\SIG_DFL === $originalHandler) { |
| 125 | + $this->signalToKill = $signal; |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private function checkForKillSignal(): void |
| 132 | + { |
| 133 | + if (\in_array($this->signalToKill, $this->targetSignals, true)) { |
| 134 | + // Try posix_kill |
| 135 | + if (\function_exists('posix_kill')) { |
| 136 | + pcntl_signal($this->signalToKill, \SIG_DFL); |
| 137 | + posix_kill(getmypid(), $this->signalToKill); |
| 138 | + } |
| 139 | + |
| 140 | + // Best attempt fallback |
| 141 | + exit(128 + $this->signalToKill); |
| 142 | + } |
| 143 | + } |
| 144 | +} |
0 commit comments