Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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']);
}
}
10 changes: 7 additions & 3 deletions src/Database/Migrations/2021-11-14-143905_AddContextColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}
}
19 changes: 15 additions & 4 deletions src/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -12,6 +14,11 @@
*/
class DatabaseHandler extends ArrayHandler
{
/**
* The DB connection for the Settings.
*/
private BaseConnection $db;

/**
* The Query Builder for the Settings table.
*/
Expand All @@ -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']);
}

/**
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions tests/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ protected function setUp(): void
{
parent::setUp();

/** @var \CodeIgniter\Settings\Config\Settings $config */
$config = config('Settings');
$config->handlers = ['database'];

Expand All @@ -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';
Expand All @@ -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';
Expand Down