Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow stdin to be a stream #30

Merged
merged 3 commits into from
Feb 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Command
public $locale;

/**
* @var string to pipe to standard input
* @var null|string|resource to pipe to standard input
*/
protected $_stdIn;

Expand Down Expand Up @@ -159,7 +159,7 @@ public function setCommand($command)
} elseif (isset($command[2]) && $command[2]===':') {
$position = 2;
} else {
$position = false;
$position = false;
}

// Absolute path. If it's a relative path, let it slide.
Expand All @@ -172,8 +172,10 @@ public function setCommand($command)
}

/**
* @param string $stdIn If set, the string will be piped to the command via standard input.
* @param string|resource $stdIn If set, the string will be piped to the command via standard input.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Arzaroth Maybe add a little note about the resource type here? E.g. "This can also be a resource like a file handle in which case the file content will be piped into the command."

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing.

* This enables the same functionality as piping on the command line.
* It can also be a resource like a file handle or a stream in which case its content will be piped
* into the command like an input redirection.
* @return static for method chaining
*/
public function setStdIn($stdIn) {
Expand Down Expand Up @@ -345,8 +347,13 @@ public function execute()

if (is_resource($process)) {

if($this->_stdIn!==null) {
fwrite($pipes[0], $this->_stdIn);
if ($this->_stdIn!==null) {
if (is_resource($this->_stdIn) &&
in_array(get_resource_type($this->_stdIn), array('file', 'stream'), true)) {
stream_copy_to_stream($this->_stdIn, $pipes[0]);
} else {
fwrite($pipes[0], $this->_stdIn);
}
fclose($pipes[0]);
}
$this->_stdOut = stream_get_contents($pipes[1]);
Expand Down
13 changes: 13 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,18 @@ public function testCanRunCommandWithStandardInput()
$this->assertTrue($command->getExecuted());
$this->assertEquals("^I", $command->getOutput());
}
public function testCanRunCommandWithStandardInputStream()
{
$tmpfile = tmpfile();
fwrite($tmpfile, "\t");
fseek($tmpfile, 0);
$command = new Command('/bin/cat');
$command->addArg('-T');
$command->setStdIn($tmpfile);
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals("^I", $command->getOutput());
fclose($tmpfile);
}

}