<?php
/**
 * Phinx 迁移引导：加载配置使 support\think\Db 可用（InstallData 等迁移需检查数据是否存在）
 */
declare(strict_types=1);

$baseDir = __DIR__;

if (!defined('BASE_PATH')) {
    define('BASE_PATH', $baseDir);
}

require $baseDir . '/vendor/autoload.php';

if (class_exists('Dotenv\Dotenv') && is_file($baseDir . '/.env')) {
    if (method_exists('Dotenv\Dotenv', 'createUnsafeImmutable')) {
        Dotenv\Dotenv::createUnsafeImmutable($baseDir)->load();
    } else {
        Dotenv\Dotenv::createMutable($baseDir)->load();
    }
}

if (!function_exists('env')) {
    function env(string $key, mixed $default = null): mixed
    {
        $value = $_ENV[$key] ?? getenv($key);
        if ($value !== false && $value !== null) {
            return $value;
        }
        if (strpos($key, '.') !== false) {
            $parts = explode('.', $key);
            $upper = strtoupper(implode('_', $parts));
            $value = $_ENV[$upper] ?? getenv($upper);
            if ($value !== false && $value !== null) {
                return $value;
            }
        }
        return $default;
    }
}

require $baseDir . '/vendor/workerman/webman-framework/src/support/helpers.php';
require $baseDir . '/app/functions.php';

Webman\Config::load($baseDir . '/config', ['route', 'middleware', 'process', 'server', 'static']);
$thinkorm = config('thinkorm', []);
if (!empty($thinkorm)) {
    support\think\Db::setConfig($thinkorm);
}
