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
|
|
|
protected 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
|
|
|
|
76
|
|
|
$instance->connections[$connectionName] = $instance->Connection->setConnection(); |
77
|
|
|
|
78
|
|
|
$instance->currentConnection = $instance->connections[$connectionName]; |
79
|
|
|
|
80
|
|
|
if ($instance->currentConnection->getDriver() == 'pgsql') { |
81
|
|
|
$instance->currentConnection->changeSchema($instance->currentConnection->getSchema()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* This get the current connection |
89
|
|
|
* If not exists the current connection this use last setting |
90
|
|
|
* @return Connection |
91
|
|
|
* @throws \Exception if not exists connections set |
92
|
|
|
*/ |
93
|
1 |
|
public function current() |
94
|
|
|
{ |
95
|
1 |
|
if (empty($this->currentConnection)) { |
96
|
1 |
|
$configs = ($this->Config) ? $this->Config->getConfigs() : null; |
97
|
|
|
|
98
|
1 |
|
if (count($configs) == 0) throw new \Exception('No connections are available.'); |
99
|
|
|
|
100
|
|
|
$default = $this->Config->getDefault(); |
101
|
|
|
|
102
|
|
|
$name = is_null($default) ? |
103
|
|
|
@end(array_keys($configs)) : |
|
|
|
|
104
|
|
|
$this->Config->getDefault(); |
105
|
|
|
|
106
|
|
|
self::open($name); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->currentConnection; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Change current connection but this needed is set |
114
|
|
|
* @param $connectionName |
115
|
|
|
* @return mixed |
116
|
|
|
* @throws \Exception if connection name isn't string |
117
|
|
|
* @throws \Exception if connection name isn't set |
118
|
|
|
* @throws \Exception if connection not actived |
119
|
|
|
*/ |
120
|
|
|
public static function change($connectionName, $open = false) |
121
|
|
|
{ |
122
|
|
|
$instance = self::initialize(); |
123
|
|
|
|
124
|
|
|
if (!is_string($connectionName)) throw new \Exception("Invalid connection name."); |
125
|
|
|
|
126
|
|
|
if (!$instance->hasConnection($connectionName)) throw new \Exception("The connection name $connectionName is not set."); |
127
|
|
|
|
128
|
|
|
if (!$instance->hasOpen($connectionName)) { |
129
|
|
|
if ($open) { |
130
|
|
|
try { |
131
|
|
|
self::open($connectionName); |
132
|
|
|
} catch (\Exception $e) { |
133
|
|
|
throw $e; |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
|
|
|
137
|
|
|
throw new \Exception("This connection isn't actived."); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$instance->_currentConnectionName = $connectionName; |
142
|
|
|
|
143
|
|
|
$instance->currentConnection = $instance->connections[$connectionName]; |
144
|
|
|
|
145
|
|
|
$instance->Connection = $instance->connections[$connectionName]; |
146
|
|
|
|
147
|
|
|
return $instance; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* All connections opened |
152
|
|
|
* @return array All connections |
153
|
|
|
* @throws \Exception This instance isnt't initialized |
154
|
|
|
*/ |
155
|
1 |
|
public function getAllActive() |
156
|
|
|
{ |
157
|
1 |
|
if (!$this->hasInstance()) throw new \Exception('This instance isnt\'t initialized'); |
158
|
|
|
|
159
|
1 |
|
return $this->connections; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Get all connection string |
165
|
|
|
* @return array if have connection but not have this method return null |
166
|
|
|
*/ |
167
|
1 |
|
final public function getConfigs() |
168
|
|
|
{ |
169
|
1 |
|
return $this->Config->getConfigs(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Verify if connection has open |
174
|
|
|
* @param $connectionName |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
private function hasOpen($connectionName) |
178
|
|
|
{ |
179
|
|
|
return array_key_exists($connectionName, $this->connections); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Verify if this is initialized |
184
|
|
|
* @return bool |
185
|
|
|
*/ |
186
|
1 |
|
private function hasInstance() |
187
|
|
|
{ |
188
|
1 |
|
return !self::$_instance ? false : true; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Verify if has connection defined |
193
|
|
|
* @param $connectionName |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
1 |
|
private function hasConnection($connectionName) |
197
|
|
|
{ |
198
|
1 |
|
return array_key_exists( |
199
|
1 |
|
$connectionName, |
200
|
1 |
|
$this->Config->getConfigs() |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.