From 9516d0dc98388d2cb3e324814061b3d7d4ce5d3d Mon Sep 17 00:00:00 2001 From: Simon Berger Date: Thu, 3 Sep 2020 08:18:07 +0200 Subject: [PATCH 1/2] Re-Fetch cached packages only once in a run --- src/Composer/Repository/ComposerRepository.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index f9018136b..344d61917 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -43,7 +43,6 @@ use React\Promise\Promise; */ class ComposerRepository extends ArrayRepository implements ConfigurableRepositoryInterface { - private $config; private $repoConfig; private $options; private $url; @@ -102,7 +101,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito } $repoConfig['url'] = rtrim($repoConfig['url'], '/'); - if ('https?' === substr($repoConfig['url'], 0, 6)) { + if (strpos($repoConfig['url'], 'https?') === 0) { $repoConfig['url'] = (extension_loaded('openssl') ? 'https' : 'http') . substr($repoConfig['url'], 6); } @@ -118,7 +117,6 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $this->allowSslDowngrade = true; } - $this->config = $config; $this->options = $repoConfig['options']; $this->url = $repoConfig['url']; @@ -571,11 +569,11 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito if ($contents = $this->cache->read($cacheKey)) { $contents = json_decode($contents, true); if (isset($contents['last-modified'])) { - $response = $this->fetchFileIfLastModified($url, $cacheKey, $contents['last-modified']); - if (true === $response) { + if (isset($alreadyLoaded[$name])) { $packages = $contents; - } elseif ($response) { - $packages = $response; + } else { + $response = $this->fetchFileIfLastModified($url, $cacheKey, $contents['last-modified']); + $packages = true === $response ? $contents : $response; } } } From cb3c71a18a7e52cc765d8be223a5f61187eb2f55 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 7 Sep 2020 17:00:02 +0200 Subject: [PATCH 2/2] Always assume the cache file is fresh when loading a package again which was already loaded, even if it lacks a last-modified header. --- src/Composer/Repository/ComposerRepository.php | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 344d61917..451f311cb 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -568,13 +568,12 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito } elseif ($useLastModifiedCheck) { if ($contents = $this->cache->read($cacheKey)) { $contents = json_decode($contents, true); - if (isset($contents['last-modified'])) { - if (isset($alreadyLoaded[$name])) { - $packages = $contents; - } else { - $response = $this->fetchFileIfLastModified($url, $cacheKey, $contents['last-modified']); - $packages = true === $response ? $contents : $response; - } + // we already loaded some packages from this file, so assume it is fresh and avoid fetching it again + if (isset($alreadyLoaded[$name])) { + $packages = $contents; + } elseif (isset($contents['last-modified'])) { + $response = $this->fetchFileIfLastModified($url, $cacheKey, $contents['last-modified']); + $packages = true === $response ? $contents : $response; } } }