DependsCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\DependencyResolver\Pool;
  13. use Composer\Plugin\CommandEvent;
  14. use Composer\Plugin\PluginEvents;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputArgument;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. /**
  20. * @author Justin Rainbow <justin.rainbow@gmail.com>
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class DependsCommand extends Command
  24. {
  25. protected $linkTypes = array(
  26. 'require' => array('requires', 'requires'),
  27. 'require-dev' => array('devRequires', 'requires (dev)'),
  28. );
  29. protected function configure()
  30. {
  31. $this
  32. ->setName('depends')
  33. ->setDescription('Shows which packages depend on the given package')
  34. ->setDefinition(array(
  35. new InputArgument('package', InputArgument::REQUIRED, 'Package to inspect'),
  36. new InputOption('link-type', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link types to show (require, require-dev)', array_keys($this->linkTypes)),
  37. ))
  38. ->setHelp(<<<EOT
  39. Displays detailed information about where a package is referenced.
  40. <info>php composer.phar depends composer/composer</info>
  41. EOT
  42. )
  43. ;
  44. }
  45. protected function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. $composer = $this->getComposer();
  48. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'depends', $input, $output);
  49. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  50. $repo = $composer->getRepositoryManager()->getLocalRepository();
  51. $needle = $input->getArgument('package');
  52. $pool = new Pool();
  53. $pool->addRepository($repo);
  54. $packages = $pool->whatProvides($needle);
  55. if (empty($packages)) {
  56. throw new \InvalidArgumentException('Could not find package "'.$needle.'" in your project.');
  57. }
  58. $linkTypes = $this->linkTypes;
  59. $types = array_map(function ($type) use ($linkTypes) {
  60. $type = rtrim($type, 's');
  61. if (!isset($linkTypes[$type])) {
  62. throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($linkTypes)));
  63. }
  64. return $type;
  65. }, $input->getOption('link-type'));
  66. $messages = array();
  67. $outputPackages = array();
  68. $io = $this->getIO();
  69. foreach ($repo->getPackages() as $package) {
  70. foreach ($types as $type) {
  71. foreach ($package->{'get'.$linkTypes[$type][0]}() as $link) {
  72. if ($link->getTarget() === $needle) {
  73. if (!isset($outputPackages[$package->getName()])) {
  74. $messages[] = '<info>'.$package->getPrettyName() . '</info> ' . $linkTypes[$type][1] . ' ' . $needle .' (<info>' . $link->getPrettyConstraint() . '</info>)';
  75. $outputPackages[$package->getName()] = true;
  76. }
  77. }
  78. }
  79. }
  80. }
  81. if ($messages) {
  82. sort($messages);
  83. $io->write($messages);
  84. } else {
  85. $io->writeError('<info>There is no installed package depending on "'.$needle.'".</info>');
  86. }
  87. }
  88. }