1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-11 16:49:51 +01:00
parallel/lib/Process/ChannelledProcess.php

113 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-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 };
use Amp\Parallel\Sync\{ ChannelledStream, 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.
*/
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 {
2016-08-23 01:25:19 +02:00
if ($this->channel === null) {
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",
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) {
throw new StatusError("The process has not been started");
}
2015-12-05 06:50:32 +01:00
if ($data instanceof ExitStatus) {
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
}