command io readSecretLine

This commit is contained in:
Yuri Kuznetsov
2023-02-22 17:40:41 +02:00
parent 474759ab6e
commit ae27f360ca
2 changed files with 27 additions and 1 deletions
@@ -76,7 +76,7 @@ class SetPassword implements Command
$io->writeLine("Enter a new password:");
$password = $io->readLine();
$password = $io->readSecretLine();
if (!$password) {
$io->writeLine("Password can not be empty.");
+26
View File
@@ -64,6 +64,19 @@ class IO
* Read a line from input. A string is trimmed.
*/
public function readLine(): string
{
return $this->readLineInternal();
}
/**
* Read a secret line from input. A string is trimmed.
*/
public function readSecretLine(): string
{
return $this->readLineInternal(true);
}
private function readLineInternal(bool $secret = false): string
{
$resource = fopen('php://stdin', 'r');
@@ -71,8 +84,16 @@ class IO
throw new RuntimeException("Could not open stdin.");
}
if ($secret && !self::isWindows()) {
shell_exec('stty -echo');
}
$readString = fgets($resource);
if ($secret && !self::isWindows()) {
shell_exec('stty echo');
}
if ($readString === false) {
$readString = '';
}
@@ -84,6 +105,11 @@ class IO
return $string;
}
private static function isWindows(): bool
{
return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') === 0;
}
/**
* Set exit-status.
*