1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Connection\ConnectionInterface; |
6
|
|
|
use Bdf\Prime\Connection\ConnectionRegistry; |
7
|
|
|
use Bdf\Prime\Connection\SubConnectionManagerInterface; |
8
|
|
|
use Bdf\Prime\Exception\DBALException; |
|
|
|
|
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* ConnectionManager |
13
|
|
|
* |
14
|
|
|
* doctrine dbal connection registry |
|
|
|
|
15
|
|
|
*/ |
16
|
|
|
class ConnectionManager implements ConnectionRegistryInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The connection registry |
20
|
|
|
* |
21
|
|
|
* @var ConnectionRegistryInterface |
22
|
|
|
*/ |
23
|
|
|
private $registry; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Connections list |
27
|
|
|
* |
28
|
|
|
* @var ConnectionInterface[] |
29
|
|
|
*/ |
30
|
|
|
private $connections = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Default connection to use |
34
|
|
|
* |
35
|
|
|
* @var string |
|
|
|
|
36
|
|
|
*/ |
37
|
|
|
private $defaultConnection; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Set default configuration |
42
|
|
|
* |
43
|
|
|
* @param ConnectionRegistryInterface $registry |
44
|
|
|
*/ |
45
|
180 |
|
public function __construct(ConnectionRegistryInterface $registry = null) |
|
|
|
|
46
|
|
|
{ |
47
|
180 |
|
$this->registry = $registry ?: new ConnectionRegistry(); |
|
|
|
|
48
|
180 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Add database connection |
52
|
|
|
* |
53
|
|
|
* @param ConnectionInterface $connection Unique name for the connection |
54
|
|
|
* @param boolean $default Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false. |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @throws LogicException if connection exists |
|
|
|
|
57
|
|
|
*/ |
58
|
145 |
|
public function addConnection(/* ConnectionInterface */ $connection, /* bool */ $default = false) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
// Legacy |
61
|
145 |
|
if (!$connection instanceof ConnectionInterface) { |
|
|
|
|
62
|
12 |
|
@trigger_error(__METHOD__.' signature change. Use addParameter to add lazy loading conenction info.', E_USER_DEPRECATED); |
|
|
|
|
63
|
|
|
|
64
|
12 |
|
$this->declareConnection($connection, $default); |
65
|
|
|
|
66
|
12 |
|
return $this->getConnection($connection); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Connection name must be unique |
70
|
145 |
|
if (isset($this->connections[$connection->getName()])) { |
71
|
1 |
|
throw new LogicException('Connection for "'.$connection->getName().'" already exists. Connection name must be unique.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Set as default connection? |
|
|
|
|
75
|
145 |
|
if (true === $default || null === $this->defaultConnection) { |
76
|
131 |
|
$this->defaultConnection = $connection->getName(); |
77
|
|
|
} |
78
|
|
|
|
79
|
145 |
|
$this->connections[$connection->getName()] = $connection; |
80
|
145 |
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Remove a connection by its name |
84
|
|
|
* |
85
|
|
|
* @param string $name |
86
|
|
|
*/ |
87
|
34 |
|
public function removeConnection(string $name) |
88
|
|
|
{ |
89
|
34 |
|
if (!isset($this->connections[$name])) { |
90
|
22 |
|
return; |
91
|
|
|
} |
92
|
|
|
|
93
|
12 |
|
$this->connections[$name]->close(); |
|
|
|
|
94
|
12 |
|
unset($this->connections[$name]); |
95
|
12 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get connection by name |
99
|
|
|
* |
100
|
|
|
* @param string $name Unique name of the connection to be returned |
101
|
|
|
* |
102
|
|
|
* @return ConnectionInterface |
103
|
|
|
* |
104
|
|
|
* @throws DBALException |
|
|
|
|
105
|
|
|
* |
106
|
|
|
* @deprecated Since 1.1 use getConnection |
|
|
|
|
107
|
|
|
*/ |
108
|
|
|
public function connection(string $name = null): ConnectionInterface |
109
|
|
|
{ |
110
|
|
|
return $this->getConnection($name); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
|
|
|
|
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
1073 |
|
public function getConnection(string $name = null): ConnectionInterface |
117
|
|
|
{ |
118
|
1073 |
|
if ($name === null) { |
119
|
6 |
|
$name = $this->defaultConnection; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// Connection name must be unique |
123
|
1073 |
|
if (!isset($this->connections[$name]) && !$this->loadSubConnection($name)) { |
124
|
140 |
|
$this->addConnection($this->registry->getConnection($name)); |
125
|
|
|
} |
126
|
|
|
|
127
|
1072 |
|
return $this->connections[$name]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Associate configuration to connection |
132
|
|
|
* |
133
|
|
|
* @param string $connectionName |
134
|
|
|
* @param string|array $parameters |
135
|
|
|
*/ |
136
|
35 |
|
public function declareConnection(string $connectionName, $parameters) |
137
|
|
|
{ |
138
|
35 |
|
if ($this->registry instanceof ConnectionRegistry) { |
139
|
35 |
|
$this->registry->declareConnection($connectionName, $parameters); |
140
|
|
|
} |
141
|
35 |
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get global config |
145
|
|
|
* |
146
|
|
|
* @return Configuration |
147
|
|
|
* |
148
|
|
|
* @deprecated Every connection should have its config. |
|
|
|
|
149
|
|
|
*/ |
150
|
147 |
|
public function config(): Configuration |
151
|
|
|
{ |
152
|
147 |
|
@trigger_error(__METHOD__.' is deprecated since 1.1 and will be removed in 1.2. Every connection should have its config.', E_USER_DEPRECATED); |
|
|
|
|
153
|
|
|
|
154
|
147 |
|
if ($this->registry instanceof ConnectionRegistry) { |
155
|
147 |
|
return $this->registry->getDefaultConfiguration(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return new Configuration(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get all connections |
163
|
|
|
* |
164
|
|
|
* @return ConnectionInterface[] Array of connection objects |
165
|
|
|
*/ |
166
|
1104 |
|
public function connections() |
167
|
|
|
{ |
168
|
1104 |
|
return $this->connections; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritDoc} |
173
|
|
|
*/ |
174
|
3 |
|
public function getConnectionNames(): array |
175
|
|
|
{ |
176
|
3 |
|
return array_unique(array_merge($this->getCurrentConnectionNames(), $this->registry->getConnectionNames())); |
177
|
|
|
} |
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get the loaded connection name |
180
|
|
|
* |
181
|
|
|
* @return string[] Array of connection name |
182
|
|
|
* |
183
|
|
|
* @deprecated Since 1.1 use getConnectionNames |
|
|
|
|
184
|
|
|
*/ |
185
|
1 |
|
public function connectionNames(): array |
186
|
|
|
{ |
187
|
1 |
|
return $this->getConnectionNames(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the name of connections in progress |
192
|
|
|
*/ |
193
|
3 |
|
public function getCurrentConnectionNames(): array |
194
|
|
|
{ |
195
|
3 |
|
return array_keys($this->connections); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set the default connection name |
200
|
|
|
* |
201
|
|
|
* @param string $name |
202
|
|
|
*/ |
203
|
1 |
|
public function setDefaultConnection($name) |
204
|
|
|
{ |
205
|
1 |
|
$this->defaultConnection = $name; |
206
|
1 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Get the default connection name |
210
|
|
|
* |
211
|
|
|
* @return string |
212
|
|
|
*/ |
213
|
2 |
|
public function getDefaultConnection() |
214
|
|
|
{ |
215
|
2 |
|
return $this->defaultConnection; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Try to load a sub connection |
220
|
|
|
* |
221
|
|
|
* This method allows connection as "name.otherName". |
222
|
|
|
* Works only if connection "name" is a SubConnectionManagerInterface. |
223
|
|
|
* |
224
|
|
|
* @param string $connectionName |
225
|
|
|
* |
226
|
|
|
* @return bool The connection has been loaded |
|
|
|
|
227
|
|
|
*/ |
228
|
140 |
|
private function loadSubConnection($connectionName) |
|
|
|
|
229
|
|
|
{ |
230
|
140 |
|
$names = explode('.', $connectionName, 2); |
231
|
|
|
|
232
|
140 |
|
if (!isset($names[1])) { |
233
|
140 |
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
2 |
|
$connection = $this->getConnection($names[0]); |
237
|
|
|
|
238
|
2 |
|
if ($connection instanceof SubConnectionManagerInterface) { |
239
|
|
|
//TODO doit on concerver une reference sur la sous connection ? |
|
|
|
|
240
|
2 |
|
$this->connections[$connectionName] = $connection->getConnection($names[1]); |
241
|
2 |
|
return true; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|