1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-30 04:19:23 +01:00

Add web SAPI input/output stream getter functions (#61)

This commit is contained in:
Daniil Gentili 2019-06-03 23:01:43 +02:00 committed by Niklas Keller
parent d5cd42a765
commit d8cc314d62
2 changed files with 53 additions and 0 deletions

View File

@ -62,6 +62,43 @@ function buffer(InputStream $source): Promise
});
}
/**
* The php://input input buffer stream for the process associated with the currently active event loop.
*
* @return ResourceInputStream
*/
function getInputBufferStream(): ResourceInputStream
{
static $key = InputStream::class . '\\input';
$stream = Loop::getState($key);
if (!$stream) {
$stream = new ResourceInputStream(\fopen('php://input', 'r'));
Loop::setState($key, $stream);
}
return $stream;
}
/**
* The php://output output buffer stream for the process associated with the currently active event loop.
*
* @return ResourceOutputStream
*/
function getOutputBufferStream(): ResourceOutputStream
{
static $key = OutputStream::class . '\\output';
$stream = Loop::getState($key);
if (!$stream) {
$stream = new ResourceOutputStream(\fopen('php://output', 'w'));
Loop::setState($key, $stream);
}
return $stream;
}
/**
* The STDIN stream for the process associated with the currently active event loop.
*

View File

@ -8,6 +8,22 @@ use Amp\PHPUnit\TestCase;
class StdStreamTest extends TestCase
{
public function testGetInputBufferStream()
{
Loop::run(function () {
$stream = ByteStream\getInputBufferStream();
$this->assertSame($stream, ByteStream\getInputBufferStream());
});
}
public function testGetOutputBufferStream()
{
Loop::run(function () {
$stream = ByteStream\getOutputBufferStream();
$this->assertSame($stream, ByteStream\getOutputBufferStream());
});
}
public function testGetStdin()
{
Loop::run(function () {