2017-01-14 01:40:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp;
|
|
|
|
|
|
|
|
/**
|
2017-02-01 00:09:38 +01:00
|
|
|
* Creates a buffered message from a Stream. The message can be consumed in chunks using the advance() and getCurrent()
|
2017-01-14 01:40:46 +01:00
|
|
|
* methods or it may be buffered and accessed in its entirety by waiting for the promise to resolve.
|
2017-02-01 00:09:38 +01:00
|
|
|
*
|
|
|
|
* Buffering Example:
|
|
|
|
*
|
|
|
|
* $message = new Message($stream); // $stream is an instance of \Amp\Stream emitting only strings.
|
|
|
|
* $content = yield $message;
|
|
|
|
*
|
|
|
|
* Streaming Example:
|
|
|
|
*
|
|
|
|
* $message = new Message($stream); // $stream is a Stream emitting only strings.
|
|
|
|
*
|
|
|
|
* while (yield $message->advance()) {
|
|
|
|
* $chunk = $message->getCurrent();
|
|
|
|
* // Immediately use $chunk, reducing memory consumption since the entire message is never buffered.
|
|
|
|
* }
|
2017-01-14 01:40:46 +01:00
|
|
|
*/
|
2017-02-01 00:09:38 +01:00
|
|
|
class Message implements Iterator, Promise {
|
2017-01-14 01:40:46 +01:00
|
|
|
use Internal\Placeholder;
|
|
|
|
|
2017-02-01 00:09:38 +01:00
|
|
|
const LISTENING = 0;
|
|
|
|
const BUFFERING = 1;
|
2017-03-10 21:58:46 +01:00
|
|
|
const WAITING = 2;
|
|
|
|
const COMPLETE = 4;
|
2017-02-01 00:09:38 +01:00
|
|
|
|
|
|
|
/** @var \Amp\Listener|null */
|
|
|
|
private $listener;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $status = self::LISTENING;
|
|
|
|
|
|
|
|
/** @var mixed Final result of the stream. */
|
|
|
|
private $value;
|
|
|
|
|
2017-01-14 01:40:46 +01:00
|
|
|
/**
|
|
|
|
* @param \Amp\Stream $stream Stream that only emits strings.
|
|
|
|
*/
|
|
|
|
public function __construct(Stream $stream) {
|
2017-02-01 00:09:38 +01:00
|
|
|
$this->listener = new Listener($stream);
|
2017-01-14 01:40:46 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$stream->onResolve(function ($exception, $value) {
|
2017-02-01 00:09:38 +01:00
|
|
|
if ($exception) {
|
|
|
|
$this->fail($exception);
|
2017-01-14 01:40:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-01 00:09:38 +01:00
|
|
|
$result = \implode($this->listener->drain());
|
|
|
|
$this->listener = null;
|
2017-02-01 00:30:05 +01:00
|
|
|
$this->status = \strlen($result) ? self::BUFFERING : self::WAITING;
|
2017-02-01 00:09:38 +01:00
|
|
|
$this->value = $value;
|
|
|
|
$this->resolve($result);
|
2017-01-14 01:40:46 +01:00
|
|
|
});
|
|
|
|
}
|
2017-02-01 00:09:38 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a promise that resolves with true when more data in the message is available or false if the message is
|
|
|
|
* complete.
|
|
|
|
*
|
2017-03-10 21:58:46 +01:00
|
|
|
* @return \Amp\Promise<bool>
|
2017-02-01 00:09:38 +01:00
|
|
|
*
|
|
|
|
* @throws \Error If the message has resolved.
|
|
|
|
*/
|
|
|
|
public function advance(): Promise {
|
|
|
|
if ($this->listener) {
|
|
|
|
return $this->listener->advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($this->status) {
|
|
|
|
case self::BUFFERING:
|
|
|
|
$this->status = self::WAITING;
|
|
|
|
return new Success(true);
|
|
|
|
|
|
|
|
case self::WAITING:
|
|
|
|
$this->status = self::COMPLETE;
|
|
|
|
return new Success(false);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Error("The stream has resolved");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string Current chunk of the message.
|
|
|
|
*
|
|
|
|
* @throws \Error If the message has resolved.
|
|
|
|
*/
|
|
|
|
public function getCurrent(): string {
|
|
|
|
if ($this->listener) {
|
|
|
|
return $this->listener->getCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($this->status) {
|
|
|
|
case self::COMPLETE:
|
|
|
|
throw new \Error("The stream has resolved");
|
|
|
|
|
|
|
|
default:
|
|
|
|
return $this->result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed Result of the Stream (may not be a string).
|
|
|
|
*
|
|
|
|
* @throws \Error If the message has not resolved.
|
|
|
|
*/
|
|
|
|
public function getResult() {
|
|
|
|
if ($this->listener) {
|
|
|
|
return $this->listener->getResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->value;
|
|
|
|
}
|
2017-01-14 01:40:46 +01:00
|
|
|
}
|