forked from codeigniter4/settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.php
More file actions
192 lines (163 loc) · 4.95 KB
/
Settings.php
File metadata and controls
192 lines (163 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace CodeIgniter\Settings;
use CodeIgniter\Settings\Config\Settings as SettingsConfig;
use CodeIgniter\Settings\Handlers\BaseHandler;
use InvalidArgumentException;
use RuntimeException;
/**
* Allows developers a single location to store and
* retrieve settings that were original set in config files
* in the core application or any third-party module.
*/
class Settings
{
/**
* An array of handlers for getting/setting the values.
*
* @var list<BaseHandler>
*/
private array $handlers = [];
/**
* An array of the config options for each handler.
*
* @var array<string,array<string,mixed>>
*/
private ?array $options = null;
/**
* Grabs instances of our handlers.
*/
public function __construct(SettingsConfig $config)
{
foreach ($config->handlers as $handler) {
$class = $config->{$handler}['class'] ?? null;
if ($class === null) {
continue;
}
$this->handlers[$handler] = new $class();
$this->options[$handler] = $config->{$handler};
}
}
/**
* Retrieve a value from any handler
* or from a config file matching the name
* file.arg.optionalArg
*/
public function get(string $key, ?string $context = null)
{
[$class, $property, $config] = $this->prepareClassAndProperty($key);
// Check each of our handlers
foreach ($this->handlers as $handler) {
if ($handler->has($class, $property, $context)) {
return $handler->get($class, $property, $context);
}
}
// If no contextual value was found then fall back to general
if ($context !== null) {
return $this->get($key);
}
return $config->{$property} ?? null;
}
/**
* Save a value to the writable handler for later retrieval.
*
* @param mixed $value
*
* @return void
*/
public function set(string $key, $value = null, ?string $context = null)
{
[$class, $property] = $this->prepareClassAndProperty($key);
foreach ($this->getWriteHandlers() as $handler) {
$handler->set($class, $property, $value, $context);
}
}
/**
* Removes a setting from the persistent storage,
* effectively returning the value to the default value
* found in the config file, if any.
*
* @return void
*/
public function forget(string $key, ?string $context = null)
{
[$class, $property] = $this->prepareClassAndProperty($key);
foreach ($this->getWriteHandlers() as $handler) {
$handler->forget($class, $property, $context);
}
}
/**
* Retrieves a value from the persistent storage and removes it.
*
* @return mixed
*/
public function take(string $key, ?string $context = null)
{
[$class, $property] = $this->prepareClassAndProperty($key);
foreach ($this->getWriteHandlers() as $handler) {
return $handler->take($class, $property, $context);
}
}
/**
* Removes all settings from the persistent storage,
* Useful during testing. Use with caution.
*
* @return void
*/
public function flush()
{
foreach ($this->getWriteHandlers() as $handler) {
$handler->flush();
}
}
/**
* Returns the handler that is set to store values.
*
* @return list<BaseHandler>
*
* @throws RuntimeException
*/
private function getWriteHandlers()
{
$handlers = [];
foreach ($this->options as $handler => $options) {
if (! empty($options['writeable'])) {
$handlers[] = $this->handlers[$handler];
}
}
if ($handlers === []) {
throw new RuntimeException('Unable to find a Settings handler that can store values.');
}
return $handlers;
}
/**
* Analyzes the given key and breaks it into the class.field parts.
*
* @return list<string>
*
* @throws InvalidArgumentException
*/
private function parseDotSyntax(string $key): array
{
// Parse the field name for class.field
$parts = explode('.', $key);
if (count($parts) === 1) {
throw new InvalidArgumentException('$key must contain both the class and field name, i.e. Foo.bar');
}
return $parts;
}
/**
* Given a key in class.property syntax, will split the values
* and determine the fully qualified class name, if possible.
*/
private function prepareClassAndProperty(string $key): array
{
[$class, $property] = $this->parseDotSyntax($key);
$config = config($class);
// Use a fully qualified class name if the
// config file was found.
if ($config !== null) {
$class = $config::class;
}
return [$class, $property, $config];
}
}