Files
XC_VM/src/Infrastructure/Database/DatabaseFactory.php
T
Divarion_D e1bb80672e refactor(bootstrap): split XC_Bootstrap into an injectable BootKernel pipeline
Replace the fully-static XC_Bootstrap god-class with a stage pipeline so the boot
logic becomes unit-testable and the per-context sequences are explicit.

- BootState replaces the 8 static readiness flags with a value object threaded
  through the pipeline; stages read/write it instead of static state.
- BootStageInterface + BootPipeline run an ordered stage list and abort loudly
  on a throwing stage.
- 16 stages under Core/Bootstrap/Stage/ hold one subsystem each, extracted
  verbatim from the old private methods (constants, config, flood, host, session,
  database, legacy core, redis, process title, admin API, translator, admin
  shutdown, status constants, admin globals, container populate, health check).
- StageProfiles builds the ordered list per context, mirroring the exact previous
  sequence; BootKernel resolves options, sets up the container and runs it.
- XC_Bootstrap is now a thin BC facade delegating to BootKernel; its getters read
  the returned BootState. reset() also clears EventDispatcher and the new
  DatabaseFactory::reset() (a side-effect-free registry clear for test isolation).

The DB-touching contexts (Cli/Stream/Admin) still require a live MySQL and the
xcvm_core extension, so they are verified on a canary rather than in CI; the
Minimal context and the pipeline/profile composition are covered by new tests.
2026-09-16 16:58:49 +03:00

80 lines
1.9 KiB
PHP

<?php
namespace XcVm\Infrastructure\Database;
use XcVm\Core\Database\DatabaseHandler;
/**
* DatabaseFactory — создание, хранение и закрытие глобального подключения к БД.
*
* Singleton-реестр: entry points вызывают set($db), потребители — get().
*
* @package XC_VM_Infrastructure_Database
* @author Divarion_D <https://github.com/Divarion-D>
* @copyright 2025-2026 Vateron Media
* @link https://github.com/Vateron-Media/XC_VM
* @license AGPL-3.0 https://www.gnu.org/licenses/agpl-3.0.html
*/
class DatabaseFactory {
/** @var DatabaseHandler|null */
private static $instance;
/**
* Сохраняет экземпляр DatabaseHandler в singleton-реестре.
*/
public static function set(DatabaseHandler $db): void {
self::$instance = $db;
}
/**
* Возвращает текущий DatabaseHandler.
*/
public static function get(): ?DatabaseHandler {
return self::$instance;
}
/**
* Создаёт DatabaseHandler из config.ini и кладёт в global $db + singleton.
*/
public static function connect() {
global $db;
if (is_object($db) && method_exists($db, 'ping') && $db->ping()) {
self::$instance = $db;
return;
}
if (is_object($db)) {
$db->close_mysql();
$db = null;
}
$db = new DatabaseHandler();
self::$instance = $db;
}
/**
* Закрывает глобальное подключение к БД.
*/
public static function close() {
global $db;
if (is_object($db)) {
$db->close_mysql();
$db = null;
}
self::$instance = null;
}
/**
* Clears the singleton registry without closing the connection.
*
* Test seam for deterministic per-test state: unlike close(), it does not
* touch the global $db or call close_mysql(), so it is safe with the fake
* DatabaseHandler subtypes tests inject.
*/
public static function reset(): void {
self::$instance = null;
}
}