mirror of
https://github.com/danog/byte-stream.git
synced 2024-11-26 20:04:51 +01:00
Add async readline/echo and add composer commands
This commit is contained in:
parent
22113f2f14
commit
eaec19ea30
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
18
examples/readline-echo.php
Normal file
18
examples/readline-echo.php
Normal file
@ -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");
|
||||
});
|
@ -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
|
||||
{
|
||||
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
|
||||
{
|
||||
return getOutputBufferStream()->write($string);
|
||||
}
|
||||
|
@ -4,7 +4,9 @@ namespace Amp\ByteStream\Test;
|
||||
|
||||
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
|
||||
@ -66,4 +68,22 @@ class ResourceOutputStreamTest extends TestCase
|
||||
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");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user