DependsCommand.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Composer;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. /**
  18. * @author Justin Rainbow <justin.rainbow@gmail.com>
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class DependsCommand extends Command
  22. {
  23. protected $linkTypes = array(
  24. 'require' => 'requires',
  25. 'require-dev' => 'devRequires',
  26. );
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('depends')
  31. ->setDescription('Shows which packages depend on the given package')
  32. ->setDefinition(array(
  33. new InputArgument('package', InputArgument::REQUIRED, 'Package to inspect'),
  34. new InputOption('link-type', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link types to show (require, require-dev)', array_keys($this->linkTypes))
  35. ))
  36. ->setHelp(<<<EOT
  37. Displays detailed information about where a package is referenced.
  38. <info>php composer.phar depends composer/composer</info>
  39. EOT
  40. )
  41. ;
  42. }
  43. protected function execute(InputInterface $input, OutputInterface $output)
  44. {
  45. $composer = $this->getComposer();
  46. $repos = $composer->getRepositoryManager()->getRepositories();
  47. $linkTypes = $this->linkTypes;
  48. $needle = $input->getArgument('package');
  49. $verbose = (bool) $input->getOption('verbose');
  50. $types = array_map(function ($type) use ($linkTypes) {
  51. $type = rtrim($type, 's');
  52. if (!isset($linkTypes[$type])) {
  53. throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($linkTypes)));
  54. }
  55. return $type;
  56. }, $input->getOption('link-type'));
  57. foreach ($repos as $repo) {
  58. $repo->filterPackages(function ($package) use ($needle, $types, $linkTypes, $output, $verbose) {
  59. static $outputPackages = array();
  60. foreach ($types as $type) {
  61. foreach ($package->{'get'.$linkTypes[$type]}() as $link) {
  62. if ($link->getTarget() === $needle) {
  63. if ($verbose) {
  64. $output->writeln($package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' <info>' . $type . '</info> ' . $link->getPrettyConstraint());
  65. } elseif (!isset($outputPackages[$package->getName()])) {
  66. $output->writeln($package->getPrettyName());
  67. $outputPackages[$package->getName()] = true;
  68. }
  69. }
  70. }
  71. }
  72. });
  73. }
  74. }
  75. }