1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MocOrm\Connection; |
4
|
|
|
|
5
|
|
|
class Config |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @connectionString array of string This attribute is internal for save the connection string |
9
|
|
|
*/ |
10
|
|
|
private $_connectionString = []; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @username array of string This attribute is internal for save the connection usernames |
14
|
|
|
*/ |
15
|
|
|
private $_username = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @password array of string This attribute is internal for save the connection passwords |
19
|
|
|
*/ |
20
|
|
|
private $_password = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* List of drivers sets on connections |
24
|
|
|
* @var array $_driver |
25
|
|
|
*/ |
26
|
|
|
private $_driver = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* List of drivers sets on connections |
30
|
|
|
* @var array $_driver |
31
|
|
|
*/ |
32
|
|
|
private $_charset = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* List of schemas sets on connections |
36
|
|
|
* @var array $_schema |
37
|
|
|
*/ |
38
|
|
|
private $_schema = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constant to define set accepted drivers. |
42
|
|
|
* @var array DRIVERS all accepted drivers |
43
|
|
|
*/ |
44
|
|
|
const DRIVERS = [ |
45
|
|
|
"mysql", |
46
|
|
|
"mssql", |
47
|
|
|
"pgsql", |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
private $default = null; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var boolean $appLogger Configure if application save log or no |
54
|
|
|
*/ |
55
|
|
|
private $appLogger; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create configuration from connection |
59
|
|
|
* @param string $driver The driver from connection |
60
|
|
|
* @param string $username The username from connection |
61
|
|
|
* @param string $password The password from connection |
62
|
|
|
* @param string $host The host from connection |
63
|
|
|
* @param string $database The database from connection |
64
|
|
|
* @param string $connectionName The connection name from connection |
65
|
|
|
* @param integer $port The port from connection |
66
|
|
|
* @return $this This object for other iterators |
67
|
|
|
* @throws \Exception case one or some elements on parameters are invalid |
68
|
|
|
*/ |
69
|
14 |
|
public function addConfig( |
70
|
|
|
$driver = 'mysql', |
71
|
|
|
$username = "root", |
72
|
|
|
$password = null, |
73
|
|
|
$host = "localhost", |
74
|
|
|
$database = null, |
75
|
|
|
$connectionName = null, |
76
|
|
|
$port = null, |
77
|
|
|
$charset = 'utf8', |
78
|
|
|
$defaultSchema = null) |
79
|
|
|
{ |
80
|
|
|
#Begin: Verify if all parameters send is valid. |
81
|
14 |
|
if (!is_string($driver) || !in_array($driver, self::DRIVERS)) throw new \Exception("The driver $driver don't supported."); |
|
|
|
|
82
|
13 |
|
if (!is_string($username) || empty($username)) throw new \Exception("Invalid username."); |
|
|
|
|
83
|
12 |
|
if (!is_string($password) || empty($password)) throw new \Exception("Invalid password."); |
84
|
11 |
|
if (!is_string($host) || empty($host)) throw new \Exception("Invalid host."); |
|
|
|
|
85
|
10 |
|
if (!is_string($database) || empty($database)) throw new \Exception("Invalid database name."); |
86
|
9 |
|
$this->validatesConnectionName($connectionName); |
87
|
|
|
|
88
|
8 |
|
$port = is_null($port) ? '' : (int)$port; |
89
|
8 |
|
if (!is_null($port) && !is_int($port)) throw new \Exception("Invalid port format."); |
|
|
|
|
90
|
|
|
|
91
|
|
|
#Constructor of the connection string |
92
|
7 |
|
$this->_connectionString[$connectionName] = $this->renderConnectionString($driver, $host, $database, $port); |
93
|
7 |
|
$this->_username[$connectionName] = $username; |
94
|
7 |
|
$this->_password[$connectionName] = $password; |
95
|
7 |
|
$this->_driver[$connectionName] = $driver; |
96
|
7 |
|
$this->_charset[$connectionName] = $charset; |
97
|
7 |
|
$this->_schema[$connectionName] = $defaultSchema; |
98
|
|
|
|
99
|
7 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
7 |
|
final private function renderConnectionString($drive, $host, $database, $port) { |
103
|
|
|
switch ($drive) { |
104
|
7 |
|
case "mssql": return "sqlsrv:Server=$host, $port;Database=$database;"; |
105
|
7 |
|
default: return "$drive:host=$host;dbname=$database;port=$port;"; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get all connection string |
111
|
|
|
* @return array if have connection but not have this method return null |
112
|
|
|
*/ |
113
|
3 |
|
final public function getConfigs() |
114
|
|
|
{ |
115
|
3 |
|
return $this->_connectionString; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the current connection on connection name. |
120
|
|
|
* @param string $connectionName name on connection |
121
|
|
|
* @return $this This object from other interator |
122
|
|
|
* @throws \Exception if the connect name haven't set; |
123
|
|
|
*/ |
124
|
1 |
|
public function setDefault($connectionName) |
125
|
|
|
{ |
126
|
1 |
|
$this->default = $connectionName; |
127
|
1 |
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set the current connection on connection name. |
132
|
|
|
* @param string $connectionName name on connection |
133
|
|
|
* @return $this This object from other interator |
134
|
|
|
* @throws \Exception if the connect name haven't set; |
135
|
|
|
*/ |
136
|
1 |
|
public function getDefault() |
137
|
|
|
{ |
138
|
1 |
|
return $this->default; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Set the current connection on connection name. |
143
|
|
|
* @param string $connectionName name on connection |
144
|
|
|
* @return Config|array This object from other interator |
145
|
|
|
* @throws \Exception if the connect name haven't set; |
146
|
|
|
*/ |
147
|
3 |
|
public function getConnection($connectionName) |
148
|
|
|
{ |
149
|
3 |
|
$this->validatesConnectionName($connectionName); |
150
|
|
|
|
151
|
2 |
|
if (array_key_exists($connectionName, $this->_connectionString)) { |
152
|
|
|
return [ |
153
|
1 |
|
'connectionString' => $this->_connectionString[$connectionName], |
154
|
1 |
|
'driver' => $this->_driver[$connectionName], |
155
|
1 |
|
'username' => $this->_username[$connectionName], |
156
|
1 |
|
'password' => $this->_password[$connectionName], |
157
|
1 |
|
'charset' => $this->_charset[$connectionName], |
158
|
1 |
|
'schema' => $this->_schema[$connectionName] |
159
|
|
|
]; |
160
|
|
|
} else { |
161
|
1 |
|
throw new \Exception("The connection name $connectionName is not set."); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
9 |
|
private function validatesConnectionName($connectionName) |
168
|
|
|
{ |
169
|
9 |
|
if (!is_string($connectionName) || empty($connectionName)) { |
170
|
2 |
|
throw new \Exception("Invalid connection name."); |
171
|
|
|
} |
172
|
8 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public function getConnectionString($connectionName) |
178
|
|
|
{ |
179
|
|
|
return $this->_connectionString[$connectionName]; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param mixed $connectionString |
184
|
|
|
*/ |
185
|
|
|
public function setSettings($connectionName, $connectionSettings) |
186
|
|
|
{ |
187
|
|
|
if (!is_array($connectionSettings)) throw new \Exception('Invalid format connectionSettings'); |
188
|
|
|
if (empty($this->_driver[$connectionName])) throw new \Exception('Driver not set.'); |
189
|
|
|
|
190
|
|
|
$this->validatesConnectionName($connectionName); |
191
|
|
|
|
192
|
|
|
$connectionSettings = (object)$connectionSettings; |
193
|
|
|
|
194
|
|
|
$this->_connectionString[$connectionName] = $this->_driver[$connectionName] . ":host=$connectionSettings->host;dbname=$connectionSettings->database;port=$connectionSettings->port;"; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return mixed |
201
|
|
|
*/ |
202
|
|
|
public function getUsername($connectionName) |
203
|
|
|
{ |
204
|
|
|
return $this->_username[$connectionName]; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param mixed $username |
209
|
|
|
*/ |
210
|
|
|
public function setUsername($connectionName, $username = "root") |
211
|
|
|
{ |
212
|
|
|
$this->validatesConnectionName($connectionName); |
213
|
|
|
|
214
|
|
|
$this->_username[$connectionName] = $username; |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return mixed |
221
|
|
|
*/ |
222
|
|
|
public function getPassword($connectionName) |
223
|
|
|
{ |
224
|
|
|
return $this->_password[$connectionName]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param mixed $password |
229
|
|
|
*/ |
230
|
|
|
public function setPassword($connectionName, $password) |
231
|
|
|
{ |
232
|
|
|
$this->validatesConnectionName($connectionName); |
233
|
|
|
|
234
|
|
|
$this->_password[$connectionName] = $password; |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @return mixed |
241
|
|
|
*/ |
242
|
|
|
public function getDriver($connectionName) |
243
|
|
|
{ |
244
|
|
|
return $this->_driver[$connectionName]; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param mixed $driver |
249
|
|
|
*/ |
250
|
|
|
public function setDriver($connectionName, $driver = 'mysql') |
251
|
|
|
{ |
252
|
|
|
$this->validatesConnectionName($connectionName); |
253
|
|
|
|
254
|
|
|
$this->_driver[$connectionName] = $driver; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @return mixed |
261
|
|
|
*/ |
262
|
|
|
public function getCharset($connectionName) |
263
|
|
|
{ |
264
|
|
|
return $this->_charset[$connectionName]; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @param mixed $charset |
269
|
|
|
*/ |
270
|
|
|
public function setCharset($connectionName, $charset = 'utf8') |
271
|
|
|
{ |
272
|
|
|
$this->validatesConnectionName($connectionName); |
273
|
|
|
|
274
|
|
|
$this->_charset[$connectionName] = $charset; |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return mixed |
281
|
|
|
*/ |
282
|
|
|
public function getSchema($connectionName) |
283
|
|
|
{ |
284
|
|
|
return $this->_schema[$connectionName]; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param mixed $schema |
289
|
|
|
*/ |
290
|
|
|
public function setSchema($connectionName, $schema) |
291
|
|
|
{ |
292
|
|
|
$this->validatesConnectionName($connectionName); |
293
|
|
|
|
294
|
|
|
$this->_schema[$connectionName] = $schema; |
295
|
|
|
|
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return $this |
301
|
|
|
*/ |
302
|
|
|
public function appEnableLogger() |
303
|
|
|
{ |
304
|
|
|
$this->appLogger = true; |
305
|
|
|
|
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @return $this |
311
|
|
|
*/ |
312
|
|
|
public function appDisableLogger() |
313
|
|
|
{ |
314
|
|
|
$this->appLogger = false; |
315
|
|
|
|
316
|
|
|
return $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @return boolean |
321
|
|
|
*/ |
322
|
|
|
public function getAppLogger() |
323
|
|
|
{ |
324
|
|
|
return $this->appLogger; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|