From 90daef75c6e8e7c4c0571c99b63385f5806649e7 Mon Sep 17 00:00:00 2001 From: Juan Traverso Date: Sun, 16 Jun 2013 11:12:20 +0200 Subject: [PATCH] [Process] Added support for stdout and stderr flush (Issue #7884) --- src/Symfony/Component/Process/Process.php | 26 +++++++++++++++++++ .../Process/Tests/AbstractProcessTest.php | 18 +++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index 4822e8863ee8..6b042db5923a 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -532,6 +532,19 @@ public function getIncrementalOutput() return $latest; } + /** + * Clears the process output. + * + * @return Process + */ + public function flushOutput() + { + $this->stdout = ''; + $this->incrementalOutputOffset = 0; + + return $this; + } + /** * Returns the current error output of the process (STDERR). * @@ -565,6 +578,19 @@ public function getIncrementalErrorOutput() return $latest; } + /** + * Clears the process output. + * + * @return Process + */ + public function flushErrorOutput() + { + $this->stderr = ''; + $this->incrementalErrorOutputOffset = 0; + + return $this; + } + /** * Returns the exit code returned by the process. * diff --git a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php index 5cd52efbd9bb..aaaf06447072 100644 --- a/src/Symfony/Component/Process/Tests/AbstractProcessTest.php +++ b/src/Symfony/Component/Process/Tests/AbstractProcessTest.php @@ -156,6 +156,15 @@ public function testGetIncrementalErrorOutput() } } + public function testFlushErrorOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n = 0; while ($n < 3) { file_put_contents(\'php://stderr\', \'ERROR\'); $n++; }'))); + + $p->run(); + $p->flushErrorOutput(); + $this->assertEmpty($p->getErrorOutput()); + } + public function testGetOutput() { $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}'))); @@ -175,6 +184,15 @@ public function testGetIncrementalOutput() } } + public function testFlushOutput() + { + $p = new Process(sprintf('php -r %s', escapeshellarg('$n=0;while ($n<3) {echo \' foo \';$n++;}'))); + + $p->run(); + $p->flushOutput(); + $this->assertEmpty($p->getOutput()); + } + public function testExitCodeCommandFailed() { if (defined('PHP_WINDOWS_VERSION_BUILD')) {