forked from codeigniter4/settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayHandler.php
More file actions
242 lines (210 loc) · 7.09 KB
/
ArrayHandler.php
File metadata and controls
242 lines (210 loc) · 7.09 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
namespace CodeIgniter\Settings\Handlers;
use CodeIgniter\Events\Events;
/**
* Array Settings Handler
*
* Uses local storage to handle non-persistent
* Settings requests. Useful mostly for testing
* or extension by true persistent handlers.
*/
class ArrayHandler extends BaseHandler
{
/**
* Storage for general settings.
* Format: ['class' => ['property' => ['value', 'type']]]
*
* @var array<string, array<string, array{mixed, string}>>
*/
private array $general = [];
/**
* Storage for context settings.
* Format: ['context' => ['class' => ['property' => ['value', 'type']]]]
*
* @var array<string, array<string, array<string, array{mixed, string}>>>
*/
private array $contexts = [];
/**
* Whether to defer writes until the end of request.
* Used by handlers that support deferred writes.
*/
protected bool $deferWrites = false;
/**
* Array of properties that have been modified but not persisted.
* Used by handlers that support deferred writes.
* Format: ['key' => ['class' => ..., 'property' => ..., 'value' => ..., 'context' => ..., 'delete' => ...]]
*
* @var array<string, array{class: string, property: string, value: mixed, context: string|null, delete: bool}>
*/
protected array $pendingProperties = [];
public function has(string $class, string $property, ?string $context = null): bool
{
return $this->hasStored($class, $property, $context);
}
public function get(string $class, string $property, ?string $context = null)
{
return $this->getStored($class, $property, $context);
}
public function getAll(?string $class, ?string $context = null)
{
return $this->getAllStored($class, $context);
}
public function set(string $class, string $property, $value = null, ?string $context = null)
{
$this->setStored($class, $property, $value, $context);
}
public function forget(string $class, string $property, ?string $context = null)
{
$this->forgetStored($class, $property, $context);
}
public function flush()
{
$this->general = [];
$this->contexts = [];
}
/**
* Checks whether this value is in storage.
*/
protected function hasStored(string $class, string $property, ?string $context): bool
{
if ($context === null) {
return isset($this->general[$class]) && array_key_exists($property, $this->general[$class]);
}
return isset($this->contexts[$context][$class]) && array_key_exists($property, $this->contexts[$context][$class]);
}
/**
* Retrieves a value from storage.
*
* @return mixed
*/
protected function getStored(string $class, string $property, ?string $context)
{
if (! $this->has($class, $property, $context)) {
return null;
}
return $context === null
? $this->parseValue(...$this->general[$class][$property])
: $this->parseValue(...$this->contexts[$context][$class][$property]);
}
/**
* Retrieves all values from storage.
*
* @return mixed|null
*/
protected function getAllStored(?string $class, ?string $context)
{
$properties = null;
if ($context === null) {
if ($class === null) {
$properties = $this->general;
} elseif (isset($this->general[$class])) {
$properties = $this->general[$class];
}
} elseif (isset($this->contexts[$context])) {
if ($class === null) {
$properties = $this->contexts[$context];
} elseif (isset($this->contexts[$context][$class])) {
$properties = $this->contexts[$context][$class];
}
}
if ($properties === null) {
return null;
}
if ($class === null) {
foreach ($properties as &$c) {
foreach ($c as &$p) {
$p = $this->parseValue(...$p);
}
}
} else {
foreach ($properties as &$p) {
$p = $this->parseValue(...$p);
}
}
return $properties;
}
/**
* Adds values to storage.
*
* @param mixed $value
*/
protected function setStored(string $class, string $property, $value, ?string $context): void
{
$type = gettype($value);
$value = $this->prepareValue($value);
if ($context === null) {
$this->general[$class][$property] = [
$value,
$type,
];
} else {
$this->contexts[$context][$class][$property] = [
$value,
$type,
];
}
}
/**
* Deletes an item from storage.
*/
protected function forgetStored(string $class, string $property, ?string $context): void
{
if ($context === null) {
unset($this->general[$class][$property]);
} else {
unset($this->contexts[$context][$class][$property]);
}
}
/**
* Marks a property as pending (needs to be persisted).
* Used by handlers that support deferred writes.
*
* @param mixed $value
*/
protected function markPending(string $class, string $property, $value, ?string $context, bool $isDelete = false): void
{
$key = $class . '::' . $property . ($context === null ? '' : '::' . $context);
$this->pendingProperties[$key] = [
'class' => $class,
'property' => $property,
'value' => $value,
'context' => $context,
'delete' => $isDelete,
];
}
/**
* Groups pending properties by class+context combination.
* Useful for handlers that need to persist changes on a per-class basis.
* Format: ['key' => ['class' => ..., 'context' => ..., 'changes' => [...]]]
*
* @return array<string, array{class: string, context: string|null, changes: list<array{class: string, property: string, value: mixed, context: string|null, delete: bool}>}>
*/
protected function getPendingPropertiesGrouped(): array
{
$grouped = [];
foreach ($this->pendingProperties as $info) {
$key = $info['class'] . ($info['context'] === null ? '' : '::' . $info['context']);
if (! isset($grouped[$key])) {
$grouped[$key] = [
'class' => $info['class'],
'context' => $info['context'],
'changes' => [],
];
}
$grouped[$key]['changes'][] = $info;
}
return $grouped;
}
/**
* Sets up deferred writes for handlers that support it.
*
* @param bool $enabled Whether deferred writes should be enabled
*/
protected function setupDeferredWrites(bool $enabled): void
{
$this->deferWrites = $enabled;
if ($this->deferWrites) {
Events::on('post_system', [$this, 'persistPendingProperties']);
}
}
}