3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
14
namespace CodeIgniter\Database;
16
use CodeIgniter\Config\BaseConfig;
17
use Config\Database as DbConfig;
18
use InvalidArgumentException;
23
* @see \CodeIgniter\Database\ConfigTest
25
class Config extends BaseConfig
28
* Cache for instance of any connections that
29
* have been requested as a "shared" instance.
33
protected static $instances = [];
36
* The main instance used to manage all of
37
* our open database connections.
41
protected static $factory;
44
* Returns the database connection
46
* @param array|BaseConnection|non-empty-string|null $group The name of the connection group to use,
47
* or an array of configuration settings.
48
* @param bool $getShared Whether to return a shared instance of the connection.
50
* @return BaseConnection
52
public static function connect($group = null, bool $getShared = true)
54
// If a DB connection is passed in, just pass it back
55
if ($group instanceof BaseConnection) {
59
if (is_array($group)) {
61
$group = 'custom-' . md5(json_encode($config));
63
$dbConfig = config(DbConfig::class);
65
if ($group === null) {
66
$group = (ENVIRONMENT === 'testing') ? 'tests' : $dbConfig->defaultGroup;
69
assert(is_string($group));
71
if (! isset($dbConfig->{$group})) {
72
throw new InvalidArgumentException('"' . $group . '" is not a valid database connection group.');
75
$config = $dbConfig->{$group};
78
if ($getShared && isset(static::$instances[$group])) {
79
return static::$instances[$group];
82
static::ensureFactory();
84
$connection = static::$factory->load($config, $group);
86
static::$instances[$group] = $connection;
92
* Returns an array of all db connections currently made.
94
public static function getConnections(): array
96
return static::$instances;
100
* Loads and returns an instance of the Forge for the specified
101
* database group, and loads the group if it hasn't been loaded yet.
103
* @param array|ConnectionInterface|string|null $group
107
public static function forge($group = null)
109
$db = static::connect($group);
111
return static::$factory->loadForge($db);
115
* Returns a new instance of the Database Utilities class.
117
* @param array|string|null $group
121
public static function utils($group = null)
123
$db = static::connect($group);
125
return static::$factory->loadUtils($db);
129
* Returns a new instance of the Database Seeder.
131
* @param non-empty-string|null $group
135
public static function seeder(?string $group = null)
137
$config = config(DbConfig::class);
139
return new Seeder($config, static::connect($group));
143
* Ensures the database Connection Manager/Factory is loaded and ready to use.
145
protected static function ensureFactory()
147
if (static::$factory instanceof Database) {
151
static::$factory = new Database();