1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
use Dotenv\Dotenv; |
4
|
|
|
|
5
|
|
|
class MySqlProperties { |
6
|
|
|
private const CONFIG_MYSQL_HOST = "MYSQL_HOST"; |
7
|
|
|
private const CONFIG_MYSQL_USER = "MYSQL_USER"; |
8
|
|
|
private const CONFIG_MYSQL_PASSWORD = "MYSQL_PASSWORD"; |
9
|
|
|
private const CONFIG_MYSQL_PORT = "MYSQL_PORT"; |
10
|
|
|
private const CONFIG_MYSQL_DATABASE = "MYSQL_DATABASE"; |
11
|
|
|
private string $host; |
12
|
|
|
private string $user; |
13
|
|
|
private string $password; |
14
|
|
|
private int $port; |
15
|
|
|
private string $databaseName; |
16
|
|
|
|
17
|
|
|
public function __construct() { |
18
|
|
|
$environmentFile = self::determineConfigEnvironmentFile(); |
19
|
|
|
$config = Dotenv::createArrayBacked(ROOT, $environmentFile); |
20
|
|
|
$array = $config->load(); |
21
|
|
|
self::validateConfig($config); |
22
|
|
|
[ |
23
|
|
|
MySqlProperties::CONFIG_MYSQL_HOST => $this->host, |
24
|
|
|
MySqlProperties::CONFIG_MYSQL_USER => $this->user, |
25
|
|
|
MySqlProperties::CONFIG_MYSQL_PASSWORD => $this->password, |
26
|
|
|
MySqlProperties::CONFIG_MYSQL_PORT => $port, |
27
|
|
|
MySqlProperties::CONFIG_MYSQL_DATABASE => $this->databaseName, |
28
|
|
|
] = $array; |
29
|
|
|
$this->port = (int)$port; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function determineConfigEnvironmentFile(): string { |
33
|
|
|
$environment = ""; |
34
|
|
|
if (isset($_ENV["MYSQL_CONFIG_ENVIRONMENT"])) { |
35
|
|
|
$environment = $_ENV["MYSQL_CONFIG_ENVIRONMENT"]; |
36
|
|
|
} |
37
|
|
|
return $environment . ".env"; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public static function validateConfig(Dotenv $config) { |
41
|
|
|
$config->required(self::CONFIG_MYSQL_HOST); |
42
|
|
|
$config->required(self::CONFIG_MYSQL_USER); |
43
|
|
|
$config->required(self::CONFIG_MYSQL_PASSWORD); |
44
|
|
|
$config->required(self::CONFIG_MYSQL_PORT)->isInteger(); |
45
|
|
|
$config->required(self::CONFIG_MYSQL_DATABASE); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getHost(): string { |
52
|
|
|
return $this->host; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getUser(): string { |
59
|
|
|
return $this->user; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getPassword(): string { |
66
|
|
|
return $this->password; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int |
71
|
|
|
*/ |
72
|
|
|
public function getPort(): int { |
73
|
|
|
return $this->port; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getDatabaseName(): string { |
80
|
|
|
return $this->databaseName; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|