SuggestsCommand.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class SuggestsCommand extends Command
  17. {
  18. protected function configure()
  19. {
  20. $this
  21. ->setName('suggests')
  22. ->setDescription('Show package suggestions')
  23. ->setDefinition(array(
  24. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'),
  25. new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.'),
  26. ))
  27. ->setHelp(<<<EOT
  28. The <info>%command.name%</info> command shows suggested packages.
  29. With <info>-v</info> you also see which package suggested it and why.
  30. EOT
  31. )
  32. ;
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $lock = $this->getComposer()->getLocker()->getLockData();
  37. if (empty($lock)) {
  38. throw new \RuntimeException('Lockfile seems to be empty?');
  39. }
  40. $packages = $lock['packages'];
  41. if (!$input->getOption('no-dev')) {
  42. $packages += $lock['packages-dev'];
  43. }
  44. $filter = $input->getArgument('packages');
  45. foreach ($packages as $package) {
  46. if (empty($package['suggest'])) {
  47. continue;
  48. }
  49. if (!empty($filter) && !in_array($package['name'], $filter)) {
  50. continue;
  51. }
  52. $this->printSuggestions($packages, $package['name'], $package['suggest']);
  53. }
  54. }
  55. protected function printSuggestions($installed, $source, $suggestions)
  56. {
  57. foreach ($suggestions as $suggestion => $reason) {
  58. foreach ($installed as $package) {
  59. if ($package['name'] === $suggestion) {
  60. continue 2;
  61. }
  62. }
  63. if (empty($reason)) {
  64. $reason = '*';
  65. }
  66. $this->printSuggestion($source, $suggestion, $reason);
  67. }
  68. }
  69. protected function printSuggestion($package, $suggestion, $reason)
  70. {
  71. $io = $this->getIO();
  72. if ($io->isVerbose()) {
  73. $io->write(sprintf('<comment>%s</comment> suggests <info>%s</info>: %s', $package, $suggestion, $reason));
  74. } else {
  75. $io->write(sprintf('<info>%s</info>', $suggestion));
  76. }
  77. }
  78. }