Browse Source

Allow forcing self-update to stick to 1.x or 2.x using --1 and --2 flags, fixes #8753

Jordi Boggiano 4 years ago
parent
commit
ccc8829ed9
2 changed files with 16 additions and 6 deletions
  1. 9 1
      src/Composer/Command/SelfUpdateCommand.php
  2. 7 5
      src/Composer/SelfUpdate/Versions.php

+ 9 - 1
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>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.</warning>');
+        }
+
         if (preg_match('{^[0-9a-f]{40}$}', $updateVersion) && $updateVersion !== $latestVersion) {
             $io->writeError('<error>You can not update to a specific SHA-1 as those phars are not available for download</error>');
 

+ 7 - 5
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;
             }