From d880ab68cf9567406457c209d93b5adc0099bdf2 Mon Sep 17 00:00:00 2001 From: Dan Wallis Date: Sat, 25 Jun 2022 16:06:58 +0100 Subject: [PATCH] Allow use of --locked with depends and prohibits (#10834) * Allow use of --locked with depends and prohibits * Only include other repos if not --locked * Move logic to appease PHPStan * Load a PlatformRepository when reading lock file --- .../Command/BaseDependencyCommand.php | 35 +++++++++++++++---- src/Composer/Command/DependsCommand.php | 1 + src/Composer/Command/ProhibitsCommand.php | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/Composer/Command/BaseDependencyCommand.php b/src/Composer/Command/BaseDependencyCommand.php index 8aa9759e0..992d6473a 100644 --- a/src/Composer/Command/BaseDependencyCommand.php +++ b/src/Composer/Command/BaseDependencyCommand.php @@ -58,12 +58,35 @@ abstract class BaseDependencyCommand extends BaseCommand $commandEvent = new CommandEvent(PluginEvents::COMMAND, $this->getName(), $input, $output); $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent); - $platformOverrides = $composer->getConfig()->get('platform') ?: array(); - $installedRepo = new InstalledRepository(array( - new RootPackageRepository($composer->getPackage()), - $composer->getRepositoryManager()->getLocalRepository(), - new PlatformRepository(array(), $platformOverrides), - )); + $repos = []; + + $repos[] = new RootPackageRepository($composer->getPackage()); + + if ($input->getOption('locked')) { + $locker = $composer->getLocker(); + + if (!$locker->isLocked()) { + throw new \UnexpectedValueException('A valid composer.lock file is required to run this command with --locked'); + } + + $repos[] = $locker->getLockedRepository(true); + $repos[] = new PlatformRepository([], $locker->getPlatformOverrides()); + } else { + $localRepo = $composer->getRepositoryManager()->getLocalRepository(); + $rootPkg = $composer->getPackage(); + + if (count($localRepo->getPackages()) === 0 && (count($rootPkg->getRequires()) > 0 || count($rootPkg->getDevRequires()) > 0)) { + $output->writeln('No dependencies installed. Try running composer install or update, or use --locked.'); + return 1; + } + + $repos[] = $localRepo; + + $platformOverrides = $composer->getConfig()->get('platform') ?: array(); + $repos[] = new PlatformRepository([], $platformOverrides); + } + + $installedRepo = new InstalledRepository($repos); // Parse package name and constraint list($needle, $textConstraint) = array_pad( diff --git a/src/Composer/Command/DependsCommand.php b/src/Composer/Command/DependsCommand.php index 1b353807a..de912add9 100644 --- a/src/Composer/Command/DependsCommand.php +++ b/src/Composer/Command/DependsCommand.php @@ -39,6 +39,7 @@ class DependsCommand extends BaseDependencyCommand new InputArgument(self::ARGUMENT_PACKAGE, InputArgument::REQUIRED, 'Package to inspect', null, $this->suggestInstalledPackage(true)), new InputOption(self::OPTION_RECURSIVE, 'r', InputOption::VALUE_NONE, 'Recursively resolves up to the root package'), new InputOption(self::OPTION_TREE, 't', InputOption::VALUE_NONE, 'Prints the results as a nested tree'), + new InputOption('locked', null, InputOption::VALUE_NONE, 'Read dependency information from composer.lock'), )) ->setHelp( <<setHelp( <<