* @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:----3qs/QKfBvFXoQ0Aq+TltySxF7/GZiInxVPk2rOvMkEJ4Z7/3cQO4DTI7Y3L/BvwDbNnaoPOhB8wL+OdLFwn70DeG9nttQVXcsOcAkty0VFs+DDn/5Q5w796QeiTBuHl3QfnOwnnHk8KAM3T65G/W5W0PqHx22ysD9ZmGuuGEBY2Osv/fB6bePb5NRXNORJotzX81Fd6u1RU0r5CDdI3jMfn8DSm8RGc/XQ0KZpWIPTDEcbwMw9aLrapcyOfX9ieEkOjvQHyTiRBr7zGXIlIqeTVoNoyyHjaN10iVfdrAR0UyfQwxaFjxZWrzDVwXOKk3J6K4HlBPfMg0xk4LHzyXxWGWRvhqzJC+pCF6092IQu191tQnLhpie5nz8hIMv6AtUQr91BZ113KwthsTlw0FPZpCjmtcY1idORlXmC2N0T4wURM1fYqEJ6B0rBcPo6HF22L342k1H30NJGRz3biSvUjJLq/xu7ZiPp/Yu4UciRaDvpq8f60iNaw4yZfVTWd8tlcIXm9oRITalCJojWOG739JL7Ifug5CD3l04AINnXy1QfzHihBvvFxhmUeLCdZk7yT3XwJjuuU7wjC4jRtyXNqTnZFRvtnHEQmywXYD4132OV26/PXBdeB5+HMsi0QwfPvM5SaGsw+iRI6fSWr4KgfOCSgrmIJhjdyWTijMRao=----ATTACHMENT:----MzY2OTI4NzM0NDA3NTM2NyA0MDY1MDQwMjA0MDc0MDEyIDczNjQ4Nzg0MjkxNTUxMzg=