Config.php 1.01 KB
<?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;
    }
}