Optimize solving by removing packages that are excluded by the root package requires

main
Jordi Boggiano 11 years ago
parent cc7920000c
commit 51e6c3a466

@ -18,6 +18,7 @@ use Composer\Package\Version\VersionParser;
use Composer\Package\Link; use Composer\Package\Link;
use Composer\Package\LinkConstraint\LinkConstraintInterface; use Composer\Package\LinkConstraint\LinkConstraintInterface;
use Composer\Package\LinkConstraint\VersionConstraint; use Composer\Package\LinkConstraint\VersionConstraint;
use Composer\Package\LinkConstraint\EmptyConstraint;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
use Composer\Repository\ComposerRepository; use Composer\Repository\ComposerRepository;
@ -38,6 +39,7 @@ class Pool
const MATCH = 1; const MATCH = 1;
const MATCH_PROVIDE = 2; const MATCH_PROVIDE = 2;
const MATCH_REPLACE = 3; const MATCH_REPLACE = 3;
const MATCH_FILTERED = 4;
protected $repositories = array(); protected $repositories = array();
protected $providerRepos = array(); protected $providerRepos = array();
@ -47,9 +49,10 @@ class Pool
protected $stabilityFlags; protected $stabilityFlags;
protected $versionParser; protected $versionParser;
protected $providerCache = array(); protected $providerCache = array();
protected $filterRequires;
protected $id = 1; protected $id = 1;
public function __construct($minimumStability = 'stable', array $stabilityFlags = array()) public function __construct($minimumStability = 'stable', array $stabilityFlags = array(), array $filterRequires = array())
{ {
$stabilities = BasePackage::$stabilities; $stabilities = BasePackage::$stabilities;
$this->versionParser = new VersionParser; $this->versionParser = new VersionParser;
@ -60,6 +63,7 @@ class Pool
} }
} }
$this->stabilityFlags = $stabilityFlags; $this->stabilityFlags = $stabilityFlags;
$this->filterRequires = $filterRequires;
} }
/** /**
@ -109,6 +113,7 @@ class Pool
if ($exempt || $this->isPackageAcceptable($names, $stability)) { if ($exempt || $this->isPackageAcceptable($names, $stability)) {
$package['id'] = $this->id++; $package['id'] = $this->id++;
$package['stability'] = $stability;
$this->packages[] = $package; $this->packages[] = $package;
foreach ($names as $provided) { foreach ($names as $provided) {
@ -275,6 +280,9 @@ class Pool
$matches[] = $this->ensurePackageIsLoaded($candidate); $matches[] = $this->ensurePackageIsLoaded($candidate);
break; break;
case self::MATCH_FILTERED:
break;
default: default:
throw new \UnexpectedValueException('Unexpected match type'); throw new \UnexpectedValueException('Unexpected match type');
} }
@ -367,18 +375,30 @@ class Pool
if (is_array($candidate)) { if (is_array($candidate)) {
$candidateName = $candidate['name']; $candidateName = $candidate['name'];
$candidateVersion = $candidate['version']; $candidateVersion = $candidate['version'];
$isDev = $candidate['stability'] === 'dev';
$isAlias = isset($candidate['alias_of']);
} else { } else {
// handle object packages // handle object packages
$candidateName = $candidate->getName(); $candidateName = $candidate->getName();
$candidateVersion = $candidate->getVersion(); $candidateVersion = $candidate->getVersion();
$isDev = $candidate->getStability() === 'dev';
$isAlias = $candidate instanceof AliasPackage;
}
if (!$isDev && !$isAlias && isset($this->filterRequires[$name])) {
$requireFilter = $this->filterRequires[$name];
} else {
$requireFilter = new EmptyConstraint;
} }
if ($candidateName === $name) { if ($candidateName === $name) {
if ($constraint === null) { $pkgConstraint = new VersionConstraint('==', $candidateVersion);
return self::MATCH;
if ($constraint === null || $constraint->matches($pkgConstraint)) {
return $requireFilter->matches($pkgConstraint) ? self::MATCH : self::MATCH_FILTERED;
} }
return $constraint->matches(new VersionConstraint('==', $candidateVersion)) ? self::MATCH : self::MATCH_NAME; return self::MATCH_NAME;
} }
if (is_array($candidate)) { if (is_array($candidate)) {
@ -393,17 +413,17 @@ class Pool
$replaces = $candidate->getReplaces(); $replaces = $candidate->getReplaces();
} }
// aliases create multiple replaces/provides for one target so they can not use the shortcut // aliases create multiple replaces/provides for one target so they can not use the shortcut below
if (isset($replaces[0]) || isset($provides[0])) { if (isset($replaces[0]) || isset($provides[0])) {
foreach ($provides as $link) { foreach ($provides as $link) {
if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) { if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
return self::MATCH_PROVIDE; return $requireFilter->matches($link->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
} }
} }
foreach ($replaces as $link) { foreach ($replaces as $link) {
if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) { if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
return self::MATCH_REPLACE; return $requireFilter->matches($link->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
} }
} }
@ -411,11 +431,11 @@ class Pool
} }
if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) { if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
return self::MATCH_PROVIDE; return $requireFilter->matches($provides[$name]->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
} }
if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) { if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
return self::MATCH_REPLACE; return $requireFilter->matches($replaces[$name]->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
} }
return self::MATCH_NONE; return self::MATCH_NONE;

@ -242,7 +242,7 @@ class Installer
// split dev and non-dev requirements by checking what would be removed if we update without the dev requirements // split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
if ($this->devMode && $this->package->getDevRequires()) { if ($this->devMode && $this->package->getDevRequires()) {
$policy = $this->createPolicy(); $policy = $this->createPolicy();
$pool = $this->createPool(); $pool = $this->createPool(true);
$pool->addRepository($installedRepo, $aliases); $pool->addRepository($installedRepo, $aliases);
// creating requirements request // creating requirements request
@ -325,7 +325,7 @@ class Installer
// creating repository pool // creating repository pool
$policy = $this->createPolicy(); $policy = $this->createPolicy();
$pool = $this->createPool(); $pool = $this->createPool($withDevReqs);
$pool->addRepository($installedRepo, $aliases); $pool->addRepository($installedRepo, $aliases);
if ($installFromLock) { if ($installFromLock) {
$pool->addRepository($lockedRepository, $aliases); $pool->addRepository($lockedRepository, $aliases);
@ -573,7 +573,7 @@ class Installer
return array_merge($installerOps, $operations); return array_merge($installerOps, $operations);
} }
private function createPool() private function createPool($withDevReqs)
{ {
$minimumStability = $this->package->getMinimumStability(); $minimumStability = $this->package->getMinimumStability();
$stabilityFlags = $this->package->getStabilityFlags(); $stabilityFlags = $this->package->getStabilityFlags();
@ -583,7 +583,16 @@ class Installer
$stabilityFlags = $this->locker->getStabilityFlags(); $stabilityFlags = $this->locker->getStabilityFlags();
} }
return new Pool($minimumStability, $stabilityFlags); $requires = $this->package->getRequires();
if ($withDevReqs) {
$requires = array_merge($requires, $this->package->getDevRequires());
}
$rootConstraints = array();
foreach ($requires as $req => $constraint) {
$rootConstraints[$req] = $constraint->getConstraint();
}
return new Pool($minimumStability, $stabilityFlags, $rootConstraints);
} }
private function createPolicy() private function createPolicy()

Loading…
Cancel
Save