Kaynağa Gözat

Output svn output to user in verbose mode

Jordi Boggiano 13 yıl önce
ebeveyn
işleme
9ed06f8853

+ 1 - 1
src/Composer/Downloader/SvnDownloader.php

@@ -73,7 +73,7 @@ class SvnDownloader extends VcsDownloader
     {
         $util = new SvnUtil($baseUrl, $this->io);
         try {
-            return $util->execute($command, $url, $cwd, $path);
+            return $util->execute($command, $url, $cwd, $path, $this->io->isVerbose());
         } catch (\RuntimeException $e) {
             throw new \RuntimeException(
                 'Package could not be downloaded, '.$e->getMessage()

+ 16 - 9
src/Composer/Util/ProcessExecutor.php

@@ -21,30 +21,28 @@ class ProcessExecutor
 {
     static protected $timeout = 300;
 
+    protected $captureOutput;
     protected $errorOutput;
 
     /**
      * runs a process on the commandline
      *
      * @param string $command the command to execute
-     * @param null   $output  the output will be written into this var if passed
+     * @param mixed  $output  the output will be written into this var if passed by ref
+     *                        if a callable is passed it will be used as output handler
      * @param string $cwd     the working directory
      * @return int statuscode
      */
     public function execute($command, &$output = null, $cwd = null)
     {
-        $captureOutput = count(func_get_args()) > 1;
+        $this->captureOutput = count(func_get_args()) > 1;
         $this->errorOutput = null;
         $process = new Process($command, $cwd, null, null, static::getTimeout());
-        $process->run(function($type, $buffer) use ($captureOutput) {
-            if ($captureOutput) {
-                return;
-            }
 
-            echo $buffer;
-        });
+        $callback = is_callable($output) ? $output : array($this, 'outputHandler');
+        $process->run($callback);
 
-        if ($captureOutput) {
+        if ($this->captureOutput && !is_callable($output)) {
             $output = $process->getOutput();
         }
 
@@ -68,6 +66,15 @@ class ProcessExecutor
         return $this->errorOutput;
     }
 
+    public function outputHandler($type, $buffer)
+    {
+        if ($this->captureOutput) {
+            return;
+        }
+
+        echo $buffer;
+    }
+
     static public function getTimeout()
     {
         return static::$timeout;

+ 16 - 4
src/Composer/Util/Svn.php

@@ -69,16 +69,28 @@ class Svn
      * @param string $command SVN command to run
      * @param string $url     SVN url
      * @param string $cwd     Working directory
-     * @param string $path Target for a checkout
+     * @param string $path    Target for a checkout
+     * @param Boolean $verbose Output all output to the user
      *
      * @return string
      *
      * @throws \RuntimeException
      */
-    public function execute($command, $url, $cwd = null, $path = null)
+    public function execute($command, $url, $cwd = null, $path = null, $verbose = false)
     {
         $svnCommand = $this->getCommand($command, $url, $path);
-        $status = $this->process->execute($svnCommand, $output, $cwd);
+        $output = null;
+        $io = $this->io;
+        $handler = function ($type, $buffer) use (&$output, $io, $verbose) {
+            if ($type !== 'out') {
+                return;
+            }
+            $output .= $buffer;
+            if ($verbose) {
+                $io->write($buffer, false);
+            }
+        };
+        $status = $this->process->execute($svnCommand, $handler, $cwd);
         if (0 === $status) {
             return $output;
         }
@@ -107,7 +119,7 @@ class Svn
             $this->doAuthDance();
 
             // restart the process
-            return $this->execute($command, $url, $cwd, $path);
+            return $this->execute($command, $url, $cwd, $path, $verbose);
         }
 
         throw new \RuntimeException(