|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpCfdi\RfcLinc\Application; |
|
6
|
|
|
|
|
7
|
|
|
class Config |
|
8
|
|
|
{ |
|
9
|
|
|
const KEY_ENVIRONMENT = 'environment'; |
|
10
|
|
|
|
|
11
|
|
|
const VALUE_PRODUCTION = 'production'; |
|
12
|
|
|
|
|
13
|
|
|
const KEY_DB_DSN = 'db.dsn'; |
|
14
|
|
|
|
|
15
|
|
|
const KEY_DB_USERNAME = 'db.username'; |
|
16
|
|
|
|
|
17
|
|
|
const KEY_DB_PASSWORD = 'db.password'; |
|
18
|
|
|
|
|
19
|
|
|
/** @var bool */ |
|
20
|
|
|
private $environmentProduction; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private $dbDsn; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private $dbUsername; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $dbPassword; |
|
30
|
|
|
|
|
31
|
13 |
|
public function __construct(bool $environmentProduction, string $dbDsn, string $dbUsername, string $dbPassword) |
|
32
|
|
|
{ |
|
33
|
13 |
|
$this->environmentProduction = $environmentProduction; |
|
34
|
13 |
|
$this->dbDsn = $dbDsn; |
|
35
|
13 |
|
$this->dbUsername = $dbUsername; |
|
36
|
13 |
|
$this->dbPassword = $dbPassword; |
|
37
|
13 |
|
} |
|
38
|
|
|
|
|
39
|
4 |
|
public static function createFromConfigFile(string $filename): self |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
4 |
|
if (! file_exists($filename)) { |
|
43
|
2 |
|
throw new \RuntimeException('It does not exists'); |
|
44
|
|
|
} |
|
45
|
2 |
|
if (is_dir($filename)) { |
|
46
|
|
|
throw new \RuntimeException('It is a directory'); |
|
47
|
|
|
} |
|
48
|
2 |
|
if (! is_readable($filename)) { |
|
49
|
|
|
throw new \RuntimeException('It is not readable'); |
|
50
|
|
|
} |
|
51
|
|
|
/** @noinspection PhpIncludeInspection */ |
|
52
|
2 |
|
$settings = require $filename; |
|
53
|
2 |
|
if (! is_array($settings)) { |
|
54
|
|
|
throw new \RuntimeException('The file did not return an array'); |
|
55
|
|
|
} |
|
56
|
2 |
|
return self::createFromArray($settings); |
|
57
|
2 |
|
} catch (\Throwable $exception) { |
|
58
|
2 |
|
throw new \RuntimeException("Cannot read config file $filename", 0, $exception); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
13 |
|
public static function createFromArray(array $values): self |
|
63
|
|
|
{ |
|
64
|
13 |
|
return new self( |
|
65
|
13 |
|
'production' === ($values[static::KEY_ENVIRONMENT] ?? ''), |
|
66
|
13 |
|
(string) ($values[static::KEY_DB_DSN] ?? ''), |
|
67
|
13 |
|
(string) ($values[static::KEY_DB_USERNAME] ?? ''), |
|
68
|
13 |
|
(string) ($values[static::KEY_DB_PASSWORD] ?? '') |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
1 |
|
public function isEnvironmentProduction(): bool |
|
73
|
|
|
{ |
|
74
|
1 |
|
return $this->environmentProduction; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
9 |
|
public function dbDsn(): string |
|
78
|
|
|
{ |
|
79
|
9 |
|
return $this->dbDsn; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
8 |
|
public function dbUsername(): string |
|
83
|
|
|
{ |
|
84
|
8 |
|
return $this->dbUsername; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
6 |
|
public function dbPassword(): string |
|
88
|
|
|
{ |
|
89
|
6 |
|
return $this->dbPassword; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|