2017-09-17 17:58:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Process;
|
|
|
|
|
|
|
|
use Amp\ByteStream\ClosedException;
|
|
|
|
use Amp\ByteStream\OutputStream;
|
|
|
|
use Amp\ByteStream\ResourceOutputStream;
|
|
|
|
use Amp\ByteStream\StreamException;
|
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Failure;
|
|
|
|
use Amp\Promise;
|
|
|
|
|
|
|
|
class ProcessOutputStream implements OutputStream {
|
2017-12-07 02:21:06 +01:00
|
|
|
/** @var \SplQueue */
|
2018-03-08 16:09:10 +01:00
|
|
|
private $queuedWrites;
|
2017-09-17 17:58:05 +02:00
|
|
|
|
|
|
|
/** @var bool */
|
|
|
|
private $shouldClose = false;
|
|
|
|
|
|
|
|
/** @var ResourceOutputStream */
|
|
|
|
private $resourceStream;
|
|
|
|
|
|
|
|
/** @var StreamException|null */
|
|
|
|
private $error;
|
|
|
|
|
|
|
|
public function __construct(Promise $resourceStreamPromise) {
|
2017-12-07 02:21:06 +01:00
|
|
|
$this->queuedWrites = new \SplQueue;
|
2017-09-17 17:58:05 +02:00
|
|
|
$resourceStreamPromise->onResolve(function ($error, $resourceStream) {
|
|
|
|
if ($error) {
|
|
|
|
$this->error = new StreamException("Failed to launch process", 0, $error);
|
|
|
|
|
2017-12-07 02:21:06 +01:00
|
|
|
while (!$this->queuedWrites->isEmpty()) {
|
|
|
|
list(, $deferred) = $this->queuedWrites->shift();
|
2017-09-17 17:58:05 +02:00
|
|
|
$deferred->fail($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-07 02:21:06 +01:00
|
|
|
while (!$this->queuedWrites->isEmpty()) {
|
|
|
|
/**
|
|
|
|
* @var string $data
|
|
|
|
* @var \Amp\Deferred $deferred
|
|
|
|
*/
|
|
|
|
list($data, $deferred) = $this->queuedWrites->shift();
|
|
|
|
$deferred->resolve($resourceStream->write($data));
|
2017-09-17 17:58:05 +02:00
|
|
|
}
|
|
|
|
|
2017-12-07 02:21:06 +01:00
|
|
|
$this->resourceStream = $resourceStream;
|
|
|
|
|
2017-09-17 17:58:05 +02:00
|
|
|
if ($this->shouldClose) {
|
|
|
|
$this->resourceStream->close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritdoc */
|
|
|
|
public function write(string $data): Promise {
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
return $this->resourceStream->write($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->error) {
|
|
|
|
return new Failure($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->shouldClose) {
|
|
|
|
throw new ClosedException("Stream has already been closed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred = new Deferred;
|
2017-12-07 02:21:06 +01:00
|
|
|
$this->queuedWrites->push([$data, $deferred]);
|
2017-09-17 17:58:05 +02:00
|
|
|
|
|
|
|
return $deferred->promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @inheritdoc */
|
|
|
|
public function end(string $finalData = ""): Promise {
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
return $this->resourceStream->end($finalData);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->error) {
|
|
|
|
return new Failure($this->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->shouldClose) {
|
|
|
|
throw new ClosedException("Stream has already been closed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred = new Deferred;
|
2017-12-07 02:21:06 +01:00
|
|
|
$this->queuedWrites->push([$finalData, $deferred]);
|
2017-09-17 17:58:05 +02:00
|
|
|
|
|
|
|
$this->shouldClose = true;
|
|
|
|
|
|
|
|
return $deferred->promise();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function close() {
|
|
|
|
$this->shouldClose = true;
|
|
|
|
|
|
|
|
if ($this->resourceStream) {
|
|
|
|
$this->resourceStream->close();
|
2017-12-07 02:21:06 +01:00
|
|
|
} elseif (!$this->queuedWrites->isEmpty()) {
|
|
|
|
$error = new ClosedException("Stream closed.");
|
|
|
|
do {
|
|
|
|
list(, $deferred) = $this->queuedWrites->shift();
|
|
|
|
$deferred->fail($error);
|
|
|
|
} while (!$this->queuedWrites->isEmpty());
|
2017-09-17 17:58:05 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-20 11:54:19 +02:00
|
|
|
}
|