Browse Source

[Process] changed run() behavior to always populate getOutput() and getErrorOutput()

Fabien Potencier 14 years ago
parent
commit
54655104ca
1 changed files with 17 additions and 15 deletions
  1. 17 15
      src/Symfony/Component/Process/Process.php

+ 17 - 15
src/Symfony/Component/Process/Process.php

@@ -70,8 +70,8 @@ class Process
      * some bytes from the output in real-time. It allows to have feedback
      * from the independent process during execution.
      *
-     * If you don't provide a callback, the STDOUT and STDERR are available only after
-     * the process is finished via the getOutput() and getErrorOutput() methods.
+     * The STDOUT and STDERR are also available after the process is finished
+     * via the getOutput() and getErrorOutput() methods.
      *
      * @param Closure|string|array $callback A PHP callback to run whenever there is some
      *                                       output available on STDOUT or STDERR
@@ -84,19 +84,21 @@ class Process
      */
     public function run($callback = null)
     {
-        if (null === $callback) {
-            $this->stdout = '';
-            $this->stderr = '';
-            $that = $this;
-            $callback = function ($type, $line) use ($that)
-            {
-                if ('out' == $type) {
-                    $that->addOutput($line);
-                } else {
-                    $that->addErrorOutput($line);
-                }
-            };
-        }
+        $this->stdout = '';
+        $this->stderr = '';
+        $that = $this;
+        $callback = function ($type, $line) use ($that, $callback)
+        {
+            if ('out' == $type) {
+                $that->addOutput($line);
+            } else {
+                $that->addErrorOutput($line);
+            }
+
+            if (null !== $callback) {
+                call_user_func($callback, $type, $line);
+            }
+        };
 
         $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));