SuggestsCommand.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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('Shows 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(
  31. <<<EOT
  32. The <info>%command.name%</info> command shows a sorted list of suggested packages.
  33. Enabling <info>-v</info> implies <info>--by-package --by-suggestion</info>, showing both lists.
  34. Read more at https://getcomposer.org/doc/03-cli.md#suggests
  35. EOT
  36. )
  37. ;
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output)
  40. {
  41. $lock = $this->getComposer()->getLocker()->getLockData();
  42. if (empty($lock)) {
  43. throw new \RuntimeException('Lockfile seems to be empty?');
  44. }
  45. $packages = $lock['packages'];
  46. if (!$input->getOption('no-dev')) {
  47. $packages += $lock['packages-dev'];
  48. }
  49. $filter = $input->getArgument('packages');
  50. // First assemble lookup list of packages that are installed, replaced or provided
  51. $installed = array();
  52. foreach ($packages as $package) {
  53. $installed[] = $package['name'];
  54. if (!empty($package['provide'])) {
  55. $installed = array_merge($installed, array_keys($package['provide']));
  56. }
  57. if (!empty($package['replace'])) {
  58. $installed = array_merge($installed, array_keys($package['replace']));
  59. }
  60. }
  61. // Undub and sort the install list into a sorted lookup array
  62. $installed = array_flip($installed);
  63. ksort($installed);
  64. // Init platform repo
  65. $platform = new PlatformRepository(array(), $this->getComposer()->getConfig()->get('platform') ?: array());
  66. // Next gather all suggestions that are not in that list
  67. $suggesters = array();
  68. $suggested = array();
  69. foreach ($packages as $package) {
  70. $packageName = $package['name'];
  71. if ((!empty($filter) && !in_array($packageName, $filter)) || empty($package['suggest'])) {
  72. continue;
  73. }
  74. foreach ($package['suggest'] as $suggestion => $reason) {
  75. if (false === strpos('/', $suggestion) && null !== $platform->findPackage($suggestion, '*')) {
  76. continue;
  77. }
  78. if (!isset($installed[$suggestion])) {
  79. $suggesters[$packageName][$suggestion] = $reason;
  80. $suggested[$suggestion][$packageName] = $reason;
  81. }
  82. }
  83. }
  84. ksort($suggesters);
  85. ksort($suggested);
  86. // Determine output mode
  87. $mode = 0;
  88. $io = $this->getIO();
  89. if ($input->getOption('by-package') || $io->isVerbose()) {
  90. $mode |= 1;
  91. }
  92. if ($input->getOption('by-suggestion')) {
  93. $mode |= 2;
  94. }
  95. // Simple mode
  96. if ($mode === 0) {
  97. foreach (array_keys($suggested) as $suggestion) {
  98. $io->write(sprintf('<info>%s</info>', $suggestion));
  99. }
  100. return 0;
  101. }
  102. // Grouped by package
  103. if ($mode & 1) {
  104. foreach ($suggesters as $suggester => $suggestions) {
  105. $io->write(sprintf('<comment>%s</comment> suggests:', $suggester));
  106. foreach ($suggestions as $suggestion => $reason) {
  107. $io->write(sprintf(' - <info>%s</info>: %s', $suggestion, $reason ?: '*'));
  108. }
  109. $io->write('');
  110. }
  111. }
  112. // Grouped by suggestion
  113. if ($mode & 2) {
  114. // Improve readability in full mode
  115. if ($mode & 1) {
  116. $io->write(str_repeat('-', 78));
  117. }
  118. foreach ($suggested as $suggestion => $suggesters) {
  119. $io->write(sprintf('<comment>%s</comment> is suggested by:', $suggestion));
  120. foreach ($suggesters as $suggester => $reason) {
  121. $io->write(sprintf(' - <info>%s</info>: %s', $suggester, $reason ?: '*'));
  122. }
  123. $io->write('');
  124. }
  125. }
  126. return 0;
  127. }
  128. }