| 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\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 |
private bool $withStty; |
| 45 |
|
| 46 |
/** |
| 47 |
* @param resource $inputStream |
| 48 |
* |
| 49 |
* @throws \RuntimeException If unable to read terminal settings |
| 50 |
*/ |
| 51 |
public function __construct($inputStream, bool $withStty = true) |
| 52 |
{ |
| 53 |
$this->inputStream = $inputStream; |
| 54 |
$this->isStdin = 'php://stdin' === stream_get_meta_data($inputStream)['uri']; |
| 55 |
$this->withStty = $withStty; |
| 56 |
|
| 57 |
if ($withStty) { |
| 58 |
if (!\is_string($state = shell_exec('stty -g'))) { |
| 59 |
throw new \RuntimeException('Unable to read the terminal settings.'); |
| 60 |
} |
| 61 |
|
| 62 |
$this->initialState = $state; |
| 63 |
|
| 64 |
$this->createSignalHandlers(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Waits for input. |
| 70 |
*/ |
| 71 |
public function waitForInput(): void |
| 72 |
{ |
| 73 |
if ($this->isStdin) { |
| 74 |
$r = [$this->inputStream]; |
| 75 |
$w = []; |
| 76 |
|
| 77 |
// Allow signal handlers to run |
| 78 |
while (0 === @stream_select($r, $w, $w, 0, 100)) { |
| 79 |
$r = [$this->inputStream]; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ($this->withStty) { |
| 84 |
$this->checkForKillSignal(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Restores terminal state and signal handlers. |
| 90 |
*/ |
| 91 |
public function finish(): void |
| 92 |
{ |
| 93 |
if (!$this->withStty) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Safeguard in case an unhandled kill signal exists |
| 98 |
$this->checkForKillSignal(); |
| 99 |
shell_exec('stty '.$this->initialState); |
| 100 |
$this->signalToKill = 0; |
| 101 |
|
| 102 |
foreach ($this->signalHandlers as $signal => $originalHandler) { |
| 103 |
pcntl_signal($signal, $originalHandler); |
| 104 |
} |
| 105 |
$this->signalHandlers = []; |
| 106 |
$this->targetSignals = []; |
| 107 |
} |
| 108 |
|
| 109 |
private function createSignalHandlers(): void |
| 110 |
{ |
| 111 |
if (!\function_exists('pcntl_async_signals') || !\function_exists('pcntl_signal')) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
pcntl_async_signals(true); |
| 116 |
$this->targetSignals = [\SIGINT, \SIGQUIT, \SIGTERM]; |
| 117 |
|
| 118 |
foreach ($this->targetSignals as $signal) { |
| 119 |
$this->signalHandlers[$signal] = pcntl_signal_get_handler($signal); |
| 120 |
|
| 121 |
pcntl_signal($signal, function ($signal) { |
| 122 |
// Save current state, then restore to initial state |
| 123 |
$currentState = shell_exec('stty -g'); |
| 124 |
shell_exec('stty '.$this->initialState); |
| 125 |
$originalHandler = $this->signalHandlers[$signal]; |
| 126 |
|
| 127 |
if (\is_callable($originalHandler)) { |
| 128 |
$originalHandler($signal); |
| 129 |
// Handler did not exit, so restore to current state |
| 130 |
shell_exec('stty '.$currentState); |
| 131 |
|
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// Not a callable, so SIG_DFL or SIG_IGN |
| 136 |
if (\SIG_DFL === $originalHandler) { |
| 137 |
$this->signalToKill = $signal; |
| 138 |
} |
| 139 |
}); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private function checkForKillSignal(): void |
| 144 |
{ |
| 145 |
if (\in_array($this->signalToKill, $this->targetSignals, true)) { |
| 146 |
// Try posix_kill |
| 147 |
if (\function_exists('posix_kill')) { |
| 148 |
pcntl_signal($this->signalToKill, \SIG_DFL); |
| 149 |
posix_kill(getmypid(), $this->signalToKill); |
| 150 |
} |
| 151 |
|
| 152 |
// Best attempt fallback |
| 153 |
exit(128 + $this->signalToKill); |
| 154 |
} |
| 155 |
} |
| 156 |
} |