Skip to content

Commit

Permalink
Merge ff425ff into 1b84c81
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Aug 23, 2019
2 parents 1b84c81 + ff425ff commit ea96ce0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 10 deletions.
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,14 @@
"platform": {
"php": "7.0.0"
}
},
"scripts": {
"check": [
"@cs",
"@test"
],
"cs": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff --dry-run",
"cs-fix": "PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix -v --diff",
"test": "@php -dzend.assertions=1 -dassert.exception=1 ./vendor/bin/phpunit --coverage-text"
}
}
10 changes: 5 additions & 5 deletions examples/gzip-compress.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\ZlibOutputStream;
use Amp\Loop;
use function Amp\ByteStream\getStdin;
use function Amp\ByteStream\getStdout;

require __DIR__ . "/../vendor/autoload.php";
require __DIR__."/../vendor/autoload.php";

Loop::run(function () {
$stdin = new ResourceInputStream(STDIN);
$stdout = new ResourceOutputStream(STDOUT);
$stdin = getStdin();
$stdout = getStdout();

$gzout = new ZlibOutputStream($stdout, ZLIB_ENCODING_GZIP);

Expand Down
10 changes: 5 additions & 5 deletions examples/gzip-decompress.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php

use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\ZlibInputStream;
use Amp\Loop;
use function Amp\ByteStream\getStdin;
use function Amp\ByteStream\getStdout;

require __DIR__ . "/../vendor/autoload.php";
require __DIR__."/../vendor/autoload.php";

Loop::run(function () {
$stdin = new ResourceInputStream(STDIN);
$stdout = new ResourceOutputStream(STDOUT);
$stdin = getStdin();
$stdout = getStdout();

$gzin = new ZlibInputStream($stdin, ZLIB_ENCODING_GZIP);

Expand Down
15 changes: 15 additions & 0 deletions examples/readline-echo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use Amp\Loop;
use function Amp\ByteStream\bufferEcho;
use function Amp\ByteStream\prompt;

require __DIR__ . "/../vendor/autoload.php";

Loop::run(function () {
yield bufferEcho("Hello from async PHP!\n");
$question = yield prompt("What is your question? ");
yield bufferEcho("I see your question is $question.\n");
yield bufferEcho("\nUnfortunately, I'm just a small script and I can't answer that, feel free to contact us at #amphp on freenode though!\n\n");
yield bufferEcho("Bye!\n\n");
});
50 changes: 50 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ function getStdin(): ResourceInputStream
return $stream;
}

/**
* Get LineReader instance associated with STDIN.
*
* @return LineReader
*/
function getStdinLineReader(): LineReader
{
static $key = LineReader::class . '\\stdin';

$stream = Loop::getState($key);

if (!$stream) {
$stream = new LineReader(getStdin());
Loop::setState($key, $stream);
}

return $stream;
}

/**
* The STDOUT stream for the process associated with the currently active event loop.
*
Expand Down Expand Up @@ -159,6 +178,37 @@ function getStderr(): ResourceOutputStream
return $stream;
}

/**
* Buffered async version of the readline() function.
*
* Please note that this function will hungrily eat data from stdin,
* buffering data even after the first newline.
* Use getStdinLineReader()->getBuffer() to obtain the remaining buffered data.
*
* @param string $prompt Optional prompt to print to console
*
* @return \Amp\Promise Will resolve with the read line
*/
function prompt(string $prompt = ''): Promise
{
return call(static function () use ($prompt) {
if ($prompt) {
yield getStdout()->write($prompt);
}
return getStdinLineReader()->readLine();
});
}

/**
* Simple wrapper function to asynchronously write a string to the PHP output buffer.
*
* @param string $string
* @return Promise
*/
function bufferEcho($string): Promise
{
return getOutputBufferStream()->write($string);
}
function parseLineDelimitedJson(InputStream $stream, bool $assoc = false, int $depth = 512, int $options = 0): Iterator
{
return new Producer(static function (callable $emit) use ($stream, $assoc, $depth, $options) {
Expand Down
20 changes: 20 additions & 0 deletions test/ResourceOutputStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\StreamException;
use Amp\Loop;
use PHPUnit\Framework\TestCase;
use function Amp\ByteStream\bufferEcho;
use function Amp\Promise\wait;

class ResourceOutputStreamTest extends TestCase
Expand Down Expand Up @@ -66,4 +68,22 @@ public function testClosedRemoteSocket()
wait($stream->write("foobar"));
wait($stream->write("foobar"));
}

public function testEcho()
{
Loop::run(function () {
$data = "\n".\base64_encode(\random_bytes(10))."\n";
$found = false;
\ob_start(static function ($match) use (&$found, $data) {
if ($match === $data) {
$found = true;
return '';
}
return $match;
});
yield bufferEcho($data);
\ob_end_flush();
$this->assertTrue($found, "Data wasn't sent to the output buffer");
});
}
}

0 comments on commit ea96ce0

Please sign in to comment.