Bicycle/System/Classes/Config.php
Egor Isaev b516ca07dc Initial commit: Bicycle PHP MVC micro-framework
Core MVC, HTTP client, Session, Cookie, Config, HTTPException — 111 PHPUnit tests.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-04 10:04:19 +03:00

66 lines
1.5 KiB
PHP

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description Config.php
* @copyright (c) 04/06/2026
*/
namespace System\Classes;
class Config
{
private static ?array $config = null;
private static function load(): void
{
if (self::$config !== null) {
return;
}
$base = APPPATH . '/config/config.php';
$local = APPPATH . '/config/config.local.php';
$config = file_exists($base) ? (include $base) : [];
if (file_exists($local)) {
$config = self::merge($config, include $local);
}
self::$config = $config;
}
public static function get(string $name, ?string $group = null): mixed
{
self::load();
if ($group === null) {
return self::$config[$name] ?? null;
}
return self::$config[$name][$group] ?? null;
}
public static function set(string $name, mixed $value): void
{
self::load();
self::$config[$name] = $value;
}
/**
* Глубокое слияние двух массивов конфигурации.
*/
public static function merge(array $base, array $override): array
{
foreach ($override as $key => $value) {
if (is_array($value) && isset($base[$key]) && is_array($base[$key])) {
$base[$key] = self::merge($base[$key], $value);
} else {
$base[$key] = $value;
}
}
return $base;
}
}