From 981477dd17fc142cf9ca49b8c2692106cb11973d Mon Sep 17 00:00:00 2001 From: Justin Rainbow Date: Wed, 4 Jan 2012 20:11:37 -0700 Subject: [PATCH] New 'depends' command added Basic command to display where a given package is referenced. This helps in figuring out where a package is used, and even can help in troubleshooting dependency issues. --- src/Composer/Command/DependsCommand.php | 98 +++++++++++++++++++++++++ src/Composer/Console/Application.php | 1 + 2 files changed, 99 insertions(+) create mode 100644 src/Composer/Command/DependsCommand.php diff --git a/src/Composer/Command/DependsCommand.php b/src/Composer/Command/DependsCommand.php new file mode 100644 index 000000000..1b8afdffa --- /dev/null +++ b/src/Composer/Command/DependsCommand.php @@ -0,0 +1,98 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Command; + +use Composer\Composer; +use Composer\Package\PackageInterface; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @author Justin Rainbow + */ +class DependsCommand extends Command +{ + protected function configure() + { + $this + ->setName('depends') + ->setDescription('Where is a package used?') + ->setDefinition(array( + new InputArgument('package', InputArgument::REQUIRED, 'the package to inspect') + )) + ->setHelp(<<php composer.phar depends composer/composer + +EOT + ) + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $composer = $this->getComposer(); + $references = $this->getReferences($input, $output, $composer); + + $this->printLinks($input, $output, $references); + } + + /** + * finds a list of packages which depend on another package + * + * @param InputInterface $input + * @param OutputInterface $output + * @param Composer $composer + * @return array + * @throws \InvalidArgumentException + */ + protected function getReferences(InputInterface $input, OutputInterface $output, Composer $composer) + { + $needle = $input->getArgument('package'); + + $references = array(); + $recommendedDependencies = array(); + + // 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() + ); + foreach ($repos as $repository) { + foreach ($repository->getPackages() as $package) { + foreach ($package->getRequires() as $link) { + if ($link->getTarget() === $needle) { + $references[] = $link; + } + } + + foreach ($package->getSuggests() as $link) { + if ($link->getTarget() === $needle) { + $references[] = $link; + } + } + } + } + + return $references; + } + + protected function printLinks(InputInterface $input, OutputInterface $output, array $links) + { + foreach ($links as $link) { + $output->writeln($link->getSource() . ' ' . $link->getPrettyConstraint() . ''); + } + } +} \ No newline at end of file diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 35af5dd79..37b3ccc95 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -171,6 +171,7 @@ class Application extends BaseApplication protected function registerCommands() { $this->add(new Command\AboutCommand()); + $this->add(new Command\DependsCommand()); $this->add(new Command\InstallCommand()); $this->add(new Command\UpdateCommand()); $this->add(new Command\DebugPackagesCommand());