|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace WebServCo\Framework\Libraries; |
|
6
|
|
|
|
|
7
|
|
|
final class Config extends \WebServCo\Framework\AbstractLibrary implements |
|
8
|
|
|
\WebServCo\Framework\Interfaces\ConfigInterface |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Stores configuration data. |
|
13
|
|
|
* |
|
14
|
|
|
* @var array<mixed> |
|
15
|
|
|
*/ |
|
16
|
|
|
private array $config = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Add base setting data. |
|
20
|
|
|
* |
|
21
|
|
|
* Keys will be preserved. |
|
22
|
|
|
* Existing values will be overwritten. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $setting Name of setting to load. |
|
25
|
|
|
* @param mixed $data Data to add. |
|
26
|
|
|
*/ |
|
27
|
|
|
public function add(string $setting, $data): bool |
|
28
|
|
|
{ |
|
29
|
|
|
$this->config = \WebServCo\Framework\ArrayStorage::append( |
|
30
|
|
|
$this->config, |
|
31
|
|
|
[$setting => $data], |
|
32
|
|
|
); |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param mixed $setting Can be an array, a string, |
|
38
|
|
|
* or a special formatted string |
|
39
|
|
|
* (eg 'i18n/lang'). |
|
40
|
|
|
* @param mixed $defaultValue |
|
41
|
|
|
* @return mixed |
|
42
|
|
|
*/ |
|
43
|
|
|
public function get($setting, $defaultValue = null) |
|
44
|
|
|
{ |
|
45
|
|
|
return \WebServCo\Framework\ArrayStorage::get($this->config, $setting, $defaultValue); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Load configuration data from a file. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $setting Name of setting to load. |
|
52
|
|
|
* @param string $pathProject Directory where the file is located. |
|
53
|
|
|
* File name must be <$setting>.php |
|
54
|
|
|
* @return array<mixed> |
|
55
|
|
|
*/ |
|
56
|
|
|
public function load(string $setting, string $pathProject): array |
|
57
|
|
|
{ |
|
58
|
|
|
$pathFull = \sprintf('%sconfig/%s.php', $pathProject, $setting); |
|
59
|
|
|
if (!\is_readable($pathFull)) { |
|
60
|
|
|
return []; |
|
61
|
|
|
} |
|
62
|
|
|
$data = (include $pathFull); |
|
63
|
|
|
return \is_array($data) ? $data : []; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Sets a configuration value. |
|
68
|
|
|
* |
|
69
|
|
|
* @param mixed $setting Can be an array, a string, |
|
70
|
|
|
* or a special formatted string |
|
71
|
|
|
* (eg 'i18n/lang'). |
|
72
|
|
|
* @param mixed $value The value to be stored. |
|
73
|
|
|
* @return bool true on success and false on failure. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function set($setting, $value): bool |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$setting) { |
|
78
|
|
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
$this->config = \WebServCo\Framework\ArrayStorage::set($this->config, $setting, $value); |
|
81
|
|
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|