2017-09-17 17:58:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Process;
|
|
|
|
|
|
|
|
use Amp\ByteStream\InputStream;
|
|
|
|
use Amp\ByteStream\PendingReadError;
|
|
|
|
use Amp\ByteStream\ResourceInputStream;
|
|
|
|
use Amp\ByteStream\StreamException;
|
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Promise;
|
|
|
|
|
|
|
|
class ProcessInputStream implements InputStream {
|
|
|
|
/** @var Deferred */
|
|
|
|
private $initialRead;
|
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $shouldClose = false;
|
|
|
|
|
2017-09-17 19:07:13 +02:00
|
|
|
/** @var bool */
|
|
|
|
private $referenced = true;
|
|
|
|
|
2017-09-17 17:58:05 +02:00
|
|
|
/** @var ResourceInputStream */
|
|
|
|
private $resourceStream;
|
|
|
|
|
|
|
|
/** @var StreamException|null */
|
|
|
|
private $error;
|
|
|
|
|
|
|
|
public function __construct(Promise $resourceStreamPromise) {
|
|
|
|
$resourceStreamPromise->onResolve(function ($error, $resourceStream) {
|
|
|
|
if ($error) {
|
|
|
|
$this->error = new StreamException("Failed to launch process", 0, $error);
|
2017-09-17 19:07:13 +02:00
|
|
|
if ($this->initialRead) {
|
|
|
|
$this->initialRead->fail($this->error);
|
|
|
|
}
|
2017-09-17 17:58:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->resourceStream = $resourceStream;
|
|
|
|
|
2017-09-17 19:07:13 +02:00
|
|
|
if (!$this->referenced) {
|
|
|
|
$this->resourceStream->unreference();
|
|
|
|
}
|
|
|
|
|
2017-09-17 17:58:05 +02:00
|
|
|
if ($this->shouldClose) {
|
|
|
|
$this->resourceStream->close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->initialRead) {
|
|
|
|
$initialRead = $this->initialRead;
|
|
|
|
$this->initialRead = null;
|
|
|
|
$initialRead->resolve($this->shouldClose ? null : "");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads data from the stream.
|
|
|
|
*
|
|
|
|
* @return Promise Resolves with a string when new data is available or `null` if the stream has closed.
|
|
|
|
*
|
|
|
|
* @throws PendingReadError Thrown if another read operation is still pending.
|
|
|
|
*/
|
|
|
|
public function read(): Promise {
|
|
|
|
if ($this->initialRead) {
|
|
|
|
throw new PendingReadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->error) {
|
|
|
|
return new Failure($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
return $this->resourceStream->read();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->initialRead = new Deferred;
|
|
|
|
|
|
|
|
return $this->initialRead->promise();
|
|
|
|
}
|
|
|
|
|
2017-09-17 19:07:13 +02:00
|
|
|
public function reference() {
|
|
|
|
$this->referenced = true;
|
|
|
|
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
$this->resourceStream->reference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unreference() {
|
|
|
|
$this->referenced = false;
|
|
|
|
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
$this->resourceStream->unreference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 17:58:05 +02:00
|
|
|
public function close() {
|
|
|
|
$this->shouldClose = true;
|
|
|
|
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
$this->resourceStream->close();
|
|
|
|
}
|
|
|
|
}
|
2017-09-20 11:54:19 +02:00
|
|
|
}
|