8000 [Process] Turn getIterator() args to flags & add ITER_SKIP_OUT/ERR modes · symfony/symfony@8399b79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8399b79

Browse files
[Process] Turn getIterator() args to flags & add ITER_SKIP_OUT/ERR modes
1 parent 0b67fa3 commit 8399b79

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class Process implements \IteratorAggregate
4343
// Timeout Precision in seconds.
4444
const TIMEOUT_PRECISION = 0.2;
4545

46+
const ITER_NON_BLOCKING = 1; // By default, iterating over outputs is a blocking call, use this flag to make it non-blocking
47+
const ITER_KEEP_OUTPUT = 2; // By default, outputs are cleared while iterating, use this flag to keep them in memory
48+
const ITER_SKIP_OUT = 4; // Use this flag to skip STDOUT while iterating
49+
const ITER_SKIP_ERR = 8; // Use this flag to skip STDERR while iterating
50+
4651
private $callback;
4752
private $hasCallback = false;
4853
private $commandline;
@@ -503,41 +508,49 @@ public function getIncrementalOutput()
503508
/**
504509
* Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR).
505510
*
506-
* @param bool $blocking Whether to use a blocking read call.
507-
* @param bool $clearOutput Whether to clear or keep output in memory.
511+
* @param int $flags A bit field of Process::ITER_* flags.
508512
*
509513
* @throws LogicException in case the output has been disabled
510514
* @throws LogicException In case the process is not started
511515
*
512516
* @return \Generator
513517
*/
514-
public function getIterator($blocking = true, $clearOutput = true)
518+
public function getIterator($flags = 0)
515519
{
516520
$this->readPipesForOutput(__FUNCTION__, false);
517521

522+
$clearOutput = !(self::ITER_KEEP_OUTPUT & $flags);
523+
$blocking = !(self::ITER_NON_BLOCKING & $flags);
524+
$yieldOut = !(self::ITER_SKIP_OUT & $flags);
525+
$yieldErr = !(self::ITER_SKIP_ERR & $flags);
526+
518527
while (null !== $this->callback || !feof($this->stdout) || !feof($this->stderr)) {
519-
$out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
528+
if ($yieldOut) {
529+
$out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
520530

521-
if (isset($out[0])) {
522-
if ($clearOutput) {
523-
$this->clearOutput();
524-
} else {
525-
$this->incrementalOutputOffset = ftell($this->stdout);
526-
}
531+
if (isset($out[0])) {
532+
if ($clearOutput) {
533+
$this->clearOutput();
534+
} else {
535+
$this->incrementalOutputOffset = ftell($this->stdout);
536+
}
527537

528-
yield self::OUT => $out;
538+
yield self::OUT => $out;
539+
}
529540
}
530541

531-
$err = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
542+
if ($yieldErr) {
543+
$err = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
532544

533-
if (isset($err[0])) {
534-
if ($clearOutput) {
535-
$this->clearErrorOutput();
536-
} else {
537-
$this->incrementalErrorOutputOffset = ftell($this->stderr);
538-
}
545+
if (isset($err[0])) {
546+
if ($clearOutput) {
547+
$this->clearErrorOutput();
548+
} else {
549+
$this->incrementalErrorOutputOffset = ftell($this->stderr);
550+
}
539551

540-
yield self::ERR => $err;
552+
yield self::ERR => $err;
553+
}
541554
}
542555

543556
if (!$blocking && !isset($out[0]) && !isset($err[0])) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,7 +1225,7 @@ public function testInputStreamWithCallable()
12251225
$input->onEmpty($stream);
12261226
$input->write($stream());
12271227

1228-
$process = new Process(self::$phpBin.' -r '.escapeshellarg('stream_copy_to_stream(STDIN, STDOUT);'));
1228+
$process = new Process(self::$phpBin.' -r '.escapeshellarg('echo fread(STDIN, 3);'));
12291229
$process->setInput($input);
12301230
$process->start(function ($type, $data) use ($input) {
12311231
$input->close();
@@ -1274,7 +1274,7 @@ public function testIteratorOutput()
12741274
{
12751275
$input = new InputStream();
12761276

1277-
$process = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, 123); fwrite(STDERR, 234); fwrite(STDOUT, fread(STDIN, 3)); fwrite(STDERR, 456);'));
1277+
$process = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, 123); fwrite(STDERR, 234); flush(); usleep(10000); fwrite(STDOUT, fread(STDIN, 3)); fwrite(STDERR, 456);'));
12781278
$process->setInput($input);
12791279
$process->start();
12801280
$output = array();
@@ -1315,7 +1315,7 @@ public function testNonBlockingNorClearingIteratorOutput()
13151315
$process->start();
13161316
$output = array();
13171317

1318-
foreach ($process->getIterator(false, false) as $type => $data) {
1318+
foreach ($process->getIterator($process::ITER_NON_BLOCKING | $process::ITER_KEEP_OUTPUT) as $type => $data) {
13191319
$output[] = array($type, $data);
13201320
break;
13211321
}
@@ -1326,7 +1326,7 @@ public function testNonBlockingNorClearingIteratorOutput()
13261326

13271327
$input->write(123);
13281328

1329-
foreach ($process->getIterator(false, false) as $type => $data) {
1329+
foreach ($process->getIterator($process::ITER_NON_BLOCKING | $process::ITER_KEEP_OUTPUT) as $type => $data) {
13301330
if ('' !== $data) {
13311331
$output[] = array($type, $data);
13321332
}

0 commit comments

Comments
 (0)
0