summaryrefslogtreecommitdiffstats
path: root/vendor/gipfl/cli/src/Tty.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:23:16 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-14 13:23:16 +0000
commit3e97c51418e6d27e9a81906f347fcb7c78e27d4f (patch)
treeee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/cli/src/Tty.php
parentInitial commit. (diff)
downloadicingaweb2-module-incubator-3e97c51418e6d27e9a81906f347fcb7c78e27d4f.tar.xz
icingaweb2-module-incubator-3e97c51418e6d27e9a81906f347fcb7c78e27d4f.zip
Adding upstream version 0.20.0.upstream/0.20.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/cli/src/Tty.php')
-rw-r--r--vendor/gipfl/cli/src/Tty.php132
1 files changed, 132 insertions, 0 deletions
diff --git a/vendor/gipfl/cli/src/Tty.php b/vendor/gipfl/cli/src/Tty.php
new file mode 100644
index 0000000..efe5924
--- /dev/null
+++ b/vendor/gipfl/cli/src/Tty.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace gipfl\Cli;
+
+use InvalidArgumentException;
+use React\EventLoop\LoopInterface;
+use React\Stream\ReadableResourceStream;
+use React\Stream\WritableResourceStream;
+use RuntimeException;
+use function defined;
+use function fstat;
+use function function_exists;
+use function is_bool;
+use function is_resource;
+use function is_string;
+use function posix_isatty;
+use function register_shutdown_function;
+use function stream_isatty;
+use function stream_set_blocking;
+use function strlen;
+use function var_export;
+
+class Tty
+{
+ protected $stdin;
+
+ protected $stdout;
+
+ protected $loop;
+
+ protected $echo = true;
+
+ /** @var TtyMode */
+ protected $ttyMode;
+
+ public function __construct(LoopInterface $loop)
+ {
+ $this->loop = $loop;
+ register_shutdown_function([$this, 'restore']);
+ $loop->futureTick(function () {
+ $this->initialize();
+ });
+ }
+
+ public function setEcho($echo)
+ {
+ if (! is_bool($echo) && ! is_string($echo) && strlen($echo) !== 1) {
+ throw new InvalidArgumentException(
+ "\$echo must be boolean or a single character, got " . var_export($echo, 1)
+ );
+ }
+ $this->echo = $echo;
+ if ($this->ttyMode) {
+ if ($echo) {
+ $this->ttyMode->enableFeature('echo');
+ } else {
+ $this->ttyMode->disableFeature('echo');
+ }
+ }
+
+ return $this;
+ }
+
+ public function stdin()
+ {
+ if ($this->stdin === null) {
+ $this->assertValidStdin();
+ $this->stdin = new ReadableResourceStream(STDIN, $this->loop);
+ }
+
+ return $this->stdin;
+ }
+
+ protected function hasStdin()
+ {
+ return defined('STDIN') && is_resource(STDIN) && fstat(STDIN) !== false;
+ }
+
+ protected function assertValidStdin()
+ {
+ if (! $this->hasStdin()) {
+ throw new RuntimeException('I have no STDIN');
+ }
+ }
+
+ public function stdout()
+ {
+ if ($this->stdout === null) {
+ $this->assertValidStdout();
+ $this->stdout = new WritableResourceStream(STDOUT, $this->loop);
+ }
+
+ return $this->stdout;
+ }
+
+ protected function hasStdout()
+ {
+ return defined('STDOUT') && is_resource(STDOUT) && fstat(STDOUT) !== false;
+ }
+
+ protected function assertValidStdout()
+ {
+ if (! $this->hasStdout()) {
+ throw new RuntimeException('I have no STDOUT');
+ }
+ }
+
+ protected function initialize()
+ {
+ $this->ttyMode = new TtyMode();
+ $this->ttyMode->setPreferredMode($this->echo);
+ }
+
+ public static function isSupported()
+ {
+ if (PHP_VERSION_ID >= 70200) {
+ return stream_isatty(STDIN);
+ } elseif (function_exists('posix_isatty')) {
+ return posix_isatty(STDIN);
+ } else {
+ return false;
+ }
+ }
+
+ public function restore()
+ {
+ if ($this->hasStdin()) {
+ // ReadableResourceStream sets blocking to false, let's restore this
+ stream_set_blocking(STDIN, true);
+ }
+ }
+}