Dragon-Knight/server/app/App.php
2024-07-17 18:41:34 -05:00

66 lines
1.4 KiB
PHP

<?php
/*
The app container exists to be a dependency container for the game.
*/
class App
{
public static Database $db;
private static string $dbPath;
public static Request $req;
public static Auth $auth;
public static array $s = []; // game settings
public static array $flashes = []; // flash messages
public function __construct(string $dbPath)
{
self::$req = new Request(); // the current request
self::$db = new Database($dbPath); // the database
self::$dbPath = $dbPath; // the database path
self::$auth = new Auth();
// load game settings
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
self::$s = $s ? $s->fetch() : [];
if (INSTALLED) {
// load the player's auth
self::$auth->good();
}
// load flash messages
self::$flashes = $_SESSION['flash'] ?? [];
}
public static function performDatabaseReset(): void
{
if (file_exists(self::$dbPath)) {
unlink(self::$dbPath);
self::$db = new Database(self::$dbPath);
}
}
public static function auth(): bool
{
return self::$auth->good();
}
public static function flash(string $key, mixed $value = null): mixed
{
// get a flash message
if ($value === null) return self::$flashes[$key] ?? null;
// set a flash message
$_SESSION['flash'][$key] = $value;
self::$flashes[$key] = $value;
}
public function cleanup()
{
// clean up flash messages
$_SESSION['flash'] = [];
unset($_SESSION['flash']);
}
}