From a40b276f0b0b1d44d262acdf63966f6bba9407bd Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Fri, 13 Sep 2024 12:06:35 +0200 Subject: [PATCH 1/3] open std streams if STDIN, STDOUT, or STDERR are not defined --- src/Communication/StdStreams.php | 82 ++++++++++++++++++++++++++++++++ src/Runtime/RuntimeProcess.php | 10 ++-- 2 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 src/Communication/StdStreams.php diff --git a/src/Communication/StdStreams.php b/src/Communication/StdStreams.php new file mode 100644 index 0000000..35a308e --- /dev/null +++ b/src/Communication/StdStreams.php @@ -0,0 +1,82 @@ +stdin === null) { + if (defined("STDIN")) { + $this->stdin = STDIN; + } else { + $this->stdin = fopen("php://stdin", "r"); + } + } + + return $this->stdin; + } + + /** + * @return resource + */ + public function getStdout() + { + if ($this->stdout === null) { + if (defined("STDOUT")) { + $this->stdout = STDOUT; + } else { + $this->stdout = fopen("php://stdout", "w"); + } + } + + return $this->stdout; + } + + /** + * @return resource + */ + public function getStderr() + { + if ($this->stderr === null) { + if (defined("STDERR")) { + $this->stderr = STDERR; + } else { + $this->stderr = fopen("php://stderr", "w"); + } + } + + return $this->stderr; + } +} diff --git a/src/Runtime/RuntimeProcess.php b/src/Runtime/RuntimeProcess.php index 3728081..5f92c93 100644 --- a/src/Runtime/RuntimeProcess.php +++ b/src/Runtime/RuntimeProcess.php @@ -4,6 +4,7 @@ use Aternos\Taskmaster\Communication\Socket\SocketInterface; use Aternos\Taskmaster\Communication\Socket\SocketPair; +use Aternos\Taskmaster\Communication\StdStreams; use Aternos\Taskmaster\TaskmasterOptions; /** @@ -33,15 +34,16 @@ public function __construct(TaskmasterOptions $options, string $runtimeClass) { $socketPair = new SocketPair(); $this->socket = $socketPair->getParentSocket(); + $stdStreams = StdStreams::getInstance(); $this->process = proc_open([ $options->getPhpExecutable(), static::BIN_PATH, $options->getBootstrap(), $runtimeClass ], [ - 0 => STDIN, - 1 => STDOUT, - 2 => STDERR, + 0 => $stdStreams->getStdin(), + 1 => $stdStreams->getStdout(), + 2 => $stdStreams->getStderr(), 3 => $socketPair->getChildSocket()->getStream(), ], $pipes); $socketPair->closeChildSocket(); @@ -83,4 +85,4 @@ public function isRunning(): bool } return proc_get_status($this->process)["running"]; } -} \ No newline at end of file +} From 61a06ca005e83654639727f7904b2d62783af77a Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Fri, 13 Sep 2024 12:11:10 +0200 Subject: [PATCH 2/3] make StdStreams instance null by default --- src/Communication/StdStreams.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Communication/StdStreams.php b/src/Communication/StdStreams.php index 35a308e..53cd6fa 100644 --- a/src/Communication/StdStreams.php +++ b/src/Communication/StdStreams.php @@ -4,7 +4,7 @@ class StdStreams { - protected static self $instance; + protected static ?self $instance = null; /** * @var resource|null @@ -26,7 +26,7 @@ class StdStreams */ public static function getInstance(): static { - if (!isset(self::$instance)) { + if (self::$instance === null) { self::$instance = new static(); } return self::$instance; From a977d4ec7f3abf36daa38bb9ac3d0711c923572f Mon Sep 17 00:00:00 2001 From: Kurt Thiemann Date: Fri, 13 Sep 2024 12:12:23 +0200 Subject: [PATCH 3/3] use static instead of self --- src/Communication/StdStreams.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Communication/StdStreams.php b/src/Communication/StdStreams.php index 53cd6fa..cf46b1c 100644 --- a/src/Communication/StdStreams.php +++ b/src/Communication/StdStreams.php @@ -26,10 +26,10 @@ class StdStreams */ public static function getInstance(): static { - if (self::$instance === null) { - self::$instance = new static(); + if (static::$instance === null) { + static::$instance = new static(); } - return self::$instance; + return static::$instance; } /**