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

Support for .docheader file.

Showing with 65 additions and 9 deletions
+65 -9
......@@ -19,12 +19,21 @@ final class Config extends PhpCsFixerConfig
*/
private $cache;
/**
* @var array|null
*/
private $composerConfig;
/**
* @var string
*/
private $cacheFile = '.php_cs.commit-cache';
/**
* @var string
*/
private $docHeaderfile = '.docheader';
/**
* Config constructor.
*/
......@@ -44,6 +53,16 @@ final class Config extends PhpCsFixerConfig
$this->cacheFile = $cacheFile;
}
/**
* Set docHeaderfile.
*
* @param string $docHeaderfile
*/
public function setDocHeaderfile($docHeaderfile)
{
$this->docHeaderfile = $docHeaderfile;
}
/**
* {@inheritdoc}
*/
......@@ -91,9 +110,28 @@ final class Config extends PhpCsFixerConfig
*/
private function headerComment()
{
$header = "Copyright (C) %yearRange% IRSTEA\nAll rights reserved.";
// TODO: load a template from a local file.
return str_replace('%yearRange%', $this->getYearRange(), $header);
$header = "Create and customize a file named {$this->docHeaderfile} at the root of the project to change this message.";
if (file_exists($this->docHeaderfile)) {
$header = trim(file_get_contents($this->docHeaderfile));
}
return str_replace(['%yearRange%', '%package%'], [$this->getYearRange(), $this->findPackageName()], $header);
}
/**
* @return string|null
*/
private function findPackageName()
{
return $this->memoize(
'package-name',
function () {
$config = $this->getComposerConfig();
return isset($config['name']) ? $config['name'] : null;
}
);
}
/**
......@@ -135,12 +173,10 @@ final class Config extends PhpCsFixerConfig
return $this->memoize(
'php-req',
function () {
if (file_exists('composer.json')) {
$data = json_decode(file_get_contents('composer.json'), true);
if (is_array($data) && isset($data['require']['php'])) {
if (preg_match('/(?:>=?|\^|~)\s*([57]\.\d)/', $data['require']['php'], $groups)) {
return (float)$groups[1];
}
$config = $this->getComposerConfig();
if (isset($config['require']['php'])) {
if (preg_match('/(?:>=?|\^|~)\s*([57]\.\d)/', $config['require']['php'], $groups)) {
return (float) $groups[1];
}
}
......@@ -149,6 +185,26 @@ final class Config extends PhpCsFixerConfig
);
}
/**
* @return array
*/
private function getComposerConfig()
{
if ($this->composerConfig !== null) {
return $this->composerConfig;
}
$this->composerConfig = [];
if (file_exists('composer.json')) {
$data = json_decode(file_get_contents('composer.json'), true);
if (is_array($data)) {
$this->composerConfig = $data;
}
}
return $this->composerConfig;
}
/**
* @param string $key
* @param callable $generator
......
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