Browse Source

Merge pull request #1052 from naderman/funding-composer

Display funding information for packages and expose funding info to Composer
Jordi Boggiano 5 years ago
parent
commit
447db8956e

+ 38 - 0
src/Packagist/WebBundle/Entity/Version.php

@@ -179,6 +179,11 @@ class Version
      */
     private $support;
 
+    /**
+     * @ORM\Column(type="json", nullable=true)
+     */
+    private $funding;
+
     /**
      * @ORM\Column(name="authors", type="json", nullable=true)
      */
@@ -265,6 +270,9 @@ class Version
         if ($serializeForApi && $this->getSupport()) {
             $data['support'] = $this->getSupport();
         }
+        if ($this->getFunding()) {
+            $data['funding'] = $this->getFunding();
+        }
         if ($this->getReleasedAt()) {
             $data['time'] = $this->getReleasedAt()->format('Y-m-d\TH:i:sP');
         }
@@ -616,6 +624,36 @@ class Version
         return json_decode($this->support, true);
     }
 
+    /**
+     * Set Funding
+     *
+     * @param array $funding
+     */
+    public function setFunding($funding)
+    {
+        // sort records when storing so to help the V2 metadata compression algo
+        if ($funding) {
+            usort($funding, function ($a, $b) {
+                $keyA = ($a['type'] ?? '') . ($a['url'] ?? '');
+                $keyB = ($b['type'] ?? '') . ($b['url'] ?? '');
+                
+                return $keyA <=> $keyB;
+            });
+        }
+
+        $this->funding = $funding;
+    }
+
+    /**
+     * Get funding
+     *
+     * @return array|null
+     */
+    public function getFunding()
+    {
+        return $this->funding;
+    }
+
     /**
      * Set createdAt
      *

+ 1 - 0
src/Packagist/WebBundle/Package/Updater.php

@@ -360,6 +360,7 @@ class Updater
         $version->setBinaries($data->getBinaries());
         $version->setIncludePaths($data->getIncludePaths());
         $version->setSupport($data->getSupport());
+        $version->setFunding($data->getFunding());
 
         if ($data->getKeywords()) {
             $keywords = array();

+ 10 - 0
src/Packagist/WebBundle/Resources/views/package/view_package.html.twig

@@ -171,6 +171,16 @@
                             {% endif %}
                         </div>
 
+                        <div class="facts col-xs-12 col-sm-6 col-md-12">
+                            {% if version and version.funding is defined and version.funding|length > 0 %}
+                                <p>
+                                    Fund package maintenance!
+                                    {% for fundingOption in version.funding %}
+                                        <br>&raquo; <a rel="nofollow noopener external noindex" href="{{ fundingOption.url }}">{{ fundingOption.type == 'custom' ? fundingOption.url : fundingOption.type }}</a>
+                                    {% endfor %}
+                            {% endif %}
+                        </div>
+
                         <div class="facts col-xs-12 col-sm-6 col-md-12">
                             <p>
                                 <span>