@@ -43,6 +43,11 @@ class Process implements \IteratorAggregate
43
43
// Timeout Precision in seconds.
44
44
const TIMEOUT_PRECISION = 0.2 ;
45
45
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
+
46
51
private $ callback ;
47
52
private $ hasCallback = false ;
48
53
private $ commandline ;
@@ -503,41 +508,49 @@ public function getIncrementalOutput()
503
508
/**
504
509
* Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR).
505
510
*
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.
508
512
*
509
513
* @throws LogicException in case the output has been disabled
510
514
* @throws LogicException In case the process is not started
511
515
*
512
516
* @return \Generator
513
517
*/
514
- public function getIterator ($ blocking = true , $ clearOutput = true )
518
+ public function getIterator ($ flags = 0 )
515
519
{
516
520
$ this ->readPipesForOutput (__FUNCTION__ , false );
517
521
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
+
518
527
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 );
520
530
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
+ }
527
537
528
- yield self ::OUT => $ out ;
538
+ yield self ::OUT => $ out ;
539
+ }
529
540
}
530
541
531
- $ err = stream_get_contents ($ this ->stderr , -1 , $ this ->incrementalErrorOutputOffset );
542
+ if ($ yieldErr ) {
543
+ $ err = stream_get_contents ($ this ->stderr , -1 , $ this ->incrementalErrorOutputOffset );
532
544
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
+ }
539
551
540
- yield self ::ERR => $ err ;
552
+ yield self ::ERR => $ err ;
553
+ }
541
554
}
542
555
543
556
if (!$ blocking && !isset ($ out [0 ]) && !isset ($ err [0 ])) {
0 commit comments