Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure repos are always initialized with a repo manager if possible #10799

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Composer/Command/ArchiveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function selectPackage(IOInterface $io, string $packageName, ?string $
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$repo = new CompositeRepository(array_merge(array($localRepo), $composer->getRepositoryManager()->getRepositories()));
} else {
$defaultRepos = RepositoryFactory::defaultRepos($this->getIO());
$defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io);
$io->writeError('No composer.json found in the current directory, searching packages from ' . implode(', ', array_keys($defaultRepos)));
$repo = new CompositeRepository($defaultRepos);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Composer/Command/BaseDependencyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Composer\Package\RootPackage;
use Composer\Repository\InstalledArrayRepository;
use Composer\Repository\CompositeRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Repository\RootPackageRepository;
use Composer\Repository\InstalledRepository;
use Composer\Repository\PlatformRepository;
Expand Down Expand Up @@ -80,7 +81,7 @@ protected function doExecute(InputInterface $input, OutputInterface $output, boo
// If the version we ask for is not installed then we need to locate it in remote repos and add it.
// This is needed for why-not to resolve conflicts from an uninstalled version against installed packages.
if (!$installedRepo->findPackage($needle, $textConstraint)) {
$defaultRepos = new CompositeRepository(RepositoryFactory::defaultRepos($this->getIO()));
$defaultRepos = new CompositeRepository(RepositoryFactory::defaultRepos($this->getIO(), $composer->getConfig(), $composer->getRepositoryManager()));
if ($match = $defaultRepos->findPackage($needle, $textConstraint)) {
$installedRepo->addRepository(new InstalledArrayRepository(array(clone $match)));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/HomeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,6 @@ private function initializeRepos(): array
);
}

return RepositoryFactory::defaultRepos($this->getIO());
return RepositoryFactory::defaultReposWithDefaultManager($this->getIO());
}
}
7 changes: 5 additions & 2 deletions src/Composer/Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ protected function interact(InputInterface $input, OutputInterface $output)
$repositories = $input->getOption('repository');
if (count($repositories) > 0) {
$config = Factory::createConfig($io);
$io->loadConfiguration($config);
$repoManager = RepositoryFactory::manager($io, $config);

$repos = array(new PlatformRepository);
$createDefaultPackagistRepo = true;
foreach ($repositories as $repo) {
Expand All @@ -236,14 +239,14 @@ protected function interact(InputInterface $input, OutputInterface $output)
$createDefaultPackagistRepo = false;
continue;
}
$repos[] = RepositoryFactory::createRepo($io, $config, $repoConfig);
$repos[] = RepositoryFactory::createRepo($io, $config, $repoConfig, $repoManager);
}

if ($createDefaultPackagistRepo) {
$repos[] = RepositoryFactory::createRepo($io, $config, array(
'type' => 'composer',
'url' => 'https://repo.packagist.org',
));
), $repoManager);
}

$this->repos = new CompositeRepository($repos);
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Command/PackageDiscoveryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function getRepos(): CompositeRepository
if (null === $this->repos) {
$this->repos = new CompositeRepository(array_merge(
array(new PlatformRepository),
RepositoryFactory::defaultRepos($this->getIO())
RepositoryFactory::defaultReposWithDefaultManager($this->getIO())
));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Command/ShowCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$repos = new CompositeRepository($composer->getRepositoryManager()->getRepositories());
$installedRepo->addRepository($composer->getRepositoryManager()->getLocalRepository());
} else {
$defaultRepos = RepositoryFactory::defaultRepos($io);
$defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io);
$repos = new CompositeRepository($defaultRepos);
$io->writeError('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
}
Expand All @@ -209,7 +209,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
$repos = new CompositeRepository(array_merge(array(new FilterRepository($installedRepo, array('canonical' => false))), $composer->getRepositoryManager()->getRepositories()));
} elseif ($input->getOption('all')) {
$defaultRepos = RepositoryFactory::defaultRepos($io);
$defaultRepos = RepositoryFactory::defaultReposWithDefaultManager($io);
$io->writeError('No composer.json found in the current directory, showing available packages from ' . implode(', ', array_keys($defaultRepos)));
$installedRepo = new InstalledRepository(array($platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
Expand Down
37 changes: 31 additions & 6 deletions src/Composer/Repository/RepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public static function fromString(IOInterface $io, Config $config, string $repos
public static function createRepo(IOInterface $io, Config $config, array $repoConfig, RepositoryManager $rm = null): RepositoryInterface
{
if (!$rm) {
$rm = static::manager($io, $config, Factory::createHttpDownloader($io, $config));
@trigger_error('Not passing a repository manager when calling createRepo is deprecated since Composer 2.3.6', E_USER_DEPRECATED);
$rm = static::manager($io, $config);
}
$repos = self::createRepos($rm, array($repoConfig));

Expand All @@ -95,14 +96,18 @@ public static function createRepo(IOInterface $io, Config $config, array $repoCo
*/
public static function defaultRepos(IOInterface $io = null, Config $config = null, RepositoryManager $rm = null): array
{
if (!$config) {
if (null === $rm) {
@trigger_error('Not passing a repository manager when calling defaultRepos is deprecated since Composer 2.3.6, use defaultReposWithDefaultManager() instead if you cannot get a manager.', E_USER_DEPRECATED);
}

if (null === $config) {
$config = Factory::createConfig($io);
}
if ($io) {
if (null !== $io) {
$io->loadConfiguration($config);
}
if (!$rm) {
if (!$io) {
if (null === $rm) {
if (null === $io) {
throw new \InvalidArgumentException('This function requires either an IOInterface or a RepositoryManager');
}
$rm = static::manager($io, $config, Factory::createHttpDownloader($io, $config));
Expand All @@ -118,8 +123,16 @@ public static function defaultRepos(IOInterface $io = null, Config $config = nul
* @param HttpDownloader $httpDownloader
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be updated to HttpDownloader|null (or removed entirely as being redundant with the native signature)

Copy link
Member Author

@Seldaek Seldaek May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'll remove all these redundant ones with php-cs-fixer at some point

* @return RepositoryManager
*/
public static function manager(IOInterface $io, Config $config, HttpDownloader $httpDownloader, EventDispatcher $eventDispatcher = null, ProcessExecutor $process = null): RepositoryManager
public static function manager(IOInterface $io, Config $config, HttpDownloader $httpDownloader = null, EventDispatcher $eventDispatcher = null, ProcessExecutor $process = null): RepositoryManager
{
if ($httpDownloader === null) {
$httpDownloader = Factory::createHttpDownloader($io, $config);
}
if ($process === null) {
$process = new ProcessExecutor($io);
$process->enableAsync();
}

$rm = new RepositoryManager($io, $config, $httpDownloader, $eventDispatcher, $process);
$rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
$rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
Expand All @@ -140,6 +153,18 @@ public static function manager(IOInterface $io, Config $config, HttpDownloader $
return $rm;
}

/**
* @return RepositoryInterface[]
*/
public static function defaultReposWithDefaultManager(IOInterface $io): array
{
$manager = RepositoryFactory::manager($io, $config = Factory::createConfig($io));
$io->loadConfiguration($config);

return RepositoryFactory::defaultRepos($io, $config, $manager);
}


/**
* @param array<int|string, mixed> $repoConfigs
*
Expand Down
4 changes: 3 additions & 1 deletion tests/Composer/Test/DependencyResolver/PoolBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,11 @@ public function testPoolBuilder(string $file, string $message, array $expect, ar
chdir(__DIR__.'/Fixtures/poolbuilder/');

$repositorySet = new RepositorySet($minimumStability, $stabilityFlags, $rootAliases, $rootReferences);
$config = new Config(false);
$rm = RepositoryFactory::manager($io = new NullIO(), $config);
foreach ($packageRepos as $packages) {
if (isset($packages['type'])) {
$repo = RepositoryFactory::createRepo(new NullIO, new Config(false), $packages);
$repo = RepositoryFactory::createRepo($io, $config, $packages, $rm);
$repositorySet->addRepository($repo);
continue;
}
Expand Down