mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-26 11:54:54 +01:00
Add web SAPI input/output stream getter functions (#61)
This commit is contained in:
parent
d5cd42a765
commit
d8cc314d62
@ -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.
|
||||
*
|
||||
|
@ -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 () {
|
||||
|
Loading…
Reference in New Issue
Block a user