Pārlūkot izejas kodu

Add cache to the getComposerInformation and add getRootIdentifier method to abstract the master/trunk names

Jordi Boggiano 13 gadi atpakaļ
vecāks
revīzija
9493483fe8

+ 20 - 8
src/Packagist/WebBundle/Repository/Repository/GitHubRepository.php

@@ -9,6 +9,7 @@ class GitHubRepository implements RepositoryInterface
     protected $repositoryData;
     protected $tags;
     protected $branches;
+    protected $infoCache = array();
 
     public function __construct($url)
     {
@@ -25,6 +26,14 @@ class GitHubRepository implements RepositoryInterface
         return 'git';
     }
 
+    /**
+     * {@inheritDoc}
+     */
+    public function getRootIdentifier()
+    {
+        return 'master';
+    }
+
     /**
      * {@inheritDoc}
      */
@@ -55,17 +64,20 @@ class GitHubRepository implements RepositoryInterface
      */
     public function getComposerInformation($identifier)
     {
-        $composer = json_decode(@file_get_contents('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json'), true);
-        if (!$composer) {
-            throw new \UnexpectedValueException('Failed to download retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
-        }
+        if (!isset($this->infoCache[$identifier])) {
+            $composer = json_decode(@file_get_contents('https://raw.github.com/'.$this->owner.'/'.$this->repository.'/'.$identifier.'/composer.json'), true);
+            if (!$composer) {
+                throw new \UnexpectedValueException('Failed to download retrieve composer information for identifier '.$identifier.' in '.$this->getUrl());
+            }
 
-        if (!isset($composer['time'])) {
-            $commit = json_decode(file_get_contents('http://github.com/api/v2/json/commits/show/'.$this->owner.'/'.$this->repository.'/'.$identifier), true);
-            $composer['time'] = $commit['commit']['committed_date'];
+            if (!isset($composer['time'])) {
+                $commit = json_decode(file_get_contents('http://github.com/api/v2/json/commits/show/'.$this->owner.'/'.$this->repository.'/'.$identifier), true);
+                $composer['time'] = $commit['commit']['committed_date'];
+            }
+            $this->infoCache[$identifier] = $composer;
         }
 
-        return $composer;
+        return $this->infoCache[$identifier];
     }
 
     /**

+ 7 - 0
src/Packagist/WebBundle/Repository/Repository/RepositoryInterface.php

@@ -12,6 +12,13 @@ interface RepositoryInterface
      */
     function getComposerInformation($identifier);
 
+    /**
+     * Return the root identifier (trunk, master, ..)
+     *
+     * @return string Identifier
+     */
+    function getRootIdentifier();
+
     /**
      * Return list of branches in the repository
      *