Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51e6c3a

Browse files
committed
Optimize solving by removing packages that are excluded by the root package requires
1 parent cc79200 commit 51e6c3a

2 files changed

Lines changed: 42 additions & 13 deletions

File tree

src/Composer/DependencyResolver/Pool.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Composer\Package\Link;
1919
use Composer\Package\LinkConstraint\LinkConstraintInterface;
2020
use Composer\Package\LinkConstraint\VersionConstraint;
21+
use Composer\Package\LinkConstraint\EmptyConstraint;
2122
use Composer\Repository\RepositoryInterface;
2223
use Composer\Repository\CompositeRepository;
2324
use Composer\Repository\ComposerRepository;
@@ -38,6 +39,7 @@ class Pool
3839
const MATCH = 1;
3940
const MATCH_PROVIDE = 2;
4041
const MATCH_REPLACE = 3;
42+
const MATCH_FILTERED = 4;
4143

4244
protected $repositories = array();
4345
protected $providerRepos = array();
@@ -47,9 +49,10 @@ class Pool
4749
protected $stabilityFlags;
4850
protected $versionParser;
4951
protected $providerCache = array();
52+
protected $filterRequires;
5053
protected $id = 1;
5154

52-
public function __construct($minimumStability = 'stable', array $stabilityFlags = array())
55+
public function __construct($minimumStability = 'stable', array $stabilityFlags = array(), array $filterRequires = array())
5356
{
5457
$stabilities = BasePackage::$stabilities;
5558
$this->versionParser = new VersionParser;
@@ -60,6 +63,7 @@ public function __construct($minimumStability = 'stable', array $stabilityFlags
6063
}
6164
}
6265
$this->stabilityFlags = $stabilityFlags;
66+
$this->filterRequires = $filterRequires;
6367
}
6468

6569
/**
@@ -109,6 +113,7 @@ public function addRepository(RepositoryInterface $repo, $rootAliases = array())
109113

110114
if ($exempt || $this->isPackageAcceptable($names, $stability)) {
111115
$package['id'] = $this->id++;
116+
$package['stability'] = $stability;
112117
$this->packages[] = $package;
113118

114119
foreach ($names as $provided) {
@@ -275,6 +280,9 @@ private function computeWhatProvides($name, $constraint)
275280
$matches[] = $this->ensurePackageIsLoaded($candidate);
276281
break;
277282

283+
case self::MATCH_FILTERED:
284+
break;
285+
278286
default:
279287
throw new \UnexpectedValueException('Unexpected match type');
280288
}
@@ -367,18 +375,30 @@ private function match($candidate, $name, LinkConstraintInterface $constraint =
367375
if (is_array($candidate)) {
368376
$candidateName = $candidate['name'];
369377
$candidateVersion = $candidate['version'];
378+
$isDev = $candidate['stability'] === 'dev';
379+
$isAlias = isset($candidate['alias_of']);
370380
} else {
371381
// handle object packages
372382
$candidateName = $candidate->getName();
373383
$candidateVersion = $candidate->getVersion();
384+
$isDev = $candidate->getStability() === 'dev';
385+
$isAlias = $candidate instanceof AliasPackage;
386+
}
387+
388+
if (!$isDev && !$isAlias && isset($this->filterRequires[$name])) {
389+
$requireFilter = $this->filterRequires[$name];
390+
} else {
391+
$requireFilter = new EmptyConstraint;
374392
}
375393

376394
if ($candidateName === $name) {
377-
if ($constraint === null) {
378-
return self::MATCH;
395+
$pkgConstraint = new VersionConstraint('==', $candidateVersion);
396+
397+
if ($constraint === null || $constraint->matches($pkgConstraint)) {
398+
return $requireFilter->matches($pkgConstraint) ? self::MATCH : self::MATCH_FILTERED;
379399
}
380400

381-
return $constraint->matches(new VersionConstraint('==', $candidateVersion)) ? self::MATCH : self::MATCH_NAME;
401+
return self::MATCH_NAME;
382402
}
383403

384404
if (is_array($candidate)) {
@@ -393,29 +413,29 @@ private function match($candidate, $name, LinkConstraintInterface $constraint =
393413
$replaces = $candidate->getReplaces();
394414
}
395415

396-
// aliases create multiple replaces/provides for one target so they can not use the shortcut
416+
// aliases create multiple replaces/provides for one target so they can not use the shortcut below
397417
if (isset($replaces[0]) || isset($provides[0])) {
398418
foreach ($provides as $link) {
399419
if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
400-
return self::MATCH_PROVIDE;
420+
return $requireFilter->matches($link->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
401421
}
402422
}
403423

404424
foreach ($replaces as $link) {
405425
if ($link->getTarget() === $name && ($constraint === null || $constraint->matches($link->getConstraint()))) {
406-
return self::MATCH_REPLACE;
426+
return $requireFilter->matches($link->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
407427
}
408428
}
409429

410430
return self::MATCH_NONE;
411431
}
412432

413433
if (isset($provides[$name]) && ($constraint === null || $constraint->matches($provides[$name]->getConstraint()))) {
414-
return self::MATCH_PROVIDE;
434+
return $requireFilter->matches($provides[$name]->getConstraint()) ? self::MATCH_PROVIDE : self::MATCH_FILTERED;
415435
}
416436

417437
if (isset($replaces[$name]) && ($constraint === null || $constraint->matches($replaces[$name]->getConstraint()))) {
418-
return self::MATCH_REPLACE;
438+
return $requireFilter->matches($replaces[$name]->getConstraint()) ? self::MATCH_REPLACE : self::MATCH_FILTERED;
419439
}
420440

421441
return self::MATCH_NONE;

src/Composer/Installer.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public function run()
242242
// split dev and non-dev requirements by checking what would be removed if we update without the dev requirements
243243
if ($this->devMode && $this->package->getDevRequires()) {
244244
$policy = $this->createPolicy();
245-
$pool = $this->createPool();
245+
$pool = $this->createPool(true);
246246
$pool->addRepository($installedRepo, $aliases);
247247

248248
// creating requirements request
@@ -325,7 +325,7 @@ protected function doInstall($localRepo, $installedRepo, $platformRepo, $aliases
325325

326326
// creating repository pool
327327
$policy = $this->createPolicy();
328-
$pool = $this->createPool();
328+
$pool = $this->createPool($withDevReqs);
329329
$pool->addRepository($installedRepo, $aliases);
330330
if ($installFromLock) {
331331
$pool->addRepository($lockedRepository, $aliases);
@@ -573,7 +573,7 @@ private function movePluginsToFront(array $operations)
573573
return array_merge($installerOps, $operations);
574574
}
575575

576-
private function createPool()
576+
private function createPool($withDevReqs)
577577
{
578578
$minimumStability = $this->package->getMinimumStability();
579579
$stabilityFlags = $this->package->getStabilityFlags();
@@ -583,7 +583,16 @@ private function createPool()
583583
$stabilityFlags = $this->locker->getStabilityFlags();
584584
}
585585

586-
return new Pool($minimumStability, $stabilityFlags);
586+
$requires = $this->package->getRequires();
587+
if ($withDevReqs) {
588+
$requires = array_merge($requires, $this->package->getDevRequires());
589+
}
590+
$rootConstraints = array();
591+
foreach ($requires as $req => $constraint) {
592+
$rootConstraints[$req] = $constraint->getConstraint();
593+
}
594+
595+
return new Pool($minimumStability, $stabilityFlags, $rootConstraints);
587596
}
588597

589598
private function createPolicy()

0 commit comments

Comments
 (0)