1
0
Fork 0
composer/src/Composer/Command/SuggestsCommand.php

158 lines
5.3 KiB
PHP
Raw Normal View History

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;
use Composer\Repository\PlatformRepository;
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;
class SuggestsCommand extends BaseCommand
2014-03-17 11:26:19 +00:00
{
protected function configure()
{
$this
->setName('suggests')
->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'),
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
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
}
/**
* {@inheritDoc}
*/
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?');
}
$packages = $lock['packages'];
2015-06-23 19:04:06 +00:00
if (!$input->getOption('no-dev')) {
$packages += $lock['packages-dev'];
2014-03-17 11:26:19 +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'];
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
}
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
// 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) {
if (preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $suggestion) && null !== $platform->findPackage($suggestion, '*')) {
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;
}
}
2016-01-28 22:01:04 +00:00
}
ksort($suggesters);
ksort($suggested);
// Determine output mode
$mode = 0;
$io = $this->getIO();
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
return 0;
2016-01-28 22:01:04 +00:00
}
// 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('');
}
}
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));
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
}
return 0;
2014-03-17 11:26:19 +00:00
}
}