|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This example counts the number of vowels in example1.txt and the number of |
| 4 | + * consonants in example2.txt, showing how concurrent slow file reads can |
| 5 | + * use Promises to defer work, with a future callback when the work is complete. |
| 6 | + * |
| 7 | + * A PeriodicLoop is used with a long period and the file reading code |
| 8 | + * is done one byte at a time, to simulate a slow connection. |
| 9 | + * |
| 10 | + * Note: This is an example wrapped in a class, showing an example of how a |
| 11 | + * framework could offer promise-driven filesystem functionality. |
| 12 | + */ |
| 13 | + |
| 14 | +use Gt\Async\Loop; |
| 15 | +use Gt\Async\Promise\Deferred; |
| 16 | +use Gt\Async\Promise\Promise; |
| 17 | +use Gt\Async\Timer\PeriodicTimer; |
| 18 | + |
| 19 | +require("../vendor/autoload.php"); |
| 20 | + |
| 21 | +class SlowFileReader { |
| 22 | + private SplFileObject $file; |
| 23 | + private Loop $loop; |
| 24 | + private Deferred $deferred; |
| 25 | + private int $characterCount; |
| 26 | + |
| 27 | + public function __construct(string $filename) { |
| 28 | + $this->file = new SplFileObject($filename); |
| 29 | + } |
| 30 | + |
| 31 | +// We need to inject the background loop that will dispatch the processing calls. |
| 32 | + public function setLoop(Loop $loop):void { |
| 33 | + $this->loop = $loop; |
| 34 | + } |
| 35 | + |
| 36 | +// This is the public function that will be called, returning a Promise that |
| 37 | +// represents the completed work. |
| 38 | + public function countCharacters(string $charMap):Promise { |
| 39 | +// A new Deferred is created to assign this class's specific process function. |
| 40 | + $this->deferred = new Deferred(); |
| 41 | + $this->deferred->addProcessFunction( |
| 42 | + fn() => $this->processNextCharacter($charMap) |
| 43 | + ); |
| 44 | +// The Deferred is added to the Loop's default timer, which will call its |
| 45 | +// process function each tick. |
| 46 | + $this->loop->addDeferredToTimer($this->deferred); |
| 47 | +// The Deferred creates its own Promise, so it knows what to resolve when the |
| 48 | +// work is complete. |
| 49 | + return $this->deferred->getPromise(); |
| 50 | + } |
| 51 | + |
| 52 | +// This functions is called by the Deferred, as the Deferred is invoked by the |
| 53 | +// background Loop. It must not do much work per call, as to not block the |
| 54 | +// execution of other deferred tasks. |
| 55 | + public function processNextCharacter(string $charMap):void { |
| 56 | +// TODO: Nice. Got this far. Now it's time to finish Promise implementation, |
| 57 | +// and then we can complete this example's functionality here. |
| 58 | + if(!isset($this->characterCount)) { |
| 59 | + $this->characterCount = 0; |
| 60 | + } |
| 61 | + |
| 62 | + if($this->file->eof()) { |
| 63 | + $this->deferred->resolve($this->characterCount); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $char = $this->file->fread(1); |
| 68 | + $char = strtolower($char); |
| 69 | + if(strlen($char) <= 0) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if(strstr($charMap, $char)) { |
| 74 | + $this->characterCount++; |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// A periodic timer will be used to call the deferred tasks ten times per |
| 80 | +// second. This is actually quite slow, used to illustrate how concurrent tasks |
| 81 | +// will behave. |
| 82 | +$timer = new PeriodicTimer(0.1, true); |
| 83 | +$loop = new Loop(); |
| 84 | +$loop->addTimer($timer); |
| 85 | +$loop->haltWhenAllDeferredComplete(true); |
| 86 | + |
| 87 | +$reader1 = new SlowFileReader("example.txt"); |
| 88 | +$reader1->setLoop($loop); |
| 89 | +$reader2 = new SlowFileReader("example.txt"); |
| 90 | +$reader2->setLoop($loop); |
| 91 | + |
| 92 | +$reader1->countCharacters("aeiou"); |
| 93 | +//->then(function(int $numVowels):void { |
| 94 | +// echo "Example text has $numVowels vowels.", PHP_EOL; |
| 95 | +//}); |
| 96 | +// |
| 97 | +//$reader2->countCharacters("bcdfghjklmnpqrstvwxyz") |
| 98 | +//->then(function(int $numConsonants):void { |
| 99 | +// echo "Example text has $numConsonants consonants.", PHP_EOL; |
| 100 | +//}); |
| 101 | + |
| 102 | +$loop->run(); |
| 103 | +echo "Complete!", PHP_EOL; |
0 commit comments