Skip to content

Commit

Permalink
Add option to bypass trim() for messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mikehaertl committed Sep 21, 2015
1 parent add3739 commit bb021f5
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 12 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# CHANGELOG

## 1.1.1
## 1.2.0

* Add option to return untrimmed output and error

WIP

## 1.1.0

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,6 @@ pass `command`, `execCommand` and `args` as options. This will call the respecti
* `getExitCode()`: The exit code.
* `getExecuted()`: Whether the command was successfully executed.
* `execute()`: Executes the command and returns `true` on success, `false` otherwhise.

> **Note:** `getError()`, `getStdErr()` and `getOutput()` return the trimmed output.
> You can pass `false` to these methods if you need any possible line breaks at the end.
23 changes: 13 additions & 10 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* This class represents a shell command.
*
* @author Michael Härtl <[email protected]>
* @version 1.1.1-dev
* @version 1.2.0-dev
* @license http://www.opensource.org/licenses/MIT
*/
class Command
Expand Down Expand Up @@ -232,27 +232,30 @@ public function addArg($key, $value = null, $escape = null)
}

/**
* @param bool $trim whether to `trim()` the return value. The default is `true`.
* @return string the command output (stdout). Empty if none.
*/
public function getOutput()
public function getOutput($trim = true)
{
return $this->_stdOut;
return $trim ? trim($this->_stdOut) : $this->_stdOut;
}

/**
* @param bool $trim whether to `trim()` the return value. The default is `true`.
* @return string the error message, either stderr or internal message. Empty if none.
*/
public function getError()
public function getError($trim = true)
{
return $this->_error;
return $trim ? trim($this->_error) : $this->_error;
}

/**
* @param bool $trim whether to `trim()` the return value. The default is `true`.
* @return string the stderr output. Empty if none.
*/
public function getStdErr()
public function getStdErr($trim = true)
{
return $this->_stdErr;
return $trim ? trim($this->_stdErr) : $this->_stdErr;
}

/**
Expand Down Expand Up @@ -288,7 +291,7 @@ public function execute()
if ($this->useExec) {
$execCommand = $this->captureStdErr ? "$command 2>&1" : $command;
exec($execCommand, $output, $this->_exitCode);
$this->_stdOut = trim(implode("\n", $output));
$this->_stdOut = implode("\n", $output);
if ($this->_exitCode!==0) {
$this->_stdErr = $this->_stdOut;
$this->_error = empty($this->_stdErr) ? 'Command failed' : $this->_stdErr;
Expand All @@ -303,8 +306,8 @@ public function execute()

if (is_resource($process)) {

$this->_stdOut = trim(stream_get_contents($pipes[1]));
$this->_stdErr = trim(stream_get_contents($pipes[2]));
$this->_stdOut = stream_get_contents($pipes[1]);
$this->_stdErr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);

Expand Down
1 change: 1 addition & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function testCanRunValidCommand()
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals("CommandTest.php", $command->getOutput());
$this->assertEquals("CommandTest.php\n", $command->getOutput(false));
$this->assertEmpty($command->getError());
$this->assertEmpty($command->getStdErr());
$this->assertEquals(0, $command->getExitCode());
Expand Down

0 comments on commit bb021f5

Please sign in to comment.