1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Database |
5
|
|
|
* |
6
|
|
|
* Platine Database is the abstraction layer using PDO with support of query and schema builder |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Database |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file DbConfig.php |
33
|
|
|
* |
34
|
|
|
* The connection configuration class |
35
|
|
|
* |
36
|
|
|
* @package Platine\Database |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link http://www.iacademy.cf |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Database; |
48
|
|
|
|
49
|
|
|
use InvalidArgumentException; |
50
|
|
|
use PDO; |
51
|
|
|
use Platine\Database\Driver\Driver; |
52
|
|
|
use Platine\Database\Driver\MySQL; |
53
|
|
|
use Platine\Database\Driver\Oracle; |
54
|
|
|
use Platine\Database\Driver\PostgreSQL; |
55
|
|
|
use Platine\Database\Driver\SQLite; |
56
|
|
|
use Platine\Database\Driver\SQLServer; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Class Configuration |
60
|
|
|
* @package Platine\Database |
61
|
|
|
*/ |
62
|
|
|
class Configuration implements ConfigurationInterface |
63
|
|
|
{ |
64
|
|
|
/** |
65
|
|
|
* The connection driver to use |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected string $driver = 'mysql'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The connection name |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected string $name = ''; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* The driver character set |
78
|
|
|
* Only for some drivers |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
protected string $charset = 'UTF8'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The application name |
85
|
|
|
* Only for Microsoft SQL server |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
protected string $appname = ''; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The connection host name |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
protected string $hostname = 'localhost'; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* The connection username |
98
|
|
|
* @var string |
99
|
|
|
*/ |
100
|
|
|
protected string $username = ''; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* The connection username |
104
|
|
|
* @var string |
105
|
|
|
*/ |
106
|
|
|
protected string $password = ''; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* The connection port. If null will use the standard |
110
|
|
|
* port for database server |
111
|
|
|
* @var int|null |
112
|
|
|
*/ |
113
|
|
|
protected ?int $port = null; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* The connection database name to use |
117
|
|
|
* Note: for SQLite this is the path to the database file |
118
|
|
|
* @var string |
119
|
|
|
*/ |
120
|
|
|
protected string $database = ''; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The database server collation to use |
124
|
|
|
* Only for MySQL |
125
|
|
|
* @var string |
126
|
|
|
*/ |
127
|
|
|
protected string $collation = 'utf8_general_ci'; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* The connection socket to use for if the driver is MySQL |
131
|
|
|
* @var string |
132
|
|
|
*/ |
133
|
|
|
protected string $socket = ''; |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Whether the connection is persistent |
137
|
|
|
* @var bool |
138
|
|
|
*/ |
139
|
|
|
protected bool $persistent = false; |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* The PDO connection options |
144
|
|
|
* @var array<mixed, mixed> |
145
|
|
|
*/ |
146
|
|
|
protected array $options = [ |
147
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
148
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, |
149
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
150
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* The connection attributes to customize some drivers |
155
|
|
|
* @var array<mixed, mixed> |
156
|
|
|
*/ |
157
|
|
|
protected array $attributes = []; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* The list of SQL command to execute after connection |
161
|
|
|
* @var array<int, string> |
162
|
|
|
*/ |
163
|
|
|
protected array $commands = []; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Class constructor |
167
|
|
|
* @param array<string, mixed> $config the connection configuration |
168
|
|
|
*/ |
169
|
|
|
public function __construct(array $config = []) |
170
|
|
|
{ |
171
|
|
|
$this->load($config); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritedoc} |
176
|
|
|
*/ |
177
|
|
|
public function getDriverName(): string |
178
|
|
|
{ |
179
|
|
|
return $this->driver; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* {@inheritedoc} |
184
|
|
|
*/ |
185
|
|
|
public function getName(): string |
186
|
|
|
{ |
187
|
|
|
return $this->name; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritedoc} |
192
|
|
|
*/ |
193
|
|
|
public function getCharset(): string |
194
|
|
|
{ |
195
|
|
|
return $this->charset; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* {@inheritedoc} |
200
|
|
|
*/ |
201
|
|
|
public function getAppname(): string |
202
|
|
|
{ |
203
|
|
|
return $this->appname; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritedoc} |
208
|
|
|
*/ |
209
|
|
|
public function getHostname(): string |
210
|
|
|
{ |
211
|
|
|
return $this->hostname; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritedoc} |
216
|
|
|
*/ |
217
|
|
|
public function getUsername(): string |
218
|
|
|
{ |
219
|
|
|
return $this->username; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritedoc} |
224
|
|
|
*/ |
225
|
|
|
public function getPassword(): string |
226
|
|
|
{ |
227
|
|
|
return $this->password; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritedoc} |
232
|
|
|
*/ |
233
|
|
|
public function getPort(): ?int |
234
|
|
|
{ |
235
|
|
|
return $this->port; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* {@inheritedoc} |
240
|
|
|
*/ |
241
|
|
|
public function getDatabase(): string |
242
|
|
|
{ |
243
|
|
|
return $this->database; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* {@inheritedoc} |
248
|
|
|
*/ |
249
|
|
|
public function getCollation(): string |
250
|
|
|
{ |
251
|
|
|
return $this->collation; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* {@inheritedoc} |
256
|
|
|
*/ |
257
|
|
|
public function getSocket(): string |
258
|
|
|
{ |
259
|
|
|
return $this->socket; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* {@inheritedoc} |
264
|
|
|
*/ |
265
|
|
|
public function getOptions(): array |
266
|
|
|
{ |
267
|
|
|
return $this->options; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* {@inheritedoc} |
272
|
|
|
*/ |
273
|
|
|
public function setOption($name, $value): self |
274
|
|
|
{ |
275
|
|
|
$this->options[$name] = $value; |
276
|
|
|
|
277
|
|
|
return $this; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritedoc} |
282
|
|
|
*/ |
283
|
|
|
public function getAttributes(): array |
284
|
|
|
{ |
285
|
|
|
return $this->attributes; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* {@inheritedoc} |
290
|
|
|
*/ |
291
|
|
|
public function setAttribute($name, $value): self |
292
|
|
|
{ |
293
|
|
|
$this->attributes[$name] = $value; |
294
|
|
|
|
295
|
|
|
return $this; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* {@inheritedoc} |
300
|
|
|
*/ |
301
|
|
|
public function hasAttribute(string $name): bool |
302
|
|
|
{ |
303
|
|
|
return array_key_exists($name, $this->attributes); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* {@inheritedoc} |
308
|
|
|
*/ |
309
|
|
|
public function getAttribute(string $name, $default = null) |
310
|
|
|
{ |
311
|
|
|
return $this->hasAttribute($name) |
312
|
|
|
? $this->attributes[$name] |
313
|
|
|
: $default; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* {@inheritedoc} |
318
|
|
|
*/ |
319
|
|
|
public function getCommands(): array |
320
|
|
|
{ |
321
|
|
|
return $this->commands; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* {@inheritedoc} |
326
|
|
|
*/ |
327
|
|
|
public function addCommand(string $command): self |
328
|
|
|
{ |
329
|
|
|
$this->commands[] = $command; |
330
|
|
|
|
331
|
|
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* {@inheritedoc} |
336
|
|
|
*/ |
337
|
|
|
public function load(array $config): void |
338
|
|
|
{ |
339
|
|
|
foreach ($config as $name => $value) { |
340
|
|
|
$key = str_replace('_', '', lcfirst(ucwords($name, '_'))); |
341
|
|
|
if (property_exists($this, $key)) { |
342
|
|
|
$this->{$key} = $value; |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* {@inheritedoc} |
349
|
|
|
*/ |
350
|
|
|
public function getDriverClassName(): string |
351
|
|
|
{ |
352
|
|
|
$class = Driver::class; |
353
|
|
|
|
354
|
|
|
switch ($this->driver) { |
355
|
|
|
case 'mysql': |
356
|
|
|
$class = MySQL::class; |
357
|
|
|
break; |
358
|
|
|
case 'pgsql': |
359
|
|
|
$class = PostgreSQL::class; |
360
|
|
|
break; |
361
|
|
|
case 'sqlsrv': |
362
|
|
|
$class = SQLServer::class; |
363
|
|
|
break; |
364
|
|
|
case 'oci': |
365
|
|
|
case 'oracle': |
366
|
|
|
$class = Oracle::class; |
367
|
|
|
break; |
368
|
|
|
case 'sqlite': |
369
|
|
|
$class = SQLite::class; |
370
|
|
|
break; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
return $class; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
/** |
377
|
|
|
* {@inheritedoc} |
378
|
|
|
*/ |
379
|
|
|
public function isPersistent(): bool |
380
|
|
|
{ |
381
|
|
|
return $this->persistent; |
382
|
|
|
} |
383
|
|
|
} |
384
|
|
|
|