Skip to content

Commit

Permalink
Merge pull request #34 from daycry/develop
Browse files Browse the repository at this point in the history
feat: Allow adding another database group
  • Loading branch information
kenjis authored Sep 21, 2022
2 parents f2e2b57 + 980dc30 commit c648852
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Config/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,7 @@ class Settings
public $database = [
'class' => DatabaseHandler::class,
'table' => 'settings',
'group' => null,
'writeable' => true,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'], [
Expand Down
23 changes: 12 additions & 11 deletions src/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeIgniter\Settings\Handlers;

use CodeIgniter\Database\BaseBuilder;
use CodeIgniter\I18n\Time;
use RuntimeException;

Expand All @@ -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.
Expand All @@ -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']);
}

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)) {
Expand All @@ -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) {
Expand Down
38 changes: 38 additions & 0 deletions tests/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CodeIgniter\I18n\Time;
use CodeIgniter\Settings\Settings;
use CodeIgniter\Test\DatabaseTestTrait;
use InvalidArgumentException;
use Tests\Support\TestCase;

/**
Expand All @@ -22,6 +23,11 @@ final class DatabaseHandlerTest extends TestCase
*/
protected $table;

/**
* @var string
*/
protected $group;

/**
* Ensures we are using the database handler.
*/
Expand All @@ -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()
Expand All @@ -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);
Expand Down

0 comments on commit c648852

Please sign in to comment.