8000 minor #18419 [Process] Fixes & testNonBlockingNorClearingIteratorOutp… · symfony/symfony@6dbda50 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6dbda50

Browse files
minor #18419 [Process] Fixes & testNonBlockingNorClearingIteratorOutput (nicolas-grekas)
This PR was merged into the 3.1-dev branch. Discussion ---------- [Process] Fixes & testNonBlockingNorClearingIteratorOutput | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17427 | License | MIT | Doc PR | - hasCallback (added on master in #17427) always returns true currently. Commits ------- 3a109a2 [Process] Fixes & testNonBlockingNorClearingIteratorOutput
2 parents a9dcce1 + 3a109a2 commit 6dbda50

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

src/Symfony/Component/Process/Pipes/UnixPipes.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,4 @@ public function areOpen()
150150
{
151151
return (bool) $this->pipes;
152152
}
153-
154-
/**
155-
* Creates a new UnixPipes instance.
156-
*
157-
* @param Process $process
158-
* @param string|resource $input
159-
*
160-
* @return UnixPipes
161-
*/
162-
public static function create(Process $process, $input)
163-
{
164-
return new static($process->isTty(), $process->isPty(), $input, !$process->isOutputDisabled() || $process->hasCallback());
165-
}
166153
}

src/Symfony/Component/Process/Pipes/WindowsPipes.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class WindowsPipes extends AbstractPipes
3838
/** @var bool */
3939
private $haveReadSupport;
4040

41-
public function __construct($haveReadSupport, $input)
41+
public function __construct($input, $haveReadSupport)
4242
{
4343
$this->haveReadSupport = (bool) $haveReadSupport;
4444

@@ -160,19 +160,6 @@ public function close()
160160
$this->fileHandles = array();
161161
}
162162

163-
/**
164-
* Creates a new WindowsPipes instance.
165-
*
166-
* @param Process $process The process
167-
* @param $input
168-
*
169-
* @return WindowsPipes
170-
*/
171-
public static function create(Process $process, $input)
172-
{
173-
return new static(!$process->isOutputDisabled() || $process->hasCallback(), $input);
174-
}
175-
176163
/**
177164
* Removes temporary files.
178165
*/

src/Symfony/Component/Process/Process.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Process implements \IteratorAggregate
4444
const TIMEOUT_PRECISION = 0.2;
4545

4646
private $callback;
47+
private $hasCallback = false;
4748
private $commandline;
4849
private $cwd;
4950
private $env;
@@ -257,6 +258,7 @@ public function start(callable $callback = null)
257258
$this->resetProcessData();
258259
$this->starttime = $this->lastOutputTime = microtime(true);
259260
$this->callback = $this->buildCallback($callback);
261+
$this->hasCallback = null !== $callback;
260262
$descriptors = $this->getDescriptors();
261263

262264
$commandline = $this->commandline;
@@ -513,7 +515,7 @@ public function getIterator($blocking = true, $clearOutput = true)
513515
{
514516
$this->readPipesForOutput(__FUNCTION__, false);
515517

516-
while (null !== $this->callback) {
518+
while (null !== $this->callback || !feof($this->stdout) || !feof($this->stderr)) {
517519
$out = stream_get_contents($this->stdout, -1, $this->incrementalOutputOffset);
518520

519521
if (isset($out[0])) {
@@ -1229,18 +1231,6 @@ public static function isPtySupported()
12291231
return $result = (bool) @proc_open('echo 1', array(array('pty'), array('pty'), array('pty')), $pipes);
12301232
}
12311233

1232-
/**
1233-
* Returns whether a callback is used on underlying process output.
1234-
*
1235-
* @internal
1236-
*
1237-
* @return bool
1238-
*/
1239-
public function hasCallback()
1240-
{
1241-
return (bool) $this->callback;
1242-
}
1243-
12441234
/**
12451235
* Creates the descriptors needed by the proc_open.
12461236
*
@@ -1252,9 +1242,9 @@ private function getDescriptors()
12521242
$this->input->rewind();
12531243
}
12541244
if ('\\' === DIRECTORY_SEPARATOR) {
1255-
$this->processPipes = WindowsPipes::create($this, $this->input);
1245+
$this->processPipes = new WindowsPipes($this->input, !$this->outputDisabled || $this->hasCallback);
12561246
} else {
1257-
$this->processPipes = UnixPipes::create($this, $this->input);
1247+
$this->processPipes = new UnixPipes($this->isTty(), $this->isPty(), $this->input, !$this->outputDisabled || $this->hasCallback);
12581248
}
12591249

12601250
return $this->processPipes->getDescriptors();

src/Symfony/Component/Process/Tests/Pro F438 cessTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,42 @@ public function testIteratorOutput()
13061306
$this->assertSame($expectedOutput, $output);
13071307
}
13081308

1309+
public function testNonBlockingNorClearingIteratorOutput()
1310+
{
1311+
$input = new InputStream();
1312+
1313+
$process = new Process(self::$phpBin.' -r '.escapeshellarg('fwrite(STDOUT, fread(STDIN, 3));'));
1314+
$process->setInput($input);
1315+
$process->start();
1316+
$output = array();
1317+
1318+
foreach ($process->getIterator(false, false) as $type => $data) {
1319+
$output[] = array($type, $data);
1320+
break;
1321+
}
1322+
$expectedOutput = array(
1323+
array($process::OUT, ''),
1324+
);
1325+
$this->assertSame($expectedOutput, $output);
1326+
1327+
$input->write(123);
1328+
1329+
foreach ($process->getIterator(false, false) as $type => $data) {
1330+
if ('' !== $data) {
1331+
$output[] = array($type, $data);
1332+
}
1333+
}
1334+
1335+
$this->assertSame('123', $process->getOutput());
1336+
$this->assertFalse($process->isRunning());
1337+
1338+
$expectedOutput = array(
1339+
array($process::OUT, ''),
1340+
array($process::OUT, '123'),
1341+
);
1342+
$this->assertSame($expectedOutput, $output);
1343+
}
1344+
13091345
/**
13101346
* @param string $commandline
13111347
* @param null|string $cwd

0 commit comments

Comments
 (0)
0