소스 검색

cache only rendered composer information

As suggested we cache now only composer information and also the rendered version again. Perforce is using the same property cache as others and the `Util\Perforce::getComposerInformation()` is using the newly created methods.
Thomas Flori 8 년 전
부모
커밋
7896b1ffab

+ 9 - 9
src/Composer/Repository/Vcs/BitbucketDriver.php

@@ -54,7 +54,11 @@ abstract class BitbucketDriver extends VcsDriver
         }
 
         if (!isset($this->infoCache[$identifier])) {
-            $composer = parent::getComposerInformation($identifier);
+            if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier)) {
+                return $this->infoCache[$identifier] = JsonFile::parseJson($res);
+            }
+
+            $composer = $this->getBaseComposerInformation($identifier);
 
             // specials for bitbucket
             if (!isset($composer['support']['source'])) {
@@ -100,6 +104,10 @@ abstract class BitbucketDriver extends VcsDriver
             }
 
             $this->infoCache[$identifier] = $composer;
+
+            if ($this->shouldCache($identifier)) {
+                $this->cache->write($identifier, json_encode($composer));
+            }
         }
 
         return $this->infoCache[$identifier];
@@ -114,10 +122,6 @@ abstract class BitbucketDriver extends VcsDriver
             return $this->fallbackDriver->getFileContent($file, $identifier);
         }
 
-        if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
-            return $res;
-        }
-
         $resource = $this->getScheme() . '://api.bitbucket.org/1.0/repositories/'
                     . $this->owner . '/' . $this->repository . '/src/' . $identifier . '/' . $file;
         $fileData = JsonFile::parseJson($this->getContents($resource), $resource);
@@ -125,10 +129,6 @@ abstract class BitbucketDriver extends VcsDriver
             return null;
         }
 
-        if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
-            $this->cache->write($identifier . ':' . $file, $fileData['data']);
-        }
-
         return $fileData['data'];
     }
 

+ 0 - 3
src/Composer/Repository/Vcs/GitBitbucketDriver.php

@@ -12,12 +12,9 @@
 
 namespace Composer\Repository\Vcs;
 
-use Composer\Cache;
 use Composer\Config;
-use Composer\Downloader\TransportException;
 use Composer\Json\JsonFile;
 use Composer\IO\IOInterface;
-use Composer\Util\Bitbucket;
 
 /**
  * @author Per Bernhardt <plb@webfactory.de>

+ 0 - 8
src/Composer/Repository/Vcs/GitDriver.php

@@ -124,10 +124,6 @@ class GitDriver extends VcsDriver
      */
     public function getFileContent($file, $identifier)
     {
-        if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
-            return $res;
-        }
-
         $resource = sprintf('%s:%s', ProcessExecutor::escape($identifier), ProcessExecutor::escape($file));
         $this->process->execute(sprintf('git show %s', $resource), $content, $this->repoDir);
 
@@ -135,10 +131,6 @@ class GitDriver extends VcsDriver
             return null;
         }
 
-        if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
-            $this->cache->write($identifier . ':' . $file, $content);
-        }
-
         return $content;
     }
 

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

@@ -147,8 +147,11 @@ class GitHubDriver extends VcsDriver
         }
 
         if (!isset($this->infoCache[$identifier])) {
+            if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier)) {
+                return $this->infoCache[$identifier] = JsonFile::parseJson($res);
+            }
 
-            $composer = parent::getComposerInformation($identifier);
+            $composer = $this->getBaseComposerInformation($identifier);
 
             // specials for github
             if (!isset($composer['support']['source'])) {
@@ -159,6 +162,10 @@ class GitHubDriver extends VcsDriver
                 $composer['support']['issues'] = sprintf('https://%s/%s/%s/issues', $this->originUrl, $this->owner, $this->repository);
             }
 
+            if ($this->shouldCache($identifier)) {
+                $this->cache->write($identifier, json_encode($composer));
+            }
+
             $this->infoCache[$identifier] = $composer;
         }
 
@@ -174,10 +181,6 @@ class GitHubDriver extends VcsDriver
             return $this->gitDriver->getFileContent($file, $identifier);
         }
 
-        if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
-            return $res;
-        }
-
         $notFoundRetries = 2;
         while ($notFoundRetries) {
             try {
@@ -188,10 +191,6 @@ class GitHubDriver extends VcsDriver
                     throw new \RuntimeException('Could not retrieve ' . $file . ' for '.$identifier);
                 }
 
-                if ($content && preg_match('{[a-f0-9]{40}}i', $identifier)) {
-                    $this->cache->write($identifier . ':' . $file, $content);
-                }
-
                 return $content;
             } catch (TransportException $e) {
                 if (404 !== $e->getCode()) {

+ 0 - 69
src/Composer/Repository/Vcs/GitLabDriver.php

@@ -32,8 +32,6 @@ class GitLabDriver extends VcsDriver
     private $owner;
     private $repository;
 
-    private $cache;
-
     /**
      * @var array Project data returned by GitLab API
      */
@@ -97,51 +95,6 @@ class GitLabDriver extends VcsDriver
         $this->remoteFilesystem = $remoteFilesystem;
     }
 
-    /**
-     * Fetches the composer.json file from the project by a identifier.
-     *
-     * if specific keys arent present it will try and infer them by default values.
-     *
-     * {@inheritDoc}
-     */
-    public function getComposerInformation($identifier)
-    {
-        // Convert the root identifier to a cachable commit id
-        if (!preg_match('{[a-f0-9]{40}}i', $identifier)) {
-            $branches = $this->getBranches();
-            if (isset($branches[$identifier])) {
-                $identifier = $branches[$identifier];
-            }
-        }
-
-        if (isset($this->infoCache[$identifier])) {
-            return $this->infoCache[$identifier];
-        }
-
-        if (preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier)) {
-            return $this->infoCache[$identifier] = JsonFile::parseJson($res, $res);
-        }
-
-        try {
-            $composer = $this->fetchComposerFile($identifier);
-        } catch (TransportException $e) {
-            if ($e->getCode() !== 404) {
-                throw $e;
-            }
-            $composer = false;
-        }
-
-        if ($composer && !isset($composer['time']) && isset($this->commits[$identifier])) {
-            $composer['time'] = $this->commits[$identifier]['committed_date'];
-        }
-
-        if (preg_match('{[a-f0-9]{40}}i', $identifier)) {
-            $this->cache->write($identifier, json_encode($composer));
-        }
-
-        return $this->infoCache[$identifier] = $composer;
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -155,10 +108,6 @@ class GitLabDriver extends VcsDriver
             }
         }
 
-        if ($isHash = preg_match('{[a-f0-9]{40}}i', $identifier) && $res = $this->cache->read($identifier . ':' . $file)) {
-            return $res;
-        }
-
         $resource = $this->getApiUrl().'/repository/blobs/'.$identifier.'?filepath=' . $file;
 
         try {
@@ -170,10 +119,6 @@ class GitLabDriver extends VcsDriver
             return null;
         }
 
-        if ($isHash) {
-            $this->cache->write($identifier . ':' . $file, $content);
-        }
-
         return $content;
     }
 
@@ -256,20 +201,6 @@ class GitLabDriver extends VcsDriver
         return $this->tags;
     }
 
-    /**
-     * Fetches composer.json file from the repository through api.
-     *
-     * @param string $identifier
-     *
-     * @return array
-     */
-    protected function fetchComposerFile($identifier)
-    {
-        $resource = $this->getApiUrl().'/repository/blobs/'.$identifier.'?filepath=composer.json';
-
-        return JsonFile::parseJson($this->getContents($resource), $resource);
-    }
-
     /**
      * @return string Base URL for GitLab API v3
      */

+ 0 - 1
src/Composer/Repository/Vcs/HgBitbucketDriver.php

@@ -12,7 +12,6 @@
 
 namespace Composer\Repository\Vcs;
 
-use Composer\Cache;
 use Composer\Config;
 use Composer\Json\JsonFile;
 use Composer\IO\IOInterface;

+ 3 - 20
src/Composer/Repository/Vcs/PerforceDriver.php

@@ -26,9 +26,6 @@ class PerforceDriver extends VcsDriver
     protected $branch;
     /** @var Perforce */
     protected $perforce;
-    protected $composerInfo;
-    protected $composerInfoIdentifier;
-
     /**
      * {@inheritDoc}
      */
@@ -60,20 +57,6 @@ class PerforceDriver extends VcsDriver
         $this->perforce = Perforce::create($repoConfig, $this->getUrl(), $repoDir, $this->process, $this->io);
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    public function getComposerInformation($identifier)
-    {
-        if (!empty($this->composerInfoIdentifier)) {
-            if (strcmp($identifier, $this->composerInfoIdentifier) === 0) {
-                return $this->composerInfo;
-            }
-        }
-        $composer_info = $this->perforce->getComposerInformation($identifier);
-
-        return $composer_info;
-    }
 
     /**
      * {@inheritdoc}
@@ -155,10 +138,10 @@ class PerforceDriver extends VcsDriver
      */
     public function hasComposerFile($identifier)
     {
-        $this->composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier);
-        $this->composerInfoIdentifier = $identifier;
+        $composerInfo = $this->perforce->getComposerInformation('//' . $this->depot . '/' . $identifier);
+        $composerInfoIdentifier = $identifier;
 
-        return !empty($this->composerInfo);
+        return !empty($composerInfo);
     }
 
     /**

+ 6 - 42
src/Composer/Repository/Vcs/SvnDriver.php

@@ -116,53 +116,23 @@ class SvnDriver extends VcsDriver
     }
 
     /**
-     * {@inheritDoc}
+     * {@inheritdoc}
      */
     public function getComposerInformation($identifier)
     {
-        $identifier = '/' . trim($identifier, '/') . '/';
-
-        if ($res = $this->cache->read($identifier.'.json')) {
-            $this->infoCache[$identifier] = JsonFile::parseJson($res);
-        }
-
         if (!isset($this->infoCache[$identifier])) {
-            preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
-            if (!empty($match[2])) {
-                $path = $match[1];
-                $rev = $match[2];
-            } else {
-                $path = $identifier;
-                $rev = '';
-            }
-
-            try {
-                $resource = $path.'composer.json';
-                $output = $this->execute('svn cat', $this->baseUrl . $resource . $rev);
-                if (!trim($output)) {
-                    return;
-                }
-            } catch (\RuntimeException $e) {
-                throw new TransportException($e->getMessage());
+            if ($res = $this->cache->read($identifier.'.json')) {
+                return $this->infoCache[$identifier] = JsonFile::parseJson($res);
             }
 
-            $composer = JsonFile::parseJson($output, $this->baseUrl . $resource . $rev);
-
-            if (empty($composer['time'])) {
-                $output = $this->execute('svn info', $this->baseUrl . $path . $rev);
-                foreach ($this->process->splitLines($output) as $line) {
-                    if ($line && preg_match('{^Last Changed Date: ([^(]+)}', $line, $match)) {
-                        $date = new \DateTime($match[1], new \DateTimeZone('UTC'));
-                        $composer['time'] = $date->format('Y-m-d H:i:s');
-                        break;
-                    }
-                }
-            }
+            $composer = $this->getBaseComposerInformation($identifier);
 
             $this->cache->write($identifier.'.json', json_encode($composer));
+
             $this->infoCache[$identifier] = $composer;
         }
 
+
         return $this->infoCache[$identifier];
     }
 
@@ -174,10 +144,6 @@ class SvnDriver extends VcsDriver
     {
         $identifier = '/' . trim($identifier, '/') . '/';
 
-        if ($res = $this->cache->read($identifier . ':' . $file)) {
-            return $res;
-        }
-
         preg_match('{^(.+?)(@\d+)?/$}', $identifier, $match);
         if (!empty($match[2])) {
             $path = $match[1];
@@ -197,8 +163,6 @@ class SvnDriver extends VcsDriver
             throw new TransportException($e->getMessage());
         }
 
-        $this->cache->write($identifier . ':' . $file, $output);
-
         return $output;
     }
 

+ 36 - 7
src/Composer/Repository/Vcs/VcsDriver.php

@@ -12,6 +12,7 @@
 
 namespace Composer\Repository\Vcs;
 
+use Composer\Cache;
 use Composer\Downloader\TransportException;
 use Composer\Config;
 use Composer\Factory;
@@ -44,6 +45,8 @@ abstract class VcsDriver implements VcsDriverInterface
     protected $remoteFilesystem;
     /** @var array */
     protected $infoCache = array();
+    /** @var Cache */
+    protected $cache;
 
     /**
      * Constructor.
@@ -69,22 +72,31 @@ abstract class VcsDriver implements VcsDriverInterface
         $this->remoteFilesystem = $remoteFilesystem ?: Factory::createRemoteFilesystem($this->io, $config);
     }
 
+    /**
+     * Returns whether or not the given $identifier should be cached or not.
+     *
+     * @param string $identifier
+     * @return boolean
+     */
+    protected function shouldCache($identifier)
+    {
+        return $this->cache && preg_match('{[a-f0-9]{40}}i', $identifier);
+    }
+
     /**
      * {@inheritdoc}
      */
     public function getComposerInformation($identifier)
     {
         if (!isset($this->infoCache[$identifier])) {
-            $composerFileContent = $this->getFileContent('composer.json', $identifier);
-
-            if (!$composerFileContent) {
-                return null;
+            if ($this->shouldCache($identifier) && $res = $this->cache->read($identifier)) {
+                return $this->infoCache[$identifier] = JsonFile::parseJson($res);
             }
 
-            $composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json');
+            $composer = $this->getBaseComposerInformation($identifier);
 
-            if (empty($composer['time']) && $changeDate = $this->getChangeDate($identifier)) {
-                $composer['time'] = $changeDate->format('Y-m-d H:i:s');
+            if ($this->shouldCache($identifier)) {
+                $this->cache->write($identifier, json_encode($composer));
             }
 
             $this->infoCache[$identifier] = $composer;
@@ -94,6 +106,23 @@ abstract class VcsDriver implements VcsDriverInterface
         return $this->infoCache[$identifier];
     }
 
+    protected function getBaseComposerInformation($identifier)
+    {
+        $composerFileContent = $this->getFileContent('composer.json', $identifier);
+
+        if (!$composerFileContent) {
+            return null;
+        }
+
+        $composer = JsonFile::parseJson($composerFileContent, $identifier . ':composer.json');
+
+        if (empty($composer['time']) && $changeDate = $this->getChangeDate($identifier)) {
+            $composer['time'] = $changeDate->format('Y-m-d H:i:s');
+        }
+
+        return $composer;
+    }
+
     /**
      * {@inheritDoc}
      */

+ 4 - 46
src/Composer/Util/Perforce.php

@@ -399,14 +399,13 @@ class Perforce
 
     public function getComposerInformation($identifier)
     {
-        $index = strpos($identifier, '@');
-        if ($index === false) {
-            $composerJson = $identifier. '/composer.json';
+        $composerFileContent = $this->getFileContent('composer.json', $identifier);
 
-            return $this->getComposerInformationFromPath($composerJson);
+        if (!$composerFileContent) {
+            return;
         }
 
-        return $this->getComposerInformationFromLabel($identifier, $index);
+        return json_decode($composerFileContent, true);
     }
 
     public function getFileContent($file, $identifier)
@@ -450,47 +449,6 @@ class Perforce
         return null;
     }
 
-    public function getComposerInformationFromPath($composerJson)
-    {
-        $command = $this->generateP4Command(' print ' . $composerJson);
-        $this->executeCommand($command);
-        $result = $this->commandResult;
-        $index = strpos($result, '{');
-        if ($index === false) {
-            return '';
-        }
-        if ($index >= 0) {
-            $rawData = substr($result, $index);
-            $composer_info = json_decode($rawData, true);
-
-            return $composer_info;
-        }
-
-        return '';
-    }
-
-    public function getComposerInformationFromLabel($identifier, $index)
-    {
-        $composerJsonPath = substr($identifier, 0, $index) . '/composer.json' . substr($identifier, $index);
-        $command = $this->generateP4Command(' files ' . $composerJsonPath, false);
-        $this->executeCommand($command);
-        $result = $this->commandResult;
-        $index2 = strpos($result, 'no such file(s).');
-        if ($index2 === false) {
-            $index3 = strpos($result, 'change');
-            if (!($index3 === false)) {
-                $phrase = trim(substr($result, $index3));
-                $fields = explode(' ', $phrase);
-                $id = $fields[1];
-                $composerJson = substr($identifier, 0, $index) . '/composer.json@' . $id;
-
-                return $this->getComposerInformationFromPath($composerJson);
-            }
-        }
-
-        return "";
-    }
-
     public function getBranches()
     {
         $possibleBranches = array();