From ccc8829ed963c8add98a60d88ad3ae6d8e7f11de Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 9 Apr 2020 15:17:30 +0200 Subject: [PATCH] Allow forcing self-update to stick to 1.x or 2.x using --1 and --2 flags, fixes #8753 --- src/Composer/Command/SelfUpdateCommand.php | 10 +++++++++- src/Composer/SelfUpdate/Versions.php | 12 +++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Composer/Command/SelfUpdateCommand.php b/src/Composer/Command/SelfUpdateCommand.php index 0dba48e28..daaab2b97 100644 --- a/src/Composer/Command/SelfUpdateCommand.php +++ b/src/Composer/Command/SelfUpdateCommand.php @@ -51,6 +51,8 @@ class SelfUpdateCommand extends BaseCommand new InputOption('stable', null, InputOption::VALUE_NONE, 'Force an update to the stable channel'), new InputOption('preview', null, InputOption::VALUE_NONE, 'Force an update to the preview channel'), new InputOption('snapshot', null, InputOption::VALUE_NONE, 'Force an update to the snapshot channel'), + new InputOption('1', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 1.x versions'), + new InputOption('2', null, InputOption::VALUE_NONE, 'Force an update to the stable channel, but only use 2.x versions'), new InputOption('set-channel-only', null, InputOption::VALUE_NONE, 'Only store the channel as the default one and then exit'), )) ->setHelp( @@ -82,9 +84,10 @@ EOT $versionsUtil = new Versions($config, $remoteFilesystem); // switch channel if requested - foreach (array('stable', 'preview', 'snapshot') as $channel) { + foreach (Versions::CHANNELS as $channel) { if ($input->getOption($channel)) { $versionsUtil->setChannel($channel); + break; } } @@ -123,9 +126,14 @@ EOT } $latest = $versionsUtil->getLatest(); + $latestStable = $versionsUtil->getLatest('stable'); $latestVersion = $latest['version']; $updateVersion = $input->getArgument('version') ?: $latestVersion; + if (is_numeric($channel) && substr($latestStable['version'], 0, 1) !== $channel) { + $io->writeError('Warning: You forced the install of '.$latestVersion.' via --'.$channel.', but '.$latestStable['version'].' is the latest stable version. Updating to it via composer self-update --stable is recommended.'); + } + if (preg_match('{^[0-9a-f]{40}$}', $updateVersion) && $updateVersion !== $latestVersion) { $io->writeError('You can not update to a specific SHA-1 as those phars are not available for download'); diff --git a/src/Composer/SelfUpdate/Versions.php b/src/Composer/SelfUpdate/Versions.php index b619bda16..f6f31b249 100644 --- a/src/Composer/SelfUpdate/Versions.php +++ b/src/Composer/SelfUpdate/Versions.php @@ -21,6 +21,8 @@ use Composer\Json\JsonFile; */ class Versions { + const CHANNELS = array('stable', 'preview', 'snapshot', '1', '2'); + private $rfs; private $config; private $channel; @@ -50,21 +52,21 @@ class Versions public function setChannel($channel) { - if (!in_array($channel, array('stable', 'preview', 'snapshot'), true)) { - throw new \InvalidArgumentException('Invalid channel '.$channel.', must be one of: stable, preview, snapshot'); + if (!in_array($channel, self::CHANNELS, true)) { + throw new \InvalidArgumentException('Invalid channel '.$channel.', must be one of: ' . implode(', ', self::CHANNELS)); } $channelFile = $this->config->get('home').'/update-channel'; $this->channel = $channel; - file_put_contents($channelFile, $channel.PHP_EOL); + file_put_contents($channelFile, (is_numeric($channel) ? 'stable' : $channel).PHP_EOL); } - public function getLatest() + public function getLatest($channel = null) { $protocol = extension_loaded('openssl') ? 'https' : 'http'; $versions = JsonFile::parseJson($this->rfs->getContents('getcomposer.org', $protocol . '://getcomposer.org/versions', false)); - foreach ($versions[$this->getChannel()] as $version) { + foreach ($versions[$channel ?: $this->getChannel()] as $version) { if ($version['min-php'] <= PHP_VERSION_ID) { return $version; }