SuggestsCommand.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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\Repository\PlatformRepository;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. class SuggestsCommand extends BaseCommand
  18. {
  19. protected function configure()
  20. {
  21. $this
  22. ->setName('suggests')
  23. ->setDescription('Show package suggestions')
  24. ->setDefinition(array(
  25. new InputOption('by-package', null, InputOption::VALUE_NONE, 'Groups output by suggesting package'),
  26. new InputOption('by-suggestion', null, InputOption::VALUE_NONE, 'Groups output by suggested package'),
  27. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'),
  28. new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.'),
  29. ))
  30. ->setHelp(<<<EOT
  31. The <info>%command.name%</info> command shows a sorted list of suggested packages.
  32. Enabling <info>-v</info> implies <info>--by-package --by-suggestion</info>, showing both lists.
  33. EOT
  34. )
  35. ;
  36. }
  37. protected function execute(InputInterface $input, OutputInterface $output)
  38. {
  39. $lock = $this->getComposer()->getLocker()->getLockData();
  40. if (empty($lock)) {
  41. throw new \RuntimeException('Lockfile seems to be empty?');
  42. }
  43. $packages = $lock['packages'];
  44. if (!$input->getOption('no-dev')) {
  45. $packages += $lock['packages-dev'];
  46. }
  47. $filter = $input->getArgument('packages');
  48. // First assemble lookup list of packages that are installed, replaced or provided
  49. $installed = array();
  50. foreach ($packages as $package) {
  51. $installed[] = $package['name'];
  52. if (!empty($package['provide'])) {
  53. $installed = array_merge($installed, array_keys($package['provide']));
  54. }
  55. if (!empty($package['replace'])) {
  56. $installed = array_merge($installed, array_keys($package['replace']));
  57. }
  58. }
  59. // Undub and sort the install list into a sorted lookup array
  60. $installed = array_flip($installed);
  61. ksort($installed);
  62. // Init platform repo
  63. $platform = new PlatformRepository(array(), $this->getComposer()->getConfig()->get('platform') ?: array());
  64. // Next gather all suggestions that are not in that list
  65. $suggesters = array();
  66. $suggested = array();
  67. foreach ($packages as $package) {
  68. $packageName = $package['name'];
  69. if ((!empty($filter) && !in_array($packageName, $filter)) || empty($package['suggest'])) {
  70. continue;
  71. }
  72. foreach ($package['suggest'] as $suggestion => $reason) {
  73. if (false === strpos('/', $suggestion) && !is_null($platform->findPackage($suggestion, '*'))) {
  74. continue;
  75. }
  76. if (!isset($installed[$suggestion])) {
  77. $suggesters[$packageName][$suggestion] = $reason;
  78. $suggested[$suggestion][$packageName] = $reason;
  79. }
  80. }
  81. }
  82. ksort($suggesters);
  83. ksort($suggested);
  84. // Determine output mode
  85. $mode = 0;
  86. $io = $this->getIO();
  87. if ($input->getOption('by-package') || $io->isVerbose()) {
  88. $mode |= 1;
  89. }
  90. if ($input->getOption('by-suggestion')) {
  91. $mode |= 2;
  92. }
  93. // Simple mode
  94. if ($mode === 0) {
  95. foreach (array_keys($suggested) as $suggestion) {
  96. $io->write(sprintf('<info>%s</info>', $suggestion));
  97. }
  98. return;
  99. }
  100. // Grouped by package
  101. if ($mode & 1) {
  102. foreach ($suggesters as $suggester => $suggestions) {
  103. $io->write(sprintf('<comment>%s</comment> suggests:', $suggester));
  104. foreach ($suggestions as $suggestion => $reason) {
  105. $io->write(sprintf(' - <info>%s</info>: %s', $suggestion, $reason ?: '*'));
  106. }
  107. $io->write('');
  108. }
  109. }
  110. // Grouped by suggestion
  111. if ($mode & 2) {
  112. // Improve readability in full mode
  113. if ($mode & 1) {
  114. $io->write(str_repeat('-', 78));
  115. }
  116. foreach ($suggested as $suggestion => $suggesters) {
  117. $io->write(sprintf('<comment>%s</comment> is suggested by:', $suggestion));
  118. foreach ($suggesters as $suggester => $reason) {
  119. $io->write(sprintf(' - <info>%s</info>: %s', $suggester, $reason ?: '*'));
  120. }
  121. $io->write('');
  122. }
  123. }
  124. }
  125. }