Skip to content

Commit

Permalink
Merge pull request #17 from codeigniter4/service
Browse files Browse the repository at this point in the history
Service Configuration
  • Loading branch information
MGatner authored Nov 15, 2021
2 parents 6a26ae8 + 44803ab commit 43230fa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/Config/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Sparks\Settings\Config;

use CodeIgniter\Config\BaseService;
use Sparks\Settings\Config\Settings as SettingsConfig;
use Sparks\Settings\Settings;

/**
Expand All @@ -23,12 +24,15 @@ class Services extends BaseService
/**
* Returns the Settings manager class.
*/
public static function settings(bool $getShared = true): Settings
public static function settings(?SettingsConfig $config = null, bool $getShared = true): Settings
{
if ($getShared) {
return static::getSharedInstance('settings');
return static::getSharedInstance('settings', $config);
}

return new Settings(config('Settings'));
/** @var SettingsConfig $config */
$config = $config ?? config('Settings');

return new Settings($config);
}
}
5 changes: 1 addition & 4 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,8 @@ class Settings
/**
* Grabs instances of our handlers.
*/
public function __construct(?SettingsConfig $config = null)
public function __construct(SettingsConfig $config)
{
/** @var SettingsConfig $config */
$config = $config ?? config('Settings');

foreach ($config->handlers as $handler) {
$class = $config->{$handler}['class'] ?? null;

Expand Down
32 changes: 16 additions & 16 deletions tests/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ public function testSettingsUsesParameter()

public function testSettingsGetsFromConfig()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

public function testSettingsDatabaseNotFound()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

public function testSetInsertsNewRows()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -58,7 +58,7 @@ public function testSetInsertsNewRows()

public function testSetInsertsBoolTrue()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -75,7 +75,7 @@ public function testSetInsertsBoolTrue()

public function testSetInsertsBoolFalse()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -92,7 +92,7 @@ public function testSetInsertsBoolFalse()

public function testSetInsertsNull()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -109,7 +109,7 @@ public function testSetInsertsNull()

public function testSetInsertsArray()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));
$data = ['foo' => 'bar'];

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

public function testSetInsertsObject()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));
$data = (object) ['foo' => 'bar'];

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

public function testSetUpdatesExistingRows()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

public function testWorksWithoutConfigClass()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -183,7 +183,7 @@ public function testWorksWithoutConfigClass()

public function testForgetSuccess()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

public function testForgetWithNoStoredRecord()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -213,7 +213,7 @@ public function testForgetWithNoStoredRecord()

public function testSetWithContext()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -229,7 +229,7 @@ public function testSetWithContext()

public function testGetWithContext()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

public function testGetWithoutContextUsesGlobal()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

Expand All @@ -249,7 +249,7 @@ public function testGetWithoutContextUsesGlobal()

public function testForgetWithContext()
{
$settings = new Settings();
$settings = new Settings(config('Settings'));

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

0 comments on commit 43230fa

Please sign in to comment.