forked from php-activerecord/activerecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionManager.php
More file actions
54 lines (47 loc) · 1.25 KB
/
ConnectionManager.php
File metadata and controls
54 lines (47 loc) · 1.25 KB
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
<?php
/**
* @package ActiveRecord
*/
namespace ActiveRecord;
/**
* Singleton to manage any and all database connections.
*
* @package ActiveRecord
*/
class ConnectionManager extends Singleton
{
/**
* Array of {@link Connection} objects.
*
* @var array<string,Connection>
*/
private static array $connections = [];
/**
* If $name is null then the default connection will be returned.
*
* @see Config
*
* @param string $name Optional name of a connection
*
* @return Connection
*/
public static function get_connection(?string $name=null)
{
$config = Config::instance();
$name = $name ?? $config->get_default_connection();
if (!isset(self::$connections[$name]->connection)) {
self::$connections[$name] = Connection::instance($config->get_connection($name));
}
return self::$connections[$name];
}
/**
* Drops the connection from the connection manager. Does not actually close it since there
* is no close method in PDO.
*
* @param string $name Name of the connection to forget about
*/
public static function drop_connection(string $name): void
{
unset(self::$connections[$name]);
}
}