فهرست منبع

Follow stability option in create-project to select the best possible version, refs #4563

Jordi Boggiano 9 سال پیش
والد
کامیت
4b269f4ded

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

@@ -281,7 +281,7 @@ EOT
 
         // find the latest version if there are multiple
         $versionSelector = new VersionSelector($pool);
-        $package = $versionSelector->findBestCandidate($name, $packageVersion, $phpVersion);
+        $package = $versionSelector->findBestCandidate($name, $packageVersion, $phpVersion, $stability);
 
         if (!$package) {
             throw new \InvalidArgumentException("Could not find package $name" . ($packageVersion ? " with version $packageVersion." : " with stability $stability."));

+ 16 - 2
src/Composer/Package/Version/VersionSelector.php

@@ -13,6 +13,7 @@
 namespace Composer\Package\Version;
 
 use Composer\DependencyResolver\Pool;
+use Composer\Package\BasePackage;
 use Composer\Package\PackageInterface;
 use Composer\Package\Loader\ArrayLoader;
 use Composer\Package\Dumper\ArrayDumper;
@@ -44,9 +45,10 @@ class VersionSelector
      * @param  string                $packageName
      * @param  string                $targetPackageVersion
      * @param  string                $targetPhpVersion
+     * @param  string                $preferredStability
      * @return PackageInterface|bool
      */
-    public function findBestCandidate($packageName, $targetPackageVersion = null, $targetPhpVersion = null, $preferStable = true)
+    public function findBestCandidate($packageName, $targetPackageVersion = null, $targetPhpVersion = null, $preferredStability = 'stable')
     {
         $constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null;
         $candidates = $this->pool->whatProvides(strtolower($packageName), $constraint, true);
@@ -65,10 +67,22 @@ class VersionSelector
 
         // select highest version if we have many
         $package = reset($candidates);
+        $minPriority = BasePackage::$stabilities[$preferredStability];
         foreach ($candidates as $candidate) {
-            if ($preferStable && $package->getStabilityPriority() < $candidate->getStabilityPriority()) {
+            $candidatePriority = $candidate->getStabilityPriority();
+            $currentPriority = $package->getStabilityPriority();
+
+            // candidate is less stable than our preferred stability, and we have a package that is more stable than it, so we skip it
+            if ($minPriority < $candidatePriority && $currentPriority < $candidatePriority) {
                 continue;
             }
+            // candidate is more stable than our preferred stability, and current package is less stable than preferred stability, then we select the candidate always
+            if ($minPriority >= $candidatePriority && $minPriority < $currentPriority) {
+                $package = $candidate;
+                continue;
+            }
+
+            // select highest version of the two
             if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) {
                 $package = $candidate;
             }

+ 22 - 1
tests/Composer/Test/Package/Version/VersionSelectorTest.php

@@ -103,7 +103,28 @@ class VersionSelectorTest extends \PHPUnit_Framework_TestCase
             ->will($this->returnValue($packages));
 
         $versionSelector = new VersionSelector($pool);
-        $best = $versionSelector->findBestCandidate($packageName, null, null, false);
+        $best = $versionSelector->findBestCandidate($packageName, null, null, 'dev');
+
+        $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
+    }
+
+    public function testHighestVersionMatchingStabilityIsReturned()
+    {
+        $packageName = 'foobar';
+
+        $package1 = $this->createPackage('1.0.0');
+        $package2 = $this->createPackage('1.1.0-beta');
+        $package3 = $this->createPackage('1.2.0-alpha');
+        $packages = array($package1, $package2, $package3);
+
+        $pool = $this->createMockPool();
+        $pool->expects($this->once())
+            ->method('whatProvides')
+            ->with($packageName, null, true)
+            ->will($this->returnValue($packages));
+
+        $versionSelector = new VersionSelector($pool);
+        $best = $versionSelector->findBestCandidate($packageName, null, null, 'beta');
 
         $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
     }