DependsCommand.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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\Package\Link;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Repository\ArrayRepository;
  16. use Composer\Repository\CompositeRepository;
  17. use Composer\Repository\PlatformRepository;
  18. use Composer\Plugin\CommandEvent;
  19. use Composer\Plugin\PluginEvents;
  20. use Composer\Semver\VersionParser;
  21. use Symfony\Component\Console\Input\InputInterface;
  22. use Symfony\Component\Console\Input\InputArgument;
  23. use Symfony\Component\Console\Input\InputOption;
  24. use Symfony\Component\Console\Output\OutputInterface;
  25. /**
  26. * @author Justin Rainbow <justin.rainbow@gmail.com>
  27. * @author Jordi Boggiano <j.boggiano@seld.be>
  28. */
  29. class DependsCommand extends Command
  30. {
  31. protected $linkTypes = array(
  32. 'require' => array('requires', 'requires'),
  33. 'require-dev' => array('devRequires', 'requires (dev)'),
  34. );
  35. protected function configure()
  36. {
  37. $this
  38. ->setName('depends')
  39. ->setDescription('Shows which packages depend on the given package')
  40. ->setDefinition(array(
  41. new InputArgument('package', InputArgument::REQUIRED, 'Package to inspect'),
  42. new InputOption('link-type', '', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Link types to show (require, require-dev)', array_keys($this->linkTypes)),
  43. new InputOption('match-constraint', 'm', InputOption::VALUE_REQUIRED, 'Filters the dependencies shown using this constraint', '*'),
  44. new InputOption('invert-match-constraint', 'i', InputOption::VALUE_NONE, 'Turns --match-constraint around into a blacklist insteead of whitelist'),
  45. new InputOption('with-replaces', '', InputOption::VALUE_NONE, 'Search for replaced packages as well'),
  46. ))
  47. ->setHelp(<<<EOT
  48. Displays detailed information about where a package is referenced.
  49. <info>php composer.phar depends composer/composer</info>
  50. EOT
  51. )
  52. ;
  53. }
  54. protected function execute(InputInterface $input, OutputInterface $output)
  55. {
  56. $composer = $this->getComposer();
  57. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'depends', $input, $output);
  58. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  59. $platformOverrides = $composer->getConfig()->get('platform') ?: array();
  60. $repo = new CompositeRepository(array(
  61. new ArrayRepository(array($composer->getPackage())),
  62. $composer->getRepositoryManager()->getLocalRepository(),
  63. new PlatformRepository(array(), $platformOverrides),
  64. ));
  65. $needle = $input->getArgument('package');
  66. $pool = new Pool();
  67. $pool->addRepository($repo);
  68. $packages = $pool->whatProvides($needle);
  69. if (empty($packages)) {
  70. throw new \InvalidArgumentException('Could not find package "'.$needle.'" in your project.');
  71. }
  72. $linkTypes = $this->linkTypes;
  73. $types = array_map(function ($type) use ($linkTypes) {
  74. $type = rtrim($type, 's');
  75. if (!isset($linkTypes[$type])) {
  76. throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($linkTypes)));
  77. }
  78. return $type;
  79. }, $input->getOption('link-type'));
  80. $versionParser = new VersionParser();
  81. $constraint = $versionParser->parseConstraints($input->getOption('match-constraint'));
  82. $matchInvert = $input->getOption('invert-match-constraint');
  83. $needles = array($needle);
  84. if (true === $input->getOption('with-replaces')) {
  85. foreach ($packages as $package) {
  86. $needles = array_merge($needles, array_map(function (Link $link) {
  87. return $link->getTarget();
  88. }, $package->getReplaces()));
  89. }
  90. }
  91. $messages = array();
  92. $outputPackages = array();
  93. $io = $this->getIO();
  94. /** @var PackageInterface $package */
  95. foreach ($repo->getPackages() as $package) {
  96. foreach ($types as $type) {
  97. /** @var Link $link */
  98. foreach ($package->{'get'.$linkTypes[$type][0]}() as $link) {
  99. foreach ($needles as $needle) {
  100. if ($link->getTarget() === $needle && ($link->getConstraint()->matches($constraint) ? !$matchInvert : $matchInvert)) {
  101. if (!isset($outputPackages[$package->getName()])) {
  102. $messages[] = '<info>'.$package->getPrettyName() . '</info> ' . $linkTypes[$type][1] . ' ' . $needle .' (<info>' . $link->getPrettyConstraint() . '</info>)';
  103. $outputPackages[$package->getName()] = true;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. if ($messages) {
  111. sort($messages);
  112. $io->write($messages);
  113. } else {
  114. $matchText = '';
  115. if ($input->getOption('match-constraint') !== '*') {
  116. $matchText = ' in versions '.($matchInvert ? 'not ':'').'matching ' . $input->getOption('match-constraint');
  117. }
  118. $io->writeError('<info>There is no installed package depending on "'.$needle.'"'.$matchText.'.</info>');
  119. }
  120. }
  121. }