1
0
Fork 0

Refactor away some unnecessary RepositorySet usages

pull/8669/head
Jordi Boggiano 2020-02-13 17:51:22 +01:00
parent f35cd8948a
commit c5c6d44a0b
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
3 changed files with 38 additions and 29 deletions

View File

@ -20,7 +20,6 @@ use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryFactory; use Composer\Repository\RepositoryFactory;
use Composer\Plugin\CommandEvent; use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
use Composer\Repository\RepositorySet;
use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Composer\Package\Version\VersionParser; use Composer\Package\Version\VersionParser;
use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Helper\Table;
@ -78,8 +77,6 @@ class BaseDependencyCommand extends BaseCommand
$composer->getRepositoryManager()->getLocalRepository(), $composer->getRepositoryManager()->getLocalRepository(),
new PlatformRepository(array(), $platformOverrides), new PlatformRepository(array(), $platformOverrides),
)); ));
$repositorySet = new RepositorySet();
$repositorySet->addRepository($repository);
// Parse package name and constraint // Parse package name and constraint
list($needle, $textConstraint) = array_pad( list($needle, $textConstraint) = array_pad(
@ -87,9 +84,15 @@ class BaseDependencyCommand extends BaseCommand
2, 2,
$input->getArgument(self::ARGUMENT_CONSTRAINT) $input->getArgument(self::ARGUMENT_CONSTRAINT)
); );
$needle = strtolower($needle);
// Find packages that are or provide the requested package first // 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)) { if (empty($packages)) {
throw new \InvalidArgumentException(sprintf('Could not find package "%s" in your project', $needle)); throw new \InvalidArgumentException(sprintf('Could not find package "%s" in your project', $needle));
} }

View File

@ -21,7 +21,6 @@ use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Package\Link; use Composer\Package\Link;
use Composer\Repository\RepositorySet;
use Composer\Semver\Constraint\Constraint; use Composer\Semver\Constraint\Constraint;
use Composer\Plugin\Capability\Capability; use Composer\Plugin\Capability\Capability;
use Composer\Util\PackageSorter; use Composer\Util\PackageSorter;
@ -158,14 +157,13 @@ class PluginManager
$localRepo = $this->composer->getRepositoryManager()->getLocalRepository(); $localRepo = $this->composer->getRepositoryManager()->getLocalRepository();
$globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null; $globalRepo = $this->globalComposer ? $this->globalComposer->getRepositoryManager()->getLocalRepository() : null;
$repositorySet = new RepositorySet('dev'); $localRepos = $localRepo;
$repositorySet->addRepository($localRepo);
if ($globalRepo) { if ($globalRepo) {
$repositorySet->addRepository($globalRepo); $localRepos = new CompositeRepository(array($localRepos, $globalRepo));
} }
$autoloadPackages = array($package->getName() => $package); $autoloadPackages = array($package->getName() => $package);
$autoloadPackages = $this->collectDependencies($repositorySet, $autoloadPackages, $package); $autoloadPackages = $this->collectDependencies($localRepos, $autoloadPackages, $package);
$generator = $this->composer->getAutoloadGenerator(); $generator = $this->composer->getAutoloadGenerator();
$autoloads = array(); $autoloads = array();
@ -376,13 +374,13 @@ class PluginManager
/** /**
* Recursively generates a map of package names to packages for all deps * Recursively generates a map of package names to packages for all deps
* *
* @param RepositorySet $repositorySet Repository set of installed packages * @param RepositoryInterface $localRepos Set of local repos
* @param array $collected Current state of the map for recursion * @param array $collected Current state of the map for recursion
* @param PackageInterface $package The package to analyze * @param PackageInterface $package The package to analyze
* *
* @return array Map of package names to packages * @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( $requires = array_merge(
$package->getRequires(), $package->getRequires(),
@ -390,10 +388,11 @@ class PluginManager
); );
foreach ($requires as $requireLink) { foreach ($requires as $requireLink) {
$requiredPackage = $this->lookupInstalledPackage($repositorySet, $requireLink); foreach ($this->lookupInstalledPackages($localRepos, $requireLink) as $requiredPackage) {
if ($requiredPackage && !isset($collected[$requiredPackage->getName()])) { if (!isset($collected[$requiredPackage->getName()])) {
$collected[$requiredPackage->getName()] = $requiredPackage; $collected[$requiredPackage->getName()] = $requiredPackage;
$collected = $this->collectDependencies($repositorySet, $collected, $requiredPackage); $collected = $this->collectDependencies($localRepos, $collected, $requiredPackage);
}
} }
} }
@ -405,16 +404,23 @@ class PluginManager
* *
* Since dependencies are already installed this should always find one. * 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 * @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;
} }
/** /**

View File

@ -134,7 +134,7 @@ class PluginInstallerTest extends TestCase
public function testInstallNewPlugin() public function testInstallNewPlugin()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$installer = new PluginInstaller($this->io, $this->composer); $installer = new PluginInstaller($this->io, $this->composer);
@ -150,7 +150,7 @@ class PluginInstallerTest extends TestCase
public function testInstallMultiplePlugins() public function testInstallMultiplePlugins()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array($this->packages[3]))); ->will($this->returnValue(array($this->packages[3])));
$installer = new PluginInstaller($this->io, $this->composer); $installer = new PluginInstaller($this->io, $this->composer);
@ -169,7 +169,7 @@ class PluginInstallerTest extends TestCase
public function testUpgradeWithNewClassName() public function testUpgradeWithNewClassName()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array($this->packages[0]))); ->will($this->returnValue(array($this->packages[0])));
$this->repository $this->repository
@ -190,7 +190,7 @@ class PluginInstallerTest extends TestCase
public function testUninstall() public function testUninstall()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array($this->packages[0]))); ->will($this->returnValue(array($this->packages[0])));
$this->repository $this->repository
@ -210,7 +210,7 @@ class PluginInstallerTest extends TestCase
public function testUpgradeWithSameClassName() public function testUpgradeWithSameClassName()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array($this->packages[1]))); ->will($this->returnValue(array($this->packages[1])));
$this->repository $this->repository
@ -230,7 +230,7 @@ class PluginInstallerTest extends TestCase
public function testRegisterPluginOnlyOneTime() public function testRegisterPluginOnlyOneTime()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
$installer = new PluginInstaller($this->io, $this->composer); $installer = new PluginInstaller($this->io, $this->composer);
@ -330,7 +330,7 @@ class PluginInstallerTest extends TestCase
public function testCommandProviderCapability() public function testCommandProviderCapability()
{ {
$this->repository $this->repository
->expects($this->once()) ->expects($this->any())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array($this->packages[7]))); ->will($this->returnValue(array($this->packages[7])));
$installer = new PluginInstaller($this->io, $this->composer); $installer = new PluginInstaller($this->io, $this->composer);