From 8b0fe2ca3c14f8b682ec3e128d60b60ff864642b Mon Sep 17 00:00:00 2001 From: Jordi de la Mano Date: Wed, 31 Aug 2022 12:02:18 +0200 Subject: [PATCH 1/7] - Improvements - Allow adding another database group --- composer.json | 8 +------ phpunit.xml.dist | 6 ++--- src/Config/Settings.php | 4 +++- .../2021-07-04-041948_CreateSettingsTable.php | 17 +++++++++++++ .../2021-11-14-143905_AddContextColumn.php | 17 +++++++++++++ src/Handlers/DatabaseHandler.php | 24 ++++++++++++------- 6 files changed, 57 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 3dc32d1..a198a3a 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ }, "require-dev": { "codeigniter/coding-standard": "^1.1", - "codeigniter4/codeigniter4": "dev-develop", + "codeigniter4/framework": "^4", "fakerphp/faker": "^1.9", "mockery/mockery": "^1.0", "nexusphp/cs-config": "^3.1", @@ -45,12 +45,6 @@ "Tests\\Support\\": "tests/_support" } }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/codeigniter4/CodeIgniter4" - } - ], "minimum-stability": "dev", "prefer-stable": true, "scripts": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a90a1f2..6204cc6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,7 @@ - + - + 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..21201c9 100644 --- a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php +++ b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php @@ -2,10 +2,22 @@ namespace CodeIgniter\Settings\Database\Migrations; +use CodeIgniter\Config\BaseConfig; +use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class CreateSettingsTable extends Migration { + private BaseConfig $config; + + public function __construct(?Forge $forge = null) + { + $this->config = $this->_getConfig(); + $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; + + parent::__construct($forge); + } + public function up() { $this->forge->addField('id'); @@ -39,6 +51,11 @@ public function up() $this->forge->createTable(config('Settings')->database['table'], true); } + private function _getConfig() + { + return config('Settings'); + } + public function down() { $this->forge->dropTable(config('Settings')->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 5898512..f0dcf00 100644 --- a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php +++ b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php @@ -2,10 +2,22 @@ namespace CodeIgniter\Settings\Database\Migrations; +use CodeIgniter\Config\BaseConfig; +use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class AddContextColumn extends Migration { + private BaseConfig $config; + + public function __construct(?Forge $forge = null) + { + $this->config = $this->_getConfig(); + $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; + + parent::__construct($forge); + } + public function up() { $this->forge->addColumn(config('Settings')->database['table'], [ @@ -18,6 +30,11 @@ public function up() ]); } + private function _getConfig() + { + return config('Settings'); + } + public function down() { $this->forge->dropColumn(config('Settings')->database['table'], 'context'); diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index ef136d3..05bb8cb 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -18,6 +18,13 @@ class DatabaseHandler extends ArrayHandler */ private $table; + /** + * The database group to use. + * + * @var string + */ + private $group; + /** * Array of contexts that have been stored. * @@ -31,6 +38,7 @@ class DatabaseHandler extends ArrayHandler public function __construct() { $this->table = config('Settings')->database['table'] ?? 'settings'; + $this->group = config('Settings')->database['group'] ?? null; } /** @@ -73,7 +81,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 = db_connect($this->group)->table($this->table) ->where('class', $class) ->where('key', $property) ->where('context', $context) @@ -85,7 +93,7 @@ public function set(string $class, string $property, $value = null, ?string $con ]); // ...otherwise insert it } else { - $result = db_connect()->table($this->table) + $result = db_connect($this->group)->table($this->table) ->insert([ 'class' => $class, 'key' => $property, @@ -98,7 +106,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($this->group)->error()['message'] ?? 'Error writing to the database.'); } // Update storage @@ -116,14 +124,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 = db_connect($this->group)->table($this->table) ->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($this->group)->error()['message'] ?? 'Error writing to the database.'); } // Delete from local storage @@ -147,9 +155,9 @@ private function hydrate(?string $context): void if ($context === null) { $this->hydrated[] = null; - $query = db_connect()->table($this->table)->where('context', null); + $query = db_connect($this->group)->table($this->table)->where('context', null); } else { - $query = db_connect()->table($this->table)->where('context', $context); + $query = db_connect($this->group)->table($this->table)->where('context', $context); // If general has not been hydrated we will do that at the same time if (! in_array(null, $this->hydrated, true)) { @@ -161,7 +169,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($this->group)->error()['message'] ?? 'Error reading from database.'); } foreach ($result->getResultObject() as $row) { From 8065827f3fa20393b895e250901fd00104676622 Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Fri, 2 Sep 2022 00:37:30 +0200 Subject: [PATCH 2/7] - remove private method --- .../Migrations/2021-07-04-041948_CreateSettingsTable.php | 7 +------ .../Migrations/2021-11-14-143905_AddContextColumn.php | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php index 21201c9..abe84b9 100644 --- a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php +++ b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php @@ -12,7 +12,7 @@ class CreateSettingsTable extends Migration public function __construct(?Forge $forge = null) { - $this->config = $this->_getConfig(); + $this->config = config('Settings'); $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; parent::__construct($forge); @@ -51,11 +51,6 @@ public function up() $this->forge->createTable(config('Settings')->database['table'], true); } - private function _getConfig() - { - return config('Settings'); - } - public function down() { $this->forge->dropTable(config('Settings')->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 f0dcf00..ad0f3c8 100644 --- a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php +++ b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php @@ -12,7 +12,7 @@ class AddContextColumn extends Migration public function __construct(?Forge $forge = null) { - $this->config = $this->_getConfig(); + $this->config = config('Settings'); $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; parent::__construct($forge); @@ -30,11 +30,6 @@ public function up() ]); } - private function _getConfig() - { - return config('Settings'); - } - public function down() { $this->forge->dropColumn(config('Settings')->database['table'], 'context'); From 3080c6be2fdf83c90306d1f2ab356d916769a214 Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Sat, 3 Sep 2022 11:10:32 +0200 Subject: [PATCH 3/7] Update src/Database/Migrations/2021-11-14-143905_AddContextColumn.php This may break backwards compatibility as if it doesn't check if it exists it will return error, because group doesn't currently exist. Co-authored-by: MGatner --- src/Database/Migrations/2021-11-14-143905_AddContextColumn.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php index ad0f3c8..8e4c715 100644 --- a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php +++ b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php @@ -13,7 +13,7 @@ class AddContextColumn extends Migration public function __construct(?Forge $forge = null) { $this->config = config('Settings'); - $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; + $this->DBGroup = $this->config->database['group']; parent::__construct($forge); } From 136734ac307c2aca761336a005aec6295db82e2e Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Sat, 3 Sep 2022 11:27:00 +0200 Subject: [PATCH 4/7] - Testing --- .../2021-07-04-041948_CreateSettingsTable.php | 6 +-- .../2021-11-14-143905_AddContextColumn.php | 6 +-- tests/DatabaseHandlerTest.php | 38 +++++++++++++++++++ 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php index abe84b9..26627ab 100644 --- a/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php +++ b/src/Database/Migrations/2021-07-04-041948_CreateSettingsTable.php @@ -2,18 +2,14 @@ namespace CodeIgniter\Settings\Database\Migrations; -use CodeIgniter\Config\BaseConfig; use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class CreateSettingsTable extends Migration { - private BaseConfig $config; - public function __construct(?Forge $forge = null) { - $this->config = config('Settings'); - $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; + $this->DBGroup = config('Settings')->database['group'] ?? null; parent::__construct($forge); } diff --git a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php index ad0f3c8..26ee063 100644 --- a/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php +++ b/src/Database/Migrations/2021-11-14-143905_AddContextColumn.php @@ -2,18 +2,14 @@ namespace CodeIgniter\Settings\Database\Migrations; -use CodeIgniter\Config\BaseConfig; use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class AddContextColumn extends Migration { - private BaseConfig $config; - public function __construct(?Forge $forge = null) { - $this->config = config('Settings'); - $this->DBGroup = (isset($this->config->database['group']) && $this->config->database['group']) ? $this->config->database['group'] : null; + $this->DBGroup = config('Settings')->database['group'] ?? null; parent::__construct($forge); } diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index fb603eb..564c94b 100644 --- a/tests/DatabaseHandlerTest.php +++ b/tests/DatabaseHandlerTest.php @@ -22,6 +22,11 @@ final class DatabaseHandlerTest extends TestCase */ protected $table; + /** + * @var string + */ + protected $group; + /** * Ensures we are using the database handler. */ @@ -34,7 +39,9 @@ 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); From 02611357f5aa497f9398b25d17dc01f3db658bb0 Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Sat, 3 Sep 2022 11:28:49 +0200 Subject: [PATCH 5/7] Update DatabaseHandlerTest.php --- tests/DatabaseHandlerTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/DatabaseHandlerTest.php b/tests/DatabaseHandlerTest.php index 564c94b..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; /** @@ -41,7 +42,6 @@ protected function setUp(): void $this->table = $config->database['table']; $this->group = $config->database['group']; } - public function testSetInsertsNewRows() { @@ -57,10 +57,10 @@ public function testSetInsertsNewRows() public function testInvalidGroup() { - $this->expectException(\InvalidArgumentException::class); + $this->expectException(InvalidArgumentException::class); - $config = config('Settings'); - $config->handlers = ['database']; + $config = config('Settings'); + $config->handlers = ['database']; $config->database['group'] = 'another'; $this->settings = new Settings($config); @@ -70,8 +70,8 @@ public function testInvalidGroup() public function testSetDefaultGroup() { - $config = config('Settings'); - $config->handlers = ['database']; + $config = config('Settings'); + $config->handlers = ['database']; $config->database['group'] = 'default'; $this->settings->set('Test.siteName', true); From 507b34091faf51f76c72121f3e45d77617b34f9d Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Mon, 5 Sep 2022 09:42:29 +0200 Subject: [PATCH 6/7] . Refactoring --- src/Handlers/DatabaseHandler.php | 33 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 05bb8cb..f5d94eb 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,18 +13,9 @@ class DatabaseHandler extends ArrayHandler { /** - * The database table to use. - * - * @var string - */ - private $table; - - /** - * The database group to use. - * - * @var string + * The Query Builder for the Settings table. */ - private $group; + private BaseBuilder $builder; /** * Array of contexts that have been stored. @@ -37,8 +29,7 @@ class DatabaseHandler extends ArrayHandler */ public function __construct() { - $this->table = config('Settings')->database['table'] ?? 'settings'; - $this->group = config('Settings')->database['group'] ?? null; + $this->builder = $builder ?? db_connect(config('Settings')->database['group'])->table(config('Settings')->database['table']); } /** @@ -81,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($this->group)->table($this->table) + $result = $this->builder ->where('class', $class) ->where('key', $property) ->where('context', $context) @@ -93,7 +84,7 @@ public function set(string $class, string $property, $value = null, ?string $con ]); // ...otherwise insert it } else { - $result = db_connect($this->group)->table($this->table) + $result = $this->builder ->insert([ 'class' => $class, 'key' => $property, @@ -106,7 +97,7 @@ public function set(string $class, string $property, $value = null, ?string $con } if ($result !== true) { - throw new RuntimeException(db_connect($this->group)->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 @@ -124,14 +115,14 @@ public function forget(string $class, string $property, ?string $context = null) $this->hydrate($context); // Delete from the database - $result = db_connect($this->group)->table($this->table) + $result = $this->builder ->where('class', $class) ->where('key', $property) ->where('context', $context) ->delete(); if (! $result) { - throw new RuntimeException(db_connect($this->group)->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 @@ -155,9 +146,9 @@ private function hydrate(?string $context): void if ($context === null) { $this->hydrated[] = null; - $query = db_connect($this->group)->table($this->table)->where('context', null); + $query = $this->builder->where('context', null); } else { - $query = db_connect($this->group)->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)) { @@ -169,7 +160,7 @@ private function hydrate(?string $context): void } if (is_bool($result = $query->get())) { - throw new RuntimeException(db_connect($this->group)->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) { From 79406edfb26a76d5422b570f508867fc4caa03be Mon Sep 17 00:00:00 2001 From: daycry <7590335+daycry@users.noreply.github.com> Date: Mon, 5 Sep 2022 10:10:07 +0200 Subject: [PATCH 7/7] Update DatabaseHandler.php --- src/Handlers/DatabaseHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index f5d94eb..78f5b72 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -29,7 +29,7 @@ class DatabaseHandler extends ArrayHandler */ public function __construct() { - $this->builder = $builder ?? db_connect(config('Settings')->database['group'])->table(config('Settings')->database['table']); + $this->builder = db_connect(config('Settings')->database['group'])->table(config('Settings')->database['table']); } /**