From 8da2811dc3392c88d99c4b3011ce613cda5e7f8f Mon Sep 17 00:00:00 2001 From: Jonas Drieghe Date: Tue, 16 Jun 2020 10:07:53 +0200 Subject: [PATCH] Add new summary format for licenses (#8973) * Add new summary format to render the number of dependencies for each used license * Array dereferencing wasn't available on php 5.3 * Add summary format to documentation --- doc/03-cli.md | 2 +- src/Composer/Command/LicensesCommand.php | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/03-cli.md b/doc/03-cli.md index 374ef952e..edfaaa9ed 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -785,7 +785,7 @@ Lists the name, version and license of every package installed. Use ### Options -* **--format:** Format of the output: text or json (default: "text") +* **--format:** Format of the output: text, json or summary (default: "text") * **--no-dev:** Remove dev dependencies from the output ## run-script diff --git a/src/Composer/Command/LicensesCommand.php b/src/Composer/Command/LicensesCommand.php index 85cb64a7f..c9a099f26 100644 --- a/src/Composer/Command/LicensesCommand.php +++ b/src/Composer/Command/LicensesCommand.php @@ -21,6 +21,7 @@ use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * @author BenoƮt Merlet @@ -111,6 +112,28 @@ EOT ))); break; + case 'summary': + $dependencies = array(); + foreach ($packages as $package) { + $license = $package->getLicense(); + $licenseName = $license[0]; + if (!isset($dependencies[$licenseName])) { + $dependencies[$licenseName] = 0; + } + $dependencies[$licenseName]++; + } + + $rows = array(); + foreach ($dependencies as $usedLicense => $numberOfDependencies) { + $rows[] = array($usedLicense, $numberOfDependencies); + } + + $symfonyIo = new SymfonyStyle($input, $output); + $symfonyIo->table( + array('License', 'Number of dependencies'), + $rows + ); + break; default: throw new \RuntimeException(sprintf('Unsupported format "%s". See help for supported formats.', $format)); }