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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
<?php
/**
*
* This file is part of Aura for PHP.
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Aura\Sql;
/**
*
* Manages PDO connection objects for default, read, and write connections.
*
* @package Aura.Sql
*
*/
class ConnectionLocator implements ConnectionLocatorInterface
{
/**
*
* A registry of PDO connection entries.
*
* @var array
*
*/
protected $registry = array(
'default' => null,
'read' => array(),
'write' => array(),
);
/**
*
* Whether or not registry entries have been converted to objects.
*
* @var array
*
*/
protected $converted = array(
'default' => false,
'read' => array(),
'write' => array(),
);
/**
*
* Whether or not to turn on profiling when retrieving a connection.
*
* @var bool
*
*/
protected $profiling = false;
/**
*
* Constructor.
*
* @param callable $default A callable to create a default connection.
*
* @param array $read An array of callables to create read connections.
*
* @param array $write An array of callables to create write connections.
*
*/
public function __construct(
$default = null,
array $read = array(),
array $write = array()
) {
if ($default) {
$this->setDefault($default);
}
foreach ($read as $name => $callable) {
$this->setRead($name, $callable);
}
foreach ($write as $name => $callable) {
$this->setWrite($name, $callable);
}
}
/**
*
* Sets the default connection registry entry.
*
* @param callable $callable The registry entry.
*
* @return null
*
*/
public function setDefault($callable)
{
$this->registry['default'] = $callable;
$this->converted['default'] = false;
}
/**
*
* Returns the default connection object.
*
* @return ExtendedPdoInterface
*
*/
public function getDefault()
{
if (! $this->converted['default']) {
$callable = $this->registry['default'];
$this->registry['default'] = call_user_func($callable);
$this->converted['default'] = true;
}
$connection = $this->registry['default'];
$this->setProfiler($connection);
return $connection;
}
/**
*
* Sets a read connection registry entry by name.
*
* @param string $name The name of the registry entry.
*
* @param callable $callable The registry entry.
*
* @return null
*
*/
public function setRead($name, $callable)
{
$this->registry['read'][$name] = $callable;
$this->converted['read'][$name] = false;
}
/**
*
* Returns a read connection by name; if no name is given, picks a
* random connection; if no read connections are present, returns the
* default connection.
*
* @param string $name The read connection name to return.
*
* @return ExtendedPdoInterface
*
*/
public function getRead($name = null)
{
return $this->getConnection('read', $name);
}
/**
*
* Sets a write connection registry entry by name.
*
* @param string $name The name of the registry entry.
*
* @param callable $callable The registry entry.
*
* @return null
*
*/
public function setWrite($name, $callable)
{
$this->registry['write'][$name] = $callable;
$this->converted['write'][$name] = false;
}
/**
*
* Returns a write connection by name; if no name is given, picks a
* random connection; if no write connections are present, returns the
* default connection.
*
* @param string $name The write connection name to return.
*
* @return ExtendedPdoInterface
*
*/
public function getWrite($name = null)
{
return $this->getConnection('write', $name);
}
/**
*
* Returns a connection by name.
*
* @param string $type The connection type ('read' or 'write').
*
* @param string $name The name of the connection.
*
* @return ExtendedPdoInterface
*
* @throws Exception\ConnectionNotFound
*/
protected function getConnection($type, $name)
{
if (! $this->registry[$type]) {
return $this->getDefault();
}
if (! $name) {
$name = array_rand($this->registry[$type]);
}
if (! isset($this->registry[$type][$name])) {
throw new Exception\ConnectionNotFound("{$type}:{$name}");
}
if (! $this->converted[$type][$name]) {
$callable = $this->registry[$type][$name];
$this->registry[$type][$name] = call_user_func($callable);
$this->converted[$type][$name] = true;
}
$connection = $this->registry[$type][$name];
$this->setProfiler($connection);
return $connection;
}
/**
*
* Given a connection, enable or disable profiling on it. If a profiler has
* not been set into the connection, this will instantiate and set one.
*
* @param ExtendedPdo $connection The connection.
*
* @return null
*
*/
protected function setProfiler(ExtendedPdo $connection)
{
$profiler = $connection->getProfiler();
if (! $this->profiling && ! $profiler) {
return;
}
if (! $profiler) {
$profiler = new Profiler();
$connection->setProfiler($profiler);
}
$profiler->setActive($this->profiling);
}
/**
*
* Set profiling on all connections at retrieval time?
*
* @param bool $profiling True to enable, or false to disable, profiling on
* each connection as it is retrieved.
*
* @return null
*
*/
public function setProfiling($profiling = true)
{
$this->profiling = (bool) $profiling;
}
/**
*
* Gets the profiles from all connections.
*
* @return array
*
*/
public function getProfiles()
{
$profiles = array();
if ($this->converted['default']) {
$connection = $this->registry['default'];
$this->addProfiles('default', $connection, $profiles);
}
foreach (array('read', 'write') as $type) {
foreach ($this->registry[$type] as $name) {
if ($this->converted[$type][$name]) {
$connection = $this->registry[$type][$name];
$this->addProfiles("{$type}:{$name}", $connection, $profiles);
}
}
}
ksort($profiles);
return $profiles;
}
/**
*
* Adds profiles from a connection, with a label for the connection name.
*
* @param string $label The connection label.
*
* @param ExtendedPdo $connection The connection.
*
* @param array &$profiles Add the connection profiles to this array, in
* place.
*
* @return null
*/
protected function addProfiles($label, ExtendedPdo $connection, &$profiles)
{
$profiler = $connection->getProfiler();
if (! $profiler) {
return;
}
foreach ($profiler->getProfiles() as $key => $profile) {
$profile = array('connection' => $label) + $profile;
$profiles[$key] = $profile;
}
}
}
|