From c5c6d44a0b094e0fefa81f10fed4c72a1ecaf7c9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 13 Feb 2020 17:51:22 +0100 Subject: [PATCH] Refactor away some unnecessary RepositorySet usages --- .../Command/BaseDependencyCommand.php | 11 +++-- src/Composer/Plugin/PluginManager.php | 42 +++++++++++-------- .../Test/Plugin/PluginInstallerTest.php | 14 +++---- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/src/Composer/Command/BaseDependencyCommand.php b/src/Composer/Command/BaseDependencyCommand.php index be7703f02..dfacb0639 100644 --- a/src/Composer/Command/BaseDependencyCommand.php +++ b/src/Composer/Command/BaseDependencyCommand.php @@ -20,7 +20,6 @@ use Composer\Repository\PlatformRepository; use Composer\Repository\RepositoryFactory; use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; -use Composer\Repository\RepositorySet; use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Composer\Package\Version\VersionParser; use Symfony\Component\Console\Helper\Table; @@ -78,8 +77,6 @@ class BaseDependencyCommand extends BaseCommand $composer->getRepositoryManager()->getLocalRepository(), new PlatformRepository(array(), $platformOverrides), )); - $repositorySet = new RepositorySet(); - $repositorySet->addRepository($repository); // Parse package name and constraint list($needle, $textConstraint) = array_pad( @@ -87,9 +84,15 @@ class BaseDependencyCommand extends BaseCommand 2, $input->getArgument(self::ARGUMENT_CONSTRAINT) ); + $needle = strtolower($needle); // Find packages that are or provide the requested package first - $packages = $repositorySet->findPackages(strtolower($needle), null, RepositorySet::ALLOW_PROVIDERS_REPLACERS); + $packages = array(); + foreach ($repository->getPackages() as $package) { + if (in_array($needle, $package->getNames(), true)) { + $packages[] = $package; + } + } if (empty($packages)) { throw new \InvalidArgumentException(sprintf('Could not find package "%s" in your project', $needle)); } diff --git a/src/Composer/Plugin/PluginManager.php b/src/Composer/Plugin/PluginManager.php index a706bf8ad..cc0b39da9 100644 --- a/src/Composer/Plugin/PluginManager.php +++ b/src/Composer/Plugin/PluginManager.php @@ -21,7 +21,6 @@ use Composer\Package\Version\VersionParser; use Composer\Repository\RepositoryInterface; use Composer\Package\PackageInterface; use Composer\Package\Link; -use Composer\Repository\RepositorySet; use Composer\Semver\Constraint\Constraint; use Composer\Plugin\Capability\Capability; use Composer\Util\PackageSorter; @@ -158,14 +157,13 @@ class PluginManager $localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); $globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null; - $repositorySet = new RepositorySet('dev'); - $repositorySet->addRepository($localRepo); + $localRepos = $localRepo; if ($globalRepo) { - $repositorySet->addRepository($globalRepo); + $localRepos = new CompositeRepository(array($localRepos, $globalRepo)); } $autoloadPackages = array($package->getName() => $package); - $autoloadPackages = $this->collectDependencies($repositorySet, $autoloadPackages, $package); + $autoloadPackages = $this->collectDependencies($localRepos, $autoloadPackages, $package); $generator = $this->composer->getAutoloadGenerator(); $autoloads = array(); @@ -376,13 +374,13 @@ class PluginManager /** * Recursively generates a map of package names to packages for all deps * - * @param RepositorySet $repositorySet Repository set of installed packages - * @param array $collected Current state of the map for recursion - * @param PackageInterface $package The package to analyze + * @param RepositoryInterface $localRepos Set of local repos + * @param array $collected Current state of the map for recursion + * @param PackageInterface $package The package to analyze * * @return array Map of package names to packages */ - private function collectDependencies(RepositorySet $repositorySet, array $collected, PackageInterface $package) + private function collectDependencies(RepositoryInterface $localRepos, array $collected, PackageInterface $package) { $requires = array_merge( $package->getRequires(), @@ -390,10 +388,11 @@ class PluginManager ); foreach ($requires as $requireLink) { - $requiredPackage = $this->lookupInstalledPackage($repositorySet, $requireLink); - if ($requiredPackage && !isset($collected[$requiredPackage->getName()])) { - $collected[$requiredPackage->getName()] = $requiredPackage; - $collected = $this->collectDependencies($repositorySet, $collected, $requiredPackage); + foreach ($this->lookupInstalledPackages($localRepos, $requireLink) as $requiredPackage) { + if (!isset($collected[$requiredPackage->getName()])) { + $collected[$requiredPackage->getName()] = $requiredPackage; + $collected = $this->collectDependencies($localRepos, $collected, $requiredPackage); + } } } @@ -405,16 +404,23 @@ class PluginManager * * Since dependencies are already installed this should always find one. * - * @param RepositorySet $repositorySet Repository set of installed packages only + * @param RepositoryInterface $localRepos Set of local repos * @param Link $link Package link to look up * - * @return PackageInterface|null The found package + * @return PackageInterface[] The found packages */ - private function lookupInstalledPackage(RepositorySet $repositorySet, Link $link) + private function lookupInstalledPackages(RepositoryInterface $localRepos, Link $link) { - $packages = $repositorySet->findPackages($link->getTarget(), $link->getConstraint(), RepositorySet::ALLOW_PROVIDERS_REPLACERS | RepositorySet::ALLOW_SHADOWED_REPOSITORIES); + $matches = array(); + foreach ($localRepos->getPackages() as $candidate) { + if (in_array($link->getTarget(), $candidate->getNames(), true)) { + if ($link->getConstraint() === null || $link->getConstraint()->matches(new Constraint('=', $candidate->getVersion()))) { + $matches[] = $candidate; + } + } + } - return !empty($packages) ? $packages[0] : null; + return $matches; } /** diff --git a/tests/Composer/Test/Plugin/PluginInstallerTest.php b/tests/Composer/Test/Plugin/PluginInstallerTest.php index c08991ca7..b73907b48 100644 --- a/tests/Composer/Test/Plugin/PluginInstallerTest.php +++ b/tests/Composer/Test/Plugin/PluginInstallerTest.php @@ -134,7 +134,7 @@ class PluginInstallerTest extends TestCase public function testInstallNewPlugin() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array())); $installer = new PluginInstaller($this->io, $this->composer); @@ -150,7 +150,7 @@ class PluginInstallerTest extends TestCase public function testInstallMultiplePlugins() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array($this->packages[3]))); $installer = new PluginInstaller($this->io, $this->composer); @@ -169,7 +169,7 @@ class PluginInstallerTest extends TestCase public function testUpgradeWithNewClassName() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array($this->packages[0]))); $this->repository @@ -190,7 +190,7 @@ class PluginInstallerTest extends TestCase public function testUninstall() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array($this->packages[0]))); $this->repository @@ -210,7 +210,7 @@ class PluginInstallerTest extends TestCase public function testUpgradeWithSameClassName() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array($this->packages[1]))); $this->repository @@ -230,7 +230,7 @@ class PluginInstallerTest extends TestCase public function testRegisterPluginOnlyOneTime() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array())); $installer = new PluginInstaller($this->io, $this->composer); @@ -330,7 +330,7 @@ class PluginInstallerTest extends TestCase public function testCommandProviderCapability() { $this->repository - ->expects($this->once()) + ->expects($this->any()) ->method('getPackages') ->will($this->returnValue(array($this->packages[7]))); $installer = new PluginInstaller($this->io, $this->composer);