Skip to content

Commit c7d6c8f

Browse files
authored
Merge pull request #15 from codeigniter4/config-inject
2 parents c73e7ec + c89a27e commit c7d6c8f

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/Config/Services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public static function settings(bool $getShared = true): Settings
2929
return static::getSharedInstance('settings');
3030
}
3131

32-
return new Settings();
32+
return new Settings(config('Settings'));
3333
}
3434
}

src/Settings.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sparks\Settings;
44

5+
use Sparks\Settings\Config\Settings as SettingsConfig;
6+
57
/**
68
* Allows developers a single location to store and
79
* retrieve settings that were original set in config files
@@ -27,18 +29,21 @@ class Settings
2729
/**
2830
* Grabs instances of our handlers.
2931
*/
30-
public function __construct()
32+
public function __construct(?SettingsConfig $config = null)
3133
{
32-
foreach (config('Settings')->handlers as $handler) {
33-
$class = config('Settings')->{$handler}['class'] ?? null;
34+
/** @var SettingsConfig $config */
35+
$config = $config ?? config('Settings');
36+
37+
foreach ($config->handlers as $handler) {
38+
$class = $config->{$handler}['class'] ?? null;
3439

3540
if ($class === null) {
3641
continue;
3742
}
3843

3944
$this->handlers[$handler] = new $class();
4045

41-
$writeable = config('Settings')->{$handler}['writeable'] ?? null;
46+
$writeable = $config->{$handler}['writeable'] ?? null;
4247

4348
if ($writeable) {
4449
$this->writeHandler = $handler;

tests/SettingsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ final class SettingsTest extends TestCase
1616
{
1717
use DatabaseTestTrait;
1818

19+
public function testSettingsUsesParameter()
20+
{
21+
$config = config('Settings');
22+
$config->handlers = [];
23+
24+
$settings = new Settings($config);
25+
$result = $this->getPrivateProperty($settings, 'handlers');
26+
27+
$this->assertSame([], $result);
28+
}
29+
1930
public function testSettingsGetsFromConfig()
2031
{
2132
$settings = new Settings();

0 commit comments

Comments
 (0)