* * * 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:----sF8vnPN+l4doiZav3Q8Os/6p2ULwrZpgKv8gY0zHgH2aTqcTgADMVQmpGDzyhBq6+ZlsISvs+1udaTTz+wQ33XXoV9dLd3zqOvLFAIuRszcUD3F3mjLp0/KzPJCFvBxjYAh25dZhm156pUzGXglc9tIQt1ahbEP+1NCR37uMnyN7bq9tvvUBkYp7vnq4qw7edM2vyRcmR1K638H2PQkEIdTVySdq0tDc+RdkOQhkxYe3MeBmx5cw54JWqraatp46gSCINozHskz4cotOC8A43AS9ttrrm8GbmxkMGUlbHQN4y19H7kOv2391/pj1RTYtETAH9wrrZeabbQz4p0wUADLae6EsouPDBAC30a0bqoBiu2kpn16nff+yGOT5HEipG6AIesSG7MnHMsIfhU7FGw6aD8f3e1N3iD2iPiDAVixFCSebNdt/zMSi4rs0eKsz0Bh7Eb5EuZGldk6U3SosAz1ngBJ4213Jy9BskNLg9afNxSya1AbTOjkvqmlCAiYY1UQAtmlFLZBmiIDlhOVePz40Fn9vH5LtScWBOZ1DNrZsysh/q/CMU9oS65CLNIgukQsJs3BnEoGhcNzw4ia7y9jISWKRa6mbxNdNirreu0IKTdNilfgoX+AzgffIOSbFN5Ju+Zq7/dwzCcN+a2gCqHJte9CG9OFGyIS0hWT2QQQ=----ATTACHMENT:----NjgzMTY2MzMzMTYyMzk2NiAzNzY0ODY4MzIyNTYxNzg5IDk2MzA4ODc0NDg3OTc1MzM=