Skip to content

Commit

Permalink
Allow db forge and utils to take an array of connection info instead …
Browse files Browse the repository at this point in the history
…of a group name. Fixes #1605
  • Loading branch information
lonnieezell committed Dec 17, 2018
1 parent 24ded3a commit 6b8b8b4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
50 changes: 41 additions & 9 deletions system/Database/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,22 @@ public static function getConnections()
* Loads and returns an instance of the Forge for the specified
* database group, and loads the group if it hasn't been loaded yet.
*
* @param string|null $group
* @param string|array|null $group
*
* @return Forge
*/
public static function forge(string $group = null)
public static function forge($group = null)
{
$config = new \Config\Database();
// Allow custom connections to be sent in
if (is_array($group))
{
$config = $group;
$group = 'custom-' . md5(json_encode($config));
}
else
{
$config = config('Database');
}

static::ensureFactory();

Expand All @@ -144,14 +153,21 @@ public static function forge(string $group = null)
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
}

if (! isset($config->$group))
if (is_string($group) && ! isset($config->$group) && ! is_array($config))
{
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
}

if (! isset(static::$instances[$group]))
{
$db = static::connect($group);
if (is_array($config))
{
$db = static::connect($config);
}
else
{
$db = static::connect($group);
}
}
else
{
Expand All @@ -172,23 +188,39 @@ public static function forge(string $group = null)
*/
public static function utils(string $group = null)
{
$config = new \Config\Database();
// Allow custom connections to be sent in
if (is_array($group))
{
$config = $group;
$group = 'custom-' . md5(json_encode($config));
}
else
{
$config = config('Database');
}

static::ensureFactory();

if (empty($group))
{
$group = $config->defaultGroup;
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
}

if (! isset($config->group))
if (is_string($group) && ! isset($config->$group) && ! is_array($config))
{
throw new \InvalidArgumentException($group . ' is not a valid database connection group.');
}

if (! isset(static::$instances[$group]))
{
$db = static::connect($group);
if (is_array($config))
{
$db = static::connect($config);
}
else
{
$db = static::connect($group);
}
}
else
{
Expand Down
11 changes: 11 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace CodeIgniter\Database\Live;

use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Forge;
use CodeIgniter\Test\CIDatabaseTestCase;

/**
Expand Down Expand Up @@ -359,4 +360,14 @@ public function testCreateTableWithArrayFieldConstraints()
$this->expectNotToPerformAssertions();
}
}

public function testConnectWithArrayGroup()
{
$group = config('Database');
$group = $group->tests;

$forge = \Config\Database::forge($group);

$this->assertInstanceOf(Forge::class, $forge);
}
}

0 comments on commit 6b8b8b4

Please sign in to comment.