2016-08-22 06:40:48 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Process;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\{ Process as ProcessContext, StatusError, Strand, SynchronizationError };
|
2016-08-31 01:27:14 +02:00
|
|
|
use Amp\Parallel\Sync\{ ChannelledSocket, Internal\ExitStatus };
|
2016-08-18 18:04:48 +02:00
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
|
|
|
|
class ChannelledProcess implements ProcessContext, Strand {
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var \Amp\Parallel\Process\Process */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $process;
|
|
|
|
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var \Amp\Parallel\Sync\Channel */
|
2015-08-27 16:10:08 +02:00
|
|
|
private $channel;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path Path to PHP script.
|
|
|
|
* @param string $cwd Working directory.
|
|
|
|
* @param mixed[] $env Array of environment variables.
|
|
|
|
*/
|
2016-08-26 18:14:42 +02:00
|
|
|
public function __construct(string $path, string $cwd = "", array $env = []) {
|
|
|
|
$command = \PHP_BINARY . " " . $path;
|
2015-08-27 16:10:08 +02:00
|
|
|
$this->process = new Process($command, $cwd, $env);
|
|
|
|
}
|
|
|
|
|
2015-08-27 20:06:39 +02:00
|
|
|
/**
|
|
|
|
* Resets process values.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __clone() {
|
2015-08-27 20:06:39 +02:00
|
|
|
$this->process = clone $this->process;
|
|
|
|
$this->channel = null;
|
|
|
|
}
|
|
|
|
|
2015-08-27 16:10:08 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function start() {
|
2015-08-27 16:10:08 +02:00
|
|
|
$this->process->start();
|
2016-08-31 01:27:14 +02:00
|
|
|
$this->channel = new ChannelledSocket($this->process->getStdOut(), $this->process->getStdIn(), false);
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function isRunning(): bool {
|
2015-08-27 16:10:08 +02:00
|
|
|
return $this->process->isRunning();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function receive(): Awaitable {
|
2016-08-23 01:25:19 +02:00
|
|
|
if ($this->channel === null) {
|
2016-08-26 18:14:42 +02:00
|
|
|
throw new StatusError("The process has not been started");
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
return \Amp\pipe($this->channel->receive(), static function ($data) {
|
|
|
|
if ($data instanceof ExitStatus) {
|
|
|
|
$data = $data->getResult();
|
|
|
|
throw new SynchronizationError(\sprintf(
|
2016-08-26 18:14:42 +02:00
|
|
|
"Thread unexpectedly exited with result of type: %s",
|
2016-08-18 18:04:48 +02:00
|
|
|
\is_object($data) ? \get_class($data) : \gettype($data)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
});
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function send($data): Awaitable {
|
2016-08-23 01:25:19 +02:00
|
|
|
if ($this->channel === null) {
|
2016-08-26 18:14:42 +02:00
|
|
|
throw new StatusError("The process has not been started");
|
2015-08-27 20:06:39 +02:00
|
|
|
}
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
if ($data instanceof ExitStatus) {
|
2016-08-26 18:14:42 +02:00
|
|
|
throw new \Error("Cannot send exit status objects");
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
return $this->channel->send($data);
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function join(): Awaitable {
|
2015-12-16 18:16:21 +01:00
|
|
|
return $this->process->join();
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function kill() {
|
2015-08-27 16:10:08 +02:00
|
|
|
$this->process->kill();
|
|
|
|
}
|
2015-10-20 07:06:43 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function getPid(): int {
|
2015-10-20 07:06:43 +02:00
|
|
|
return $this->process->getPid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function signal(int $signo) {
|
2015-10-20 07:06:43 +02:00
|
|
|
$this->process->signal($signo);
|
|
|
|
}
|
2015-08-27 16:10:08 +02:00
|
|
|
}
|