1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 17:52:14 +01:00
parallel/lib/Process/ChannelledProcess.php

117 lines
2.8 KiB
PHP
Raw Normal View History

2016-08-22 06:40:48 +02:00
<?php declare(strict_types = 1);
2015-08-27 16:10:08 +02:00
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);
}
/**
* Resets process values.
*/
2016-08-18 18:04:48 +02:00
public function __clone() {
$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();
$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 {
if (null === $this->channel) {
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 {
if (null === $this->channel) {
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 {
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();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function getPid(): int {
return $this->process->getPid();
}
/**
* {@inheritdoc}
*/
2016-08-18 18:04:48 +02:00
public function signal(int $signo) {
$this->process->signal($signo);
}
2015-08-27 16:10:08 +02:00
}