-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from KurtThiemann/master
Open std streams if STDIN, STDOUT, or STDERR are not defined
- Loading branch information
Showing
2 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters