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
|
198 |
|
public function __construct(ConnectionRegistryInterface $registry = null) |
|
|
|
|
46
|
|
|
{ |
47
|
198 |
|
$this->registry = $registry ?: new ConnectionRegistry(); |
|
|
|
|
48
|
198 |
|
} |
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
|
167 |
|
public function addConnection(ConnectionInterface $connection, bool $default = false) |
59
|
|
|
{ |
60
|
|
|
// Connection name must be unique |
61
|
167 |
|
if (isset($this->connections[$connection->getName()])) { |
62
|
1 |
|
throw new LogicException('Connection for "'.$connection->getName().'" already exists. Connection name must be unique.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Set as default connection? |
66
|
167 |
|
if (true === $default || null === $this->defaultConnection) { |
67
|
151 |
|
$this->defaultConnection = $connection->getName(); |
68
|
|
|
} |
69
|
|
|
|
70
|
167 |
|
$this->connections[$connection->getName()] = $connection; |
71
|
167 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove a connection by its name |
75
|
|
|
* |
76
|
|
|
* @param string $name |
77
|
|
|
*/ |
78
|
40 |
|
public function removeConnection(string $name) |
79
|
|
|
{ |
80
|
40 |
|
if (!isset($this->connections[$name])) { |
81
|
26 |
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
14 |
|
$this->connections[$name]->close(); |
|
|
|
|
85
|
14 |
|
unset($this->connections[$name]); |
86
|
14 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get connection by name |
90
|
|
|
* |
91
|
|
|
* @param string $name Unique name of the connection to be returned |
92
|
|
|
* |
93
|
|
|
* @return ConnectionInterface |
94
|
|
|
* |
95
|
|
|
* @throws DBALException |
96
|
|
|
* |
97
|
|
|
* @deprecated Since 1.1 use getConnection |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
public function connection(string $name = null): ConnectionInterface |
100
|
|
|
{ |
101
|
|
|
return $this->getConnection($name); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
|
|
|
|
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
1118 |
|
public function getConnection(string $name = null): ConnectionInterface |
108
|
|
|
{ |
109
|
1118 |
|
if ($name === null) { |
110
|
6 |
|
$name = $this->defaultConnection; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Connection name must be unique |
114
|
1118 |
|
if (!isset($this->connections[$name]) && !$this->loadSubConnection($name)) { |
115
|
162 |
|
$this->addConnection($this->registry->getConnection($name)); |
116
|
|
|
} |
117
|
|
|
|
118
|
1117 |
|
return $this->connections[$name]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Associate configuration to connection |
123
|
|
|
* |
124
|
|
|
* @param string $connectionName |
125
|
|
|
* @param string|array $parameters |
126
|
|
|
*/ |
127
|
41 |
|
public function declareConnection(string $connectionName, $parameters) |
128
|
|
|
{ |
129
|
41 |
|
if ($this->registry instanceof ConnectionRegistry) { |
130
|
41 |
|
$this->registry->declareConnection($connectionName, $parameters); |
131
|
|
|
} |
132
|
41 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get global config |
136
|
|
|
* |
137
|
|
|
* @return Configuration |
138
|
|
|
* |
139
|
|
|
* @deprecated Every connection should have its config. |
|
|
|
|
140
|
|
|
*/ |
141
|
134 |
|
public function config(): Configuration |
142
|
|
|
{ |
143
|
134 |
|
@trigger_error(__METHOD__.' is deprecated since 1.1 and will be removed in 1.2. Every connection should have its config.', E_USER_DEPRECATED); |
|
|
|
|
144
|
|
|
|
145
|
134 |
|
if ($this->registry instanceof ConnectionRegistry) { |
146
|
134 |
|
return $this->registry->getDefaultConfiguration(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return new Configuration(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get all connections |
154
|
|
|
* |
155
|
|
|
* @return ConnectionInterface[] Array of connection objects |
156
|
|
|
*/ |
157
|
1149 |
|
public function connections() |
158
|
|
|
{ |
159
|
1149 |
|
return $this->connections; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritDoc} |
164
|
|
|
*/ |
165
|
3 |
|
public function getConnectionNames(): array |
166
|
|
|
{ |
167
|
3 |
|
return array_unique(array_merge($this->getCurrentConnectionNames(), $this->registry->getConnectionNames())); |
168
|
|
|
} |
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get the loaded connection name |
171
|
|
|
* |
172
|
|
|
* @return string[] Array of connection name |
173
|
|
|
* |
174
|
|
|
* @deprecated Since 1.1 use getConnectionNames |
|
|
|
|
175
|
|
|
*/ |
176
|
1 |
|
public function connectionNames(): array |
177
|
|
|
{ |
178
|
1 |
|
return $this->getConnectionNames(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Gets the name of connections in progress |
183
|
|
|
*/ |
184
|
3 |
|
public function getCurrentConnectionNames(): array |
185
|
|
|
{ |
186
|
3 |
|
return array_keys($this->connections); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set the default connection name |
191
|
|
|
* |
192
|
|
|
* @param string $name |
193
|
|
|
*/ |
194
|
1 |
|
public function setDefaultConnection($name) |
195
|
|
|
{ |
196
|
1 |
|
$this->defaultConnection = $name; |
197
|
1 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get the default connection name |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
2 |
|
public function getDefaultConnection() |
205
|
|
|
{ |
206
|
2 |
|
return $this->defaultConnection; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Try to load a sub connection |
211
|
|
|
* |
212
|
|
|
* This method allows connection as "name.otherName". |
213
|
|
|
* Works only if connection "name" is a SubConnectionManagerInterface. |
214
|
|
|
* |
215
|
|
|
* @param string $connectionName |
216
|
|
|
* |
217
|
|
|
* @return bool The connection has been loaded |
|
|
|
|
218
|
|
|
*/ |
219
|
162 |
|
private function loadSubConnection($connectionName) |
|
|
|
|
220
|
|
|
{ |
221
|
162 |
|
$names = explode('.', $connectionName, 2); |
222
|
|
|
|
223
|
162 |
|
if (!isset($names[1])) { |
224
|
162 |
|
return false; |
225
|
|
|
} |
226
|
|
|
|
227
|
2 |
|
$connection = $this->getConnection($names[0]); |
228
|
|
|
|
229
|
2 |
|
if ($connection instanceof SubConnectionManagerInterface) { |
230
|
|
|
//TODO doit on concerver une reference sur la sous connection ? |
|
|
|
|
231
|
2 |
|
$this->connections[$connectionName] = $connection->getConnection($names[1]); |
232
|
2 |
|
return true; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return false; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|