Skip to content

Commit aaeb0fe

Browse files
authored
Merge pull request #5 from KurtThiemann/master
Open std streams if STDIN, STDOUT, or STDERR are not defined
2 parents fb5660f + a977d4e commit aaeb0fe

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

src/Communication/StdStreams.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Aternos\Taskmaster\Communication;
4+
5+
class StdStreams
6+
{
7+
protected static ?self $instance = null;
8+
9+
/**
10+
* @var resource|null
11+
*/
12+
protected $stdin = null;
13+
14+
/**
15+
* @var resource|null
16+
*/
17+
protected $stdout = null;
18+
19+
/**
20+
* @var resource|null
21+
*/
22+
protected $stderr = null;
23+
24+
/**
25+
* @return static
26+
*/
27+
public static function getInstance(): static
28+
{
29+
if (static::$instance === null) {
30+
static::$instance = new static();
31+
}
32+
return static::$instance;
33+
}
34+
35+
/**
36+
* @return resource
37+
*/
38+
public function getStdin()
39+
{
40+
if ($this->stdin === null) {
41+
if (defined("STDIN")) {
42+
$this->stdin = STDIN;
43+
} else {
44+
$this->stdin = fopen("php://stdin", "r");
45+
}
46+
}
47+
48+
return $this->stdin;
49+
}
50+
51+
/**
52+
* @return resource
53+
*/
54+
public function getStdout()
55+
{
56+
if ($this->stdout === null) {
57+
if (defined("STDOUT")) {
58+
$this->stdout = STDOUT;
59+
} else {
60+
$this->stdout = fopen("php://stdout", "w");
61+
}
62+
}
63+
64+
return $this->stdout;
65+
}
66+
67+
/**
68+
* @return resource
69+
*/
70+
public function getStderr()
71+
{
72+
if ($this->stderr === null) {
73+
if (defined("STDERR")) {
74+
$this->stderr = STDERR;
75+
} else {
76+
$this->stderr = fopen("php://stderr", "w");
77+
}
78+
}
79+
80+
return $this->stderr;
81+
}
82+
}

src/Runtime/RuntimeProcess.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Aternos\Taskmaster\Communication\Socket\SocketInterface;
66
use Aternos\Taskmaster\Communication\Socket\SocketPair;
7+
use Aternos\Taskmaster\Communication\StdStreams;
78
use Aternos\Taskmaster\TaskmasterOptions;
89

910
/**
@@ -33,15 +34,16 @@ public function __construct(TaskmasterOptions $options, string $runtimeClass)
3334
{
3435
$socketPair = new SocketPair();
3536
$this->socket = $socketPair->getParentSocket();
37+
$stdStreams = StdStreams::getInstance();
3638
$this->process = proc_open([
3739
$options->getPhpExecutable(),
3840
static::BIN_PATH,
3941
$options->getBootstrap(),
4042
$runtimeClass
4143
], [
42-
0 => STDIN,
43-
1 => STDOUT,
44-
2 => STDERR,
44+
0 => $stdStreams->getStdin(),
45+
1 => $stdStreams->getStdout(),
46+
2 => $stdStreams->getStderr(),
4547
3 => $socketPair->getChildSocket()->getStream(),
4648
], $pipes);
4749
$socketPair->closeChildSocket();
@@ -83,4 +85,4 @@ public function isRunning(): bool
8385
}
8486
return proc_get_status($this->process)["running"];
8587
}
86-
}
88+
}

0 commit comments

Comments
 (0)