1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @see http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace JAQB; |
13
|
|
|
|
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use JAQB\Exception\JAQBException; |
16
|
|
|
use PDO; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This class manages one or more PDO database connections. |
20
|
|
|
*/ |
21
|
|
|
class ConnectionManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $config; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $connections = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private static $connectionParams = [ |
37
|
|
|
'host' => 'host', |
38
|
|
|
'port' => 'port', |
39
|
|
|
'name' => 'dbname', |
40
|
|
|
'charset' => 'charset', |
41
|
|
|
]; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $config |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $config = []) |
47
|
|
|
{ |
48
|
|
|
$this->config = $config; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Gets a database connection by ID. |
53
|
|
|
* |
54
|
|
|
* @param string $id |
55
|
|
|
* |
56
|
|
|
* @throws JAQBException if the connection does not exist |
57
|
|
|
* |
58
|
|
|
* @return QueryBuilder |
59
|
|
|
*/ |
60
|
|
|
public function get($id) |
61
|
|
|
{ |
62
|
|
|
if (isset($this->connections[$id])) { |
63
|
|
|
return $this->connections[$id]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!isset($this->config[$id])) { |
67
|
|
|
throw new JAQBException('No configuration or connection has been supplied for the ID "'.$id.'".'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->connections[$id] = $this->buildFromConfig($this->config[$id], $id); |
71
|
|
|
|
72
|
|
|
return $this->connections[$id]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Gets the default database connection. |
77
|
|
|
* |
78
|
|
|
* @throws JAQBException if there is not a default connection |
79
|
|
|
* |
80
|
|
|
* @return QueryBuilder |
81
|
|
|
*/ |
82
|
|
|
public function getDefault() |
83
|
|
|
{ |
84
|
|
|
if (count($this->connections) === 1) { |
85
|
|
|
return reset($this->connections); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
throw new JAQBException('There is no default connection.'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Adds a connection. |
93
|
|
|
* |
94
|
|
|
* @param string $id |
95
|
|
|
* @param QueryBuilder $connection |
96
|
|
|
* |
97
|
|
|
* @throws InvalidArgumentException if a connection with the given ID already exists |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function add($id, QueryBuilder $connection) |
102
|
|
|
{ |
103
|
|
|
if (isset($this->connections[$id])) { |
104
|
|
|
throw new InvalidArgumentException('A connection with the ID "'.$id.'" already exists.'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->connections[$id] = $connection; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Builds a new query builder instance from a configuration. |
114
|
|
|
* NOTE: This is not intended to be used outside of this class. |
115
|
|
|
* |
116
|
|
|
* @param array $config |
117
|
|
|
* @param string $id |
118
|
|
|
* |
119
|
|
|
* @return QueryBuilder |
120
|
|
|
*/ |
121
|
|
|
public function buildFromConfig(array $config, $id) |
122
|
|
|
{ |
123
|
|
|
// generate the dsn needed for PDO |
124
|
|
|
if (isset($config['dsn'])) { |
125
|
|
|
$dsn = $config['dsn']; |
126
|
|
|
} else { |
127
|
|
|
$dsn = $this->buildDsn($config, $id); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$user = isset($config['user']) ? $config['user'] : null; |
131
|
|
|
$password = isset($config['password']) ? $config['password'] : null; |
132
|
|
|
|
133
|
|
|
$options = []; |
134
|
|
|
if (isset($config['errorMode'])) { |
135
|
|
|
$options[PDO::ATTR_ERRMODE] = $config['errorMode']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$pdo = new PDO($dsn, $user, $password, $options); |
139
|
|
|
|
140
|
|
|
return new QueryBuilder($pdo); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Builds a PDO DSN string from a JAQB connection configuration. |
145
|
|
|
* |
146
|
|
|
* @param array $config |
147
|
|
|
* @param string $id configuration ID |
148
|
|
|
* |
149
|
|
|
* @throws JAQBException if the configuration is invalid |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function buildDsn(array $config, $id) |
154
|
|
|
{ |
155
|
|
|
if (!isset($config['type'])) { |
156
|
|
|
throw new JAQBException('Missing connection type for configuration "'.$id.'"!'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$dsn = $config['type'].':'; |
160
|
|
|
$params = []; |
161
|
|
|
foreach (self::$connectionParams as $j => $k) { |
162
|
|
|
if (isset($config[$j])) { |
163
|
|
|
$params[] = $k.'='.$config[$j]; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
$dsn .= implode(';', $params); |
167
|
|
|
|
168
|
|
|
return $dsn; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|