<?php
namespace App\Shared\Support;

final class AppConfigSupport
{
    private static ?array $config = null;

    public static function read(string $key, $default = null)
    {
        if (self::$config === null) {
            $cfg = require dirname(__DIR__, 2) . '/config.php';
            self::$config = is_array($cfg) ? $cfg : [];
        }

        $parts = explode('.', $key);
        $val = self::$config;
        foreach ($parts as $part) {
            if (is_array($val) && array_key_exists($part, $val)) {
                $val = $val[$part];
                continue;
            }
            return $default;
        }
        return $val;
    }
}
