Skip to content

Commit 43230fa

Browse files
authored
Merge pull request #17 from codeigniter4/service
Service Configuration
2 parents 6a26ae8 + 44803ab commit 43230fa

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/Config/Services.php

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

55
use CodeIgniter\Config\BaseService;
6+
use Sparks\Settings\Config\Settings as SettingsConfig;
67
use Sparks\Settings\Settings;
78

89
/**
@@ -23,12 +24,15 @@ class Services extends BaseService
2324
/**
2425
* Returns the Settings manager class.
2526
*/
26-
public static function settings(bool $getShared = true): Settings
27+
public static function settings(?SettingsConfig $config = null, bool $getShared = true): Settings
2728
{
2829
if ($getShared) {
29-
return static::getSharedInstance('settings');
30+
return static::getSharedInstance('settings', $config);
3031
}
3132

32-
return new Settings(config('Settings'));
33+
/** @var SettingsConfig $config */
34+
$config = $config ?? config('Settings');
35+
36+
return new Settings($config);
3337
}
3438
}

src/Settings.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ class Settings
3131
/**
3232
* Grabs instances of our handlers.
3333
*/
34-
public function __construct(?SettingsConfig $config = null)
34+
public function __construct(SettingsConfig $config)
3535
{
36-
/** @var SettingsConfig $config */
37-
$config = $config ?? config('Settings');
38-
3936
foreach ($config->handlers as $handler) {
4037
$class = $config->{$handler}['class'] ?? null;
4138

tests/SettingsTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ public function testSettingsUsesParameter()
2929

3030
public function testSettingsGetsFromConfig()
3131
{
32-
$settings = new Settings();
32+
$settings = new Settings(config('Settings'));
3333

3434
$this->assertSame(config('Test')->siteName, $settings->get('Test.siteName'));
3535
}
3636

3737
public function testSettingsDatabaseNotFound()
3838
{
39-
$settings = new Settings();
39+
$settings = new Settings(config('Settings'));
4040

4141
$this->assertSame(config('Test')->siteName, $settings->get('Test.siteName'));
4242
}
4343

4444
public function testSetInsertsNewRows()
4545
{
46-
$settings = new Settings();
46+
$settings = new Settings(config('Settings'));
4747

4848
$results = $settings->set('Test.siteName', 'Foo');
4949

@@ -58,7 +58,7 @@ public function testSetInsertsNewRows()
5858

5959
public function testSetInsertsBoolTrue()
6060
{
61-
$settings = new Settings();
61+
$settings = new Settings(config('Settings'));
6262

6363
$results = $settings->set('Test.siteName', true);
6464

@@ -75,7 +75,7 @@ public function testSetInsertsBoolTrue()
7575

7676
public function testSetInsertsBoolFalse()
7777
{
78-
$settings = new Settings();
78+
$settings = new Settings(config('Settings'));
7979

8080
$results = $settings->set('Test.siteName', false);
8181

@@ -92,7 +92,7 @@ public function testSetInsertsBoolFalse()
9292

9393
public function testSetInsertsNull()
9494
{
95-
$settings = new Settings();
95+
$settings = new Settings(config('Settings'));
9696

9797
$results = $settings->set('Test.siteName', null);
9898

@@ -109,7 +109,7 @@ public function testSetInsertsNull()
109109

110110
public function testSetInsertsArray()
111111
{
112-
$settings = new Settings();
112+
$settings = new Settings(config('Settings'));
113113
$data = ['foo' => 'bar'];
114114

115115
$results = $settings->set('Test.siteName', $data);
@@ -127,7 +127,7 @@ public function testSetInsertsArray()
127127

128128
public function testSetInsertsObject()
129129
{
130-
$settings = new Settings();
130+
$settings = new Settings(config('Settings'));
131131
$data = (object) ['foo' => 'bar'];
132132

133133
$results = $settings->set('Test.siteName', $data);
@@ -145,7 +145,7 @@ public function testSetInsertsObject()
145145

146146
public function testSetUpdatesExistingRows()
147147
{
148-
$settings = new Settings();
148+
$settings = new Settings(config('Settings'));
149149

150150
$this->hasInDatabase($this->table, [
151151
'class' => 'Tests\Support\Config\Test',
@@ -167,7 +167,7 @@ public function testSetUpdatesExistingRows()
167167

168168
public function testWorksWithoutConfigClass()
169169
{
170-
$settings = new Settings();
170+
$settings = new Settings(config('Settings'));
171171

172172
$results = $settings->set('Nada.siteName', 'Bar');
173173

@@ -183,7 +183,7 @@ public function testWorksWithoutConfigClass()
183183

184184
public function testForgetSuccess()
185185
{
186-
$settings = new Settings();
186+
$settings = new Settings(config('Settings'));
187187

188188
$this->hasInDatabase($this->table, [
189189
'class' => 'Tests\Support\Config\Test',
@@ -204,7 +204,7 @@ public function testForgetSuccess()
204204

205205
public function testForgetWithNoStoredRecord()
206206
{
207-
$settings = new Settings();
207+
$settings = new Settings(config('Settings'));
208208

209209
$results = $settings->forget('Test.siteName');
210210

@@ -213,7 +213,7 @@ public function testForgetWithNoStoredRecord()
213213

214214
public function testSetWithContext()
215215
{
216-
$settings = new Settings();
216+
$settings = new Settings(config('Settings'));
217217

218218
$results = $settings->set('Test.siteName', 'Banana', 'environment:test');
219219

@@ -229,7 +229,7 @@ public function testSetWithContext()
229229

230230
public function testGetWithContext()
231231
{
232-
$settings = new Settings();
232+
$settings = new Settings(config('Settings'));
233233

234234
$settings->set('Test.siteName', 'NoContext');
235235
$settings->set('Test.siteName', 'YesContext', 'testing:true');
@@ -240,7 +240,7 @@ public function testGetWithContext()
240240

241241
public function testGetWithoutContextUsesGlobal()
242242
{
243-
$settings = new Settings();
243+
$settings = new Settings(config('Settings'));
244244

245245
$settings->set('Test.siteName', 'NoContext');
246246

@@ -249,7 +249,7 @@ public function testGetWithoutContextUsesGlobal()
249249

250250
public function testForgetWithContext()
251251
{
252-
$settings = new Settings();
252+
$settings = new Settings(config('Settings'));
253253

254254
$settings->set('Test.siteName', 'Bar');
255255
$settings->set('Test.siteName', 'Amnesia', 'category:disease');

0 commit comments

Comments
 (0)