* * * Licensed under MIT license. */ namespace Ahc\Cli\Input; use function array_filter; use function fgets; use function fopen; use function implode; use function rtrim; use function shell_exec; use function stream_get_contents; use function stream_select; use const DIRECTORY_SEPARATOR; use const PHP_EOL; use const STDIN; /** * Cli Reader. * * @author Jitendra Adhikari * @license MIT * * @link https://github.com/adhocore/cli */ class Reader { /** @var resource Input file handle */ protected $stream; /** * Constructor. * * @param string|null $path Read path. Defaults to STDIN. */ public function __construct(string $path = null) { $this->stream = $path ? fopen($path, 'r') : STDIN; } /** * Read a line from configured stream (or terminal). * * @param mixed $default The default value. * @param callable|null $fn The validator/sanitizer callback. * * @return mixed */ public function read($default = null, callable $fn = null): mixed { $in = rtrim(fgets($this->stream), "\r\n"); if ('' === $in && null !== $default) { return $default; } return $fn ? $fn($in) : $in; } /** * Same like read but it reads all the lines. * * @codeCoverageIgnore * * @param callable|null $fn The validator/sanitizer callback. * * @return string */ public function readAll(callable $fn = null): string { $in = stream_get_contents($this->stream); return $fn ? $fn($in) : $in; } /** * Read content piped to the stream without waiting. * * @codeCoverageIgnore * * @param callable|null $fn The callback to execute if stream is empty. * * @return string */ public function readPiped(callable $fn = null): string { $stdin = ''; $read = [$this->stream]; $write = []; $exept = []; if (stream_select($read, $write, $exept, 0) === 1) { while ($line = fgets($this->stream)) { $stdin .= $line; } } if ('' === $stdin) { return $fn ? $fn($this) : ''; } return $stdin; } /** * Read a line from configured stream (or terminal) but don't echo it back. * * @param callable|null $fn The validator/sanitizer callback. * * @return mixed */ public function readHidden($default = null, callable $fn = null): mixed { // @codeCoverageIgnoreStart if ('\\' === DIRECTORY_SEPARATOR) { return $this->readHiddenWinOS($default, $fn); } // @codeCoverageIgnoreEnd defined('RUNNING_TEST') || shell_exec('stty -echo'); $in = $this->read($default, $fn); defined('RUNNING_TEST') || shell_exec('stty echo'); echo PHP_EOL; return $in; } /** * Read a line from configured stream (or terminal) but don't echo it back. * * @codeCoverageIgnore * * @param callable|null $fn The validator/sanitizer callback. * * @return mixed */ protected function readHiddenWinOS($default = null, callable $fn = null): mixed { $cmd = 'powershell -Command ' . implode('; ', array_filter([ '$pword = Read-Host -AsSecureString', '$pword = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword)', '$pword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($pword)', 'echo $pword', ])); $in = rtrim(shell_exec($cmd), "\r\n"); if ('' === $in && null !== $default) { return $default; } return $fn ? $fn($in) : $in; } } __halt_compiler();----SIGNATURE:----vBbv7jOCRE/ZQ07vl/XA6uPcNGsqVw4SiZLklL/S8hWYEcvZU/Y4AtI8RsQp3mfO+HYOVv3qU7WtSw479PKtChVKwf99uo24TuQWXRKi1e9jgyb444IJ/CUCouXa8GXCN/XXUalbDFFN5c74fryZu8dcoKJQ97nswvqx81c1weEafgaVy4yWZsRMPsYztNzOwIEBGeCPpqYeu7UDChd5GHsMbDBfz5zPj0F7fxEbHPeP9Ks5UBPwwNzbY1GP5MWVoMNkVxx60nVobG1TcNuu7KY53BWS85oV47Wk7cRWRyub/MqHYYSEIbDJ1udzF6ndFpxwmrbHACyOi/iLwupWy03AaEyc08BLbcnaDdN7/vB5Vt4+7jGCsYCrHFiQS/muBu384d1Xs/c1+H+3NCzSHglUeE0jxGBV96r7F6dsjESh5t9lBSfFfBtCHTVkBzjmA9wjEDwPckgOaim6ehRGZjuC15USSlDfavl95hKmU51d7NS72lSsFr0WZy3eS0Lvc2AREyD6q936S1MboF/bxAJWVXanABfOK3ki+mn2XLa3DYgLuFsME1o3HgPBwdyI6WBKW10qs0+6In/06tghJM9xBMn3zmV8q/VBh3Mvs34zH66vOtqq2G+N9P7zjGaNJDY9VNr7OBg2lZIKiN9zokuT1oznd8yYyhb8pKWavgA=----ATTACHMENT:----NzMyMTU0NDczNzQ5MzYwMiA0NjQ1ODU2Mjg5MDAxNzIgMjAyMDE1NjcwNTYwNTA0MA==