Skip to content

Instantly share code, notes, and snippets.

@paales
Created June 26, 2018 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paales/d58cdfbbb5c52843ef53a9d242b0231d to your computer and use it in GitHub Desktop.
Save paales/d58cdfbbb5c52843ef53a9d242b0231d to your computer and use it in GitHub Desktop.
<?php
class DataProvider
{
public function execute(): \Generator
{
while(true) {
$requests = yield;
foreach ($requests as $request) {
yield $request . ' - appended';
}
}
}
}
class DataRequester
{
/** @var DataProvider */
private $dataProvider;
public function __construct(DataProvider $dataProvider)
{
$this->dataProvider = $dataProvider;
}
public function execute(array $batches): void
{
$coroutine = $this->dataProvider->execute();
foreach ($batches as $batch) {
while($result = $coroutine->send($batch)) {
echo $result . "\n";
}
}
}
}
$requester = new DataRequester(
new DataProvider()
);
$requester->execute([['a','b','c'],['d','e','f'],['g','h','i']]);
/*
Results in:
a - appended
b - appended
c - appended
d - appended
e - appended
f - appended
g - appended
h - appended
i - appended
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment