|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lagdo\DbAdmin\Config; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Config\ConfigReader; |
|
6
|
|
|
use Jaxon\Config\ConfigSetter; |
|
7
|
|
|
|
|
8
|
|
|
use function array_filter; |
|
9
|
|
|
use function env; |
|
|
|
|
|
|
10
|
|
|
use function is_array; |
|
11
|
|
|
use function is_file; |
|
12
|
|
|
|
|
13
|
|
|
class UserFileReader |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The constructor |
|
17
|
|
|
* |
|
18
|
|
|
* @param AuthInterface $auth |
|
19
|
|
|
* @param string $configFilePath |
|
20
|
|
|
* @param bool $useEnv |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(private AuthInterface $auth, |
|
23
|
|
|
private string $configFilePath, private bool $useEnv = false) |
|
24
|
|
|
{} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Replace options with values from the .env config. |
|
28
|
|
|
* |
|
29
|
|
|
* @param array $values |
|
30
|
|
|
* |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
private function getOptionValues(array $values): array |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$this->useEnv) { |
|
36
|
|
|
return $values; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
// The values in the server options are the names of the |
|
40
|
|
|
// corresponding options in the .env file. |
|
41
|
|
|
$values['servers'] = array_map(function(array $server) { |
|
42
|
|
|
foreach (['host', 'port', 'username', 'password'] as $option) { |
|
43
|
|
|
if (isset($server[$option])) { |
|
44
|
|
|
$server[$option] = env($server[$option]); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
}, $values['servers'] ?? []); |
|
48
|
|
|
return $values; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the options for the authenticated user. |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $defaultOptions |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getOptions(array $defaultOptions): array |
|
59
|
|
|
{ |
|
60
|
|
|
$setter = new ConfigSetter(); |
|
61
|
|
|
$reader = new ConfigReader($setter); |
|
62
|
|
|
$userConfig = $setter->newConfig($defaultOptions); |
|
63
|
|
|
|
|
64
|
|
|
if (!is_file($this->configFilePath)) { |
|
65
|
|
|
return $userConfig->getValues(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$config = $reader->load($setter->newConfig(), $this->configFilePath); |
|
69
|
|
|
$commonOptions = $config->getOption('common', null); |
|
70
|
|
|
if (is_array($commonOptions)) { |
|
71
|
|
|
$userConfig = $setter->setOptions($config, $commonOptions); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$fallbackOptions = $config->getOption('fallback', null); |
|
75
|
|
|
$userOptions = array_filter($config->getOption('users', []), |
|
76
|
|
|
fn($options) => $options['id'] ?? null === $this->auth->user()); |
|
77
|
|
|
$userOptions = $userOptions[0] ?? $fallbackOptions; |
|
78
|
|
|
|
|
79
|
|
|
if (!is_array($userOptions)) { |
|
80
|
|
|
return $this->getOptionValues($userConfig->getValues()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
unset($userOptions['id']); // Remove the id field. |
|
84
|
|
|
$userConfig = $setter->setOptions($userConfig, $userOptions); |
|
85
|
|
|
|
|
86
|
|
|
return $this->getOptionValues($userConfig->getValues()); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|