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 1 commit
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
18 changes: 18 additions & 0 deletions examples/readline-echo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use Amp\ByteStream\ResourceInputStream;
use Amp\ByteStream\ResourceOutputStream;
use Amp\ByteStream\ZlibOutputStream;
use Amp\Loop;
use function Amp\ByteStream\bufferEcho;
use function Amp\ByteStream\readLine;

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

Loop::run(function () {
yield bufferEcho("Hello from async PHP!\n");
$question = yield readLine("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");
});
36 changes: 36 additions & 0 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,39 @@ function getStderr(): ResourceOutputStream

return $stream;
}


/**
* Buffered async readline function.
*
* @param string $prompt Optional prompt to print to console
*
* @return \Amp\Promise Will resolve with the read line
*/
function readLine(string $prompt = ''): Promise
kelunik marked this conversation as resolved.
Show resolved Hide resolved
{
return call(static function () use ($prompt) {
$stdin = getStdin();
if ($prompt) {
yield getStdout()->write($prompt);
}
static $lines = [''];
while (\count($lines) < 2 && ($chunk = yield $stdin->read()) !== null) {
$chunk = \explode("\n", \str_replace(["\r", "\n\n"], "\n", $chunk));
$lines[\count($lines) - 1] .= \array_shift($chunk);
$lines = \array_merge($lines, $chunk);
}
return \array_shift($lines);
});
}

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