Skip to content

Commit

Permalink
Merge pull request #5 from KurtThiemann/master
Browse files Browse the repository at this point in the history
Open std streams if STDIN, STDOUT, or STDERR are not defined
  • Loading branch information
matthi4s authored Sep 13, 2024
2 parents fb5660f + a977d4e commit aaeb0fe
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
82 changes: 82 additions & 0 deletions src/Communication/StdStreams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace Aternos\Taskmaster\Communication;

class StdStreams
{
protected static ?self $instance = null;

/**
* @var resource|null
*/
protected $stdin = null;

/**
* @var resource|null
*/
protected $stdout = null;

/**
* @var resource|null
*/
protected $stderr = null;

/**
* @return static
*/
public static function getInstance(): static
{
if (static::$instance === null) {
static::$instance = new static();
}
return static::$instance;
}

/**
* @return resource
*/
public function getStdin()
{
if ($this->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;
}
}
10 changes: 6 additions & 4 deletions src/Runtime/RuntimeProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -83,4 +85,4 @@ public function isRunning(): bool
}
return proc_get_status($this->process)["running"];
}
}
}

0 comments on commit aaeb0fe

Please sign in to comment.