DependsCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_OPTIONAL | 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. $references = $this->getReferences($input, $output, $composer);
  47. if ($input->getOption('verbose')) {
  48. $this->printReferences($input, $output, $references);
  49. } else {
  50. $this->printPackages($input, $output, $references);
  51. }
  52. }
  53. /**
  54. * finds a list of packages which depend on another package
  55. *
  56. * @param InputInterface $input
  57. * @param OutputInterface $output
  58. * @param Composer $composer
  59. * @return array
  60. * @throws \InvalidArgumentException
  61. */
  62. private function getReferences(InputInterface $input, OutputInterface $output, Composer $composer)
  63. {
  64. $needle = $input->getArgument('package');
  65. $references = array();
  66. $verbose = (bool) $input->getOption('verbose');
  67. $repos = $composer->getRepositoryManager()->getRepositories();
  68. $types = $input->getOption('link-type');
  69. foreach ($repos as $repository) {
  70. foreach ($repository->getPackages() as $package) {
  71. foreach ($types as $type) {
  72. $type = rtrim($type, 's');
  73. if (!isset($this->linkTypes[$type])) {
  74. throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($this->linkTypes)));
  75. }
  76. foreach ($package->{'get'.$this->linkTypes[$type]}() as $link) {
  77. if ($link->getTarget() === $needle) {
  78. if ($verbose) {
  79. $references[] = array($type, $package, $link);
  80. } else {
  81. $references[$package->getName()] = $package->getPrettyName();
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return $references;
  89. }
  90. private function printReferences(InputInterface $input, OutputInterface $output, array $references)
  91. {
  92. foreach ($references as $ref) {
  93. $output->writeln($ref[1]->getPrettyName() . ' ' . $ref[1]->getPrettyVersion() . ' <info>' . $ref[0] . '</info> ' . $ref[2]->getPrettyConstraint());
  94. }
  95. }
  96. private function printPackages(InputInterface $input, OutputInterface $output, array $packages)
  97. {
  98. ksort($packages);
  99. foreach ($packages as $package) {
  100. $output->writeln($package);
  101. }
  102. }
  103. }