1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MocOrm\Connection; |
4
|
|
|
|
5
|
|
|
class ConnectionManager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @Connection object is a of Connection |
9
|
|
|
*/ |
10
|
|
|
private $Connection; |
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @Connection array is all connections opened |
14
|
|
|
*/ |
15
|
|
|
private $connections = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @currentConnection object is the current connection |
19
|
|
|
*/ |
20
|
|
|
private $currentConnection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @_instance object this instance |
24
|
|
|
*/ |
25
|
|
|
private static $_instance; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @currentConnectionName string of the current connection name |
29
|
|
|
*/ |
30
|
|
|
private $_currentConnectionName; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @Config This is a singleton object for save all configs |
34
|
|
|
*/ |
35
|
|
|
private $Config; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Initialize the object or return this object if have value set in attribute $_instance |
39
|
|
|
* @param \Closure $config |
40
|
|
|
* @return ConnectionManager |
41
|
|
|
*/ |
42
|
6 |
|
public static function initialize($config = null) |
43
|
|
|
{ |
44
|
6 |
|
if (!self::$_instance) self::$_instance = new ConnectionManager(); |
45
|
|
|
|
46
|
6 |
|
if (is_callable($config)) { |
47
|
|
|
|
48
|
1 |
|
$config($_config = new Config()); |
49
|
|
|
|
50
|
1 |
|
self::$_instance->Config = $_config; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
return self::$_instance; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $connectionName String |
58
|
|
|
* @return $this |
59
|
|
|
* @throws \Exception if isn't an string |
60
|
|
|
* @throws \Exception if isn't set |
61
|
|
|
*/ |
62
|
2 |
|
public static function open($connectionName) |
63
|
|
|
{ |
64
|
2 |
|
$instance = self::$_instance; |
65
|
|
|
|
66
|
2 |
|
if (!is_string($connectionName) || empty($connectionName)) throw new \Exception("Invalid connection name."); |
67
|
1 |
|
if (!$instance->hasConnection($connectionName)) throw new \Exception("The connection name $connectionName is not set."); |
68
|
|
|
if ($instance->hasOpen($connectionName)) throw new \Exception('This connection is actived.'); |
69
|
|
|
|
70
|
|
|
$instance->_currentConnectionName = $connectionName; |
71
|
|
|
|
72
|
|
|
$configs = $instance->Config->getConnection($connectionName); |
73
|
|
|
|
74
|
|
|
$instance->Connection = new Connection($configs); |
75
|
|
|
$instance->Connection->setAppLogger($instance->Config->getAppLogger()); |
76
|
|
|
|
77
|
|
|
$instance->connections[$connectionName] = $instance->Connection->setConnection(); |
78
|
|
|
|
79
|
|
|
$instance->currentConnection = $instance->connections[$connectionName]; |
80
|
|
|
|
81
|
|
|
if ($instance->currentConnection->getDriver() == 'pgsql') { |
82
|
|
|
$instance->currentConnection->changeSchema($instance->currentConnection->getSchema()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $instance; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This get the current connection |
90
|
|
|
* If not exists the current connection this use last setting |
91
|
|
|
* @return Connection |
92
|
|
|
* @throws \Exception if not exists connections set |
93
|
|
|
*/ |
94
|
1 |
|
public function current() |
95
|
|
|
{ |
96
|
1 |
|
if (empty($this->currentConnection)) { |
97
|
1 |
|
$configs = ($this->Config) ? $this->Config->getConfigs() : null; |
98
|
|
|
|
99
|
1 |
|
if (count($configs) == 0) throw new \Exception('No connections are available.'); |
100
|
|
|
|
101
|
|
|
$default = $this->Config->getDefault(); |
102
|
|
|
|
103
|
|
|
$name = is_null($default) ? |
104
|
|
|
@end(array_keys($configs ?: [])) : |
105
|
|
|
$default; |
106
|
|
|
|
107
|
|
|
$this->open($name); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->currentConnection; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Change current connection but this needed is set |
115
|
|
|
* @param $connectionName |
116
|
|
|
* @return mixed |
117
|
|
|
* @throws \Exception if connection name isn't string |
118
|
|
|
* @throws \Exception if connection name isn't set |
119
|
|
|
* @throws \Exception if connection not actived |
120
|
|
|
*/ |
121
|
|
|
public static function change($connectionName, $open = false) |
122
|
|
|
{ |
123
|
|
|
$instance = self::initialize(); |
124
|
|
|
|
125
|
|
|
if (!is_string($connectionName)) throw new \Exception("Invalid connection name."); |
126
|
|
|
|
127
|
|
|
if (!$instance->hasConnection($connectionName)) throw new \Exception("The connection name $connectionName is not set."); |
128
|
|
|
|
129
|
|
|
if (!$instance->hasOpen($connectionName)) { |
130
|
|
|
if ($open) { |
131
|
|
|
try { |
132
|
|
|
self::open($connectionName); |
133
|
|
|
} catch (\Exception $e) { |
134
|
|
|
throw $e; |
135
|
|
|
} |
136
|
|
|
} else { |
137
|
|
|
|
138
|
|
|
throw new \Exception("This connection isn't actived."); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$instance->_currentConnectionName = $connectionName; |
143
|
|
|
|
144
|
|
|
$instance->currentConnection = $instance->connections[$connectionName]; |
145
|
|
|
|
146
|
|
|
$instance->Connection = $instance->connections[$connectionName]; |
147
|
|
|
|
148
|
|
|
return $instance; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* All connections opened |
153
|
|
|
* @return array All connections |
154
|
|
|
* @throws \Exception This instance isnt't initialized |
155
|
|
|
*/ |
156
|
1 |
|
public function getAllActive() |
157
|
|
|
{ |
158
|
1 |
|
if (!$this->hasInstance()) throw new \Exception('This instance isnt\'t initialized'); |
159
|
|
|
|
160
|
1 |
|
return $this->connections; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get all connection string |
166
|
|
|
* @return array if have connection but not have this method return null |
167
|
|
|
*/ |
168
|
1 |
|
final public function getConfigs() |
169
|
|
|
{ |
170
|
1 |
|
return $this->Config->getConfigs(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Verify if connection has open |
175
|
|
|
* @param $connectionName |
176
|
|
|
* @return bool |
177
|
|
|
*/ |
178
|
|
|
private function hasOpen($connectionName) |
179
|
|
|
{ |
180
|
|
|
return array_key_exists($connectionName, $this->connections); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Verify if this is initialized |
185
|
|
|
* @return bool |
186
|
|
|
*/ |
187
|
1 |
|
private function hasInstance() |
188
|
|
|
{ |
189
|
1 |
|
return !self::$_instance ? false : true; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Verify if has connection defined |
194
|
|
|
* @param $connectionName |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
1 |
|
private function hasConnection($connectionName) |
198
|
|
|
{ |
199
|
1 |
|
return array_key_exists( |
200
|
1 |
|
$connectionName, |
201
|
1 |
|
$this->Config->getConfigs() |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.