Skip to content

[Process] Accept Traversable input #18350

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 31, 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
43 changes: 28 additions & 15 deletions src/Symfony/Component/Process/Pipes/AbstractPipes.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ abstract class AbstractPipes implements PipesInterface
public $pipes = array();

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

private $inputBuffer = '';
/** @var resource|\Iterator|null */
private $input;
/** @var bool */
private $blocked = true;

public function __construct($input)
{
if (is_resource($input)) {
if (is_resource($input) || $input instanceof \Iterator) {
$this->input = $input;
} elseif (is_string($input)) {
$this->inputBuffer = $input;
Expand Down Expand Up @@ -76,7 +75,7 @@ protected function unblock()
foreach ($this->pipes as $pipe) {
stream_set_blocking($pipe, 0);
}
if (null !== $this->input) {
if (is_resource($this->input)) {
stream_set_blocking($this->input, 0);
}

Expand All @@ -91,9 +90,21 @@ protected function write()
if (!isset($this->pipes[0])) {
return;
}
$input = $this->input;

if ($input instanceof \Iterator) {
if (!$input->valid()) {
$input = null;
} elseif (is_resource($input = $input->current())) {
stream_set_blocking($input, 0);
} else {
$this->inputBuffer .= $input;
$this->input->next();
$input = null;
}
}

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

// let's have a look if something changed in streams
Expand All @@ -109,8 +120,7 @@ protected function write()
return array($this->pipes[0]);
}
}

foreach ($r as $input) {
if ($input) {
for (;;) {
$data = fread($input, self::CHUNK_SIZE);
if (!isset($data[0])) {
Expand All @@ -124,16 +134,19 @@ protected function write()
return array($this->pipes[0]);
}
}
if (!isset($data[0]) && feof($input)) {
// no more data to read on input resource
// use an empty buffer in the next reads
$this->input = null;
if (feof($input)) {
if ($this->input instanceof \Iterator) {
$this->input->next();
} else {
$this->input = null;
}
}
}
}

// no input to read on resource, buffer is empty
if (null === $this->input && !isset($this->inputBuffer[0])) {
if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we use isset() to test for the empty string instead of comparing it with the empty string like we usually do?

Copy link
Member Author

Choose a reason for hiding this comment

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

because false is also a possible value here

$this->input = null;
fclose($this->pipes[0]);
unset($this->pipes[0]);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Process/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,9 @@ public function hasCallback()
*/
private function getDescriptors()
{
if ($this->input instanceof \Iterator) {
$this->input->rewind();
}
if ('\\' === DIRECTORY_SEPARATOR) {
$this->processPipes = WindowsPipes::create($this, $this->input);
} else {
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/Process/ProcessUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ public static function validateInput($caller, $input)
if (is_scalar($input)) {
return (string) $input;
}
if ($input instanceof \Iterator) {
return $input;
}
if ($input instanceof \Traversable) {
return new \IteratorIterator($input);
}

throw new InvalidArgumentException(sprintf('%s only accepts strings or stream resources.', $caller));
throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller));
Copy link
Member

Choose a reason for hiding this comment

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

technically, your code won't accept Traversable implemented in C without implementing Iterator, or implemented in PHP with IteratorAggregate returning such a traversable.

you should use IteratorIterator instead to wrap any Traversable into an Iterator.

Copy link
Member Author

Choose a reason for hiding this comment

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

}

return $input;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Process/Tests/ProcessBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function testShouldReturnProcessWithEnabledOutput()

/**
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
* @expectedExceptionMessage Symfony\Component\Process\ProcessBuilder::setInput only accepts strings or stream resources.
* @expectedExceptionMessage Symfony\Component\Process\ProcessBuilder::setInput only accepts strings, Traversable objects or stream resources.
*/
public function testInvalidInput()
{
Expand Down
31 changes: 30 additions & 1 deletion src/Symfony/Component/Process/Tests/ProcessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function testSetInputWhileRunningThrowsAnException()
/**
* @dataProvider provideInvalidInputValues
* @expectedException \Symfony\Component\Process\Exception\InvalidArgumentException
* @expectedExceptionMessage Symfony\Component\Process\Process::setInput only accepts strings or stream resources.
* @expectedExceptionMessage Symfony\Component\Process\Process::setInput only accepts strings, Traversable objects or stream resources.
*/
public function testInvalidInput($value)
{
Expand Down Expand Up @@ -1156,6 +1156,35 @@ public function provideVariousIncrementals() {
);
}

public function testIteratorInput()
{
$nextData = 'ping';
$input = function () use (&$nextData) {
while (false !== $nextData) {
yield $nextData;
yield $nextData = '';
}
};
$input = $input();

$process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'));
$process->setInput($input);
$process->start(function ($type, $data) use ($input, &$nextData) {
if ('ping' === $data) {
$h = fopen('php://memory', 'r+');
fwrite($h, 'pong');
rewind($h);
$nextData = $h;
$input->next();
} else {
$nextData = false;
}
});

$process->wait();
$this->assertSame('pingpong', $process->getOutput());
}

/**
* @param string $commandline
* @param null|string $cwd
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy