Skip to content

Commit

Permalink
added a local option to the depends command
Browse files Browse the repository at this point in the history
  • Loading branch information
bamarni committed Dec 3, 2012
1 parent 111c42b commit 4e02cbd
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/Composer/Command/DependsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Composer\Command;

use Composer\Composer;
use Composer\DependencyResolver\Pool;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -36,7 +36,7 @@ protected function configure()
->setDescription('Shows which packages depend on the given package')
->setDefinition(array(
new InputArgument('package', InputArgument::REQUIRED, 'Package to inspect'),
new InputOption('link-type', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link types to show (require, require-dev)', array_keys($this->linkTypes))
new InputOption('link-type', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link types to show (require, require-dev)', array_keys($this->linkTypes)),
))
->setHelp(<<<EOT
Displays detailed information about where a package is referenced.
Expand All @@ -50,12 +50,21 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$composer = $this->getComposer();
$repos = $composer->getRepositoryManager()->getRepositories();
$repos = $this->getComposer()->getRepositoryManager()->getLocalRepositories();
$needle = $input->getArgument('package');

$pool = new Pool();
foreach ($repos as $repo) {
$pool->addRepository($repo);
}

$packages = $pool->whatProvides($needle);
if (empty($packages)) {
throw new \InvalidArgumentException('Could not find package "'.$needle.'" in your project.');
}

$linkTypes = $this->linkTypes;

$needle = $input->getArgument('package');
$verbose = (bool) $input->getOption('verbose');
$types = array_map(function ($type) use ($linkTypes) {
$type = rtrim($type, 's');
Expand All @@ -66,13 +75,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
return $type;
}, $input->getOption('link-type'));

$dependsOnPackages = false;
foreach ($repos as $repo) {
$repo->filterPackages(function ($package) use ($needle, $types, $linkTypes, $output, $verbose) {
$repo->filterPackages(function ($package) use ($needle, $types, $linkTypes, $output, $verbose, &$dependsOnPackages) {
static $outputPackages = array();

foreach ($types as $type) {
foreach ($package->{'get'.$linkTypes[$type]}() as $link) {
if ($link->getTarget() === $needle) {
$dependsOnPackages = true;
if ($verbose) {
$output->writeln($package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' <info>' . $type . '</info> ' . $link->getPrettyConstraint());
} elseif (!isset($outputPackages[$package->getName()])) {
Expand All @@ -84,5 +95,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
});
}

if (!$dependsOnPackages) {
$output->writeln('<info>There is no installed package depending on "'.$needle.'".</info>');
}
}
}

0 comments on commit 4e02cbd

Please sign in to comment.