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

Enable feeding a string from php to standard input #28

Merged
merged 3 commits into from
Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ $command->addArg('--name=', "d'Artagnan");
// Add argument with several values
// results in --keys key1 key2
$command->addArg('--keys', array('key1','key2'));

// Add string to pipe to command on standard input
$command->setStdIn('string');
```

## API
Expand Down Expand Up @@ -108,6 +111,7 @@ pass `command`, `execCommand` and `args` as options. This will call the respecti
An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))`
which will create the option "--exclude 'val1' 'val2'".
* `$escape`: If set, this overrides the `$escapeArgs` setting and enforces escaping/no escaping
* `setStdIn()`: String to supply command via standard input.
* `getOutput()`: The command output as string. Empty if none.
* `getError()`: The error message, either stderr or internal message. Empty if no error.
* `getStdErr()`: The stderr output. Empty if none.
Expand Down
23 changes: 23 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class Command
*/
public $locale;

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

/**
* @var string the command to execute
*/
Expand Down Expand Up @@ -166,6 +171,16 @@ public function setCommand($command)
return $this;
}

/**
* @param string $stdIn If set, the string will be piped to the command via standard input.
* This enables the same functionality as piping on the command line.
* @return static for method chaining
*/
public function setStdIn($stdIn) {
$this->_stdIn = $stdIn;
return $this;
}

/**
* @return string|null the command that was set through setCommand() or passed to the constructor. Null if none.
*/
Expand Down Expand Up @@ -322,10 +337,18 @@ public function execute()
1 => array('pipe','w'),
2 => array('pipe', $this->getIsWindows() ? 'a' : 'w'),
);
if (isset($this->_stdIn)) {
Copy link
Owner

Choose a reason for hiding this comment

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

I usually prefer !== null instead of isset() as I find it more clear. Could you maybe change that?

Copy link
Author

Choose a reason for hiding this comment

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

Glad that you liked it!
I have changed this as requested.

$descriptors[0] = array('pipe', 'r');
}

$process = proc_open($command, $descriptors, $pipes, $this->procCwd, $this->procEnv, $this->procOptions);

if (is_resource($process)) {

if(isset($this->_stdIn)) {
Copy link
Owner

Choose a reason for hiding this comment

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

Same here, see above.

fwrite($pipes[0], $this->_stdIn);
fclose($pipes[0]);
}
$this->_stdOut = stream_get_contents($pipes[1]);
$this->_stdErr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
Expand Down
9 changes: 9 additions & 0 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,14 @@ public function testCanProvideProcDir()
$this->assertTrue($command->getExecuted());
$this->assertEquals($tmpDir, $command->getOutput());
}
public function testCanRunCommandWithStandardInput()
{
$command = new Command('/bin/cat');
$command->addArg('-T');
$command->setStdIn("\t");
$this->assertTrue($command->execute());
$this->assertTrue($command->getExecuted());
$this->assertEquals("^I", $command->getOutput());
}

}