<?php
declare(strict_types=1);

// Loads config, classes and helpers. Used by the web front controller and the CLI scripts.

define('APP_ROOT', dirname(__DIR__));

spl_autoload_register(function (string $class): void {
    if (str_starts_with($class, 'App\\')) {
        $file = APP_ROOT . '/src/' . str_replace('\\', '/', substr($class, 4)) . '.php';
        if (is_file($file)) {
            require $file;
        }
    }
});

$configFile = getenv('NERGMUD_CONFIG') ?: APP_ROOT . '/config/config.php';
if (!is_file($configFile)) {
    http_response_code(500);
    exit("Missing config: copy config/config.example.php to config/config.php and fill it in.\n");
}
$GLOBALS['config'] = require $configFile;

date_default_timezone_set('UTC');

if (!empty($GLOBALS['config']['dev_mode'])) {
    ini_set('display_errors', '1');
    error_reporting(E_ALL);
} else {
    ini_set('display_errors', '0');
}

require APP_ROOT . '/src/helpers.php';
