-
-
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
1 changed file
with
43 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
require __DIR__ . "/../vendor/autoload.php"; | ||
|
||
use Amp\ByteStream\Parser; | ||
use Amp\Loop; | ||
|
||
Loop::run(function () { | ||
// Defines a generator that yields integers (number of bytes to read), strings (delimiter to search for), or | ||
// null (read any amount of bytes). | ||
$generator = function (callable $printer): \Generator { | ||
while (true) { | ||
$buffer = yield "\n"; // Reads until a new-line character is found. | ||
$buffer = trim($buffer); | ||
$printer($buffer); // Use the received data. | ||
} | ||
}; | ||
|
||
// The user of Parser is responsible for creating the Generator object, allowing anything to be passed into the | ||
// generator that may be required. | ||
$parser = new Parser($generator(function (string $parsedData) { | ||
static $i = 0; | ||
printf("[%d] %s\n", $i++, $parsedData); | ||
})); | ||
|
||
$parser->write("This\nis\n"); | ||
|
||
Loop::delay(1000, function () use ($parser) { | ||
$parser->write("an\nexample\nof\n"); | ||
}); | ||
|
||
Loop::delay(2000, function () use ($parser) { | ||
$parser->write("a\nsimple\n"); | ||
}); | ||
|
||
Loop::delay(3000, function () use ($parser) { | ||
$parser->write("incremental\nstream\nparser"); | ||
}); | ||
|
||
Loop::delay(4000, function () use ($parser) { | ||
$parser->end(); // Marks the end of data. | ||
}); | ||
}); |