8000 [Process] Fix stream_select priority when writing to stdin by nicolas-grekas · Pull Request #18349 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Process] Fix stream_select priority when writing to stdin #18349

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

Merged
merged 1 commit into from
Mar 30, 2016
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
14 changes: 6 additions & 8 deletions src/Symfony/Component/Process/Pipes/AbstractPipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ abstract class AbstractPipes implements PipesInterface
public $pipes = array();

/** @var string */
protected $inputBuffer = '';
private $inputBuffer = '';
/** @var resource|null */
protected $input;

private $input;
/** @var bool */
private $blocked = true;

Expand Down Expand Up @@ -91,9 +90,8 @@ protected function write()
if (!isset($this->pipes[0])) {
return;
}

$e = array();
$r = null !== $this->input ? array($this->input) : $e;
$input = $this->input;
$r = $e = array();
$w = array($this->pipes[0]);

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

foreach ($r as $input) {
if ($input) {
for (;;) {
$data = fread($input, self::CHUNK_SIZE);
if (!isset($data[0])) {
Expand All @@ -124,7 +122,7 @@ protected function write()
return array($this->pipes[0]);
}
}
if (!isset($data[0]) && feof($input)) {
if (feof($input)) {
// no more data to read on input resource
// use an empty buffer in the next reads
$this->input = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Process/Process.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function __construct($commandline, $cwd = null, array $env = null, $input
$this->setEnv($env);
}

$this->input = $input;
$this->setInput($input);
$this->setTimeout($timeout);
$this->useFileHandles = '\\' === DIRECTORY_SEPARATOR;
$this->pty = false;
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Process/ProcessUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static function escapeArgument($argument)
* @param string $caller The name of method call that validates the input
* @param mixed $input The input to validate
*
* @return string The validated input
* @return mixed The validated input
*
* @throws InvalidArgumentException In case the input is not valid
*
Expand All @@ -92,6 +92,9 @@ public static function validateInput($caller, $input)
if (is_resource($input)) {
return $input;
}
if (is_string($input)) {
return $input;
}
if (is_scalar($input)) {
return (string) $input;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ public function testSetStreamAsInput($code, $size)
$this->assertEquals($expectedLength, strlen($p->getErrorOutput()));
}

public function testLiveStreamAsInput()
{
$stream = fopen('php://memory', 'r+');
fwrite($stream, 'hello');
rewind($stream);

$p = $this->getProcess(sprintf('%s -r %s', self::$phpBin, escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);')));
$p->setInput($stream);
$p->start(function ($type, $data) use ($stream) {
if ('hello' === $data) {
fclose($stream);
}
});
$p->wait();

$this->assertSame('hello', $p->getOutput());
}

/**
* @expectedException \Symfony\Component\Process\Exception\LogicException
* @expectedExceptionMessage Input can not be set while the process is running.
Expand Down
0