forked from codeigniter4/settings
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseHandler.php
More file actions
209 lines (179 loc) · 5.87 KB
/
DatabaseHandler.php
File metadata and controls
209 lines (179 loc) · 5.87 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
<?php
namespace CodeIgniter\Settings\Handlers;
use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\Database\BaseConnection;
use CodeIgniter\I18n\Time;
use CodeIgniter\Settings\Config\Settings;
use RuntimeException;
/**
* Provides database persistence for Settings.
* Uses ArrayHandler for storage to minimize database calls.
*/
class DatabaseHandler extends ArrayHandler
{
/**
* The DB connection for the Settings.
*/
private BaseConnection $db;
/**
* The Query Builder for the Settings table.
*/
private BaseBuilder $builder;
/**
* Array of contexts that have been stored.
*
* @var list<null>|list<string>
*/
private array $hydrated = [];
private Settings $config;
/**
* Stores the configured database table.
*/
public function __construct()
{
$this->config = config('Settings');
$this->db = db_connect($this->config->database['group']);
$this->builder = $this->db->table($this->config->database['table']);
}
/**
* Checks whether this handler has a value set.
*/
public function has(string $class, string $property, ?string $context = null): bool
{
$this->hydrate($context);
return $this->hasStored($class, $property, $context);
}
/**
* Attempt to retrieve a value from the database.
* To boost performance, all of the values are
* read and stored the first call for each contexts
* and then retrieved from storage.
*
* @return mixed|null
*/
public function get(string $class, string $property, ?string $context = null)
{
return $this->getStored($class, $property, $context);
}
/**
* Stores values into the database for later retrieval.
*
* @param mixed $value
*
* @return void
*
* @throws RuntimeException For database failures
*/
public function set(string $class, string $property, $value = null, ?string $context = null)
{
$time = Time::now()->format('Y-m-d H:i:s');
$type = gettype($value);
$prepared = $this->prepareValue($value);
// If it was stored then we need to update
if ($this->has($class, $property, $context)) {
$result = $this->builder
->where('class', $class)
->where('key', $property)
->where('context', $context)
->update([
'value' => $prepared,
'type' => $type,
'context' => $context,
'updated_at' => $time,
]);
// ...otherwise insert it
} else {
$result = $this->builder
->insert([
'class' => $class,
'key' => $property,
'value' => $prepared,
'type' => $type,
'context' => $context,
'created_at' => $time,
'updated_at' => $time,
]);
}
if ($result !== true) {
throw new RuntimeException($this->db->error()['message'] ?? 'Error writing to the database.');
}
// Update storage
$this->setStored($class, $property, $value, $context);
}
/**
* Deletes the record from persistent storage, if found,
* and from the local cache.
*
* @return void
*/
public function forget(string $class, string $property, ?string $context = null)
{
$this->hydrate($context);
// Delete from the database
$result = $this->builder
->where('class', $class)
->where('key', $property)
->where('context', $context)
->delete();
if (! $result) {
throw new RuntimeException($this->db->error()['message'] ?? 'Error writing to the database.');
}
// Delete from local storage
$this->forgetStored($class, $property, $context);
}
/**
* Retrieves a value from persistent storage
* and deletes it from the local cache.
*
* @return mixed|null
*/
public function take(string $class, string $property, ?string $context = null): mixed
{
$value = $this->get($class, $property, $context);
$this->forget($class, $property, $context);
return $value;
}
/**
* Deletes all records from persistent storage, if found,
* and from the local cache.
*
* @return void
*/
public function flush()
{
$this->builder->truncate();
parent::flush();
}
/**
* Fetches values from the database in bulk to minimize calls.
* General (null) is always fetched once, contexts are fetched
* in their entirety for each new request.
*
* @throws RuntimeException For database failures
*/
private function hydrate(?string $context): void
{
// Check for completion
if (in_array($context, $this->hydrated, true)) {
return;
}
if ($context === null) {
$this->hydrated[] = null;
$query = $this->builder->where('context', null);
} else {
$query = $this->builder->where('context', $context);
// If general has not been hydrated we will do that at the same time
if (! in_array(null, $this->hydrated, true)) {
$this->hydrated[] = null;
$query->orWhere('context', null);
}
$this->hydrated[] = $context;
}
if (is_bool($result = $query->get())) {
throw new RuntimeException($this->db->error()['message'] ?? 'Error reading from database.');
}
foreach ($result->getResultObject() as $row) {
$this->setStored($row->class, $row->key, $this->parseValue($row->value, $row->type), $row->context);
}
}
}