12 namespace Symfony\Component\Console\Helper;
49 return $this->
doAsk($output, $question);
54 $interviewer =
function () use ($output, $question, $that) {
55 return $that->doAsk($output, $question);
72 if (!is_resource($stream)) {
73 throw new \InvalidArgumentException(
'Input stream must be a valid resource.');
76 $this->inputStream = $stream;
122 }
catch (\RuntimeException $e) {
129 if (
false === $ret) {
131 if (
false === $ret) {
132 throw new \RuntimeException(
'Aborted');
143 return $normalizer($ret);
160 $width = max(array_map(
'strlen', array_keys($question->getChoices())));
163 foreach ($question->getChoices() as $key => $value) {
164 $messages[] = sprintf(
" [<info>%-${width}s</info>] %s", $key, $value);
169 $message = $question->getPrompt();
172 $output->
write($message);
184 $message = $this->
getHelperSet()->get(
'formatter')->formatBlock($error->getMessage(),
'error');
186 $message =
'<error>'.$error->getMessage().
'</error>';
207 $matches = $autocomplete;
208 $numMatches = count($matches);
210 $sttyMode = shell_exec(
'stty -g');
213 shell_exec(
'stty -icanon -echo');
224 if (0 === $numMatches && 0 !== $i) {
227 $output->
write(
"\033[1D");
232 $matches = $autocomplete;
233 $numMatches = count($matches);
239 $ret = substr($ret, 0, $i);
240 }
elseif (
"\033" === $c) {
245 if (isset($c[2]) && (
'A' === $c[2] ||
'B' === $c[2])) {
246 if (
'A' === $c[2] && -1 === $ofs) {
250 if (0 === $numMatches) {
254 $ofs += (
'A' === $c[2]) ? -1 : 1;
255 $ofs = ($numMatches + $ofs) % $numMatches;
258 if (
"\t" === $c ||
"\n" === $c) {
259 if ($numMatches > 0 && -1 !== $ofs) {
260 $ret = $matches[$ofs];
262 $output->
write(substr($ret, $i));
283 foreach ($autocomplete as $value) {
285 if (0 === strpos($value, $ret) && $i !==
strlen($value)) {
286 $matches[$numMatches++] = $value;
292 $output->
write(
"\033[K");
294 if ($numMatches > 0 && -1 !== $ofs) {
296 $output->
write(
"\0337");
298 $output->
write(
'<hl>'.substr($matches[$ofs], $i).
'</hl>');
300 $output->
write(
"\0338");
305 shell_exec(sprintf(
'stty %s', $sttyMode));
321 if (
'\\' === DIRECTORY_SEPARATOR) {
322 $exe = __DIR__.
'/../Resources/bin/hiddeninput.exe';
325 if (
'phar:' === substr(__FILE__, 0, 5)) {
326 $tmpExe = sys_get_temp_dir().
'/hiddeninput.exe';
331 $value = rtrim(shell_exec($exe));
334 if (isset($tmpExe)) {
342 $sttyMode = shell_exec(
'stty -g');
344 shell_exec(
'stty -echo');
346 shell_exec(sprintf(
'stty %s', $sttyMode));
348 if (
false === $value) {
349 throw new \RuntimeException(
'Aborted');
352 $value = trim($value);
359 $readCmd =
$shell ===
'csh' ?
'set mypassword = $<' :
'read -r mypassword';
360 $command = sprintf(
"/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'",
$shell, $readCmd);
361 $value = rtrim(shell_exec($command));
367 throw new \RuntimeException(
'Unable to hide the response.');
385 while (null === $attempts || $attempts--) {
386 if (null !== $error) {
391 return call_user_func($question->
getValidator(), $interviewer());
392 }
catch (\Exception $error) {
406 if (null !== self::$shell) {
410 self::$shell =
false;
412 if (file_exists(
'/usr/bin/env')) {
414 $test =
"/usr/bin/env %s -c 'echo OK' 2> /dev/null";
415 foreach (array(
'bash',
'zsh',
'ksh',
'csh') as $sh) {
416 if (
'OK' === rtrim(shell_exec(sprintf($test, $sh)))) {
433 if (null !== self::$stty) {
437 exec(
'stty 2>&1', $output, $exitcode);
439 return self::$stty = $exitcode === 0;