Skip to content

Commit 742c622

Browse files
committed
feat(reader): add read hidden for win os
1 parent 0db193b commit 742c622

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/Input/Reader.php

+33
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ public function read($default = null, callable $fn = null)
6262
*/
6363
public function readHidden($default = null, callable $fn = null)
6464
{
65+
// @codeCoverageIgnoreStart
66+
if ('\\' === \DIRECTORY_SEPARATOR) {
67+
return $this->readHiddenWinOS($default, $fn);
68+
}
69+
// @codeCoverageIgnoreEnd
70+
6571
\shell_exec('stty -echo');
6672
$in = $this->read($default, $fn);
6773
\shell_exec('stty echo');
@@ -70,4 +76,31 @@ public function readHidden($default = null, callable $fn = null)
7076

7177
return $in;
7278
}
79+
80+
/**
81+
* Read a line from configured stream (or terminal) but don't echo it back.
82+
*
83+
* @codeCoverageIgnore
84+
*
85+
* @param callable|null $fn The validator/sanitizer callback.
86+
*
87+
* @return mixed
88+
*/
89+
private function readHiddenWinOS($default = null, callable $fn = null)
90+
{
91+
$cmd = 'powershell -Command ' . \implode('; ', \array_filter([
92+
'$pword = Read-Host -AsSecureString',
93+
'$pword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword)',
94+
'$pword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($pword)',
95+
'echo $pword',
96+
]));
97+
98+
$in = \rtrim(\shell_exec($cmd), "\r\n");
99+
100+
if ('' === $in && null !== $default) {
101+
return $default;
102+
}
103+
104+
return $fn ? $fn($in) : $in;
105+
}
73106
}

0 commit comments

Comments
 (0)