-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
220 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Amp\ByteStream; | ||
|
||
use Amp\Promise; | ||
use function Amp\call; | ||
|
||
final class LineReader | ||
{ | ||
/** @var string */ | ||
private $buffer = ""; | ||
|
||
/** @var InputStream */ | ||
private $source; | ||
|
||
public function __construct(InputStream $inputStream) | ||
{ | ||
$this->source = $inputStream; | ||
} | ||
|
||
/** | ||
* @return Promise<string|null> | ||
*/ | ||
public function readLine(): Promise | ||
{ | ||
return call(function () { | ||
while (null !== $chunk = yield $this->source->read()) { | ||
$this->buffer .= $chunk; | ||
|
||
if (($pos = \strpos($this->buffer, "\n")) !== false) { | ||
$line = \substr($this->buffer, 0, $pos); | ||
$this->buffer = \substr($this->buffer, $pos + 1); | ||
return \rtrim($line, "\r"); | ||
} | ||
} | ||
|
||
if ($this->buffer === "") { | ||
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"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php /** @noinspection PhpUnhandledExceptionInspection */ | ||
|
||
namespace Amp\ByteStream; | ||
|
||
use Amp\Iterator; | ||
use Amp\PHPUnit\TestCase; | ||
use function Amp\call; | ||
use function Amp\Promise\wait; | ||
|
||
class LineReaderTest extends TestCase | ||
{ | ||
public function testSingleLine() | ||
{ | ||
$this->check(["abc"], ["abc"]); | ||
} | ||
|
||
public function testMultiLineLf() | ||
{ | ||
$this->check(["abc\nef"], ["abc", "ef"]); | ||
} | ||
|
||
public function testMultiLineCrLf() | ||
{ | ||
$this->check(["abc\r\nef"], ["abc", "ef"]); | ||
} | ||
|
||
public function testMultiLineEmptyNewlineStart() | ||
{ | ||
$this->check(["\r\nabc\r\nef\r\n"], ["", "abc", "ef"]); | ||
} | ||
|
||
public function testMultiLineEmptyNewlineEnd() | ||
{ | ||
$this->check(["abc\r\nef\r\n"], ["abc", "ef"]); | ||
} | ||
|
||
public function testMultiLineEmptyNewlineMiddle() | ||
{ | ||
$this->check(["abc\r\n\r\nef\r\n"], ["abc", "", "ef"]); | ||
} | ||
|
||
public function testEmpty() | ||
{ | ||
$this->check([], []); | ||
} | ||
|
||
public function testEmptyCrLf() | ||
{ | ||
$this->check(["\r\n"], [""]); | ||
} | ||
|
||
public function testMultiLineSlow() | ||
{ | ||
$this->check(["a", "bc", "\r", "\n\r\nef\r", "\n"], ["abc", "", "ef"]); | ||
} | ||
|
||
private function check(array $chunks, array $expectedLines) | ||
{ | ||
wait(call(static function () use ($chunks, $expectedLines) { | ||
$inputStream = new IteratorStream(Iterator\fromIterable($chunks)); | ||
|
||
$reader = new LineReader($inputStream); | ||
$lines = []; | ||
|
||
while (null !== $line = yield $reader->readLine()) { | ||
$lines[] = $line; | ||
} | ||
|
||
self::assertSame($expectedLines, $lines); | ||
})); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters