From be62fcca7f9c658f1abd52cb5371a9768a9e0907 Mon Sep 17 00:00:00 2001 From: boombatower Date: Fri, 7 Sep 2012 00:03:48 -0700 Subject: [PATCH] [process] provide a restart method. --- src/Symfony/Component/Process/Process.php | 38 +++++++++++++++++++ .../Component/Process/Tests/ProcessTest.php | 18 +++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Symfony/Component/Process/Process.php b/src/Symfony/Component/Process/Process.php index cd1e8b42155a..e4b4640074a8 100644 --- a/src/Symfony/Component/Process/Process.php +++ b/src/Symfony/Component/Process/Process.php @@ -143,6 +143,20 @@ public function __destruct() $this->stop(); } + public function __clone() + { + $this->exitcode = null; + $this->fallbackExitcode = null; + $this->processInformation = null; + $this->stdout = null; + $this->stderr = null; + $this->pipes = null; + $this->process = null; + $this->status = self::STATUS_READY; + $this->fileHandles = null; + $this->readBytes = null; + } + /** * Runs the process. * @@ -300,6 +314,30 @@ public function start($callback = null) $this->updateStatus(); } + /** + * Restarts the process by cloning and invoking start(). + * + * @param callable $callback A PHP callback to run whenever there is some + * output available on STDOUT or STDERR + * + * @return Process The new process. + * + * @throws \RuntimeException When process can't be launch or is stopped + * @throws \RuntimeException When process is already running + * @see start() + */ + public function restart($callback = null) + { + if ($this->isRunning()) { + throw new \RuntimeException('Process is already running'); + } + + $process = clone $this; + $process->start($callback); + + return $process; + } + /** * Waits for the process to terminate. * diff --git a/src/Symfony/Component/Process/Tests/ProcessTest.php b/src/Symfony/Component/Process/Tests/ProcessTest.php index 64f929cc20d6..5548cb7a696d 100644 --- a/src/Symfony/Component/Process/Tests/ProcessTest.php +++ b/src/Symfony/Component/Process/Tests/ProcessTest.php @@ -155,6 +155,24 @@ public function testStop() } } + public function testRestart() + { + $process1 = new Process('php -r "echo getmypid();"'); + $process1->run(); + $process2 = $process1->restart(); + + usleep(300000); // wait for output + + // Ensure that both processed finished and the output is numeric + $this->assertFalse($process1->isRunning()); + $this->assertFalse($process2->isRunning()); + $this->assertTrue(is_numeric($process1->getOutput())); + $this->assertTrue(is_numeric($process2->getOutput())); + + // Ensure that restart returned a new process by check that the output is different + $this->assertNotEquals($process1->getOutput(), $process2->getOutput()); + } + public function testPhpDeadlock() { $this->markTestSkipped('Can course php to hang');