From 77a477fb57ba82caf5ee4e0640fd6c1dcab89807 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 14 Feb 2020 14:08:26 +0100 Subject: [PATCH] Add FundCommand --- doc/03-cli.md | 5 ++ src/Composer/Command/FundCommand.php | 89 ++++++++++++++++++++++++++++ src/Composer/Console/Application.php | 1 + 3 files changed, 95 insertions(+) create mode 100644 src/Composer/Command/FundCommand.php diff --git a/doc/03-cli.md b/doc/03-cli.md index f7df60b92..29eeb398c 100644 --- a/doc/03-cli.md +++ b/doc/03-cli.md @@ -420,6 +420,11 @@ This implies `--by-package --by-suggestion`, showing both lists. * **--by-suggestion:** Groups output by suggested package. * **--no-dev:** Excludes suggestions from `require-dev` packages. +## fund + +Discover how to help fund the maintenance of your dependencies. This lists +all funding links from the installed dependencies. + ## depends (why) The `depends` command tells you which other packages depend on a certain diff --git a/src/Composer/Command/FundCommand.php b/src/Composer/Command/FundCommand.php new file mode 100644 index 000000000..3c80abc23 --- /dev/null +++ b/src/Composer/Command/FundCommand.php @@ -0,0 +1,89 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Command; + +use Composer\Package\CompletePackageInterface; +use Composer\Package\AliasPackage; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\OutputInterface; + +/** + * @author Nicolas Grekas + * @author Jordi Boggiano + */ +class FundCommand extends BaseCommand +{ + protected function configure() + { + $this->setName('fund') + ->setDescription('Discover how to help fund the maintenance of your dependencies.') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $composer = $this->getComposer(); + + $repo = $composer->getRepositoryManager()->getLocalRepository(); + $fundings = array(); + foreach ($repo->getPackages() as $package) { + if ($package instanceof AliasPackage) { + continue; + } + if ($package instanceof CompletePackageInterface && $funding = $package->getFunding()) { + foreach ($funding as $fundingOption) { + list($vendor, $packageName) = explode('/', $package->getPrettyName()); + $url = $fundingOption['url']; + if (!empty($fundingOption['type']) && $fundingOption['type'] === 'github' && preg_match('{^https://github.com/([^/]+)$}', $url, $match)) { + $url = 'https://github.com/sponsors/'.$match[1]; + } + $fundings[$vendor][$url][] = $packageName; + } + } + } + + ksort($fundings); + + $io = $this->getIO(); + + if ($fundings) { + $prev = null; + + $io->write('The following packages were found in your dependencies which publish funding information:'); + + foreach ($fundings as $vendor => $links) { + $io->write(''); + $io->write(sprintf("%s", $vendor)); + foreach ($links as $url => $packages) { + $line = sprintf(' %s', implode(', ', $packages)); + + if ($prev !== $line) { + $io->write($line); + $prev = $line; + } + + $io->write(sprintf(' %s', $url)); + } + } + + $io->write(""); + $io->write("Please consider following these links and sponsoring the work of package authors!"); + $io->write("Thank you!"); + } else { + $io->write("No funding links were found in your package dependencies. This doesn't mean they don't need your support!"); + } + + return 0; + } +} diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index b884de15e..74e270a96 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -436,6 +436,7 @@ class Application extends BaseApplication new Command\ExecCommand(), new Command\OutdatedCommand(), new Command\CheckPlatformReqsCommand(), + new Command\FundCommand(), )); if ('phar:' === substr(__FILE__, 0, 5)) {