diff --git a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php index 26627ab..eb45235 100644 --- a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php +++ b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php @@ -4,12 +4,16 @@ use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; +use CodeIgniter\Settings\Config\Settings; class CreateSettingsTable extends Migration { + private Settings $config; + public function __construct(?Forge $forge = null) { - $this->DBGroup = config('Settings')->database['group'] ?? null; + $this->config = config('Settings'); + $this->DBGroup = $this->config->database['group'] ?? null; parent::__construct($forge); } @@ -44,11 +48,11 @@ public function up() 'null' => false, ], ]); - $this->forge->createTable(config('Settings')->database['table'], true); + $this->forge->createTable($this->config->database['table'], true); } public function down() { - $this->forge->dropTable(config('Settings')->database['table']); + $this->forge->dropTable($this->config->database['table']); } } diff --git a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php index 26ee063..9bf7fe2 100644 --- a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php +++ b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php @@ -4,19 +4,23 @@ use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; +use CodeIgniter\Settings\Config\Settings; class AddContextColumn extends Migration { + private Settings $config; + public function __construct(?Forge $forge = null) { - $this->DBGroup = config('Settings')->database['group'] ?? null; + $this->config = config('Settings'); + $this->DBGroup = $this->config->database['group'] ?? null; parent::__construct($forge); } public function up() { - $this->forge->addColumn(config('Settings')->database['table'], [ + $this->forge->addColumn($this->config->database['table'], [ 'context' => [ 'type' => 'varchar', 'constraint' => 255, @@ -28,6 +32,6 @@ public function up() public function down() { - $this->forge->dropColumn(config('Settings')->database['table'], 'context'); + $this->forge->dropColumn($this->config->database['table'], 'context'); } } diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index abeffda..240ee2b 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -3,7 +3,9 @@ namespace CodeIgniter\Settings\Handlers; use CodeIgniter\Database\BaseBuilder; +use CodeIgniter\Database\BaseConnection; use CodeIgniter\I18n\Time; +use CodeIgniter\Settings\Config\Settings; use RuntimeException; /** @@ -12,6 +14,11 @@ */ class DatabaseHandler extends ArrayHandler { + /** + * The DB connection for the Settings. + */ + private BaseConnection $db; + /** * The Query Builder for the Settings table. */ @@ -24,12 +31,16 @@ class DatabaseHandler extends ArrayHandler */ private $hydrated = []; + private Settings $config; + /** * Stores the configured database table. */ public function __construct() { - $this->builder = db_connect(config('Settings')->database['group'])->table(config('Settings')->database['table']); + $this->config = config('Settings'); + $this->db = db_connect($this->config->database['group']); + $this->builder = $this->db->table($this->config->database['table']); } /** @@ -97,7 +108,7 @@ public function set(string $class, string $property, $value = null, ?string $con } if ($result !== true) { - throw new RuntimeException(db_connect(config('Settings')->database['group'])->error()['message'] ?? 'Error writing to the database.'); + throw new RuntimeException($this->db->error()['message'] ?? 'Error writing to the database.'); } // Update storage @@ -122,7 +133,7 @@ public function forget(string $class, string $property, ?string $context = null) ->delete(); if (! $result) { - throw new RuntimeException(db_connect(config('Settings')->database['group'])->error()['message'] ?? 'Error writing to the database.'); + throw new RuntimeException($this->db->error()['message'] ?? 'Error writing to the database.'); } // Delete from local storage @@ -160,7 +171,7 @@ private function hydrate(?string $context): void } if (is_bool($result = $query->get())) { - throw new RuntimeException(db_connect(config('Settings')->database['group'])->error()['message'] ?? 'Error reading from database.'); + throw new RuntimeException($this->db->error()['message'] ?? 'Error reading from database.'); } foreach ($result->getResultObject() as $row) { diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index 251d149..2244c2c 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -35,6 +35,7 @@ protected function setUp(): void { parent::setUp(); + /** @var \CodeIgniter\Settings\Config\Settings $config */ $config = config('Settings'); $config->handlers = ['database']; @@ -59,6 +60,7 @@ public function testInvalidGroup() { $this->expectException(InvalidArgumentException::class); + /** @var \CodeIgniter\Settings\Config\Settings $config */ $config = config('Settings'); $config->handlers = ['database']; $config->database['group'] = 'another'; @@ -70,6 +72,7 @@ public function testInvalidGroup() public function testSetDefaultGroup() { + /** @var \CodeIgniter\Settings\Config\Settings $config */ $config = config('Settings'); $config->handlers = ['database']; $config->database['group'] = 'default';