1
0
Fork 0

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

pull/4616/head
Jordi Boggiano 2015-11-14 15:04:04 +00:00
parent adc0bbeeb9
commit 4b269f4ded
3 changed files with 39 additions and 4 deletions

View File

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

View File

@ -13,6 +13,7 @@
namespace Composer\Package\Version; namespace Composer\Package\Version;
use Composer\DependencyResolver\Pool; use Composer\DependencyResolver\Pool;
use Composer\Package\BasePackage;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Package\Loader\ArrayLoader; use Composer\Package\Loader\ArrayLoader;
use Composer\Package\Dumper\ArrayDumper; use Composer\Package\Dumper\ArrayDumper;
@ -44,9 +45,10 @@ class VersionSelector
* @param string $packageName * @param string $packageName
* @param string $targetPackageVersion * @param string $targetPackageVersion
* @param string $targetPhpVersion * @param string $targetPhpVersion
* @param string $preferredStability
* @return PackageInterface|bool * @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; $constraint = $targetPackageVersion ? $this->getParser()->parseConstraints($targetPackageVersion) : null;
$candidates = $this->pool->whatProvides(strtolower($packageName), $constraint, true); $candidates = $this->pool->whatProvides(strtolower($packageName), $constraint, true);
@ -65,10 +67,22 @@ class VersionSelector
// select highest version if we have many // select highest version if we have many
$package = reset($candidates); $package = reset($candidates);
$minPriority = BasePackage::$stabilities[$preferredStability];
foreach ($candidates as $candidate) { 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; 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(), '<')) { if (version_compare($package->getVersion(), $candidate->getVersion(), '<')) {
$package = $candidate; $package = $candidate;
} }

View File

@ -103,7 +103,28 @@ class VersionSelectorTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue($packages)); ->will($this->returnValue($packages));
$versionSelector = new VersionSelector($pool); $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)'); $this->assertSame($package2, $best, 'Latest version should be returned (1.1.0-beta)');
} }