Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async readline/echo and add composer commands #63

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
danog marked this conversation as resolved.
Show resolved Hide resolved
*
* @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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see much value in such a function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it'd be nice to have an async way to log to stdout

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just do getStdout()->write() then, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any value in this function either. The name seems misleading as well, as buffering depends on SAPI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use buffer to match the name of the functions used to get the output stream.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. Those names have the same problem I guess.

More to the main point though – how many functions do we want to introduce to save another method call? I don't think this function is necessary, nor is prompt.

{
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");
});
}
}