Config.php
1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
final class Config
{
private static ?array $data = null;
public static function get(string $path, $default = null)
{
if (self::$data === null) {
self::$data = self::load();
}
$value = self::$data;
foreach (explode('.', $path) as $segment) {
if (!is_array($value) || !array_key_exists($segment, $value)) {
return $default;
}
$value = $value[$segment];
}
return $value;
}
private static function load(): array
{
$localFile = __DIR__ . '/../config/local.php';
if (!is_file($localFile)) {
throw new RuntimeException(
'config/local.php não encontrado. Copie config/config.example.php para config/local.php e preencha os valores.'
);
}
$data = require $localFile;
if (!is_array($data)) {
throw new RuntimeException('config/local.php deve retornar um array.');
}
return $data;
}
}