Browse Source

Fix issue when requiring multiple packages once without and once with explicit version, fixes #6859

Jordi Boggiano 7 years ago
parent
commit
00f6c0a44a
2 changed files with 16 additions and 13 deletions
  1. 15 12
      src/Composer/Command/InitCommand.php
  2. 1 1
      src/Composer/Command/RequireCommand.php

+ 15 - 12
src/Composer/Command/InitCommand.php

@@ -40,8 +40,8 @@ class InitCommand extends BaseCommand
     /** @var array */
     private $gitConfig;
 
-    /** @var Pool */
-    private $pool;
+    /** @var Pool[] */
+    private $pools;
 
     /**
      * {@inheritdoc}
@@ -598,12 +598,14 @@ EOT
 
     private function getPool(InputInterface $input, $minimumStability = null)
     {
-        if (!$this->pool) {
-            $this->pool = new Pool($minimumStability ?: $this->getMinimumStability($input));
-            $this->pool->addRepository($this->getRepos());
+        $key = $minimumStability ?: 'default';
+
+        if (!isset($this->pools[$key])) {
+            $this->pools[$key] = $pool = new Pool($minimumStability ?: $this->getMinimumStability($input));
+            $pool->addRepository($this->getRepos());
         }
 
-        return $this->pool;
+        return $this->pools[$key];
     }
 
     private function getMinimumStability(InputInterface $input)
@@ -642,13 +644,14 @@ EOT
         $package = $versionSelector->findBestCandidate($name, $requiredVersion, $phpVersion, $preferredStability);
 
         if (!$package) {
+            // Check whether the PHP version was the problem
+            if ($phpVersion && $versionSelector->findBestCandidate($name, $requiredVersion, null, $preferredStability)) {
+                throw new \InvalidArgumentException(sprintf(
+                    'Package %s at version %s has a PHP requirement incompatible with your PHP version (%s)', $name, $requiredVersion, $phpVersion
+                ));
+            }
+            // Check whether the required version was the problem
             if ($requiredVersion && $versionSelector->findBestCandidate($name, null, $phpVersion, $preferredStability)) {
-                // Check whether the PHP version was the problem
-                if ($phpVersion && $versionSelector->findBestCandidate($name, null, null, $preferredStability)) {
-                    throw new \InvalidArgumentException(sprintf(
-                        'Package %s at version %s has a PHP requirement incompatible with your PHP version (%s)', $name, $requiredVersion, $phpVersion
-                    ));
-                }
                 throw new \InvalidArgumentException(sprintf(
                     'Could not find package %s in a version matching %s', $name, $requiredVersion
                 ));

+ 1 - 1
src/Composer/Command/RequireCommand.php

@@ -113,7 +113,7 @@ EOT
             $preferredStability = $composer->getPackage()->getMinimumStability();
         }
 
-        $phpVersion = $this->repos->findPackage('php', '*')->getVersion();
+        $phpVersion = $this->repos->findPackage('php', '*')->getPrettyVersion();
         $requirements = $this->determineRequirements($input, $output, $input->getArgument('packages'), $phpVersion, $preferredStability);
 
         $requireKey = $input->getOption('dev') ? 'require-dev' : 'require';