From d6589ac15189cfda7225c950989ece227bc78703 Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Thu, 28 Jan 2016 23:01:04 +0100 Subject: [PATCH 1/4] Rewrote SuggestsCommand --- src/Composer/Command/SuggestsCommand.php | 101 ++++++++++++++++------- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/src/Composer/Command/SuggestsCommand.php b/src/Composer/Command/SuggestsCommand.php index d59ef1dc6..93aae86a0 100644 --- a/src/Composer/Command/SuggestsCommand.php +++ b/src/Composer/Command/SuggestsCommand.php @@ -25,14 +25,16 @@ class SuggestsCommand extends Command ->setName('suggests') ->setDescription('Show package suggestions') ->setDefinition(array( + new InputOption('by-package', null, InputOption::VALUE_NONE, 'Groups output by suggesting package'), + new InputOption('by-suggestion', null, InputOption::VALUE_NONE, 'Groups output by suggested package'), new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.'), )) ->setHelp(<<%command.name% command shows suggested packages. +The %command.name% command shows a sorted list of suggested packages. -With -v you also see which package suggested it and why. +Enabling -v implies --by-package --by-suggestion, showing both lists. EOT ) @@ -55,44 +57,85 @@ EOT $filter = $input->getArgument('packages'); - foreach ($packages as $package) { - if (empty($package['suggest'])) { - continue; + // First assemble list of packages that are installed, replaced or provided + $installed = array(); + foreach($packages as $package) { + $installed[] = $package['name']; + + if (!empty($package['provide'])) { + $installed = array_merge($installed, array_keys($package['provide'])); } - if (!empty($filter) && !in_array($package['name'], $filter)) { - continue; + if (!empty($package['replace'])) { + $installed = array_merge($installed, array_keys($package['replace'])); } - - $this->printSuggestions($packages, $package['name'], $package['suggest']); } - } + sort($installed); + $installed = array_unique($installed); - protected function printSuggestions($installed, $source, $suggestions) - { - foreach ($suggestions as $suggestion => $reason) { - foreach ($installed as $package) { - if ($package['name'] === $suggestion) { - continue 2; + // Next gather all suggestions that are not in that list + $suggesters = array(); + $suggested = array(); + foreach ($packages as $package) { + if ((empty($filter) || in_array($package['name'], $filter)) && !empty($package['suggest'])) { + foreach ($package['suggest'] as $suggestion => $reason) { + if (!in_array($suggestion, $installed)) { + $suggesters[$package['name']][$suggestion] = $reason; + $suggested[$suggestion][$package['name']] = $reason; + } } } - - if (empty($reason)) { - $reason = '*'; - } - - $this->printSuggestion($source, $suggestion, $reason); } - } + ksort($suggesters); + ksort($suggested); - protected function printSuggestion($package, $suggestion, $reason) - { + // Determine output mode + $mode = 0; $io = $this->getIO(); - + if ($input->getOption('by-package')) { + $mode |= 1; + } + if ($input->getOption('by-suggestion')) { + $mode |= 2; + } if ($io->isVerbose()) { - $io->write(sprintf('%s suggests %s: %s', $package, $suggestion, $reason)); - } else { - $io->write(sprintf('%s', $suggestion)); + $mode = ~0; + } + + // Simple mode + if ($mode == 0) { + foreach (array_keys($suggested) as $suggestion) { + $io->write(sprintf('%s', $suggestion)); + } + return; + } + + // Grouped by package + if ($mode & 1) { + foreach ($suggesters as $suggester => $suggestions) { + $io->write(sprintf('%s suggests:', $suggester)); + + foreach ($suggestions as $suggestion => $reason) { + $io->write(sprintf(' - %s: %s', $suggestion, $reason ?: '*')); + } + $io->write(''); + } + } + + // Grouped by suggestion + if ($mode & 2) { + // Improve readability in full mode + if ($mode & 1) { + $io->write(str_repeat('-', 78)); + } + foreach ($suggested as $suggestion => $suggesters) { + $io->write(sprintf('%s is suggested by:', $suggestion)); + + foreach ($suggesters as $suggester => $reason) { + $io->write(sprintf(' - %s: %s', $suggester, $reason ?: '*')); + } + $io->write(''); + } } } } From 1b97d07bcad6e44c52fb9d7bde98955d5c51659d Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Fri, 29 Jan 2016 10:12:32 +0100 Subject: [PATCH 2/4] Processed PR feedback --- src/Composer/Command/SuggestsCommand.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/Composer/Command/SuggestsCommand.php b/src/Composer/Command/SuggestsCommand.php index 93aae86a0..35d5e01a3 100644 --- a/src/Composer/Command/SuggestsCommand.php +++ b/src/Composer/Command/SuggestsCommand.php @@ -57,7 +57,7 @@ EOT $filter = $input->getArgument('packages'); - // First assemble list of packages that are installed, replaced or provided + // First assemble lookup list of packages that are installed, replaced or provided $installed = array(); foreach($packages as $package) { $installed[] = $package['name']; @@ -70,19 +70,23 @@ EOT $installed = array_merge($installed, array_keys($package['replace'])); } } - sort($installed); - $installed = array_unique($installed); + + // Undub and sort the install list into a sorted lookup array + $installed = array_flip($installed); + ksort($installed); // Next gather all suggestions that are not in that list $suggesters = array(); $suggested = array(); foreach ($packages as $package) { - if ((empty($filter) || in_array($package['name'], $filter)) && !empty($package['suggest'])) { - foreach ($package['suggest'] as $suggestion => $reason) { - if (!in_array($suggestion, $installed)) { - $suggesters[$package['name']][$suggestion] = $reason; - $suggested[$suggestion][$package['name']] = $reason; - } + $packageName = $package['name']; + if ((!empty($filter) && !in_array($packageName, $filter)) || empty($package['suggest'])) { + continue; + } + foreach ($package['suggest'] as $suggestion => $reason) { + if (!isset($installed[$suggestion])) { + $suggesters[$packageName][$suggestion] = $reason; + $suggested[$suggestion][$packageName] = $reason; } } } @@ -103,7 +107,7 @@ EOT } // Simple mode - if ($mode == 0) { + if ($mode === 0) { foreach (array_keys($suggested) as $suggestion) { $io->write(sprintf('%s', $suggestion)); } From b20cc22ebb1d9f8ed467f0ea353391be7442ad3d Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Wed, 3 Feb 2016 00:14:16 +0100 Subject: [PATCH 3/4] Improved output of SuggestsCommand by correctly filtering installed platform requirements if suggested. --- src/Composer/Command/SuggestsCommand.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Composer/Command/SuggestsCommand.php b/src/Composer/Command/SuggestsCommand.php index 35d5e01a3..4ccb44853 100644 --- a/src/Composer/Command/SuggestsCommand.php +++ b/src/Composer/Command/SuggestsCommand.php @@ -12,6 +12,7 @@ namespace Composer\Command; +use Composer\Repository\PlatformRepository; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -75,6 +76,9 @@ EOT $installed = array_flip($installed); ksort($installed); + // Init platform repo + $platform = new PlatformRepository(array(), $this->getComposer()->getConfig()->get('platform') ?: array()); + // Next gather all suggestions that are not in that list $suggesters = array(); $suggested = array(); @@ -84,6 +88,8 @@ EOT continue; } foreach ($package['suggest'] as $suggestion => $reason) { + if (false === strpos('/', $suggestion) && !is_null($platform->findPackage($suggestion, '*'))) + continue; if (!isset($installed[$suggestion])) { $suggesters[$packageName][$suggestion] = $reason; $suggested[$suggestion][$packageName] = $reason; From 575e3fd3d8d9a3793c2a18f42b55307a390506ed Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Sun, 14 Feb 2016 21:31:15 +0100 Subject: [PATCH 4/4] Updated docs for new suggest parameters. --- doc/03-cli.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/03-cli.md b/doc/03-cli.md index b35d8f805..e2f117b0f 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -319,8 +319,13 @@ Lists all packages suggested by currently installed set of packages. You can optionally pass one or multiple package names in the format of `vendor/package` to limit output to suggestions made by those packages only. +Use the `--by-package` or `--by-suggestion` flags to group the output by +the package offering the suggestions or the suggested packages respectively. + ### Options +* **--by-package:** Groups output by suggesting package. +* **--by-suggestion:** Groups output by suggested package. * **--no-dev:** Excludes suggestions from `require-dev` packages. * **--verbose (-v):** Increased verbosity adds suggesting package name and reason for suggestion.