Browse Source

Add FundCommand

Jordi Boggiano 5 years ago
parent
commit
77a477fb57
3 changed files with 95 additions and 0 deletions
  1. 5 0
      doc/03-cli.md
  2. 89 0
      src/Composer/Command/FundCommand.php
  3. 1 0
      src/Composer/Console/Application.php

+ 5 - 0
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

+ 89 - 0
src/Composer/Command/FundCommand.php

@@ -0,0 +1,89 @@
+<?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\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 <p@tchwork.com>
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ */
+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("<comment>%s</comment>", $vendor));
+                foreach ($links as $url => $packages) {
+                    $line = sprintf('  <info>%s</info>', 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;
+    }
+}

+ 1 - 0
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)) {