Bladeren bron

[Process] made the callback optional

Fabien Potencier 15 jaren geleden
bovenliggende
commit
865031fa24
2 gewijzigde bestanden met toevoegingen van 65 en 2 verwijderingen
  1. 1 1
      src/Symfony/Components/Process/PhpProcess.php
  2. 64 1
      src/Symfony/Components/Process/Process.php

+ 1 - 1
src/Symfony/Components/Process/PhpProcess.php

@@ -50,7 +50,7 @@ class PhpProcess extends Process
    *
    * @return integer The exit status code
    */
-  public function run($callback)
+  public function run($callback = null)
   {
     if (null === $this->commandline)
     {

+ 64 - 1
src/Symfony/Components/Process/Process.php

@@ -29,6 +29,8 @@ class Process
   protected $options;
   protected $exitcode;
   protected $status;
+  protected $stdout;
+  protected $stderr;
 
   /**
    * Constructor.
@@ -62,13 +64,38 @@ class Process
   /**
    * Forks and run the process.
    *
+   * The callback receives the type of output (out or err) and
+   * some bytes from the output in real-time. It allows to have feedback
+   * from the forked process during exection.
+   *
+   * 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.
+   *
    * @param Closure|string|array $callback A PHP callback to run whenever there is some
    *                                       output available on STDOUT or STDERR
    *
    * @return integer The exit status code
    */
-  public function run($callback)
+  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);
+        }
+      };
+    }
+
     $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
 
     $proccess = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
@@ -149,6 +176,32 @@ class Process
     return $this->exitcode = $this->status['exitcode'];
   }
 
+  /**
+   * Returns the output of the process (STDOUT).
+   *
+   * This only returns the output if you have not supplied a callback
+   * to the run() method.
+   *
+   * @return string The process output
+   */
+  public function getOutput()
+  {
+    return $this->stdout;
+  }
+
+  /**
+   * Returns the error output of the process (STDERR).
+   *
+   * This only returns the error output if you have not supplied a callback
+   * to the run() method.
+   *
+   * @return string The process error output
+   */
+  public function getErrorOutput()
+  {
+    return $this->stderr;
+  }
+
   /**
    * Returns the exit code returned by the process.
    *
@@ -206,4 +259,14 @@ class Process
   {
     return $this->status['stopsig'];
   }
+
+  public function addOutput($line)
+  {
+    $this->stdout .= $line;
+  }
+
+  public function addErrorOutput($line)
+  {
+    $this->stderr .= $line;
+  }
 }