Skip to content

Commit a0c445a

Browse files
authored
Merge pull request #28 from netconsult-sweden/master
Enable feeding a string from php to standard input
2 parents 35a81f3 + 6fd6caf commit a0c445a

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ $command->addArg('--name=', "d'Artagnan");
5858
// Add argument with several values
5959
// results in --keys key1 key2
6060
$command->addArg('--keys', array('key1','key2'));
61+
62+
// Add string to pipe to command on standard input
63+
$command->setStdIn('string');
6164
```
6265

6366
## API
@@ -108,6 +111,7 @@ pass `command`, `execCommand` and `args` as options. This will call the respecti
108111
An array can be passed to add more than one value for a key, e.g. `addArg('--exclude', array('val1','val2'))`
109112
which will create the option "--exclude 'val1' 'val2'".
110113
* `$escape`: If set, this overrides the `$escapeArgs` setting and enforces escaping/no escaping
114+
* `setStdIn()`: String to supply command via standard input.
111115
* `getOutput()`: The command output as string. Empty if none.
112116
* `getError()`: The error message, either stderr or internal message. Empty if no error.
113117
* `getStdErr()`: The stderr output. Empty if none.

src/Command.php

+23
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class Command
5959
*/
6060
public $locale;
6161

62+
/**
63+
* @var string to pipe to standard input
64+
*/
65+
protected $_stdIn;
66+
6267
/**
6368
* @var string the command to execute
6469
*/
@@ -166,6 +171,16 @@ public function setCommand($command)
166171
return $this;
167172
}
168173

174+
/**
175+
* @param string $stdIn If set, the string will be piped to the command via standard input.
176+
* This enables the same functionality as piping on the command line.
177+
* @return static for method chaining
178+
*/
179+
public function setStdIn($stdIn) {
180+
$this->_stdIn = $stdIn;
181+
return $this;
182+
}
183+
169184
/**
170185
* @return string|null the command that was set through setCommand() or passed to the constructor. Null if none.
171186
*/
@@ -322,10 +337,18 @@ public function execute()
322337
1 => array('pipe','w'),
323338
2 => array('pipe', $this->getIsWindows() ? 'a' : 'w'),
324339
);
340+
if ($this->_stdIn!==null) {
341+
$descriptors[0] = array('pipe', 'r');
342+
}
343+
325344
$process = proc_open($command, $descriptors, $pipes, $this->procCwd, $this->procEnv, $this->procOptions);
326345

327346
if (is_resource($process)) {
328347

348+
if($this->_stdIn!==null) {
349+
fwrite($pipes[0], $this->_stdIn);
350+
fclose($pipes[0]);
351+
}
329352
$this->_stdOut = stream_get_contents($pipes[1]);
330353
$this->_stdErr = stream_get_contents($pipes[2]);
331354
fclose($pipes[1]);

tests/CommandTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,14 @@ public function testCanProvideProcDir()
224224
$this->assertTrue($command->getExecuted());
225225
$this->assertEquals($tmpDir, $command->getOutput());
226226
}
227+
public function testCanRunCommandWithStandardInput()
228+
{
229+
$command = new Command('/bin/cat');
230+
$command->addArg('-T');
231+
$command->setStdIn("\t");
232+
$this->assertTrue($command->execute());
233+
$this->assertTrue($command->getExecuted());
234+
$this->assertEquals("^I", $command->getOutput());
235+
}
227236

228237
}

0 commit comments

Comments
 (0)