From 79974426ae202ec25e2c96516a4c7fdc651376f2 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 15 Jan 2017 09:23:32 -0600 Subject: [PATCH] Changes suggested by @kelunik plus some cleanup --- lib/Process.php | 29 ++++++++++++++++------------- lib/StreamedProcess.php | 8 +++----- test/ProcessTest.php | 1 - 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/Process.php b/lib/Process.php index 4fdc0d5..573ee31 100644 --- a/lib/Process.php +++ b/lib/Process.php @@ -54,15 +54,14 @@ class Process { $command = \implode(" ", \array_map("escapeshellarg", $command)); } $this->command = $command; - - if ($cwd !== null) { - $this->cwd = $cwd; - } + $this->cwd = $cwd ?? ""; foreach ($env as $key => $value) { - if (!\is_array($value)) { // $env cannot accept array values. - $this->env[(string) $key] = (string) $value; + if (\is_array($value)) { + throw new \Error("\$env cannot accept array values"); } + + $this->env[(string) $key] = (string) $value; } $this->options = $options; @@ -171,8 +170,7 @@ class Process { if (!\is_resource($resource) || \feof($resource)) { throw new ProcessException("Process ended unexpectedly"); } - $code = @\fread($resource, 3); // Single byte written as string - $code = \rtrim($code); + $code = \rtrim(@\fread($resource, 3)); // Single byte written as string if (!\strlen($code) || !\is_numeric($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"); } - \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 - * compiled with --enable-sigchild. + * Returns the PID of the child process. Value is only meaningful if PHP was not compiled with --enable-sigchild. * * @return int + * + * @throws \Amp\Process\StatusError */ public function getPid(): int { + if ($this->pid === 0) { + throw new StatusError("The process has not been started"); + } + return $this->pid; } @@ -252,7 +255,7 @@ class Process { /** * 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 { if ($this->cwd === "") { @@ -265,7 +268,7 @@ class Process { /** * Gets the environment variables array. * - * @return mixed[] Array of environment variables. + * @return string[] Array of environment variables. */ public function getEnv(): array { return $this->env; diff --git a/lib/StreamedProcess.php b/lib/StreamedProcess.php index d47a912..63be0e7 100644 --- a/lib/StreamedProcess.php +++ b/lib/StreamedProcess.php @@ -29,9 +29,6 @@ class StreamedProcess { /** @var \SplQueue Queue of data to write to STDIN. */ private $writeQueue; - /** @var \AsyncInterop\Promise Promise resolved when process ends. */ - private $promise; - /** * @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 @@ -57,7 +54,6 @@ class StreamedProcess { $this->stdoutEmitter = new Emitter; $this->stderrEmitter = new Emitter; $this->writeQueue = new \SplQueue; - $this->promise = null; } /** @@ -68,7 +64,9 @@ class StreamedProcess { $process = $this->process; $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 { while (!$writes->isEmpty()) { /** @var \Amp\Deferred $deferred */ diff --git a/test/ProcessTest.php b/test/ProcessTest.php index fc86e20..14ee981 100644 --- a/test/ProcessTest.php +++ b/test/ProcessTest.php @@ -31,7 +31,6 @@ class ProcessTest extends \PHPUnit_Framework_TestCase { public function testCommandCanRun() { Loop::execute(function() { $process = new Process(self::CMD_PROCESS); - $this->assertSame(0, $process->getPid()); $promise = $process->execute(); $completed = false;