1
0
mirror of https://github.com/danog/process.git synced 2024-11-30 04:39:04 +01:00

Changes suggested by @kelunik plus some cleanup

This commit is contained in:
Aaron Piotrowski 2017-01-15 09:23:32 -06:00
parent d899d24f18
commit 79974426ae
3 changed files with 19 additions and 19 deletions

View File

@ -54,15 +54,14 @@ class Process {
$command = \implode(" ", \array_map("escapeshellarg", $command)); $command = \implode(" ", \array_map("escapeshellarg", $command));
} }
$this->command = $command; $this->command = $command;
$this->cwd = $cwd ?? "";
if ($cwd !== null) {
$this->cwd = $cwd;
}
foreach ($env as $key => $value) { foreach ($env as $key => $value) {
if (!\is_array($value)) { // $env cannot accept array values. if (\is_array($value)) {
$this->env[(string) $key] = (string) $value; throw new \Error("\$env cannot accept array values");
} }
$this->env[(string) $key] = (string) $value;
} }
$this->options = $options; $this->options = $options;
@ -171,8 +170,7 @@ class Process {
if (!\is_resource($resource) || \feof($resource)) { if (!\is_resource($resource) || \feof($resource)) {
throw new ProcessException("Process ended unexpectedly"); throw new ProcessException("Process ended unexpectedly");
} }
$code = @\fread($resource, 3); // Single byte written as string $code = \rtrim(@\fread($resource, 3)); // Single byte written as string
$code = \rtrim($code);
if (!\strlen($code) || !\is_numeric($code)) { if (!\strlen($code) || !\is_numeric($code)) {
throw new ProcessException("Process ended without providing a status code"); throw new ProcessException("Process ended without providing a status code");
} }
@ -227,16 +225,21 @@ class Process {
throw new StatusError("The process is not running"); throw new StatusError("The process is not running");
} }
\proc_terminate($this->process, (int) $signo); \proc_terminate($this->process, $signo);
} }
/** /**
* Returns the PID of the child process. Value is only meaningful if the process has been started and PHP was not * Returns the PID of the child process. Value is only meaningful if PHP was not compiled with --enable-sigchild.
* compiled with --enable-sigchild.
* *
* @return int * @return int
*
* @throws \Amp\Process\StatusError
*/ */
public function getPid(): int { public function getPid(): int {
if ($this->pid === 0) {
throw new StatusError("The process has not been started");
}
return $this->pid; return $this->pid;
} }
@ -252,7 +255,7 @@ class Process {
/** /**
* Gets the current working directory. * Gets the current working directory.
* *
* @return string The current working directory or null if inherited from the current PHP process. * @return string The current working directory an empty string if inherited from the current PHP process.
*/ */
public function getWorkingDirectory(): string { public function getWorkingDirectory(): string {
if ($this->cwd === "") { if ($this->cwd === "") {
@ -265,7 +268,7 @@ class Process {
/** /**
* Gets the environment variables array. * Gets the environment variables array.
* *
* @return mixed[] Array of environment variables. * @return string[] Array of environment variables.
*/ */
public function getEnv(): array { public function getEnv(): array {
return $this->env; return $this->env;

View File

@ -29,9 +29,6 @@ class StreamedProcess {
/** @var \SplQueue Queue of data to write to STDIN. */ /** @var \SplQueue Queue of data to write to STDIN. */
private $writeQueue; private $writeQueue;
/** @var \AsyncInterop\Promise Promise resolved when process ends. */
private $promise;
/** /**
* @param string|array $command Command to run. * @param string|array $command Command to run.
* @param string|null $cwd Working directory or use an empty string to use the working directory of the current * @param string|null $cwd Working directory or use an empty string to use the working directory of the current
@ -57,7 +54,6 @@ class StreamedProcess {
$this->stdoutEmitter = new Emitter; $this->stdoutEmitter = new Emitter;
$this->stderrEmitter = new Emitter; $this->stderrEmitter = new Emitter;
$this->writeQueue = new \SplQueue; $this->writeQueue = new \SplQueue;
$this->promise = null;
} }
/** /**
@ -68,7 +64,9 @@ class StreamedProcess {
$process = $this->process; $process = $this->process;
$writes = $this->writeQueue; $writes = $this->writeQueue;
$this->stdinWatcher = Loop::onWritable($this->process->getStdin(), static function ($watcher, $resource) use ($process, $writes) { $this->stdinWatcher = Loop::onWritable($this->process->getStdin(), static function ($watcher, $resource) use (
$process, $writes
) {
try { try {
while (!$writes->isEmpty()) { while (!$writes->isEmpty()) {
/** @var \Amp\Deferred $deferred */ /** @var \Amp\Deferred $deferred */

View File

@ -31,7 +31,6 @@ class ProcessTest extends \PHPUnit_Framework_TestCase {
public function testCommandCanRun() { public function testCommandCanRun() {
Loop::execute(function() { Loop::execute(function() {
$process = new Process(self::CMD_PROCESS); $process = new Process(self::CMD_PROCESS);
$this->assertSame(0, $process->getPid());
$promise = $process->execute(); $promise = $process->execute();
$completed = false; $completed = false;