From 94be5b5c14f1e0b535718291b3506d826cf3665a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 2 Apr 2024 17:38:41 +0200 Subject: [PATCH] Allow restricting allowed types as well, and allow configured ignored/allowed types in Installer class --- .../DependencyResolver/PoolBuilder.php | 22 ++++++++++++--- src/Composer/Installer.php | 28 +++++++++++++++++-- src/Composer/Repository/RepositorySet.php | 8 ++++-- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/Composer/DependencyResolver/PoolBuilder.php b/src/Composer/DependencyResolver/PoolBuilder.php index 82b89cb3a..52aefa251 100644 --- a/src/Composer/DependencyResolver/PoolBuilder.php +++ b/src/Composer/DependencyResolver/PoolBuilder.php @@ -108,6 +108,8 @@ class PoolBuilder private $skippedLoad = []; /** @var list */ private $ignoredTypes = []; + /** @var list|null */ + private $allowedTypes = null; /** * If provided, only these package names are loaded @@ -173,6 +175,8 @@ class PoolBuilder } /** + * Packages of those types are ignored + * * @param list $types */ public function setIgnoredTypes(array $types): void @@ -180,6 +184,16 @@ class PoolBuilder $this->ignoredTypes = $types; } + /** + * Only packages of those types are allowed if set to non-null + * + * @param list|null $types + */ + public function setAllowedTypes(?array $types): void + { + $this->allowedTypes = $types; + } + /** * @param RepositoryInterface[] $repositories */ @@ -416,6 +430,10 @@ class PoolBuilder } foreach ($result['packages'] as $package) { $this->loadedPerRepo[$repoIndex][$package->getName()][$package->getVersion()] = $package; + + if (in_array($package->getType(), $this->ignoredTypes, true) || ($this->allowedTypes !== null && !in_array($package->getType(), $this->allowedTypes, true))) { + continue; + } $this->loadPackage($request, $repositories, $package, !isset($this->pathRepoUnlocked[$package->getName()])); } } @@ -426,10 +444,6 @@ class PoolBuilder */ private function loadPackage(Request $request, array $repositories, BasePackage $package, bool $propagateUpdate): void { - if (in_array($package->getType(), $this->ignoredTypes, true)) { - return; - } - $index = $this->indexCounter++; $this->packages[$index] = $package; diff --git a/src/Composer/Installer.php b/src/Composer/Installer.php index fe50c8fde..d1d943d36 100644 --- a/src/Composer/Installer.php +++ b/src/Composer/Installer.php @@ -176,6 +176,10 @@ class Installer protected $errorOnAudit = false; /** @var Auditor::FORMAT_* */ protected $auditFormat = Auditor::FORMAT_SUMMARY; + /** @var list */ + private $ignoredTypes = ['php-ext', 'php-ext-zend']; + /** @var list|null */ + private $allowedTypes = null; /** @var bool */ protected $updateMirrors = false; @@ -492,7 +496,7 @@ class Installer $request->setUpdateAllowList($this->updateAllowList, $this->updateAllowTransitiveDependencies); } - $pool = $repositorySet->createPool($request, $this->io, $this->eventDispatcher, $this->createPoolOptimizer($policy)); + $pool = $repositorySet->createPool($request, $this->io, $this->eventDispatcher, $this->createPoolOptimizer($policy), $this->ignoredTypes, $this->allowedTypes); $this->io->writeError('Updating dependencies'); @@ -750,7 +754,7 @@ class Installer $request->requireName($link->getTarget(), $link->getConstraint()); } - $pool = $repositorySet->createPool($request, $this->io, $this->eventDispatcher); + $pool = $repositorySet->createPool($request, $this->io, $this->eventDispatcher, null, $this->ignoredTypes, $this->allowedTypes); // solve dependencies $solver = new Solver($policy, $pool, $this->io); @@ -1103,6 +1107,26 @@ class Installer ); } + /** + * Packages of those types are ignored, by default php-ext and php-ext-zend are ignored + * + * @param list $types + */ + public function setIgnoredTypes(array $types): void + { + $this->ignoredTypes = $types; + } + + /** + * Only packages of those types are allowed if set to non-null + * + * @param list|null $types + */ + public function setAllowedTypes(?array $types): void + { + $this->allowedTypes = $types; + } + /** * @return $this */ diff --git a/src/Composer/Repository/RepositorySet.php b/src/Composer/Repository/RepositorySet.php index 2a9a49b7d..f6e8c7802 100644 --- a/src/Composer/Repository/RepositorySet.php +++ b/src/Composer/Repository/RepositorySet.php @@ -310,11 +310,15 @@ class RepositorySet /** * Create a pool for dependency resolution from the packages in this repository set. + * + * @param list $ignoredTypes Packages of those types are ignored + * @param list|null $allowedTypes Only packages of those types are allowed if set to non-null */ - public function createPool(Request $request, IOInterface $io, ?EventDispatcher $eventDispatcher = null, ?PoolOptimizer $poolOptimizer = null): Pool + public function createPool(Request $request, IOInterface $io, ?EventDispatcher $eventDispatcher = null, ?PoolOptimizer $poolOptimizer = null, array $ignoredTypes = [], ?array $allowedTypes = null): Pool { $poolBuilder = new PoolBuilder($this->acceptableStabilities, $this->stabilityFlags, $this->rootAliases, $this->rootReferences, $io, $eventDispatcher, $poolOptimizer, $this->temporaryConstraints); - $poolBuilder->setIgnoredTypes(['php-ext', 'php-ext-zend']); + $poolBuilder->setIgnoredTypes($ignoredTypes); + $poolBuilder->setAllowedTypes($allowedTypes); foreach ($this->repositories as $repo) { if (($repo instanceof InstalledRepositoryInterface || $repo instanceof InstalledRepository) && !$this->allowInstalledRepositories) {