[Process] Fixes & testNonBlockingNorClearingIteratorOutput

This commit is contained in:
Nicolas Grekas 2016-04-03 17:15:29 +02:00
parent a9dcce1f51
commit 3a109a296c
4 changed files with 42 additions and 42 deletions

View File

@ -150,17 +150,4 @@ class UnixPipes extends AbstractPipes
{
return (bool) $this->pipes;
}
/**
* Creates a new UnixPipes instance.
*
* @param Process $process
* @param string|resource $input
*
* @return UnixPipes
*/
public static function create(Process $process, $input)
{
return new static($process->isTty(), $process->isPty(), $input, !$process->isOutputDisabled() || $process->hasCallback());
}
}

View File

@ -38,7 +38,7 @@ class WindowsPipes extends AbstractPipes
/** @var bool */
private $haveReadSupport;
public function __construct($haveReadSupport, $input)
public function __construct($input, $haveReadSupport)
{
$this->haveReadSupport = (bool) $haveReadSupport;
@ -160,19 +160,6 @@ class WindowsPipes extends AbstractPipes
$this->fileHandles = array();
}
/**
* Creates a new WindowsPipes instance.
*
* @param Process $process The process
* @param $input
*
* @return WindowsPipes
*/
public static function create(Process $process, $input)
{
return new static(!$process->isOutputDisabled() || $process->hasCallback(), $input);
}
/**
* Removes temporary files.
*/

View File

@ -44,6 +44,7 @@ class Process implements \IteratorAggregate
const TIMEOUT_PRECISION = 0.2;
private $callback;
private $hasCallback = false;
private $commandline;
private $cwd;
private $env;
@ -257,6 +258,7 @@ class Process implements \IteratorAggregate
$this->resetProcessData();
$this->starttime = $this->lastOutputTime = microtime(true);
$this->callback = $this->buildCallback($callback);
$this->hasCallback = null !== $callback;
$descriptors = $this->getDescriptors();
$commandline = $this->commandline;
@ -513,7 +515,7 @@ class Process implements \IteratorAggregate
{
$this->readPipesForOutput(__FUNCTION__, false);
while (null !== $this->callback) {
while (null !== $this->callback || !feof($this->stdout) || !feof($this->stderr)) {
$out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
if (isset($out[0])) {
@ -1229,18 +1231,6 @@ class Process implements \IteratorAggregate
return $result = (bool) @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
}
/**
* Returns whether a callback is used on underlying process output.
*
* @internal
*
* @return bool
*/
public function hasCallback()
{
return (bool) $this->callback;
}
/**
* Creates the descriptors needed by the proc_open.
*
@ -1252,9 +1242,9 @@ class Process implements \IteratorAggregate
$this->input->rewind();
}
if ('\\' === DIRECTORY_SEPARATOR) {
$this->processPipes = WindowsPipes::create($this, $this->input);
$this->processPipes = new WindowsPipes($this->input, !$this->outputDisabled || $this->hasCallback);
} else {
$this->processPipes = UnixPipes::create($this, $this->input);
$this->processPipes = new UnixPipes($this->isTty(), $this->isPty(), $this->input, !$this->outputDisabled || $this->hasCallback);
}
return $this->processPipes->getDescriptors();

View File

@ -1306,6 +1306,42 @@ class ProcessTest extends \PHPUnit_Framework_TestCase
$this->assertSame($expectedOutput, $output);
}
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);
}
/**
* @param string $commandline
* @param null|string $cwd