瀏覽代碼

code improvements as suggested

Thanks for your input!
Thomas Flori 8 年之前
父節點
當前提交
ec27777341

+ 24 - 32
src/Composer/Repository/Vcs/BitbucketDriver.php

@@ -20,16 +20,16 @@ abstract class BitbucketDriver extends VcsDriver
     protected $infoCache = array();
 
     /**
-     * @var GitDriver
+     * @var VcsDriver
      */
-    protected $sshDriver;
+    protected $fallbackDriver;
 
     /**
      * {@inheritDoc}
      */
     public function initialize()
     {
-        preg_match('#^https?://bitbucket\.org/([^/]+)/(.+?)(\.git|/?)$#', $this->url, $match);
+        preg_match('#^https?://bitbucket\.org/([^/]+)/([^/]+?)(\.git|/?)$#', $this->url, $match);
         $this->owner = $match[1];
         $this->repository = $match[2];
         $this->originUrl = 'bitbucket.org';
@@ -49,8 +49,8 @@ abstract class BitbucketDriver extends VcsDriver
      */
     public function getComposerInformation($identifier)
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getComposerInformation($identifier);
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getComposerInformation($identifier);
         }
 
         if (!isset($this->infoCache[$identifier])) {
@@ -110,8 +110,8 @@ abstract class BitbucketDriver extends VcsDriver
      */
     public function getFileContent($file, $identifier)
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getFileContent($file, $identifier);
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getFileContent($file, $identifier);
         }
 
         if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
@@ -137,8 +137,8 @@ abstract class BitbucketDriver extends VcsDriver
      */
     public function getChangeDate($identifier)
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getChangeDate($identifier);
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getChangeDate($identifier);
         }
 
         $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'
@@ -163,22 +163,19 @@ abstract class BitbucketDriver extends VcsDriver
         } catch (TransportException $e) {
             $bitbucketUtil = new Bitbucket($this->io, $this->config, $this->process, $this->remoteFilesystem);
 
-            switch ($e->getCode()) {
-                case 403:
-                    if (!$this->io->hasAuthentication($this->originUrl)
-                        && $bitbucketUtil->authorizeOAuth($this->originUrl)) {
-                        return parent::getContents($url);
-                    }
-
-                    if (!$this->io->isInteractive() && $fetchingRepoData) {
-                        return $this->attemptCloneFallback();
-                    }
-
-                    throw $e;
+            if (403 === $e->getCode()) {
+                if (!$this->io->hasAuthentication($this->originUrl)
+                    && $bitbucketUtil->authorizeOAuth($this->originUrl)
+                ) {
+                    return parent::getContents($url);
+                }
 
-                default:
-                    throw $e;
+                if (!$this->io->isInteractive() && $fetchingRepoData) {
+                    return $this->attemptCloneFallback();
+                }
             }
+
+            throw $e;
         }
     }
 
@@ -187,19 +184,14 @@ abstract class BitbucketDriver extends VcsDriver
      *
      * @return string
      */
-    protected function generateSshUrl()
-    {
-        return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
-    }
+    abstract protected function generateSshUrl();
 
     protected function attemptCloneFallback()
     {
         try {
-            $this->setupSshDriver($this->generateSshUrl());
-
-            return;
+            $this->setupFallbackDriver($this->generateSshUrl());
         } catch (\RuntimeException $e) {
-            $this->sshDriver = null;
+            $this->fallbackDriver = null;
 
             $this->io->writeError(
                 '<error>Failed to clone the ' . $this->generateSshUrl() . ' repository, try running in interactive mode'
@@ -209,5 +201,5 @@ abstract class BitbucketDriver extends VcsDriver
         }
     }
 
-    abstract protected function setupSshDriver($url);
+    abstract protected function setupFallbackDriver($url);
 }

+ 21 - 13
src/Composer/Repository/Vcs/GitBitbucketDriver.php

@@ -32,8 +32,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
      */
     public function getRootIdentifier()
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getRootIdentifier();
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getRootIdentifier();
         }
 
         if (null === $this->rootIdentifier) {
@@ -51,8 +51,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
      */
     public function getUrl()
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getUrl();
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getUrl();
         }
 
         return 'https://' . $this->originUrl . '/'.$this->owner.'/'.$this->repository.'.git';
@@ -63,8 +63,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
      */
     public function getSource($identifier)
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getSource($identifier);
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getSource($identifier);
         }
 
         return array('type' => 'git', 'url' => $this->getUrl(), 'reference' => $identifier);
@@ -86,8 +86,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
      */
     public function getTags()
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getTags();
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getTags();
         }
 
         if (null === $this->tags) {
@@ -107,8 +107,8 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
      */
     public function getBranches()
     {
-        if ($this->sshDriver) {
-            return $this->sshDriver->getBranches();
+        if ($this->fallbackDriver) {
+            return $this->fallbackDriver->getBranches();
         }
 
         if (null === $this->branches) {
@@ -144,15 +144,23 @@ class GitBitbucketDriver extends BitbucketDriver implements VcsDriverInterface
     /**
      * @param string $url
      */
-    protected function setupSshDriver($url)
+    protected function setupFallbackDriver($url)
     {
-        $this->sshDriver = new GitDriver(
+        $this->fallbackDriver = new GitDriver(
             array('url' => $url),
             $this->io,
             $this->config,
             $this->process,
             $this->remoteFilesystem
         );
-        $this->sshDriver->initialize();
+        $this->fallbackDriver->initialize();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function generateSshUrl()
+    {
+        return 'git@' . $this->originUrl . ':' . $this->owner.'/'.$this->repository.'.git';
     }
 }

+ 2 - 2
src/Composer/Repository/Vcs/GitLabDriver.php

@@ -155,7 +155,7 @@ class GitLabDriver extends VcsDriver
             }
         }
 
-        if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
+        if ($isHash = preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
             return $res;
         }
 
@@ -170,7 +170,7 @@ class GitLabDriver extends VcsDriver
             return null;
         }
 
-        if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
+        if ($isHash) {
             $this->cache->write($identifier . ':' . $file, $content);
         }
 

+ 11 - 3
src/Composer/Repository/Vcs/HgBitbucketDriver.php

@@ -120,15 +120,23 @@ class HgBitbucketDriver extends BitbucketDriver
         return true;
     }
 
-    protected function setupSshDriver($url)
+    protected function setupFallbackDriver($url)
     {
-        $this->sshDriver = new HgDriver(
+        $this->fallbackDriver = new HgDriver(
             array('url' => $url),
             $this->io,
             $this->config,
             $this->process,
             $this->remoteFilesystem
         );
-        $this->sshDriver->initialize();
+        $this->fallbackDriver->initialize();
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    protected function generateSshUrl()
+    {
+        return 'hg@' . $this->originUrl . '/' . $this->owner.'/'.$this->repository;
     }
 }

+ 1 - 1
src/Composer/Repository/Vcs/PerforceDriver.php

@@ -88,7 +88,7 @@ class PerforceDriver extends VcsDriver
      */
     public function getChangeDate($identifier)
     {
-        return new \DateTime();
+        return null;
     }
 
     /**

+ 2 - 0
src/Composer/Repository/Vcs/SvnDriver.php

@@ -224,6 +224,8 @@ class SvnDriver extends VcsDriver
                 return new \DateTime($match[1], new \DateTimeZone('UTC'));
             }
         }
+
+        return null;
     }
 
     /**

+ 2 - 2
src/Composer/Repository/Vcs/VcsDriver.php

@@ -83,8 +83,8 @@ abstract class VcsDriver implements VcsDriverInterface
 
             $composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json');
 
-            if (empty($composer['time'])) {
-                $composer['time'] = $this->getChangeDate($identifier)->format('Y-m-d H:i:s');
+            if (empty($composer['time']) && $changeDate = $this->getChangeDate($identifier)) {
+                $composer['time'] = $changeDate->format('Y-m-d H:i:s');
             }
 
             $this->infoCache[$identifier] = $composer;

+ 2 - 5
src/Composer/Util/Perforce.php

@@ -439,13 +439,10 @@ class Perforce
             $index2 = strpos($result, 'no such file(s).');
             if ($index2 === false) {
                 $index3 = strpos($result, 'change');
-                if (!($index3 === false)) {
+                if ($index3 !== false) {
                     $phrase = trim(substr($result, $index3));
                     $fields = explode(' ', $phrase);
-                    $id = $fields[1];
-                    $path = substr($identifier, 0, $index) . '/' . $file . '@' . $id;
-
-                    return $path;
+                    return substr($identifier, 0, $index) . '/' . $file . '@' . $fields[1];
                 }
             }
         }