Symfony\Component\Process\Process::getIterator PHP Method

getIterator() public method

Returns an iterator to the output of the process, with the output type as keys (Process::OUT/ERR).
public getIterator ( integer $flags ) : Generator
$flags integer A bit field of Process::ITER_* flags
return Generator
    public function getIterator($flags = 0)
    {
        $this->readPipesForOutput(__FUNCTION__, false);
        $clearOutput = !(self::ITER_KEEP_OUTPUT & $flags);
        $blocking = !(self::ITER_NON_BLOCKING & $flags);
        $yieldOut = !(self::ITER_SKIP_OUT & $flags);
        $yieldErr = !(self::ITER_SKIP_ERR & $flags);
        while (null !== $this->callback || $yieldOut && !feof($this->stdout) || $yieldErr && !feof($this->stderr)) {
            if ($yieldOut) {
                $out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
                if (isset($out[0])) {
                    if ($clearOutput) {
                        $this->clearOutput();
                    } else {
                        $this->incrementalOutputOffset = ftell($this->stdout);
                    }
                    (yield self::OUT => $out);
                }
            }
            if ($yieldErr) {
                $err = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
                if (isset($err[0])) {
                    if ($clearOutput) {
                        $this->clearErrorOutput();
                    } else {
                        $this->incrementalErrorOutputOffset = ftell($this->stderr);
                    }
                    (yield self::ERR => $err);
                }
            }
            if (!$blocking && !isset($out[0]) && !isset($err[0])) {
                (yield self::OUT => '');
            }
            $this->checkTimeout();
            $this->readPipesForOutput(__FUNCTION__, $blocking);
        }
    }

Usage Example

Beispiel #1
0
 public function testNonBlockingNorClearingIteratorOutput()
 {
     $input = new InputStream();
     $process = new Process(self::$phpBin . ' -r ' . escapeshellarg('fwrite(STDOUT, fread(STDIN, 3));'));
     $process->setInput($input);
     $process->start();
     $output = array();
     foreach ($process->getIterator(false, false) as $type => $data) {
         $output[] = array($type, $data);
         break;
     }
     $expectedOutput = array(array($process::OUT, ''));
     $this->assertSame($expectedOutput, $output);
     $input->write(123);
     foreach ($process->getIterator(false, false) as $type => $data) {
         if ('' !== $data) {
             $output[] = array($type, $data);
         }
     }
     $this->assertSame('123', $process->getOutput());
     $this->assertFalse($process->isRunning());
     $expectedOutput = array(array($process::OUT, ''), array($process::OUT, '123'));
     $this->assertSame($expectedOutput, $output);
 }