|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Archivr; |
|
4
|
|
|
|
|
5
|
|
|
use Archivr\Exception\ConfigurationException; |
|
6
|
|
|
use Archivr\Exception\Exception; |
|
7
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
8
|
|
|
|
|
9
|
|
|
class ConfigurationFileReader |
|
10
|
|
|
{ |
|
11
|
|
|
public function getConfiguration(string $configurationFilePath) |
|
12
|
|
|
{ |
|
13
|
|
|
if (!is_file($configurationFilePath) || !is_readable($configurationFilePath)) |
|
14
|
|
|
{ |
|
15
|
|
|
throw new Exception(sprintf('Configuration file path "%s" does not exist or is not readable.', $configurationFilePath)); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
$configurationFilePath = realpath($configurationFilePath); |
|
19
|
|
|
|
|
20
|
|
|
$json = file_get_contents($configurationFilePath); |
|
21
|
|
|
|
|
22
|
|
|
if (!$json) |
|
23
|
|
|
{ |
|
24
|
|
|
throw new Exception(sprintf('Failed to read config file "%s".', $configurationFilePath)); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$array = json_decode($json, true); |
|
28
|
|
|
|
|
29
|
|
|
if ($array === null) |
|
30
|
|
|
{ |
|
31
|
|
|
throw new Exception(sprintf('Invalid configuration file: %s.', $configurationFilePath)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$array = ArrayUtils::merge([ |
|
35
|
|
|
'path' => dirname($configurationFilePath) |
|
36
|
|
|
], $array); |
|
37
|
|
|
|
|
38
|
|
|
foreach (['path', 'vaults'] as $requiredKey) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!array_key_exists($requiredKey, $array)) |
|
41
|
|
|
{ |
|
42
|
|
|
throw new ConfigurationException(sprintf('Missing config key: %s.', $requiredKey)); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (!is_array($array['vaults'])) |
|
47
|
|
|
{ |
|
48
|
|
|
throw new ConfigurationException(sprintf('Configuration key \'vaults\' has to be an array.')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (empty($array['vaults'])) |
|
52
|
|
|
{ |
|
53
|
|
|
throw new ConfigurationException(sprintf('At least one vault configuration has to be present.')); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$configuration = new Configuration(); |
|
57
|
|
|
$configuration->setLocalPath($array['path']); |
|
58
|
|
|
$configuration->setIdentity(empty($array['identity']) ? sprintf('%s@%s', get_current_user(), gethostname()) : $array['identity']); |
|
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
if (!empty($array['exclude'])) |
|
61
|
|
|
{ |
|
62
|
|
|
if (!is_array($array['exclude'])) |
|
63
|
|
|
{ |
|
64
|
|
|
throw new ConfigurationException('Config key "exclude" has to be an array.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$configuration->setExclusions($array['exclude']); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
foreach ($array['vaults'] as $index => $vaultConfig) |
|
71
|
|
|
{ |
|
72
|
|
|
if (empty($vaultConfig['adapter'])) |
|
73
|
|
|
{ |
|
74
|
|
|
throw new ConfigurationException(sprintf('Vault configuration #%d is missing the obligatory \'adapter\' key.', $index)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$lockAdapter = empty($vaultConfig['lockAdapter']) ? 'connection' : $vaultConfig['lockAdapter']; |
|
78
|
|
|
|
|
79
|
|
|
$connectionConfig = new ConnectionConfiguration($vaultConfig['adapter'], $lockAdapter); |
|
80
|
|
|
$connectionConfig->setSettings($vaultConfig['settings'] ?: []); |
|
81
|
|
|
|
|
82
|
|
|
if (!empty($vaultConfig['title'])) |
|
83
|
|
|
{ |
|
84
|
|
|
$connectionConfig->setTitle($vaultConfig['title']); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$configuration->addConnectionConfiguration($connectionConfig); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $configuration; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.