1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-12-02 17:28:21 +01:00
byte-stream/lib/IteratorStream.php

139 lines
3.8 KiB
PHP
Raw Normal View History

2017-05-05 16:55:43 +02:00
<?php
namespace Amp\ByteStream;
use Amp\Coroutine;
use Amp\Deferred;
2017-05-11 01:04:10 +02:00
use Amp\Failure;
2017-05-05 16:55:43 +02:00
use Amp\Iterator;
use Amp\Promise;
use Amp\Success;
/**
* Creates a buffered message from an Iterator. The message can be consumed in chunks using the read() API or it may be
* buffered and accessed in its entirety by waiting for the promise to resolve.
*
* Buffering Example:
*
* $stream = new IteratorStream($iterator); // $iterator is an instance of \Amp\Iterator emitting only strings.
* $content = yield $stream;
*
* Streaming Example:
*
* $stream = new IteratorStream($iterator); // $iterator is an instance of \Amp\Iterator emitting only strings.
*
* while (($chunk = yield $stream->read()) !== null) {
* // Immediately use $chunk, reducing memory consumption since the entire message is never buffered.
* }
*/
class IteratorStream implements InputStream, Promise {
/** @var string */
private $buffer = "";
/** @var \Amp\Deferred|null */
private $pendingRead;
/** @var \Amp\Coroutine */
private $coroutine;
2017-05-11 01:04:10 +02:00
/** @var bool True if onResolve() has been called. */
private $buffering = false;
/** @var \Amp\Deferred|null */
private $backpressure;
/** @var bool True if close() is called or the iterator has completed. */
private $closed = false;
2017-05-05 16:55:43 +02:00
/**
* @param \Amp\Iterator $iterator An iterator that only emits strings.
*/
public function __construct(Iterator $iterator) {
2017-05-11 01:04:10 +02:00
$this->coroutine = new Coroutine($this->iterate($iterator));
2017-05-05 16:55:43 +02:00
}
2017-05-11 01:04:10 +02:00
private function iterate(Iterator $iterator): \Generator {
while (yield $iterator->advance()) {
$buffer = $this->buffer .= $iterator->getCurrent();
if ($buffer === "") {
continue; // Do not succeed reads with empty string.
} elseif ($this->pendingRead) {
2017-05-05 16:55:43 +02:00
$deferred = $this->pendingRead;
$this->pendingRead = null;
$this->buffer = "";
$deferred->resolve($buffer);
2017-05-11 01:04:10 +02:00
} elseif (!$this->buffering) {
$this->backpressure = new Deferred;
yield $this->backpressure->promise();
2017-05-05 16:55:43 +02:00
}
2017-05-11 01:04:10 +02:00
$buffer = ""; // Destroy last emitted chunk to free memory.
2017-05-05 16:55:43 +02:00
}
2017-05-11 01:04:10 +02:00
$this->closed = true;
2017-05-05 16:55:43 +02:00
if ($this->pendingRead) {
$deferred = $this->pendingRead;
$this->pendingRead = null;
2017-05-11 01:04:10 +02:00
$deferred->resolve($this->buffer !== "" ? $this->buffer : null);
2017-05-05 16:55:43 +02:00
$this->buffer = "";
}
return $this->buffer;
}
public function read(): Promise {
2017-05-11 01:04:10 +02:00
if ($this->pendingRead) {
return new Failure(new PendingReadException);
}
if ($this->closed) {
return new Success;
}
2017-05-05 16:55:43 +02:00
if ($this->buffer !== "") {
$buffer = $this->buffer;
$this->buffer = "";
2017-05-11 01:04:10 +02:00
if ($this->backpressure) {
$backpressure = $this->backpressure;
$this->backpressure = null;
$backpressure->resolve();
}
return new Success($buffer);
2017-05-05 16:55:43 +02:00
}
$this->pendingRead = new Deferred;
return $this->pendingRead->promise();
}
/**
* {@inheritdoc}
*/
public function onResolve(callable $onResolved) {
2017-05-11 01:04:10 +02:00
$this->buffering = true;
if ($this->backpressure) {
$backpressure = $this->backpressure;
$this->backpressure = null;
$backpressure->resolve();
}
2017-05-05 16:55:43 +02:00
$this->coroutine->onResolve($onResolved);
}
public function close() {
2017-05-11 01:04:10 +02:00
$this->buffering = true;
$this->closed = true;
if ($this->pendingRead) {
$deferred = $this->pendingRead;
$this->pendingRead = null;
$deferred->resolve($this->buffer === "" ? $this->buffer : null);
$this->buffer = "";
}
2017-05-05 16:55:43 +02:00
}
}