Skip to content

Commit

Permalink
Add funding field to composer.json
Browse files Browse the repository at this point in the history
You can specify a list of funding options each with a type and URL. The
type is used to specify the kind of funding or the platform through
which funding is possible.
  • Loading branch information
naderman committed Nov 28, 2019
1 parent 89f6b2c commit 2e695d3
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 0 deletions.
32 changes: 32 additions & 0 deletions doc/04-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,38 @@ An example:

Optional.

### funding

A list of URLs to provide funding to the package authors for maintenance and
development of new functionality.

Each entry consists of the following

* **type:** The type of funding or the platform through which funding can be provided, e.g. patreon, opencollective, tidelift or github.
* **url:** URL to a website with details and a way to fund the package.

An example:

```json
{
"funding": [
{
"type": "patreon",
"url": "https://www.patreon.com/phpdoctrine"
},
{
"type": "tidelift",
"url": "https://tidelift.com/subscription/pkg/packagist-doctrine_doctrine-bundle"
},
{
"type": "other",
"url": "https://www.doctrine-project.org/sponsorship.html"
}
}
```

Optional.

### Package links

All of the following take an object which maps package names to
Expand Down
18 changes: 18 additions & 0 deletions res/composer-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,24 @@
}
}
},
"funding": {
"type": "array",
"description": "A list of options to fund the development and maintenance of the package.",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string",
"description": "Type of funding or platform through which funding is possible."
},
"url": {
"type": "string",
"description": "URL to a website with details on funding and a way to fund the package.",
"format": "uri"
}
}
}
},
"non-feature-branches": {
"type": ["array"],
"description": "A set of string or regex patterns for non-numeric branch names that will not be handled as feature branches.",
Expand Down
5 changes: 5 additions & 0 deletions src/Composer/Package/AliasPackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ public function getSupport()
return $this->aliasOf->getSupport();
}

public function getFunding()
{
return $this->aliasOf->getFunding();
}

public function getNotificationUrl()
{
return $this->aliasOf->getNotificationUrl();
Expand Down
19 changes: 19 additions & 0 deletions src/Composer/Package/CompletePackage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CompletePackage extends Package implements CompletePackageInterface
protected $homepage;
protected $scripts = array();
protected $support = array();
protected $funding = array();
protected $abandoned = false;

/**
Expand Down Expand Up @@ -171,6 +172,24 @@ public function getSupport()
return $this->support;
}

/**
* Set the funding
*
* @param array $funding
*/
public function setFunding(array $funding)
{
$this->funding = $funding;
}

/**
* {@inheritDoc}
*/
public function getFunding()
{
return $this->funding;
}

/**
* @return bool
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Composer/Package/CompletePackageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ public function getAuthors();
*/
public function getSupport();

/**
* Returns an array of funding options for the package
*
* Each item will contain type and url keys
*
* @return array
*/
public function getFunding();

/**
* Returns if the package is abandoned or not
*
Expand Down
1 change: 1 addition & 0 deletions src/Composer/Package/Dumper/ArrayDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function dump(PackageInterface $package)
'keywords',
'repositories',
'support',
'funding',
);

$data = $this->dumpValues($package, $keys, $data);
Expand Down
4 changes: 4 additions & 0 deletions src/Composer/Package/Loader/ArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ public function load(array $config, $class = 'Composer\Package\CompletePackage')
$package->setSupport($config['support']);
}

if (!empty($config['funding']) && is_array($config['funding'])) {
$package->setFunding($config['funding']);
}

if (isset($config['abandoned'])) {
$package->setAbandoned($config['abandoned']);
}
Expand Down
26 changes: 26 additions & 0 deletions src/Composer/Package/Loader/ValidatingArrayLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,32 @@ public function load(array $config, $class = 'Composer\Package\CompletePackage')
}
}

if ($this->validateArray('funding') && !empty($this->config['funding'])) {
foreach ($this->config['funding'] as $key => $fundingOption) {
if (!is_array($fundingOption)) {
$this->errors[] = 'funding.'.$key.' : should be an array, '.gettype($fundingOption).' given';
unset($this->config['funding'][$key]);
continue;
}
foreach (array('type', 'url') as $fundingData) {
if (isset($fundingOption[$fundingData]) && !is_string($fundingOption[$fundingData])) {
$this->errors[] = 'funding.'.$key.'.'.$fundingData.' : invalid value, must be a string';
unset($this->config['funding'][$key][$fundingData]);
}
}
if (isset($fundingOption['url']) && !$this->filterUrl($fundingOption['url'])) {
$this->warnings[] = 'funding.'.$key.'.url : invalid value ('.$fundingOption['url'].'), must be an http/https URL';
unset($this->config['funding'][$key]['url']);
}
if (empty($this->config['funding'][$key])) {
unset($this->config['funding'][$key]);
}
}
if (empty($this->config['funding'])) {
unset($this->config['funding']);
}
}

$unboundConstraint = new Constraint('=', $this->versionParser->normalize('dev-master'));
$stableConstraint = new Constraint('=', '1.0.0');

Expand Down
6 changes: 6 additions & 0 deletions tests/Composer/Test/Json/Fixtures/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
"irc": "irc://irc.freenode.org/composer",
"issues": "https://github.com/composer/composer/issues"
},
"funding": [
{
"type": "service-subscription",
"url": "https://packagist.com"
}
],
"require": {
"php": ">=5.3.2",
"justinrainbow/json-schema": "~1.4",
Expand Down
4 changes: 4 additions & 0 deletions tests/Composer/Test/Package/Dumper/ArrayDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ public function getKeys()
'support',
array('foo' => 'bar'),
),
array(
'funding',
array('type' => 'foo', 'url' => 'https://example.com'),
),
array(
'require',
array(new Link('foo', 'foo/bar', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0'), new Link('bar', 'bar/baz', new Constraint('=', '1.0.0.0'), 'requires', '1.0.0')),
Expand Down
3 changes: 3 additions & 0 deletions tests/Composer/Test/Package/Loader/ArrayLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public function testParseDumpProvider()
'authors' => array(
array('name' => 'Bob', 'email' => 'bob@example.org', 'homepage' => 'example.org', 'role' => 'Developer'),
),
'funding' => array(
array('type' => 'example', 'url' => 'https://example.org/fund'),
),
'require' => array(
'foo/bar' => '1.0',
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public function successProvider()
'rss' => 'http://example.org/rss',
'chat' => 'http://example.org/chat',
),
'funding' => array(
array(
'type' => 'example',
'url' => 'https://example.org/fund'
),
array(
'url' => 'https://example.org/fund'
),
),
'require' => array(
'a/b' => '1.*',
'b/c' => '~2',
Expand Down

0 comments on commit 2e695d3

Please sign in to comment.