• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 14575173072

21 Apr 2025 02:26PM UTC coverage: 84.402% (+0.007%) from 84.395%
14575173072

Pull #9528

github

web-flow
Merge 681f2d691 into 3d3ba0512
Pull Request #9528: feat: add Time::addCalendarMonths() function

11 of 11 new or added lines in 1 file covered. (100.0%)

136 existing lines in 21 files now uncovered.

20827 of 24676 relevant lines covered (84.4%)

191.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/system/Database/Config.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Database;
15

16
use CodeIgniter\Config\BaseConfig;
17
use CodeIgniter\Exceptions\InvalidArgumentException;
18
use Config\Database as DbConfig;
19

20
/**
21
 * @see \CodeIgniter\Database\ConfigTest
22
 */
23
class Config extends BaseConfig
24
{
25
    /**
26
     * Cache for instance of any connections that
27
     * have been requested as a "shared" instance.
28
     *
29
     * @var array
30
     */
31
    protected static $instances = [];
32

33
    /**
34
     * The main instance used to manage all of
35
     * our open database connections.
36
     *
37
     * @var Database|null
38
     */
39
    protected static $factory;
40

41
    /**
42
     * Returns the database connection
43
     *
44
     * @param array|BaseConnection|non-empty-string|null $group     The name of the connection group to use,
45
     *                                                              or an array of configuration settings.
46
     * @param bool                                       $getShared Whether to return a shared instance of the connection.
47
     *
48
     * @return BaseConnection
49
     */
50
    public static function connect($group = null, bool $getShared = true)
51
    {
52
        // If a DB connection is passed in, just pass it back
53
        if ($group instanceof BaseConnection) {
810✔
54
            return $group;
720✔
55
        }
56

57
        if (is_array($group)) {
803✔
58
            $config = $group;
40✔
59
            $group  = 'custom-' . md5(json_encode($config));
40✔
60
        } else {
61
            $dbConfig = config(DbConfig::class);
794✔
62

63
            if ($group === null) {
794✔
64
                $group = (ENVIRONMENT === 'testing') ? 'tests' : $dbConfig->defaultGroup;
790✔
65
            }
66

67
            assert(is_string($group));
68

69
            if (! isset($dbConfig->{$group})) {
794✔
70
                throw new InvalidArgumentException('"' . $group . '" is not a valid database connection group.');
3✔
71
            }
72

73
            $config = $dbConfig->{$group};
794✔
74
        }
75

76
        if ($getShared && isset(static::$instances[$group])) {
803✔
77
            return static::$instances[$group];
789✔
78
        }
79

80
        static::ensureFactory();
53✔
81

82
        $connection = static::$factory->load($config, $group);
53✔
83

84
        static::$instances[$group] = $connection;
53✔
85

86
        return $connection;
53✔
87
    }
88

89
    /**
90
     * Returns an array of all db connections currently made.
91
     */
92
    public static function getConnections(): array
93
    {
94
        return static::$instances;
86✔
95
    }
96

97
    /**
98
     * Loads and returns an instance of the Forge for the specified
99
     * database group, and loads the group if it hasn't been loaded yet.
100
     *
101
     * @param array|ConnectionInterface|string|null $group
102
     *
103
     * @return Forge
104
     */
105
    public static function forge($group = null)
106
    {
107
        $db = static::connect($group);
725✔
108

109
        return static::$factory->loadForge($db);
725✔
110
    }
111

112
    /**
113
     * Returns a new instance of the Database Utilities class.
114
     *
115
     * @param array|string|null $group
116
     *
117
     * @return BaseUtils
118
     */
119
    public static function utils($group = null)
120
    {
UNCOV
121
        $db = static::connect($group);
×
122

123
        return static::$factory->loadUtils($db);
×
124
    }
125

126
    /**
127
     * Returns a new instance of the Database Seeder.
128
     *
129
     * @param non-empty-string|null $group
130
     *
131
     * @return Seeder
132
     */
133
    public static function seeder(?string $group = null)
134
    {
135
        $config = config(DbConfig::class);
710✔
136

137
        return new Seeder($config, static::connect($group));
710✔
138
    }
139

140
    /**
141
     * Ensures the database Connection Manager/Factory is loaded and ready to use.
142
     *
143
     * @return void
144
     */
145
    protected static function ensureFactory()
146
    {
147
        if (static::$factory instanceof Database) {
53✔
148
            return;
45✔
149
        }
150

151
        static::$factory = new Database();
9✔
152
    }
153
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc