Skip to content

Commit 0c3fbd5

Browse files
authored
Merge pull request #22 from codeigniter4/multiple-writes
Multiple Write Handlers
2 parents 04a3044 + 9f65521 commit 0c3fbd5

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

src/Handlers/BaseHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ abstract public function get(string $class, string $property, ?string $context =
2222
* If the Handler supports saving values, it
2323
* MUST override this method to provide that functionality.
2424
* Not all Handlers will support writing values.
25+
* Must throw RuntimeException for any failures.
2526
*
2627
* @param mixed $value
2728
*
2829
* @throws RuntimeException
2930
*
30-
* @return mixed
31+
* @return void
3132
*/
3233
public function set(string $class, string $property, $value = null, ?string $context = null)
3334
{
@@ -38,10 +39,11 @@ public function set(string $class, string $property, $value = null, ?string $con
3839
* If the Handler supports forgetting values, it
3940
* MUST override this method to provide that functionality.
4041
* Not all Handlers will support writing values.
42+
* Must throw RuntimeException for any failures.
4143
*
4244
* @throws RuntimeException
4345
*
44-
* @return mixed
46+
* @return void
4547
*/
4648
public function forget(string $class, string $property, ?string $context = null)
4749
{

src/Handlers/DatabaseHandler.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function get(string $class, string $property, ?string $context = null)
6363
*
6464
* @throws RuntimeException For database failures
6565
*
66-
* @return mixed|void
66+
* @return void
6767
*/
6868
public function set(string $class, string $property, $value = null, ?string $context = null)
6969
{
@@ -103,13 +103,13 @@ public function set(string $class, string $property, $value = null, ?string $con
103103

104104
// Update storage
105105
$this->setStored($class, $property, $value, $context);
106-
107-
return $result;
108106
}
109107

110108
/**
111109
* Deletes the record from persistent storage, if found,
112110
* and from the local cache.
111+
*
112+
* @return void
113113
*/
114114
public function forget(string $class, string $property, ?string $context = null)
115115
{
@@ -123,13 +123,11 @@ public function forget(string $class, string $property, ?string $context = null)
123123
->delete();
124124

125125
if (! $result) {
126-
return $result;
126+
throw new RuntimeException(db_connect()->error()['message'] ?? 'Error writing to the database.');
127127
}
128128

129129
// Delete from local storage
130130
$this->forgetStored($class, $property, $context);
131-
132-
return $result;
133131
}
134132

135133
/**
@@ -139,7 +137,7 @@ public function forget(string $class, string $property, ?string $context = null)
139137
*
140138
* @throws RuntimeException For database failures
141139
*/
142-
private function hydrate(?string $context)
140+
private function hydrate(?string $context): void
143141
{
144142
// Check for completion
145143
if (in_array($context, $this->hydrated, true)) {

src/Settings.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace CodeIgniter\Settings;
44

55
use CodeIgniter\Settings\Config\Settings as SettingsConfig;
6+
use CodeIgniter\Settings\Handlers\BaseHandler;
67
use InvalidArgumentException;
78
use RuntimeException;
89

@@ -14,19 +15,18 @@
1415
class Settings
1516
{
1617
/**
17-
* An array of Setting Stores that handle
18-
* the actual act of getting/setting the values.
18+
* An array of handlers for getting/setting the values.
1919
*
20-
* @var array
20+
* @var BaseHandler[]
2121
*/
2222
private $handlers = [];
2323

2424
/**
25-
* The name of the handler that handles writes.
25+
* An array of the config options for each handler.
2626
*
27-
* @var string
27+
* @var array<string,array<string,mixed>>
2828
*/
29-
private $writeHandler;
29+
private $options;
3030

3131
/**
3232
* Grabs instances of our handlers.
@@ -41,12 +41,7 @@ public function __construct(SettingsConfig $config)
4141
}
4242

4343
$this->handlers[$handler] = new $class();
44-
45-
$writeable = $config->{$handler}['writeable'] ?? null;
46-
47-
if ($writeable) {
48-
$this->writeHandler = $handler;
49-
}
44+
$this->options[$handler] = $config->{$handler};
5045
}
5146
}
5247

@@ -79,41 +74,55 @@ public function get(string $key, ?string $context = null)
7974
*
8075
* @param mixed $value
8176
*
82-
* @return void|null
77+
* @return void
8378
*/
8479
public function set(string $key, $value = null, ?string $context = null)
8580
{
8681
[$class, $property] = $this->prepareClassAndProperty($key);
8782

88-
return $this->getWriteHandler()->set($class, $property, $value, $context);
83+
foreach ($this->getWriteHandlers() as $handler) {
84+
$handler->set($class, $property, $value, $context);
85+
}
8986
}
9087

9188
/**
9289
* Removes a setting from the persistent storage,
9390
* effectively returning the value to the default value
9491
* found in the config file, if any.
92+
*
93+
* @return void
9594
*/
9695
public function forget(string $key, ?string $context = null)
9796
{
9897
[$class, $property] = $this->prepareClassAndProperty($key);
9998

100-
return $this->getWriteHandler()->forget($class, $property, $context);
99+
foreach ($this->getWriteHandlers() as $handler) {
100+
$handler->forget($class, $property, $context);
101+
}
101102
}
102103

103104
/**
104105
* Returns the handler that is set to store values.
105106
*
106107
* @throws RuntimeException
107108
*
108-
* @return mixed
109+
* @return BaseHandler[]
109110
*/
110-
private function getWriteHandler()
111+
private function getWriteHandlers()
111112
{
112-
if (empty($this->writeHandler) || ! isset($this->handlers[$this->writeHandler])) {
113+
$handlers = [];
114+
115+
foreach ($this->options as $handler => $options) {
116+
if (! empty($options['writeable'])) {
117+
$handlers[] = $this->handlers[$handler];
118+
}
119+
}
120+
121+
if ($handlers === []) {
113122
throw new RuntimeException('Unable to find a Settings handler that can store values.');
114123
}
115124

116-
return $this->handlers[$this->writeHandler];
125+
return $handlers;
117126
}
118127

119128
/**

tests/DatabaseHandlerTest.php

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ protected function setUp(): void
3838

3939
public function testSetInsertsNewRows()
4040
{
41-
$results = $this->settings->set('Test.siteName', 'Foo');
41+
$this->settings->set('Test.siteName', 'Foo');
4242

43-
$this->assertTrue($results);
4443
$this->seeInDatabase($this->table, [
4544
'class' => 'Tests\Support\Config\Test',
4645
'key' => 'siteName',
@@ -51,9 +50,8 @@ public function testSetInsertsNewRows()
5150

5251
public function testSetInsertsBoolTrue()
5352
{
54-
$results = $this->settings->set('Test.siteName', true);
53+
$this->settings->set('Test.siteName', true);
5554

56-
$this->assertTrue($results);
5755
$this->seeInDatabase($this->table, [
5856
'class' => 'Tests\Support\Config\Test',
5957
'key' => 'siteName',
@@ -66,9 +64,8 @@ public function testSetInsertsBoolTrue()
6664

6765
public function testSetInsertsBoolFalse()
6866
{
69-
$results = $this->settings->set('Test.siteName', false);
67+
$this->settings->set('Test.siteName', false);
7068

71-
$this->assertTrue($results);
7269
$this->seeInDatabase($this->table, [
7370
'class' => 'Tests\Support\Config\Test',
7471
'key' => 'siteName',
@@ -81,9 +78,8 @@ public function testSetInsertsBoolFalse()
8178

8279
public function testSetInsertsNull()
8380
{
84-
$results = $this->settings->set('Test.siteName', null);
81+
$this->settings->set('Test.siteName', null);
8582

86-
$this->assertTrue($results);
8783
$this->seeInDatabase($this->table, [
8884
'class' => 'Tests\Support\Config\Test',
8985
'key' => 'siteName',
@@ -96,10 +92,9 @@ public function testSetInsertsNull()
9692

9793
public function testSetInsertsArray()
9894
{
99-
$data = ['foo' => 'bar'];
100-
$results = $this->settings->set('Test.siteName', $data);
95+
$data = ['foo' => 'bar'];
96+
$this->settings->set('Test.siteName', $data);
10197

102-
$this->assertTrue($results);
10398
$this->seeInDatabase($this->table, [
10499
'class' => 'Tests\Support\Config\Test',
105100
'key' => 'siteName',
@@ -112,10 +107,9 @@ public function testSetInsertsArray()
112107

113108
public function testSetInsertsObject()
114109
{
115-
$data = (object) ['foo' => 'bar'];
116-
$results = $this->settings->set('Test.siteName', $data);
110+
$data = (object) ['foo' => 'bar'];
111+
$this->settings->set('Test.siteName', $data);
117112

118-
$this->assertTrue($results);
119113
$this->seeInDatabase($this->table, [
120114
'class' => 'Tests\Support\Config\Test',
121115
'key' => 'siteName',
@@ -136,9 +130,8 @@ public function testSetUpdatesExistingRows()
136130
'updated_at' => Time::now()->toDateTimeString(),
137131
]);
138132

139-
$results = $this->settings->set('Test.siteName', 'Bar');
133+
$this->settings->set('Test.siteName', 'Bar');
140134

141-
$this->assertTrue($results);
142135
$this->seeInDatabase($this->table, [
143136
'class' => 'Tests\Support\Config\Test',
144137
'key' => 'siteName',
@@ -148,9 +141,8 @@ public function testSetUpdatesExistingRows()
148141

149142
public function testWorksWithoutConfigClass()
150143
{
151-
$results = $this->settings->set('Nada.siteName', 'Bar');
144+
$this->settings->set('Nada.siteName', 'Bar');
152145

153-
$this->assertTrue($results);
154146
$this->seeInDatabase($this->table, [
155147
'class' => 'Nada',
156148
'key' => 'siteName',
@@ -170,9 +162,8 @@ public function testForgetSuccess()
170162
'updated_at' => Time::now()->toDateTimeString(),
171163
]);
172164

173-
$results = $this->settings->forget('Test.siteName');
165+
$this->settings->forget('Test.siteName');
174166

175-
$this->assertTrue($results);
176167
$this->dontSeeInDatabase($this->table, [
177168
'class' => 'Tests\Support\Config\Test',
178169
'key' => 'siteName',
@@ -181,16 +172,18 @@ public function testForgetSuccess()
181172

182173
public function testForgetWithNoStoredRecord()
183174
{
184-
$results = $this->settings->forget('Test.siteName');
175+
$this->settings->forget('Test.siteName');
185176

186-
$this->assertTrue($results);
177+
$this->dontSeeInDatabase($this->table, [
178+
'class' => 'Tests\Support\Config\Test',
179+
'key' => 'siteName',
180+
]);
187181
}
188182

189183
public function testSetWithContext()
190184
{
191-
$results = $this->settings->set('Test.siteName', 'Banana', 'environment:test');
185+
$this->settings->set('Test.siteName', 'Banana', 'environment:test');
192186

193-
$this->assertTrue($results);
194187
$this->seeInDatabase($this->table, [
195188
'class' => 'Tests\Support\Config\Test',
196189
'key' => 'siteName',

0 commit comments

Comments
 (0)