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 5 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
17 changes: 11 additions & 6 deletions lib/LineReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public function __construct(InputStream $inputStream)
public function readLine(): Promise
{
return call(function () {
if (($pos = \strpos($this->buffer, "\n")) !== false) {
$line = \substr($this->buffer, 0, $pos);
$this->buffer = \substr($this->buffer, $pos + 1);
return \rtrim($line, "\r");
}

while (null !== $chunk = yield $this->source->read()) {
$this->buffer .= $chunk;

Expand All @@ -38,15 +44,14 @@ public function readLine(): Promise
return null;
}

if (($pos = \strpos($this->buffer, "\n")) !== false) {
$line = \substr($this->buffer, 0, $pos);
$this->buffer = \substr($this->buffer, $pos + 1);
return \rtrim($line, "\r");
}

$line = $this->buffer;
$this->buffer = "";
return \rtrim($line, "\r");
});
}

public function getBuffer(): string
{
return $this->buffer;
}
}
37 changes: 26 additions & 11 deletions lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getInputBufferStream(): ResourceInputStream
$stream = Loop::getState($key);

if (!$stream) {
$stream = new ResourceInputStream(\fopen('php://input', 'r'));
$stream = new ResourceInputStream(\fopen('php://input', 'rb'));
Loop::setState($key, $stream);
}

Expand All @@ -93,7 +93,7 @@ function getOutputBufferStream(): ResourceOutputStream
$stream = Loop::getState($key);

if (!$stream) {
$stream = new ResourceOutputStream(\fopen('php://output', 'w'));
$stream = new ResourceOutputStream(\fopen('php://output', 'wb'));
Loop::setState($key, $stream);
}

Expand All @@ -118,6 +118,25 @@ function getStdin(): ResourceInputStream
return $stream;
}

/**
* Get LineReader instance associated with STDIN
*
* @return LineReader
*/
function getStdinLineReader(): LineReader
{
static $key = InputStream::class . '\\stdinLineReader';
danog marked this conversation as resolved.
Show resolved Hide resolved

$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 @@ -160,25 +179,21 @@ function getStderr(): ResourceOutputStream
/**
* Buffered async 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 readLine(string $prompt = ''): Promise
kelunik marked this conversation as resolved.
Show resolved Hide resolved
{
return call(static function () use ($prompt) {
static $key = InputStream::class . '\\stdinLine';

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

if (!$stream) {
$stream = new LineReader(getStdin());
Loop::setState($key, $stream);
}
if ($prompt) {
yield getStdout()->write($prompt);
}
return $stream->readLine();
return getStdinLineReader()->readLine();
});
}

Expand Down
6 changes: 6 additions & 0 deletions test/LineReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public function testEmptyCrLf()
$this->check(["\r\n"], [""]);
}

public function testEmptyCr()
{
$this->check(["\r"], [""]);
}

public function testMultiLineSlow()
{
$this->check(["a", "bc", "\r", "\n\r\nef\r", "\n"], ["abc", "", "ef"]);
Expand All @@ -67,6 +72,7 @@ private function check(array $chunks, array $expectedLines)
}

self::assertSame($expectedLines, $lines);
self::assertSame("", $reader->getBuffer());
}));
}
}