2015-08-27 16:10:08 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Concurrent\Process;
|
2015-08-27 16:10:08 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
use Amp\Concurrent\{ Process as ProcessContext, StatusError, Strand, SynchronizationError };
|
|
|
|
use Amp\Concurrent\Sync\{ ChannelledStream, Internal\ExitStatus };
|
|
|
|
use Interop\Async\Awaitable;
|
|
|
|
|
|
|
|
class ChannelledProcess implements ProcessContext, Strand {
|
2015-08-27 16:10:08 +02:00
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @var \Amp\Concurrent\Process\Process
|
2015-08-27 16:10:08 +02:00
|
|
|
*/
|
|
|
|
private $process;
|
|
|
|
|
|
|
|
/**
|
2016-08-18 18:04:48 +02:00
|
|
|
* @var \Amp\Concurrent\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-18 18:04:48 +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();
|
2015-12-06 07:32:06 +01:00
|
|
|
$this->channel = new ChannelledStream($this->process->getStdOut(), $this->process->getStdIn());
|
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 {
|
2015-09-20 04:34:41 +02:00
|
|
|
if (null === $this->channel) {
|
2015-08-27 20:06:39 +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(
|
|
|
|
'Thread unexpectedly exited with result of type: %s',
|
|
|
|
\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 {
|
2015-09-20 04:34:41 +02:00
|
|
|
if (null === $this->channel) {
|
2015-08-27 20:06:39 +02:00
|
|
|
throw new StatusError('The process has not been started.');
|
|
|
|
}
|
|
|
|
|
2015-12-05 06:50:32 +01:00
|
|
|
if ($data instanceof ExitStatus) {
|
2016-08-18 18:04:48 +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
|
|
|
}
|