Skip to content

Commit

Permalink
Add web SAPI input/output stream getter functions (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
danog authored and kelunik committed Jun 3, 2019
1 parent d5cd42a commit d8cc314
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/functions.php
Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions test/StdStreamTest.php
Expand Up @@ -8,6 +8,22 @@

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 () {
Expand Down

0 comments on commit d8cc314

Please sign in to comment.