Files
webman-buildadmin/phinx-bootstrap.php
2026-04-01 14:08:45 +08:00

69 lines
2.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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';
use Webman\Config;
Config::clear();
Config::load($baseDir . '/config', ['route', 'middleware', 'process', 'server', 'static']);
// 与 Webman\ThinkOrm\ThinkOrm::start() 一致,并与 phinx.php 中 $thinkorm 来源一致
$thinkorm = array_replace_recursive(config('thinkorm', []), config('think-orm', []));
if (!empty($thinkorm)) {
support\think\Db::setConfig($thinkorm);
// Webman DbManager 使用连接池且忽略 force安装向导在同进程内迁移时若不清理会沿用旧前缀连接
// 导致 Phinx 已建带前缀表而 Db::name() 仍查无前缀表(如 menu_rule 不存在)。
if (class_exists(\Webman\ThinkOrm\DbManager::class)) {
$ref = new \ReflectionClass(\Webman\ThinkOrm\DbManager::class);
if ($ref->hasProperty('pools')) {
$poolsProp = $ref->getProperty('pools');
$poolsProp->setAccessible(true);
$poolsProp->setValue(null, []);
}
}
if (class_exists(\Webman\Context::class)) {
foreach (array_keys($thinkorm['connections'] ?? []) as $connName) {
\Webman\Context::set('think-orm.connections.' . $connName, null);
}
}
}