2014-03-17 11:26:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Command;
|
|
|
|
|
2016-02-02 23:14:16 +00:00
|
|
|
use Composer\Repository\PlatformRepository;
|
2015-06-23 19:29:48 +00:00
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2014-03-17 11:26:19 +00:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
2016-02-19 22:56:46 +00:00
|
|
|
class SuggestsCommand extends BaseCommand
|
2014-03-17 11:26:19 +00:00
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('suggests')
|
2017-04-06 04:50:24 +00:00
|
|
|
->setDescription('Shows package suggestions.')
|
2014-03-17 11:26:19 +00:00
|
|
|
->setDefinition(array(
|
2016-01-28 22:01:04 +00:00
|
|
|
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'),
|
2015-06-23 19:04:06 +00:00
|
|
|
new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Exclude suggestions from require-dev packages'),
|
2015-06-23 19:29:48 +00:00
|
|
|
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that you want to list suggestions from.'),
|
2014-03-17 11:26:19 +00:00
|
|
|
))
|
2018-07-24 12:32:52 +00:00
|
|
|
->setHelp(
|
|
|
|
<<<EOT
|
2014-03-17 11:26:19 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
The <info>%command.name%</info> command shows a sorted list of suggested packages.
|
2014-03-17 11:26:19 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
Enabling <info>-v</info> implies <info>--by-package --by-suggestion</info>, showing both lists.
|
2015-07-04 11:02:57 +00:00
|
|
|
|
2019-03-04 11:55:38 +00:00
|
|
|
Read more at https://getcomposer.org/doc/03-cli.md#suggests
|
2014-03-17 11:26:19 +00:00
|
|
|
EOT
|
2015-06-23 19:04:06 +00:00
|
|
|
)
|
|
|
|
;
|
2014-03-17 11:26:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2015-06-23 19:04:06 +00:00
|
|
|
$lock = $this->getComposer()->getLocker()->getLockData();
|
|
|
|
|
|
|
|
if (empty($lock)) {
|
|
|
|
throw new \RuntimeException('Lockfile seems to be empty?');
|
|
|
|
}
|
|
|
|
|
2015-06-24 09:06:21 +00:00
|
|
|
$packages = $lock['packages'];
|
2015-06-23 19:04:06 +00:00
|
|
|
|
|
|
|
if (!$input->getOption('no-dev')) {
|
2015-06-24 09:06:21 +00:00
|
|
|
$packages += $lock['packages-dev'];
|
2014-03-17 11:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 09:06:21 +00:00
|
|
|
$filter = $input->getArgument('packages');
|
|
|
|
|
2016-01-29 09:12:32 +00:00
|
|
|
// First assemble lookup list of packages that are installed, replaced or provided
|
2016-01-28 22:01:04 +00:00
|
|
|
$installed = array();
|
2016-02-29 15:04:05 +00:00
|
|
|
foreach ($packages as $package) {
|
2016-01-28 22:01:04 +00:00
|
|
|
$installed[] = $package['name'];
|
2015-06-23 19:29:48 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
if (!empty($package['provide'])) {
|
|
|
|
$installed = array_merge($installed, array_keys($package['provide']));
|
2015-06-23 20:03:35 +00:00
|
|
|
}
|
2015-06-24 09:06:21 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
if (!empty($package['replace'])) {
|
|
|
|
$installed = array_merge($installed, array_keys($package['replace']));
|
|
|
|
}
|
2015-06-23 20:03:35 +00:00
|
|
|
}
|
|
|
|
|
2016-01-29 09:12:32 +00:00
|
|
|
// Undub and sort the install list into a sorted lookup array
|
|
|
|
$installed = array_flip($installed);
|
|
|
|
ksort($installed);
|
2015-06-23 20:03:35 +00:00
|
|
|
|
2016-02-02 23:14:16 +00:00
|
|
|
// Init platform repo
|
|
|
|
$platform = new PlatformRepository(array(), $this->getComposer()->getConfig()->get('platform') ?: array());
|
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
// Next gather all suggestions that are not in that list
|
|
|
|
$suggesters = array();
|
|
|
|
$suggested = array();
|
|
|
|
foreach ($packages as $package) {
|
2016-01-29 09:12:32 +00:00
|
|
|
$packageName = $package['name'];
|
|
|
|
if ((!empty($filter) && !in_array($packageName, $filter)) || empty($package['suggest'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($package['suggest'] as $suggestion => $reason) {
|
2017-09-11 17:40:43 +00:00
|
|
|
if (false === strpos('/', $suggestion) && null !== $platform->findPackage($suggestion, '*')) {
|
2016-02-02 23:14:16 +00:00
|
|
|
continue;
|
2016-02-29 15:04:05 +00:00
|
|
|
}
|
2016-01-29 09:12:32 +00:00
|
|
|
if (!isset($installed[$suggestion])) {
|
|
|
|
$suggesters[$packageName][$suggestion] = $reason;
|
|
|
|
$suggested[$suggestion][$packageName] = $reason;
|
2015-06-24 09:06:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 22:01:04 +00:00
|
|
|
}
|
|
|
|
ksort($suggesters);
|
|
|
|
ksort($suggested);
|
|
|
|
|
|
|
|
// Determine output mode
|
|
|
|
$mode = 0;
|
|
|
|
$io = $this->getIO();
|
2016-02-25 18:49:23 +00:00
|
|
|
if ($input->getOption('by-package') || $io->isVerbose()) {
|
2016-01-28 22:01:04 +00:00
|
|
|
$mode |= 1;
|
|
|
|
}
|
|
|
|
if ($input->getOption('by-suggestion')) {
|
|
|
|
$mode |= 2;
|
|
|
|
}
|
2015-06-23 20:03:35 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
// Simple mode
|
2016-01-29 09:12:32 +00:00
|
|
|
if ($mode === 0) {
|
2016-01-28 22:01:04 +00:00
|
|
|
foreach (array_keys($suggested) as $suggestion) {
|
|
|
|
$io->write(sprintf('<info>%s</info>', $suggestion));
|
2015-06-23 20:03:35 +00:00
|
|
|
}
|
2016-02-29 15:04:05 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grouped by package
|
|
|
|
if ($mode & 1) {
|
|
|
|
foreach ($suggesters as $suggester => $suggestions) {
|
|
|
|
$io->write(sprintf('<comment>%s</comment> suggests:', $suggester));
|
2015-06-23 19:04:06 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
foreach ($suggestions as $suggestion => $reason) {
|
|
|
|
$io->write(sprintf(' - <info>%s</info>: %s', $suggestion, $reason ?: '*'));
|
|
|
|
}
|
|
|
|
$io->write('');
|
|
|
|
}
|
2015-06-24 09:06:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
// 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('<comment>%s</comment> is suggested by:', $suggestion));
|
2015-06-24 09:06:21 +00:00
|
|
|
|
2016-01-28 22:01:04 +00:00
|
|
|
foreach ($suggesters as $suggester => $reason) {
|
|
|
|
$io->write(sprintf(' - <info>%s</info>: %s', $suggester, $reason ?: '*'));
|
|
|
|
}
|
|
|
|
$io->write('');
|
|
|
|
}
|
2014-03-17 11:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|