8000 [Process] getIncrementalOutput should work without calling getOutput · symfony/symfony@260aeb2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 260aeb2

Browse files
committed
[Process] getIncrementalOutput should work without calling getOutput
1 parent e187f8b commit 260aeb2

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/Symfony/Component/Process/Process.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -463,13 +463,7 @@ public function isOutputDisabled()
463463
*/
464464
public function getOutput()
465465
{
466-
if ($this->outputDisabled) {
467-
throw new LogicException('Output has been disabled.');
468-
}
469-
470-
$this->requireProcessIsStarted(__FUNCTION__);
471-
472-
$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);
466+
$this->readPipesForOutput(__FUNCTION__);
473467

474468
if (false === $ret = stream_get_contents($this->stdout, -1, 0)) {
475469
return '';
@@ -491,11 +485,7 @@ public function getOutput()
491485
*/
492486
public function getIncrementalOutput()
493487
{
494-
if ($this->outputDisabled) {
495-
throw new LogicException('Output has been disabled.');
496-
}
497-
498-
$this->requireProcessIsStarted(__FUNCTION__);
488+
$this->readPipesForOutput(__FUNCTION__);
499489

500490
$latest = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
501491
$this->incrementalOutputOffset = ftell($this->stdout);
@@ -531,13 +521,7 @@ public function clearOutput()
531521
*/
532522
public function getErrorOutput()
533523
{
534-
if ($this->outputDisabled) {
535-
throw new LogicException('Output has been disabled.');
536-
}
537-
538-
$this->requireProcessIsStarted(__FUNCTION__);
539-
540-
$this->readPipes(false, '\\' === DIRECTORY_SEPARATOR ? !$this->processInformation['running'] : true);
524+
$this->readPipesForOutput(__FUNCTION__);
541525

542526
if (false === $ret = stream_get_contents($this->stderr, -1, 0)) {
543527
return '';
@@ -560,11 +544,7 @@ public function getErrorOutput()
560544
*/
561545
public function getIncrementalErrorOutput()
562546
{
563-
if ($this->outputDisabled) {
564-
throw new LogicException('Output has been disabled.');
565-
}
566-
567-
$this->requireProcessIsStarted(__FUNCTION__);
547+
$this->readPipesForOutput(__FUNCTION__);
568548

569549
$latest = stream_get_contents($this->stderr, -1, $this->incrementalErrorOutputOffset);
570550
$this->incrementalErrorOutputOffset = ftell($this->stderr);
@@ -1328,6 +1308,24 @@ protected function isSigchildEnabled()
13281308
return self::$sigchild = false !== strpos(ob_get_clean(), '--enable-sigchild');
13291309
}
13301310

1311+
/**
1312+
* Reads pipes for the freshest output.
1313+
*
1314+
* @param $caller The name of the method that needs fresh outputs
1315+
*
1316+
* @throw LogicException in case output has been disabled or process is not started
1317+
*/
1318+
private function readPipesForOutput($caller)
1319+
{
1320+
if ($this->outputDisabled) {
1321+
throw new LogicException('Output has been disabled.');
1322+
}
1323+
1324+
$this->requireProcessIsStarted($caller);
1325+
1326+
$this->updateStatus(false);
1327+
}
1328+
13311329
/**
13321330
* Validates and returns the filtered timeout.
13331331
*

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,31 @@ public function pipesCodeProvider()
11651165
return $codes;
11661166
}
11671167

1168+
/**
1169+
* @dataProvider provideVariousIncrementals
1170+
*/
1171+
public function testIncrementalOutputDoesNotRequiresAnotherCall($stream, $method) {
1172+
$process = new Process(self::$phpBin." -r '\$n = 0; while (\$n < 3) { file_put_contents(\"$stream\", \$n, 1); \$n++; usleep(1000); }'", null, null, null, null);
1173+
$process->start();
1174+
$result = '';
1175+
$limit = microtime(true) + 3;
1176+
$expected = '012';
1177+
1178+
while ($result !== $expected && microtime(true) < $limit) {
1179+
$result .= $process->$method();
1180+
}
1181+
1182+
$this->assertSame($expected, $result);
1183+
$process->stop();
1184+
}
1185+
1186+
public function provideVariousIncrementals() {
1187+
return array(
1188+
array('php://stdout', 'getIncrementalOutput'),
1189+
array('php://stderr', 'getIncrementalErrorOutput'),
1190+
);
1191+
}
1192+
11681193
/**
11691194
* provides default method names for simple getter/setter.
11701195
*/

0 commit comments

Comments
 (0)
0