-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Process] Fix pipes handling #18066
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
[Process] Fix pipes handling #18066
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,17 @@ abstract class AbstractPipes implements PipesInterface | |
/** @var bool */ | ||
private $blocked = true; | ||
|
||
public function __construct($input) | ||
{ | ||
if (is_resource($input)) { | ||
$this->input = $input; | ||
} elseif (is_string($input)) { | ||
$this->inputBuffer = $input; | ||
} else { | ||
$this->inputBuffer = (string) $input; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the |
||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -71,4 +82,64 @@ protected function unblock() | |
|
||
$this->blocked = false; | ||
} | ||
|
||
/** | ||
* Writes input to stdin. | ||
*/ | ||
protected function write() | ||
{ | ||
if (!isset($this->pipes[0])) { | ||
return; | ||
} | ||
|
||
$e = array(); | ||
$r = null !== $this->input ? array($this->input) : $e; | ||
$w = array($this->pipes[0]); | ||
|
||
// let's have a look if something changed in streams | ||
if (false === $n = @stream_select($r, $w, $e, 0, 0)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to wait here as this is done in readAndWrite |
||
return; | ||
} | ||
|
||
foreach ($w as $stdin) { | ||
if (isset($this->inputBuffer[0])) { | ||
$written = fwrite($stdin, $this->inputBuffer); | ||
$this->inputBuffer = substr($this->inputBuffer, $written); | ||
if (isset($this->inputBuffer[0])) { | ||
return array($this->pipes[0]); | ||
} | ||
} | ||
|
||
foreach ($r as $input) { | ||
for (;;) { | ||
$data = fread($input, self::CHUNK_SIZE); | ||
if (!isset($data[0])) { | ||
break; | ||
} | ||
$written = fwrite($stdin, $data); | ||
$data = substr($data, $written); | ||
if (isset($data[0])) { | ||
$this->inputBuffer = $data; | ||
|
||
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; | ||
} | ||
} | ||
} | ||
|
||
// no input to read on resource, buffer is empty | ||
if (null === $this->input && !isset($this->inputBuffer[0])) { | ||
fclose($this->pipes[0]); | ||
unset($this->pipes[0]); | ||
} | ||
|
||
if (!$w) { | ||
return array($this->pipes[0]); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the string already fits in memory, there is no gain in putting it in a
php://temp
stream