* @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:----HEjPF4/kKC2IN+McJmy3Tf9RqG1N/qUu0ll5tCpMGKL69bgtN32Rd+Ul2p0J2yswX4S8UPv61bbC2YvAJSeoYoabq91xuF8peobfYshw+i/zmCvoecBXpY2S40eMsJV6duwQngHUBbyHI847y0y9fexWIW4cNErIsrBVNQW8a+IaXhcZwlFN5n9ouw8sNYFhPbxa1o+hYMPmATvf/wBX9T4/ckm/USPCXvfj/COioUxZ5nKUeO4SlR2ht7iybjoUI2w1l0+cgDjrHnMmyMerpxVYJgqQJK3Lf2XnnFY4G9BE1XJ4ePOLLMDs3LRccpgWBuI6LJJIBuOrteZJFi6vl9hHYyHr8wqz51Ip/Qro3XGe+dlH2CnC4iD0QlgSO0sXfka6qHwcEfqNM4hhWaSk1v2M6XCz5RBYuGs+yRsJUYNi4uGFg8yip5tXDNmKGjhFG17PKVa223yCk+T/5JDv3UhdOLV/03DM4s9pghw/t2UrADwL9AqVAs9Lmw4136O48a8b/5u3SqY1dWE+iWasK++0MrONjrAkGuIQSyoTHjiryk2STeJbDorPYvTPMnNhetFIuXrSia9ikQojXLrhd0zyz6DQ4H0qAjBYDHyAnc1XlporpLGadY/txEQMMQ43BDHp8g4QOJvSLfhbEcds5ACbke3NIDRqtpJ9V9QjKT8=----ATTACHMENT:----ODM4NzMzMDE1Nzk1MzgzMSAyNTY2MDI4MTE3Mjc1NDI4IDI0OTM5NjA3NjE1NzMxMDI=