Permalink
Browse files

Added error output buffering for commands.

  • Loading branch information...
1 parent 7ed526b commit e362903fb94f59df3892c6696aa98704bfb65021 @ncuesta committed Jul 20, 2012
View
@@ -3,3 +3,4 @@ tests/output
.idea
phpunit.xml
vendor/*
+composer.lock
@@ -62,6 +62,13 @@ class Callback implements CommandInterface, PipeableCommandInterface
private $_output;
/**
+ * Error output for this command.
+ *
+ * @var string
+ */
+ private $_errorOutput;
+
+ /**
* Callback function/method that will be invoked when command is run.
*
* @var \Closure
@@ -143,7 +150,18 @@ public function getOutput()
{
return $this->_output;
}
-
+
+ /**
+ * Get the output for this command's execution.
+ * This method will only return a valid value after the command has been executed.
+ *
+ * @return string
+ */
+ public function getErrorOutput()
+ {
+ return $this->_errorOutput;
+ }
+
/**
* Get the code of the inner callback as string.
*
@@ -120,6 +120,14 @@ class Command implements CommandInterface, PipingCommandInterface, PipeableComma
private $_output;
/**
+ * Error output for this command.
+ * This value will only be set after it has been run.
+ *
+ * @var string
+ */
+ private $_errorOutput;
+
+ /**
* Factory method for creating new Commands and allowing to take advantage of the
* fluent API provided by this class.
*
@@ -395,6 +403,16 @@ public function getOutput()
}
/**
+ * Get the error output for this command.
+ *
+ * @return string
+ */
+ public function getErrorOutput()
+ {
+ return $this->_errorOutput;
+ }
+
+ /**
* Get the output of this command as an array, splitting it with a given $delimiter.
* If no output has been generated by this command, an empty array will be returned.
*
@@ -442,10 +460,12 @@ public function run($input = null)
protected function _run($input)
{
$this->_output = '';
+ $this->_errorOutput = '';
$descriptors = array(
0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
+ 2 => array('pipe', 'w'),
);
$pipes = array();
@@ -463,6 +483,9 @@ protected function _run($input)
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
+ $errorOutput = stream_get_contents($pipes[2]);
+ fclose($pipes[2]);
+
$exitCode = proc_close($childProcess);
// Run the piped command, if any
@@ -471,11 +494,13 @@ protected function _run($input)
$pipedCommand->run($output);
- $output = $pipedCommand->getOutput();
- $exitCode = $pipedCommand->getExitCode();
+ $output = $pipedCommand->getOutput();
+ $exitCode = $pipedCommand->getExitCode();
+ $errorOutput = $pipedCommand->getErrorOutput();
}
$this->_output = $output;
+ $this->_errorOutput = $errorOutput;
return $exitCode;
}
@@ -43,7 +43,15 @@ public function getExitCode();
* @return string
*/
public function getOutput();
-
+
+ /**
+ * Get the output for this command's execution.
+ * This method will only return a valid value after the command has been executed.
+ *
+ * @return string
+ */
+ public function getErrorOutput();
+
/**
* Get a string representing this command.
*
@@ -104,4 +104,15 @@ public function hasPipedCommand()
{
return false;
}
+
+ /**
+ * Get the output for this command's execution.
+ * This method will only return a valid value after the command has been executed.
+ *
+ * @return string
+ */
+ public function getErrorOutput()
+ {
+ return '';
+ }
}

0 comments on commit e362903

Please sign in to comment.