From 09ea35f7dd36db130f732750a02df62c01c81b4c Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 21 Sep 2022 11:22:22 +0900 Subject: [PATCH 1/4] refactor: add property $config --- .../2021-07-04-041948_CreateSettingsTable.php | 10 +++++++--- .../Migrations/2021-11-14-143905_AddContextColumn.php | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) 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'); } } From a220b5cf9ddb35496bcefcc45bd82481b91c80a6 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 21 Sep 2022 11:23:00 +0900 Subject: [PATCH 2/4] refactor: add property $config --- src/Handlers/DatabaseHandler.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index abeffda..6421bc7 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -4,6 +4,7 @@ use CodeIgniter\Database\BaseBuilder; use CodeIgniter\I18n\Time; +use CodeIgniter\Settings\Config\Settings; use RuntimeException; /** @@ -24,12 +25,15 @@ 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->builder = db_connect($this->config->database['group'])->table($this->config->database['table']); } /** @@ -97,7 +101,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(db_connect($this->config->database['group'])->error()['message'] ?? 'Error writing to the database.'); } // Update storage @@ -122,7 +126,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(db_connect($this->config->database['group'])->error()['message'] ?? 'Error writing to the database.'); } // Delete from local storage @@ -160,7 +164,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(db_connect($this->config->database['group'])->error()['message'] ?? 'Error reading from database.'); } foreach ($result->getResultObject() as $row) { From 95012c6126c0cdc5ce47b46de021624dd12f39ac Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 21 Sep 2022 11:23:19 +0900 Subject: [PATCH 3/4] docs: add @var --- tests/DatabaseHandlerTest.php | 3 +++ 1 file changed, 3 insertions(+) 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'; From 922b24b31a5d39791d2bb603e748d3fb31574520 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 21 Sep 2022 11:28:53 +0900 Subject: [PATCH 4/4] refactor: add property $db --- src/Handlers/DatabaseHandler.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 6421bc7..240ee2b 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -3,6 +3,7 @@ namespace CodeIgniter\Settings\Handlers; use CodeIgniter\Database\BaseBuilder; +use CodeIgniter\Database\BaseConnection; use CodeIgniter\I18n\Time; use CodeIgniter\Settings\Config\Settings; use RuntimeException; @@ -13,6 +14,11 @@ */ class DatabaseHandler extends ArrayHandler { + /** + * The DB connection for the Settings. + */ + private BaseConnection $db; + /** * The Query Builder for the Settings table. */ @@ -33,7 +39,8 @@ class DatabaseHandler extends ArrayHandler public function __construct() { $this->config = config('Settings'); - $this->builder = db_connect($this->config->database['group'])->table($this->config->database['table']); + $this->db = db_connect($this->config->database['group']); + $this->builder = $this->db->table($this->config->database['table']); } /** @@ -101,7 +108,7 @@ public function set(string $class, string $property, $value = null, ?string $con } if ($result !== true) { - throw new RuntimeException(db_connect($this->config->database['group'])->error()['message'] ?? 'Error writing to the database.'); + throw new RuntimeException($this->db->error()['message'] ?? 'Error writing to the database.'); } // Update storage @@ -126,7 +133,7 @@ public function forget(string $class, string $property, ?string $context = null) ->delete(); if (! $result) { - throw new RuntimeException(db_connect($this->config->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 @@ -164,7 +171,7 @@ private function hydrate(?string $context): void } if (is_bool($result = $query->get())) { - throw new RuntimeException(db_connect($this->config->database['group'])->error()['message'] ?? 'Error reading from database.'); + throw new RuntimeException($this->db->error()['message'] ?? 'Error reading from database.'); } foreach ($result->getResultObject() as $row) {