1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BestIt\LicenseCheck\Configuration; |
6
|
|
|
|
7
|
|
|
use BestIt\LicenseCheck\Configuration\Exception\ConfigurationNotFoundException; |
8
|
|
|
use BestIt\LicenseCheck\Configuration\Exception\ConfigurationParseException; |
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Loader to create a configuration object by a configuration yaml file. |
14
|
|
|
* |
15
|
|
|
* @author best it AG <[email protected]> |
16
|
|
|
* @package BestIt\LicenseCheck\Configuration |
17
|
|
|
*/ |
18
|
|
|
class ConfigurationLoader |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Constant for the array key of allowed licenses. |
22
|
|
|
* |
23
|
|
|
* @var string KEY_ALLOWED_LICENSES |
24
|
|
|
*/ |
25
|
|
|
private const KEY_ALLOWED_LICENSES = 'allowed-licenses'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constant for the array key of allowed packages. |
29
|
|
|
* |
30
|
|
|
* @var string KEY_ALLOWED_PACKAGES |
31
|
|
|
*/ |
32
|
|
|
private const KEY_ALLOWED_PACKAGES = 'allowed-packages'; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Extract allowed licenses from whole configuration array and validates the type. |
37
|
|
|
* |
38
|
|
|
* @param string[] $items |
39
|
|
|
* |
40
|
|
|
* @return array<string> |
41
|
|
|
*/ |
42
|
|
|
private function getAllowedLicenses(array $items): array |
43
|
|
|
{ |
44
|
|
|
$result = []; |
45
|
|
|
|
46
|
|
|
foreach ($items as $item) { |
47
|
|
|
if (!is_string($item)) { |
48
|
|
|
throw new ConfigurationParseException('Invalid value.'); |
49
|
|
|
} |
50
|
|
|
$result[] = $item; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Extract allowed packages from whole configuration array and validates the type. |
58
|
|
|
* |
59
|
|
|
* @param string[][] $list |
60
|
|
|
* |
61
|
|
|
* @return array<string, array<string>> |
62
|
|
|
*/ |
63
|
|
|
private function getAllowedPackages(array $list): array |
64
|
|
|
{ |
65
|
|
|
$result = []; |
66
|
|
|
|
67
|
|
|
foreach ($list as $type => $items) { |
68
|
|
|
if (!is_array($items)) { |
69
|
|
|
$items = []; |
70
|
|
|
} |
71
|
|
|
foreach ($items as $item) { |
72
|
|
|
if (!is_string($item)) { |
73
|
|
|
throw new ConfigurationParseException('Invalid value.'); |
74
|
|
|
} |
75
|
|
|
$result[$type][] = $item; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a configuration object from a file. |
84
|
|
|
* |
85
|
|
|
* @param string[] $configFiles Array of configuration files with path. |
86
|
|
|
* |
87
|
|
|
* @throws ConfigurationNotFoundException Exception if configuration file was not found. |
88
|
|
|
* @throws ConfigurationParseException Exception if configuration cannot be parsed. |
89
|
|
|
* |
90
|
|
|
* @return Configuration |
91
|
|
|
*/ |
92
|
|
|
public function load(array $configFiles): Configuration |
93
|
|
|
{ |
94
|
|
|
$configArrays = []; |
95
|
|
|
foreach ($configFiles as $configFile) { |
96
|
|
|
$configArrays[] = $this->parseConfigFile($configFile); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$mergedConfig = [ |
100
|
|
|
'allowed_licenses' => [], |
101
|
|
|
'allowed_packages' => [], |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
foreach ($configArrays as $configArray) { |
105
|
|
|
$mergedConfig = array_merge_recursive($mergedConfig, $configArray); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return new Configuration( |
109
|
|
|
$mergedConfig['allowed_licenses'], |
110
|
|
|
$mergedConfig['allowed_packages'], |
111
|
|
|
); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Reads a single config file and returns the config as array. |
116
|
|
|
* |
117
|
|
|
* @param string $fileName Path to config file. |
118
|
|
|
* |
119
|
|
|
* @throws ConfigurationNotFoundException Exception if configuration file was not found. |
120
|
|
|
* @throws ConfigurationParseException Exception if configuration cannot be parsed. |
121
|
|
|
* |
122
|
|
|
* @return array<string, array<array<string>|string>> |
123
|
|
|
*/ |
124
|
|
|
private function parseConfigFile(string $fileName): array |
125
|
|
|
{ |
126
|
|
|
if (!file_exists($fileName)) { |
127
|
|
|
throw new ConfigurationNotFoundException(sprintf('Configuration file %s not found.', $fileName)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$yaml = Yaml::parseFile($fileName); |
132
|
|
|
|
133
|
|
|
$allowedLicences = $this->getAllowedLicenses($yaml[self::KEY_ALLOWED_LICENSES] ?? []); |
134
|
|
|
$allowedPackages = $this->getAllowedPackages($yaml[self::KEY_ALLOWED_PACKAGES] ?? []); |
135
|
|
|
} catch (Throwable $e) { |
136
|
|
|
throw new ConfigurationParseException('Configuration file cannot be parsed.', 0, $e); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return ['allowed_licenses' => $allowedLicences, 'allowed_packages' => $allowedPackages]; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|