Commit 52427210 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Permet d'utiliser n'importe quelle licence listée sur le SPDX.

cf. https://spdx.org/licenses/
No related merge requests found
Showing with 90 additions and 3 deletions
+90 -3
Copyright (c) 2018-2019 Irstea
Copyright (c) 2020 INRAE
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
......@@ -14,3 +15,4 @@ LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE A
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
......@@ -18,6 +18,7 @@ use Irstea\CS\HeaderComment\ChainTemplateProvider;
use Irstea\CS\HeaderComment\FormattedHeaderProvider;
use Irstea\CS\HeaderComment\HeaderProviderInterface;
use Irstea\CS\HeaderComment\LicenseTemplateProvider;
use Irstea\CS\HeaderComment\SPDXLicenseTemplateProvider;
use Irstea\CS\HeaderComment\TemplateFormatter;
use Irstea\CS\HeaderComment\UserDefinedTemplateProvider;
use PhpCsFixer\Config as PhpCsFixerConfig;
......@@ -172,6 +173,7 @@ final class Config extends PhpCsFixerConfig
new ChainTemplateProvider(
[
new UserDefinedTemplateProvider($fileLocator),
new SPDXLicenseTemplateProvider($composerPackage),
new LicenseTemplateProvider($composerPackage),
]
),
......
<?php declare(strict_types=1);
/*
* irstea/php-cs-fixer-config - Jeux de règles pour php-cs-fixer.
* Copyright (C) 2018-2020 IRSTEA
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
*/
namespace Irstea\CS\HeaderComment;
use Irstea\CS\Composer\ComposerPackageInterface;
/**
* Class SPDXLicenseTemplateProvider.
*/
final class SPDXLicenseTemplateProvider implements TemplateProviderInterface
{
/**
* @var ComposerPackageInterface
*/
private $composerPackage;
/**
* @var string
*/
private $urlTemplate;
/**
* SPDXLicenseTemplateProvider constructor.
*/
public function __construct(ComposerPackageInterface $composerPackage, string $urlTemplate = 'https://spdx.org/licenses/%s.json')
{
$this->composerPackage = $composerPackage;
$this->urlTemplate = $urlTemplate;
}
public function getTemplate(): ?string
{
$licenses = $this->composerPackage->getLicenses();
foreach ($licenses as $id) {
$template = $this->fetchTemplate($id);
if ($template) {
return $this->convertTemplate($template);
}
}
return null;
}
private function fetchTemplate(string $spdxId): ?string
{
$licenseJson = file_get_contents(sprintf($this->urlTemplate, $spdxId));
if ($licenseJson === false) {
return null;
}
$licenseInfo = json_decode($licenseJson, true);
if ($licenseInfo === false || !isset($licenseInfo['standardLicenseHeader'])) {
return null;
}
return $licenseInfo['standardLicenseHeader'];
}
private function convertTemplate(string $template): string
{
return preg_replace(
'/<<var;name=("?)(.+?)\1;original=.+?;match=.+?>>/',
'%%\2%%',
preg_replace('/<<(begin|end)Optional>>/', '', $template)
);
}
}
......@@ -43,11 +43,18 @@ final class TemplateFormatter implements TemplateFormatterInterface
public function format(string $template): string
{
$variables = [
'%package%' => $this->composerPackage->getName(),
'%description%' => $this->composerPackage->getDescription(),
'%yearRange%' => $this->gitRepository->getYearRange(),
'%package%' => $this->composerPackage->getName(),
'%description%' => $this->composerPackage->getDescription(),
'%yearRange%' => $this->gitRepository->getYearRange(),
'%copyrightHolder%' => 'INRAE',
];
$variables['%copyright%'] = str_replace(
array_keys($variables),
array_values($variables),
'Copyright (c) %yearRange% %copyrightHolder'
);
return str_replace(
array_keys($variables),
array_values($variables),
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment