Browse Source

Fix usage of the ProcessExecutor

Jordi Boggiano 13 years ago
parent
commit
702d415472

+ 2 - 2
src/Composer/Downloader/GitDownloader.php

@@ -74,8 +74,8 @@ class GitDownloader implements DownloaderInterface
 
     private function enforceCleanDirectory($path)
     {
-        $this->process->execute(sprintf('cd %s && git status --porcelain', $path),$output);
-        if (implode('', $output)) {
+        $this->process->execute(sprintf('cd %s && git status --porcelain', $path), $output);
+        if (trim($output)) {
             throw new \RuntimeException('Source directory has uncommitted changes');
         }
     }

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

@@ -75,7 +75,7 @@ class HgDownloader implements DownloaderInterface
     private function enforceCleanDirectory($path)
     {
         $this->process->execute(sprintf('cd %s && hg st', $path), $output);
-        if (implode('', $output)) {
+        if (trim($output)) {
             throw new \RuntimeException('Source directory has uncommitted changes');
         }
     }

+ 5 - 6
src/Composer/Repository/Vcs/GitDriver.php

@@ -48,7 +48,7 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
         if (null === $this->rootIdentifier) {
             $this->rootIdentifier = 'master';
             $this->process->execute(sprintf('cd %s && git branch --no-color -r', escapeshellarg($this->tmpDir)), $output);
-            foreach ($output as $branch) {
+            foreach ($this->process->splitLines($output) as $branch) {
                 if ($branch && preg_match('{/HEAD +-> +[^/]+/(\S+)}', $branch, $match)) {
                     $this->rootIdentifier = $match[1];
                     break;
@@ -91,11 +91,9 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $this->process->execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
-            $composer = implode("\n", $output);
-            unset($output);
+            $this->process->execute(sprintf('cd %s && git show %s:composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer);
 
-            if (!$composer) {
+            if (!trim($composer)) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
             }
 
@@ -119,6 +117,7 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
     {
         if (null === $this->tags) {
             $this->process->execute(sprintf('cd %s && git tag', escapeshellarg($this->tmpDir)), $output);
+            $output = $this->process->splitLines($output);
             $this->tags = $output ? array_combine($output, $output) : array();
         }
 
@@ -134,7 +133,7 @@ class GitDriver extends VcsDriver implements VcsDriverInterface
             $branches = array();
 
             $this->process->execute(sprintf('cd %s && git branch --no-color -rv', escapeshellarg($this->tmpDir)), $output);
-            foreach ($output as $branch) {
+            foreach ($this->process->splitLines($output) as $branch) {
                 if ($branch && !preg_match('{^ *[^/]+/HEAD }', $branch)) {
                     preg_match('{^ *[^/]+/(\S+) *([a-f0-9]+) .*$}', $branch, $match);
                     $branches[$match[1]] = $match[2];

+ 9 - 8
src/Composer/Repository/Vcs/HgDriver.php

@@ -58,6 +58,7 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
         $tmpDir = escapeshellarg($this->tmpDir);
         if (null === $this->rootIdentifier) {
             $this->process->execute(sprintf('cd %s && hg tip --template "{node}"', $tmpDir), $output);
+            $output = $this->process->splitLines($output);
             $this->rootIdentifier = $output[0];
         }
 
@@ -96,11 +97,9 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $this->process->execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $output);
-            $composer = implode("\n", $output);
-            unset($output);
+            $this->process->execute(sprintf('cd %s && hg cat -r %s composer.json', escapeshellarg($this->tmpDir), escapeshellarg($identifier)), $composer);
 
-            if (!$composer) {
+            if (!trim($composer)) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier ' . $identifier . ' in ' . $this->getUrl());
             }
 
@@ -126,9 +125,10 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
             $tags = array();
 
             $this->process->execute(sprintf('cd %s && hg tags', escapeshellarg($this->tmpDir)), $output);
-            foreach ($output as $tag) {
-                if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match))
+            foreach ($this->process->splitLines($output) as $tag) {
+                if ($tag && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $tag, $match)) {
                     $tags[$match[1]] = $match[2];
+                }
             }
 
             $this->tags = $tags;
@@ -146,9 +146,10 @@ class HgDriver extends VcsDriver implements VcsDriverInterface
             $branches = array();
 
             $this->process->execute(sprintf('cd %s && hg branches', escapeshellarg($this->tmpDir)), $output);
-            foreach ($output as $branch) {
-                if (preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match))
+            foreach ($this->process->splitLines($output) as $branch) {
+                if ($branch && preg_match('(^([^\s]+)\s+\d+:(.*)$)', $branch, $match)) {
                     $branches[$match[1]] = $match[2];
+                }
             }
 
             $this->branches = $branches;

+ 10 - 10
src/Composer/Repository/Vcs/SvnDriver.php

@@ -80,11 +80,9 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
                 $rev = '';
             }
 
-            $this->process->execute(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)),$output);
-            $composer = implode("\n", $output);
-            unset($output);
+            $this->process->execute(sprintf('svn cat --non-interactive %s', escapeshellarg($this->baseUrl.$identifier.'composer.json'.$rev)), $composer);
 
-            if (!$composer) {
+            if (!trim($composer)) {
                 throw new \UnexpectedValueException('Failed to retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
             }
 
@@ -92,8 +90,8 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
 
             if (!isset($composer['time'])) {
                 $this->process->execute(sprintf('svn info %s', escapeshellarg($this->baseUrl.$identifier.$rev)), $output);
-                foreach ($output as $line) {
-                    if (preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
+                foreach ($this->process->splitLines($output) as $line) {
+                    if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
                         $date = new \DateTime($match[1]);
                         $composer['time'] = $date->format('Y-m-d H:i:s');
                         break;
@@ -114,8 +112,10 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
         if (null === $this->tags) {
             $this->process->execute(sprintf('svn ls --non-interactive %s', escapeshellarg($this->baseUrl.'/tags')), $output);
             $this->tags = array();
-            foreach ($output as $tag) {
-                $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
+            foreach ($this->process->splitLines($output) as $tag) {
+                if ($tag) {
+                    $this->tags[rtrim($tag, '/')] = '/tags/'.$tag;
+                }
             }
         }
 
@@ -131,7 +131,7 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
             $this->process->execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/')), $output);
 
             $this->branches = array();
-            foreach ($output as $line) {
+            foreach ($this->process->splitLines($output) as $line) {
                 preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
                 if ($match[2] === 'trunk/') {
                     $this->branches['trunk'] = '/trunk/@'.$match[1];
@@ -141,7 +141,7 @@ class SvnDriver extends VcsDriver implements VcsDriverInterface
             unset($output);
 
             $this->process->execute(sprintf('svn ls --verbose --non-interactive %s', escapeshellarg($this->baseUrl.'/branches')), $output);
-            foreach ($output as $line) {
+            foreach ($this->process->splitLines($output) as $line) {
                 preg_match('{^\s*(\S+).*?(\S+)\s*$}', $line, $match);
                 if ($match[2] === './') {
                     continue;

+ 11 - 5
src/Composer/Util/ProcessExecutor.php

@@ -28,19 +28,25 @@ class ProcessExecutor
      */
     public function execute($command, &$output = null)
     {
+        $captureOutput = count(func_get_args()) > 1;
         $process = new Process($command);
-        $process->run(function($type, $buffer) use ($output) {
-            if (null === $output) {
-               return;
+        $process->run(function($type, $buffer) use ($captureOutput) {
+            if ($captureOutput) {
+                return;
             }
 
             echo $buffer;
         });
 
-        if (null !== $output) {
-           $output = $process->getOutput();
+        if ($captureOutput) {
+            $output = $process->getOutput();
         }
 
         return $process->getExitCode();
     }
+
+    public function splitLines($output)
+    {
+        return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
+    }
 }