-
Notifications
You must be signed in to change notification settings - Fork 7
/
ConnectionManager.php
214 lines (197 loc) · 7.19 KB
/
ConnectionManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.10.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Datasource;
use Cake\Core\StaticConfigTrait;
use Cake\Database\Connection;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Postgres;
use Cake\Database\Driver\Sqlite;
use Cake\Database\Driver\Sqlserver;
use Cake\Datasource\Exception\MissingDatasourceConfigException;
/**
* Manages and loads instances of Connection
*
* Provides an interface to loading and creating connection objects. Acts as
* a registry for the connections defined in an application.
*
* Provides an interface for loading and enumerating connections defined in
* config/app.php
*/
class ConnectionManager
{
use StaticConfigTrait {
setConfig as protected _setConfig;
parseDsn as protected _parseDsn;
}
/**
* A map of connection aliases.
*
* @var string[]
*/
protected static $_aliasMap = [];
/**
* An array mapping url schemes to fully qualified driver class names
*
* @var string[]
* @psalm-var array<string, class-string>
*/
protected static $_dsnClassMap = [
'mysql' => Mysql::class,
'postgres' => Postgres::class,
'sqlite' => Sqlite::class,
'sqlserver' => Sqlserver::class,
];
/**
* The ConnectionRegistry used by the manager.
*
* @var \Cake\Datasource\ConnectionRegistry
*/
protected static $_registry;
/**
* Configure a new connection object.
*
* The connection will not be constructed until it is first used.
*
* @param string|array $key The name of the connection config, or an array of multiple configs.
* @param array|null $config An array of name => config data for adapter.
* @return void
* @throws \Cake\Core\Exception\CakeException When trying to modify an existing config.
* @see \Cake\Core\StaticConfigTrait::config()
*/
public static function setConfig($key, $config = null): void
{
if (is_array($config)) {
$config['name'] = $key;
}
static::_setConfig($key, $config);
}
/**
* Parses a DSN into a valid connection configuration
*
* This method allows setting a DSN using formatting similar to that used by PEAR::DB.
* The following is an example of its usage:
*
* ```
* $dsn = 'mysql://user:pass@localhost/database';
* $config = ConnectionManager::parseDsn($dsn);
*
* $dsn = 'Cake\Database\Driver\Mysql://localhost:3306/database?className=Cake\Database\Connection';
* $config = ConnectionManager::parseDsn($dsn);
*
* $dsn = 'Cake\Database\Connection://localhost:3306/database?driver=Cake\Database\Driver\Mysql';
* $config = ConnectionManager::parseDsn($dsn);
* ```
*
* For all classes, the value of `scheme` is set as the value of both the `className` and `driver`
* unless they have been otherwise specified.
*
* Note that query-string arguments are also parsed and set as values in the returned configuration.
*
* @param string $config The DSN string to convert to a configuration array
* @return array The configuration array to be stored after parsing the DSN
*/
public static function parseDsn(string $config): array
{
$config = static::_parseDsn($config);
if (isset($config['path']) && empty($config['database'])) {
$config['database'] = substr($config['path'], 1);
}
if (empty($config['driver'])) {
$config['driver'] = $config['className'];
$config['className'] = Connection::class;
}
unset($config['path']);
return $config;
}
/**
* Set one or more connection aliases.
*
* Connection aliases allow you to rename active connections without overwriting
* the aliased connection. This is most useful in the test-suite for replacing
* connections with their test variant.
*
* Defined aliases will take precedence over normal connection names. For example,
* if you alias 'default' to 'test', fetching 'default' will always return the 'test'
* connection as long as the alias is defined.
*
* You can remove aliases with ConnectionManager::dropAlias().
*
* ### Usage
*
* ```
* // Make 'things' resolve to 'test_things' connection
* ConnectionManager::alias('test_things', 'things');
* ```
*
* @param string $alias The alias to add. Fetching $source will return $alias when loaded with get.
* @param string $source The connection to add an alias to.
* @return void
* @throws \Cake\Datasource\Exception\MissingDatasourceConfigException When aliasing a
* connection that does not exist.
*/
public static function alias(string $alias, string $source): void
{
if (empty(static::$_config[$source]) && empty(static::$_config[$alias])) {
throw new MissingDatasourceConfigException(
sprintf('Cannot create alias of "%s" as it does not exist.', $alias)
);
}
static::$_aliasMap[$source] = $alias;
}
/**
* Drop an alias.
*
* Removes an alias from ConnectionManager. Fetching the aliased
* connection may fail if there is no other connection with that name.
*
* @param string $name The connection name to remove aliases for.
* @return void
*/
public static function dropAlias(string $name): void
{
unset(static::$_aliasMap[$name]);
}
/**
* Get a connection.
*
* If the connection has not been constructed an instance will be added
* to the registry. This method will use any aliases that have been
* defined. If you want the original unaliased connections pass `false`
* as second parameter.
*
* @param string $name The connection name.
* @param bool $useAliases Set to false to not use aliased connections.
* @return \Cake\Datasource\ConnectionInterface A connection object.
* @throws \Cake\Datasource\Exception\MissingDatasourceConfigException When config
* data is missing.
*/
public static function get(string $name, bool $useAliases = true)
{
if ($useAliases && isset(static::$_aliasMap[$name])) {
$name = static::$_aliasMap[$name];
}
if (empty(static::$_config[$name])) {
throw new MissingDatasourceConfigException(['name' => $name]);
}
if (empty(static::$_registry)) {
static::$_registry = new ConnectionRegistry();
}
if (isset(static::$_registry->{$name})) {
return static::$_registry->{$name};
}
return static::$_registry->load($name, static::$_config[$name]);
}
}