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 Configuration.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 https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Database; |
48
|
|
|
|
49
|
|
|
use PDO; |
50
|
|
|
use Platine\Database\Driver\Driver; |
51
|
|
|
use Platine\Database\Driver\MySQL; |
52
|
|
|
use Platine\Database\Driver\Oracle; |
53
|
|
|
use Platine\Database\Driver\PostgreSQL; |
54
|
|
|
use Platine\Database\Driver\SQLite; |
55
|
|
|
use Platine\Database\Driver\SQLServer; |
56
|
|
|
use Platine\Stdlib\Config\AbstractConfiguration; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @class Configuration |
60
|
|
|
* @package Platine\Database |
61
|
|
|
*/ |
62
|
|
|
class Configuration extends AbstractConfiguration |
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 = 'default'; |
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
|
|
|
* The slow query time in second |
143
|
|
|
* @var float |
144
|
|
|
*/ |
145
|
|
|
protected float $slowQueryTime = 1.0; |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* The PDO connection options |
150
|
|
|
* @var array<mixed, mixed> |
151
|
|
|
*/ |
152
|
|
|
protected array $options = [ |
153
|
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
154
|
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ, |
155
|
|
|
PDO::ATTR_STRINGIFY_FETCHES => false, |
156
|
|
|
PDO::ATTR_EMULATE_PREPARES => false, |
157
|
|
|
]; |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* The connection attributes to customize some drivers |
161
|
|
|
* @var array<mixed, mixed> |
162
|
|
|
*/ |
163
|
|
|
protected array $attributes = []; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* The list of SQL command to execute after connection |
167
|
|
|
* @var array<int, string> |
168
|
|
|
*/ |
169
|
|
|
protected array $commands = []; |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return the driver name |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
public function getDriverName(): string |
176
|
|
|
{ |
177
|
|
|
return $this->driver; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Return the slow query time in second |
182
|
|
|
* @return float |
183
|
|
|
*/ |
184
|
|
|
public function getSlowQueryTime(): float |
185
|
|
|
{ |
186
|
|
|
return $this->slowQueryTime; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Set the slow query time in second |
191
|
|
|
* @param float $slowQueryTime |
192
|
|
|
* @return $this |
193
|
|
|
*/ |
194
|
|
|
public function setSlowQueryTime(float $slowQueryTime): self |
195
|
|
|
{ |
196
|
|
|
$this->slowQueryTime = $slowQueryTime; |
197
|
|
|
return $this; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Return the name of the configuration connection |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getName(): string |
206
|
|
|
{ |
207
|
|
|
return $this->name; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Return the character set |
212
|
|
|
* @return string |
213
|
|
|
*/ |
214
|
|
|
public function getCharset(): string |
215
|
|
|
{ |
216
|
|
|
return $this->charset; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Return the application name |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
|
|
public function getAppname(): string |
224
|
|
|
{ |
225
|
|
|
return $this->appname; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Return the host |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
public function getHostname(): string |
233
|
|
|
{ |
234
|
|
|
return $this->hostname; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
public function getUsername(): string |
242
|
|
|
{ |
243
|
|
|
return $this->username; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
|
|
public function getPassword(): string |
251
|
|
|
{ |
252
|
|
|
return $this->password; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* |
257
|
|
|
* @return int|null |
258
|
|
|
*/ |
259
|
|
|
public function getPort(): ?int |
260
|
|
|
{ |
261
|
|
|
return $this->port; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* |
266
|
|
|
* @return string |
267
|
|
|
*/ |
268
|
|
|
public function getDatabase(): string |
269
|
|
|
{ |
270
|
|
|
return $this->database; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* |
275
|
|
|
* @return string |
276
|
|
|
*/ |
277
|
|
|
public function getCollation(): string |
278
|
|
|
{ |
279
|
|
|
return $this->collation; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function getSocket(): string |
287
|
|
|
{ |
288
|
|
|
return $this->socket; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* |
293
|
|
|
* @return array<mixed, mixed> |
294
|
|
|
*/ |
295
|
|
|
public function getOptions(): array |
296
|
|
|
{ |
297
|
|
|
return $this->options; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Set the PDO connection option |
302
|
|
|
* @param mixed $name |
303
|
|
|
* @param mixed $value |
304
|
|
|
* @return $this |
305
|
|
|
*/ |
306
|
|
|
public function setOption(mixed $name, mixed $value): self |
307
|
|
|
{ |
308
|
|
|
$this->options[$name] = $value; |
309
|
|
|
|
310
|
|
|
return $this; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Set an array of options |
315
|
|
|
* @param array<mixed, mixed> $options |
316
|
|
|
* @return $this |
317
|
|
|
*/ |
318
|
|
|
public function setOptions(array $options): self |
319
|
|
|
{ |
320
|
|
|
foreach ($options as $name => $value) { |
321
|
|
|
$this->setOption($name, $value); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
return $this; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* |
329
|
|
|
* @return array<string, mixed> |
330
|
|
|
*/ |
331
|
|
|
public function getAttributes(): array |
332
|
|
|
{ |
333
|
|
|
return $this->attributes; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Set the connection attribute |
338
|
|
|
* @param string $name |
339
|
|
|
* @param mixed $value |
340
|
|
|
* @return $this |
341
|
|
|
*/ |
342
|
|
|
public function setAttribute(string $name, mixed $value): self |
343
|
|
|
{ |
344
|
|
|
$this->attributes[$name] = $value; |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Set an array of attributes |
351
|
|
|
* @param array<string, mixed> $attributes |
352
|
|
|
* @return $this |
353
|
|
|
*/ |
354
|
|
|
public function setAttributes(array $attributes): self |
355
|
|
|
{ |
356
|
|
|
foreach ($attributes as $name => $value) { |
357
|
|
|
$this->setAttribute($name, $value); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
return $this; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Check whether the attribute exist |
365
|
|
|
* @param string $name |
366
|
|
|
* @return bool |
367
|
|
|
*/ |
368
|
|
|
public function hasAttribute(string $name): bool |
369
|
|
|
{ |
370
|
|
|
return array_key_exists($name, $this->attributes); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* |
375
|
|
|
* @param string $name |
376
|
|
|
* @param mixed $default |
377
|
|
|
* |
378
|
|
|
* @return mixed |
379
|
|
|
*/ |
380
|
|
|
public function getAttribute(string $name, mixed $default = null): mixed |
381
|
|
|
{ |
382
|
|
|
return $this->hasAttribute($name) |
383
|
|
|
? $this->attributes[$name] |
384
|
|
|
: $default; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* |
389
|
|
|
* @return array<int, mixed> |
390
|
|
|
*/ |
391
|
|
|
public function getCommands(): array |
392
|
|
|
{ |
393
|
|
|
return $this->commands; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Add the connection command |
398
|
|
|
* @param string $command |
399
|
|
|
* @return $this |
400
|
|
|
*/ |
401
|
|
|
public function addCommand(string $command): self |
402
|
|
|
{ |
403
|
|
|
$this->commands[] = $command; |
404
|
|
|
|
405
|
|
|
return $this; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
/** |
409
|
|
|
* Add an array of commands |
410
|
|
|
* @param array<int, string> $commands |
411
|
|
|
* @return $this |
412
|
|
|
*/ |
413
|
|
|
public function addCommands(array $commands): self |
414
|
|
|
{ |
415
|
|
|
foreach ($commands as $command) { |
416
|
|
|
$this->addCommand($command); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
return $this; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Return the connection driver class name |
424
|
|
|
* @return string |
425
|
|
|
*/ |
426
|
|
|
public function getDriverClassName(): string |
427
|
|
|
{ |
428
|
|
|
$maps = [ |
429
|
|
|
'mysql' => MySQL::class, |
430
|
|
|
'pgsql' => PostgreSQL::class, |
431
|
|
|
'sqlsrv' => SQLServer::class, |
432
|
|
|
'oci' => Oracle::class, |
433
|
|
|
'oracle' => Oracle::class, |
434
|
|
|
'sqlite' => SQLite::class, |
435
|
|
|
]; |
436
|
|
|
|
437
|
|
|
return isset($maps[$this->driver]) |
438
|
|
|
? $maps[$this->driver] |
439
|
|
|
: Driver::class; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Whether the connection is persistent |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
|
|
public function isPersistent(): bool |
447
|
|
|
{ |
448
|
|
|
return $this->persistent; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* {@inheritdoc} |
453
|
|
|
*/ |
454
|
|
|
public function getValidationRules(): array |
455
|
|
|
{ |
456
|
|
|
return [ |
457
|
|
|
'options' => 'array', |
458
|
|
|
'commands' => 'array', |
459
|
|
|
'attributes' => 'array', |
460
|
|
|
'persistent' => 'boolean', |
461
|
|
|
'socket' => 'string', |
462
|
|
|
'collation' => 'string', |
463
|
|
|
'database' => 'string', |
464
|
|
|
'port' => 'integer', |
465
|
|
|
'password' => 'string', |
466
|
|
|
'username' => 'string', |
467
|
|
|
'hostname' => 'string', |
468
|
|
|
'appname' => 'string', |
469
|
|
|
'charset' => 'string', |
470
|
|
|
'name' => 'string', |
471
|
|
|
'driver' => 'string', |
472
|
|
|
'slow_query_time' => 'double', |
473
|
|
|
]; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* {@inheritdoc} |
478
|
|
|
*/ |
479
|
|
|
public function getSetterMaps(): array |
480
|
|
|
{ |
481
|
|
|
return [ |
482
|
|
|
'commands' => 'addCommands', |
483
|
|
|
]; |
484
|
|
|
} |
485
|
|
|
} |
486
|
|
|
|