1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Churn\Configuration; |
6
|
|
|
|
7
|
|
|
use Churn\Configuration\Validator\CachePath; |
8
|
|
|
use Churn\Configuration\Validator\CommitsSince; |
9
|
|
|
use Churn\Configuration\Validator\DirectoriesToScan; |
10
|
|
|
use Churn\Configuration\Validator\FileExtensions; |
11
|
|
|
use Churn\Configuration\Validator\FilesToIgnore; |
12
|
|
|
use Churn\Configuration\Validator\FilesToShow; |
13
|
|
|
use Churn\Configuration\Validator\Hooks; |
14
|
|
|
use Churn\Configuration\Validator\MaxScoreThreshold; |
15
|
|
|
use Churn\Configuration\Validator\MinScoreToShow; |
16
|
|
|
use Churn\Configuration\Validator\ParallelJobs; |
17
|
|
|
use Churn\Configuration\Validator\Vcs; |
18
|
|
|
use InvalidArgumentException; |
19
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
20
|
|
|
use Symfony\Component\Yaml\Yaml; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @internal |
24
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
25
|
|
|
*/ |
26
|
|
|
final class Loader |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @param string $confPath Path of the configuration file to load. |
30
|
|
|
* @param boolean $isDefaultValue Indicates whether $confPath contains the default value. |
31
|
|
|
* @throws InvalidArgumentException If the configuration file cannot be loaded. |
32
|
|
|
*/ |
33
|
|
|
public static function fromPath(string $confPath, bool $isDefaultValue): Config |
34
|
|
|
{ |
35
|
|
|
$originalConfPath = $confPath; |
36
|
|
|
$confPath = self::normalizePath($isDefaultValue ? '.' : $confPath); |
37
|
|
|
|
38
|
|
|
if (false !== $confPath && \is_readable($confPath)) { |
39
|
|
|
$config = new EditableConfig($confPath); |
40
|
|
|
$config->setUnrecognizedKeys(self::validate($config, self::loadYaml($confPath))); |
41
|
|
|
|
42
|
|
|
return $config; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($isDefaultValue) { |
46
|
|
|
return new ReadOnlyConfig(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new InvalidArgumentException('The configuration file can not be read at ' . $originalConfPath); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $confPath Path to normalize. |
54
|
|
|
* @return string|false |
55
|
|
|
*/ |
56
|
|
|
private static function normalizePath(string $confPath) |
57
|
|
|
{ |
58
|
|
|
if (!\is_dir($confPath)) { |
59
|
|
|
return \realpath($confPath); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$confPath = \rtrim($confPath, '/\\') . '/churn.yml'; |
63
|
|
|
$realConfPath = \realpath($confPath); |
64
|
|
|
|
65
|
|
|
if (false !== $realConfPath) { |
66
|
|
|
return $realConfPath; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$confPath .= '.dist'; |
70
|
|
|
|
71
|
|
|
return \realpath($confPath); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $confPath Path of the yaml file to load. |
76
|
|
|
* @return array<mixed> |
77
|
|
|
* @throws InvalidArgumentException If the configuration file is invalid. |
78
|
|
|
*/ |
79
|
|
|
private static function loadYaml(string $confPath): array |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
|
|
$content = Yaml::parse((string) \file_get_contents($confPath)) ?? []; |
83
|
|
|
} catch (ParseException $e) { |
84
|
|
|
$content = null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!\is_array($content)) { |
88
|
|
|
throw new InvalidArgumentException('The content of the configuration file is invalid'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $content; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param EditableConfig $config The configuration object. |
96
|
|
|
* @param array<mixed> $configuration The array containing the configuration values. |
97
|
|
|
* @return array<int|string> |
98
|
|
|
*/ |
99
|
|
|
private static function validate(EditableConfig $config, array $configuration): array |
100
|
|
|
{ |
101
|
|
|
$validators = [new CachePath(), new CommitsSince(), new DirectoriesToScan(), new FileExtensions(), |
102
|
|
|
new FilesToIgnore(), new FilesToShow(), new Hooks(), new MaxScoreThreshold(), new MinScoreToShow(), |
103
|
|
|
new ParallelJobs(), new Vcs()]; |
104
|
|
|
foreach ($validators as $validator) { |
105
|
|
|
$validator->validate($config, $configuration); |
106
|
|
|
unset($configuration[$validator->getKey()]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return \array_keys($configuration); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|