diff --git a/src/Config/Settings.php b/src/Config/Settings.php index 7ac4fdc..a4695e3 100644 --- a/src/Config/Settings.php +++ b/src/Config/Settings.php @@ -2,10 +2,11 @@ namespace CodeIgniter\Settings\Config; +use CodeIgniter\Config\BaseConfig; use CodeIgniter\Settings\Handlers\ArrayHandler; use CodeIgniter\Settings\Handlers\DatabaseHandler; -class Settings +class Settings extends BaseConfig { /** * The available handlers. The alias must @@ -30,6 +31,7 @@ class Settings public $database = [ 'class' => DatabaseHandler::class, 'table' => 'settings', + 'group' => null, 'writeable' => true, ]; } diff --git a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php index c7b0053..26627ab 100644 --- a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php +++ b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php @@ -2,10 +2,18 @@ namespace CodeIgniter\Settings\Database\Migrations; +use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class CreateSettingsTable extends Migration { + public function __construct(?Forge $forge = null) + { + $this->DBGroup = config('Settings')->database['group'] ?? null; + + parent::__construct($forge); + } + public function up() { $this->forge->addField('id'); diff --git a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php index 5898512..26ee063 100644 --- a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php +++ b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php @@ -2,10 +2,18 @@ namespace CodeIgniter\Settings\Database\Migrations; +use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class AddContextColumn extends Migration { + public function __construct(?Forge $forge = null) + { + $this->DBGroup = config('Settings')->database['group'] ?? null; + + parent::__construct($forge); + } + public function up() { $this->forge->addColumn(config('Settings')->database['table'], [ diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index cd35583..78f5b72 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -2,6 +2,7 @@ namespace CodeIgniter\Settings\Handlers; +use CodeIgniter\Database\BaseBuilder; use CodeIgniter\I18n\Time; use RuntimeException; @@ -12,9 +13,9 @@ class DatabaseHandler extends ArrayHandler { /** - * The database table to use. + * The Query Builder for the Settings table. */ - private string $table; + private BaseBuilder $builder; /** * Array of contexts that have been stored. @@ -28,7 +29,7 @@ class DatabaseHandler extends ArrayHandler */ public function __construct() { - $this->table = config('Settings')->database['table'] ?? 'settings'; + $this->builder = db_connect(config('Settings')->database['group'])->table(config('Settings')->database['table']); } /** @@ -71,7 +72,7 @@ public function set(string $class, string $property, $value = null, ?string $con // If it was stored then we need to update if ($this->has($class, $property, $context)) { - $result = db_connect()->table($this->table) + $result = $this->builder ->where('class', $class) ->where('key', $property) ->where('context', $context) @@ -83,7 +84,7 @@ public function set(string $class, string $property, $value = null, ?string $con ]); // ...otherwise insert it } else { - $result = db_connect()->table($this->table) + $result = $this->builder ->insert([ 'class' => $class, 'key' => $property, @@ -96,7 +97,7 @@ public function set(string $class, string $property, $value = null, ?string $con } if ($result !== true) { - throw new RuntimeException(db_connect()->error()['message'] ?? 'Error writing to the database.'); + throw new RuntimeException(db_connect(config('Settings')->database['group'])->error()['message'] ?? 'Error writing to the database.'); } // Update storage @@ -114,14 +115,14 @@ public function forget(string $class, string $property, ?string $context = null) $this->hydrate($context); // Delete from the database - $result = db_connect()->table($this->table) + $result = $this->builder ->where('class', $class) ->where('key', $property) ->where('context', $context) ->delete(); if (! $result) { - throw new RuntimeException(db_connect()->error()['message'] ?? 'Error writing to the database.'); + throw new RuntimeException(db_connect(config('Settings')->database['group'])->error()['message'] ?? 'Error writing to the database.'); } // Delete from local storage @@ -145,9 +146,9 @@ private function hydrate(?string $context): void if ($context === null) { $this->hydrated[] = null; - $query = db_connect()->table($this->table)->where('context', null); + $query = $this->builder->where('context', null); } else { - $query = db_connect()->table($this->table)->where('context', $context); + $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)) { @@ -159,7 +160,7 @@ private function hydrate(?string $context): void } if (is_bool($result = $query->get())) { - throw new RuntimeException(db_connect()->error()['message'] ?? 'Error reading from database.'); + throw new RuntimeException(db_connect(config('Settings')->database['group'])->error()['message'] ?? 'Error reading from database.'); } foreach ($result->getResultObject() as $row) { diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index fb603eb..251d149 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -5,6 +5,7 @@ use CodeIgniter\I18n\Time; use CodeIgniter\Settings\Settings; use CodeIgniter\Test\DatabaseTestTrait; +use InvalidArgumentException; use Tests\Support\TestCase; /** @@ -22,6 +23,11 @@ final class DatabaseHandlerTest extends TestCase */ protected $table; + /** + * @var string + */ + protected $group; + /** * Ensures we are using the database handler. */ @@ -34,6 +40,7 @@ protected function setUp(): void $this->settings = new Settings($config); $this->table = $config->database['table']; + $this->group = $config->database['group']; } public function testSetInsertsNewRows() @@ -48,6 +55,37 @@ public function testSetInsertsNewRows() ]); } + public function testInvalidGroup() + { + $this->expectException(InvalidArgumentException::class); + + $config = config('Settings'); + $config->handlers = ['database']; + $config->database['group'] = 'another'; + + $this->settings = new Settings($config); + + $this->settings->set('Test.siteName', true); + } + + public function testSetDefaultGroup() + { + $config = config('Settings'); + $config->handlers = ['database']; + $config->database['group'] = 'default'; + + $this->settings->set('Test.siteName', true); + + $this->seeInDatabase($this->table, [ + 'class' => 'Tests\Support\Config\Test', + 'key' => 'siteName', + 'value' => '1', + 'type' => 'boolean', + ]); + + $this->assertTrue($this->settings->get('Test.siteName')); + } + public function testSetInsertsBoolTrue() { $this->settings->set('Test.siteName', true);