From cc1dbbc36ffd4784b1d6debcb7e17f57666a94a3 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 15 Jan 2012 13:00:39 +0100 Subject: [PATCH] Add --link-type option to select link types in DependsCommand --- src/Composer/Command/DependsCommand.php | 41 +++++++++++++++++-------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/src/Composer/Command/DependsCommand.php b/src/Composer/Command/DependsCommand.php index aa720adaa..f695487ff 100644 --- a/src/Composer/Command/DependsCommand.php +++ b/src/Composer/Command/DependsCommand.php @@ -16,10 +16,12 @@ use Composer\Composer; use Composer\Package\PackageInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * @author Justin Rainbow + * @author Jordi Boggiano */ class DependsCommand extends Command { @@ -27,13 +29,14 @@ class DependsCommand extends Command { $this ->setName('depends') - ->setDescription('Where is a package used?') + ->setDescription('Shows which packages depend on the given package') ->setDefinition(array( - new InputArgument('package', InputArgument::REQUIRED, 'the package to inspect') + new InputArgument('package', InputArgument::REQUIRED, 'Package to inspect'), + new InputOption('link-type', '', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Link types to show', array('requires', 'recommends', 'suggests')) )) ->setHelp(<<php composer.phar depends composer/composer EOT @@ -46,7 +49,11 @@ EOT $composer = $this->getComposer(); $references = $this->getReferences($input, $output, $composer); - $this->printReferences($input, $output, $references); + if ($input->getOption('verbose')) { + $this->printReferences($input, $output, $references); + } else { + $this->printPackages($input, $output, $references); + } } /** @@ -63,18 +70,20 @@ EOT $needle = $input->getArgument('package'); $references = array(); + $verbose = (Boolean) $input->getOption('verbose'); - // check if we have a local installation so we can grab the right package/version - $repos = array_merge( - array($composer->getRepositoryManager()->getLocalRepository()), - $composer->getRepositoryManager()->getRepositories() - ); + $repos = $composer->getRepositoryManager()->getRepositories(); + $types = $input->getOption('link-type'); foreach ($repos as $repository) { foreach ($repository->getPackages() as $package) { - foreach (array('requires', 'recommends', 'suggests') as $type) { + foreach ($types as $type) { foreach ($package->{'get'.$type}() as $link) { if ($link->getTarget() === $needle) { - $references[] = array($type, $package, $link); + if ($verbose) { + $references[] = array($type, $package, $link); + } else { + $references[$package->getName()] = $package->getPrettyName(); + } } } } @@ -90,4 +99,12 @@ EOT $output->writeln($ref[1]->getPrettyName() . ' ' . $ref[1]->getPrettyVersion() . ' ' . $ref[0] . ' ' . $ref[2]->getPrettyConstraint()); } } + + private function printPackages(InputInterface $input, OutputInterface $output, array $packages) + { + ksort($packages); + foreach ($packages as $package) { + $output->writeln($package); + } + } } \ No newline at end of file