ci4

Форк
0
/
Config.php 
153 строки · 3.8 Кб
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 Config\Database as DbConfig;
18
use InvalidArgumentException;
19

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

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

43
    /**
44
     * Returns the database connection
45
     *
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.
49
     *
50
     * @return BaseConnection
51
     */
52
    public static function connect($group = null, bool $getShared = true)
53
    {
54
        // If a DB connection is passed in, just pass it back
55
        if ($group instanceof BaseConnection) {
56
            return $group;
57
        }
58

59
        if (is_array($group)) {
60
            $config = $group;
61
            $group  = 'custom-' . md5(json_encode($config));
62
        } else {
63
            $dbConfig = config(DbConfig::class);
64

65
            if ($group === null) {
66
                $group = (ENVIRONMENT === 'testing') ? 'tests' : $dbConfig->defaultGroup;
67
            }
68

69
            assert(is_string($group));
70

71
            if (! isset($dbConfig->{$group})) {
72
                throw new InvalidArgumentException('"' . $group . '" is not a valid database connection group.');
73
            }
74

75
            $config = $dbConfig->{$group};
76
        }
77

78
        if ($getShared && isset(static::$instances[$group])) {
79
            return static::$instances[$group];
80
        }
81

82
        static::ensureFactory();
83

84
        $connection = static::$factory->load($config, $group);
85

86
        static::$instances[$group] = $connection;
87

88
        return $connection;
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;
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);
110

111
        return static::$factory->loadForge($db);
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);
124

125
        return static::$factory->loadUtils($db);
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);
138

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

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

151
        static::$factory = new Database();
152
    }
153
}
154

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.