From d798b5f672b017e3d53cd9555e3e4cc637dd803e Mon Sep 17 00:00:00 2001 From: Dan Cryer Date: Wed, 30 Jul 2014 13:34:45 +0100 Subject: [PATCH] Updating the CommandExecutor::executeCommand() method to properly catch stderr output from commands. Fixes #456 --- PHPCI/Helper/BaseCommandExecutor.php | 35 ++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/PHPCI/Helper/BaseCommandExecutor.php b/PHPCI/Helper/BaseCommandExecutor.php index d5984a06..1b1d757f 100644 --- a/PHPCI/Helper/BaseCommandExecutor.php +++ b/PHPCI/Helper/BaseCommandExecutor.php @@ -30,6 +30,7 @@ abstract class BaseCommandExecutor implements CommandExecutor protected $verbose; protected $lastOutput; + protected $lastError; public $logExecOutput = true; @@ -78,16 +79,42 @@ abstract class BaseCommandExecutor implements CommandExecutor } $status = 0; - exec($command, $this->lastOutput, $status); + $descriptorSpec = array( + 0 => array("pipe", "r"), // stdin + 1 => array("pipe", "w"), // stdout + 2 => array("pipe", "w"), // stderr + ); - foreach ($this->lastOutput as &$lastOutput) { - $lastOutput = trim($lastOutput, '"'); + $pipes = array(); + + $process = proc_open($command, $descriptorSpec, $pipes, dirname($this->buildPath), null); + + if (is_resource($process)) { + fclose($pipes[0]); + + $this->lastOutput = stream_get_contents($pipes[1]); + $this->lastError = stream_get_contents($pipes[2]); + + fclose($pipes[1]); + fclose($pipes[2]); + + $status = proc_close($process); } - if ($this->logExecOutput && !empty($this->lastOutput) && ($this->verbose|| $status != 0)) { + $this->lastOutput = explode(PHP_EOL, $this->lastOutput); + $this->lastError = "\033[0;31m" . $this->lastError . "\033[0m"; + + $shouldOutput = ($this->logExecOutput && ($this->verbose || $status != 0)); + + if ($shouldOutput && !empty($this->lastOutput)) { $this->logger->log($this->lastOutput); } + if (!empty($this->lastError)) { + $this->logger->log('Error trying to execute: ' . $command); + $this->logger->log($this->lastError, LogLevel::ERROR); + } + $rtn = false; if ($status == 0) {