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

codeigniter4 / CodeIgniter4 / 21262059095

22 Jan 2026 07:29PM UTC coverage: 85.402% (-0.1%) from 85.526%
21262059095

Pull #9889

github

web-flow
Merge e55be0bba into c7e784fe4
Pull Request #9889: feat: FrankenPHP Worker Mode

154 of 238 new or added lines in 19 files covered. (64.71%)

1 existing line in 1 file now uncovered.

22119 of 25900 relevant lines covered (85.4%)

205.29 hits per line

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

97.37
/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) {
908✔
54
            return $group;
814✔
55
        }
56

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

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

67
            assert(is_string($group));
68

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

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

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

80
        static::ensureFactory();
61✔
81

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

84
        if ($getShared) {
61✔
85
            static::$instances[$group] = $connection;
27✔
86
        }
87

88
        return $connection;
61✔
89
    }
90

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

99
    /**
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.
102
     *
103
     * @param array|ConnectionInterface|string|null $group
104
     *
105
     * @return Forge
106
     */
107
    public static function forge($group = null)
108
    {
109
        $db = static::connect($group);
821✔
110

111
        return static::$factory->loadForge($db);
821✔
112
    }
113

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

125
        return static::$factory->loadUtils($db);
2✔
126
    }
127

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

139
        return new Seeder($config, static::connect($group));
803✔
140
    }
141

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

153
        static::$factory = new Database();
10✔
154
    }
155

156
    /**
157
     * Reconnect database connections for worker mode at the start of a request.
158
     *
159
     * This should be called at the beginning of each request in worker mode,
160
     * before the application runs.
161
     */
162
    public static function reconnectForWorkerMode(): void
163
    {
164
        if (static::$instances === []) {
1✔
NEW
165
            return;
×
166
        }
167

168
        foreach (static::$instances as $connection) {
1✔
169
            $connection->reconnect();
1✔
170
        }
171
    }
172

173
    /**
174
     * Cleanup database connections for worker mode.
175
     *
176
     * Rolls back any uncommitted transactions and resets transaction status
177
     * to ensure a clean state for the next request.
178
     *
179
     * Uncommitted transactions at this point indicate a bug in the
180
     * application code (transactions should be completed before request ends).
181
     *
182
     * Called at the END of each request to clean up state.
183
     */
184
    public static function cleanupForWorkerMode(): void
185
    {
186
        foreach (static::$instances as $group => $connection) {
1✔
187
            if ($connection->transDepth > 0) {
1✔
188
                log_message('error', "Uncommitted transaction detected in database group '{$group}'. Transactions must be completed before request ends.");
1✔
189

190
                while ($connection->transDepth > 0) {
1✔
191
                    $connection->transRollback();
1✔
192
                }
193
            }
194

195
            $connection->resetTransStatus();
1✔
196
        }
197
    }
198
}
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