You are here

public function DialogHelper::askHiddenResponse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/console/Helper/DialogHelper.php \Symfony\Component\Console\Helper\DialogHelper::askHiddenResponse()

Asks a question to the user, the response is hidden.

Parameters

OutputInterface $output An Output instance:

string|array $question The question:

bool $fallback In case the response can not be hidden, whether to fallback on non-hidden question or not:

Return value

string The answer

Throws

\RuntimeException In case the fallback is deactivated and the response can not be hidden

File

vendor/symfony/console/Helper/DialogHelper.php, line 270

Class

DialogHelper
The Dialog class provides helpers to interact with the user.

Namespace

Symfony\Component\Console\Helper

Code

public function askHiddenResponse(OutputInterface $output, $question, $fallback = true) {
  if ('\\' === DIRECTORY_SEPARATOR) {
    $exe = __DIR__ . '/../Resources/bin/hiddeninput.exe';

    // handle code running from a phar
    if ('phar:' === substr(__FILE__, 0, 5)) {
      $tmpExe = sys_get_temp_dir() . '/hiddeninput.exe';
      copy($exe, $tmpExe);
      $exe = $tmpExe;
    }
    $output
      ->write($question);
    $value = rtrim(shell_exec($exe));
    $output
      ->writeln('');
    if (isset($tmpExe)) {
      unlink($tmpExe);
    }
    return $value;
  }
  if ($this
    ->hasSttyAvailable()) {
    $output
      ->write($question);
    $sttyMode = shell_exec('stty -g');
    shell_exec('stty -echo');
    $value = fgets($this->inputStream ?: STDIN, 4096);
    shell_exec(sprintf('stty %s', $sttyMode));
    if (false === $value) {
      throw new \RuntimeException('Aborted');
    }
    $value = trim($value);
    $output
      ->writeln('');
    return $value;
  }
  if (false !== ($shell = $this
    ->getShell())) {
    $output
      ->write($question);
    $readCmd = $shell === 'csh' ? 'set mypassword = $<' : 'read -r mypassword';
    $command = sprintf("/usr/bin/env %s -c 'stty -echo; %s; stty echo; echo \$mypassword'", $shell, $readCmd);
    $value = rtrim(shell_exec($command));
    $output
      ->writeln('');
    return $value;
  }
  if ($fallback) {
    return $this
      ->ask($output, $question);
  }
  throw new \RuntimeException('Unable to hide the response');
}