From 5bd61ac55c45e031e749f4b0234821636ba2a6c5 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 30 Jul 2020 15:43:28 +0200 Subject: [PATCH] Cache versions data to avoid redownloading it twice during self-update --- src/Composer/SelfUpdate/Versions.php | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Composer/SelfUpdate/Versions.php b/src/Composer/SelfUpdate/Versions.php index 6a0a1bdbf..c2c54e984 100644 --- a/src/Composer/SelfUpdate/Versions.php +++ b/src/Composer/SelfUpdate/Versions.php @@ -26,6 +26,7 @@ class Versions private $rfs; private $config; private $channel; + private $versionsData; public function __construct(Config $config, RemoteFilesystem $rfs) { @@ -63,13 +64,7 @@ class Versions public function getLatest($channel = null) { - if ($this->config->get('disable-tls') === true) { - $protocol = 'http'; - } else { - $protocol = 'https'; - } - - $versions = JsonFile::parseJson($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/versions', false)); + $versions = $this->getVersionsData(); foreach ($versions[$channel ?: $this->getChannel()] as $version) { if ($version['min-php'] <= PHP_VERSION_ID) { @@ -79,4 +74,19 @@ class Versions throw new \LogicException('There is no version of Composer available for your PHP version ('.PHP_VERSION.')'); } + + private function getVersionsData() + { + if (!$this->versionsData) { + if ($this->config->get('disable-tls') === true) { + $protocol = 'http'; + } else { + $protocol = 'https'; + } + + $this->versionsData = JsonFile::parseJson($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/versions', false)); + } + + return $this->versionsData; + } }