Skip to content

Commit 9d7f85e

Browse files
committed
bug #18349 [Process] Fix stream_select priority when writing to stdin (nicolas-grekas)
This PR was merged into the 2.7 branch. Discussion ---------- [Process] Fix stream_select priority when writing to stdin | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- f31e783 [Process] Fix stream_select priority when writing to stdin
2 parents 2e6982a + f31e783 commit 9d7f85e

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

src/Symfony/Component/Process/Pipes/AbstractPipes.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ abstract class AbstractPipes implements PipesInterface
2222
public $pipes = array();
2323

2424
/** @var string */
25-
protected $inputBuffer = '';
25+
private $inputBuffer = '';
2626
/** @var resource|null */
27-
protected $input;
28-
27+
private $input;
2928
/** @var bool */
3029
private $blocked = true;
3130

@@ -91,9 +90,8 @@ protected function write()
9190
if (!isset($this->pipes[0])) {
9291
return;
9392
}
94-
95-
$e = array();
96-
$r = null !== $this->input ? array($this->input) : $e;
93+
$input = $this->input;
94+
$r = $e = array();
9795
$w = array($this->pipes[0]);
9896

9997
// let's have a look if something changed in streams
@@ -110,7 +108,7 @@ protected function write()
110108
}
111109
}
112110

113-
foreach ($r as $input) {
111+
if ($input) {
114112
for (;;) {
115113
$data = fread($input, self::CHUNK_SIZE);
116114
if (!isset($data[0])) {
@@ -124,7 +122,7 @@ protected function write()
124122
return array($this->pipes[0]);
125123
}
126124
}
127-
if (!isset($data[0]) && feof($input)) {
125+
if (feof($input)) {
128126
// no more data to read on input resource
129127
// use an empty buffer in the next reads
130128
$this->input = null;

src/Symfony/Component/Process/Process.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public function __construct($commandline, $cwd = null, array $env = null, $input
158158
$this->setEnv($env);
159159
}
160160

161-
$this->input = $input;
161+
$this->setInput($input);
162162
$this->setTimeout($timeout);
163163
$this->useFileHandles = '\\' === DIRECTORY_SEPARATOR;
164164
$this->pty = false;

src/Symfony/Component/Process/ProcessUtils.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function escapeArgument($argument)
8080
* @param string $caller The name of method call that validates the input
8181
* @param mixed $input The input to validate
8282
*
83-
* @return string The validated input
83+
* @return mixed The validated input
8484
*
8585
* @throws InvalidArgumentException In case the input is not valid
8686
*
@@ -92,6 +92,9 @@ public static function validateInput($caller, $input)
9292
if (is_resource($input)) {
9393
return $input;
9494
}
95+
if (is_string($input)) {
96+
return $input;
97+
}
9598
if (is_scalar($input)) {
9699
return (string) $input;
97100
}

src/Symfony/Component/Process/Tests/ProcessTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,24 @@ public function testSetStreamAsInput($code, $size)
209209
$this->assertEquals($expectedLength, strlen($p->getErrorOutput()));
210210
}
211211

212+
public function testLiveStreamAsInput()
213+
{
214+
$stream = fopen('php://memory', 'r+');
215+
fwrite($stream, 'hello');
216+
rewind($stream);
217+
218+
$p = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')));
219+
$p->setInput($stream);
220+
$p->start(function ($type, $data) use ($stream) {
221+
if ('hello' === $data) {
222+
fclose($stream);
223+
}
224+
});
225+
$p->wait();
226+
227+
$this->assertSame('hello', $p->getOutput());
228+
}
229+
212230
/**
213231
* @expectedException \Symfony\Component\Process\Exception\LogicException
214232
* @expectedExceptionMessage Input can not be set while the process is running.

0 commit comments

Comments
 (0)