Skip to content

Commit

Permalink
Allow use of --locked with depends and prohibits
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden committed Jun 9, 2022
1 parent 6186d0c commit f45fb30
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/Composer/Command/BaseDependencyCommand.php
Expand Up @@ -58,12 +58,26 @@ protected function doExecute(InputInterface $input, OutputInterface $output, boo
$commandEvent = new CommandEvent(PluginEvents::COMMAND, $this->getName(), $input, $output);
$composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);

$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[] = $localRepo = $composer->getRepositoryManager()->getLocalRepository();

$platformOverrides = $composer->getConfig()->get('platform') ?: array();
$installedRepo = new InstalledRepository(array(
new RootPackageRepository($composer->getPackage()),
$composer->getRepositoryManager()->getLocalRepository(),
new PlatformRepository(array(), $platformOverrides),
));
$repos[] = new PlatformRepository([], $platformOverrides);

$installedRepo = new InstalledRepository($repos);

// Parse package name and constraint
list($needle, $textConstraint) = array_pad(
Expand All @@ -72,6 +86,12 @@ protected function doExecute(InputInterface $input, OutputInterface $output, boo
$input->hasArgument(self::ARGUMENT_CONSTRAINT) ? $input->getArgument(self::ARGUMENT_CONSTRAINT) : '*'
);

$rootPkg = $composer->getPackage();
if (!$input->getOption('locked') && count($localRepo->getPackages()) === 0 && (count($rootPkg->getRequires()) + count($rootPkg->getDevRequires()))) {
$output->writeln('<warning>No dependencies installed. Try running composer install or update, or use --locked.</warning>');
return 1;
}

// Find packages that are or provide the requested package first
$packages = $installedRepo->findPackagesWithReplacersAndProviders($needle);
if (empty($packages)) {
Expand Down
1 change: 1 addition & 0 deletions src/Composer/Command/DependsCommand.php
Expand Up @@ -39,6 +39,7 @@ protected function configure(): void
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(
<<<EOT
Expand Down
1 change: 1 addition & 0 deletions src/Composer/Command/ProhibitsCommand.php
Expand Up @@ -40,6 +40,7 @@ protected function configure(): void
new InputArgument(self::ARGUMENT_CONSTRAINT, InputArgument::REQUIRED, 'Version constraint, which version you expected to be installed'),
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(
<<<EOT
Expand Down

0 comments on commit f45fb30

Please sign in to comment.