2024-07-11 17:21:33 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
The app container exists to be a dependency container for the game.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class App
|
|
|
|
{
|
2024-07-17 22:11:45 -05:00
|
|
|
private array $routes = [];
|
2024-07-11 17:21:33 -05:00
|
|
|
public static Database $db;
|
|
|
|
public static Request $req;
|
2024-07-15 19:54:50 -05:00
|
|
|
public static Auth $auth;
|
2024-07-15 14:24:13 -05:00
|
|
|
public static array $s = []; // game settings
|
2024-07-17 12:51:50 -05:00
|
|
|
public static array $flashes = []; // flash messages
|
2024-07-11 17:21:33 -05:00
|
|
|
|
2024-07-17 22:11:45 -05:00
|
|
|
public function __construct(Database $db, Request $req, Auth $auth)
|
2024-07-11 17:21:33 -05:00
|
|
|
{
|
2024-07-17 22:11:45 -05:00
|
|
|
self::$req = $req; // the current request
|
|
|
|
self::$db = $db; // the database
|
|
|
|
self::$auth = $auth; // the auth system
|
2024-07-15 14:24:13 -05:00
|
|
|
|
2024-07-17 18:41:34 -05:00
|
|
|
// load game settings
|
|
|
|
$s = self::$db->q('SELECT * FROM settings WHERE id = 1;');
|
|
|
|
self::$s = $s ? $s->fetch() : [];
|
2024-07-15 19:54:50 -05:00
|
|
|
|
2024-07-17 18:41:34 -05:00
|
|
|
if (INSTALLED) {
|
2024-07-17 12:58:13 -05:00
|
|
|
// load the player's auth
|
|
|
|
self::$auth->good();
|
2024-07-17 12:51:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// load flash messages
|
|
|
|
self::$flashes = $_SESSION['flash'] ?? [];
|
2024-07-13 23:15:25 -05:00
|
|
|
}
|
|
|
|
|
2024-07-17 22:11:45 -05:00
|
|
|
public static function auth(): bool
|
2024-07-13 23:15:25 -05:00
|
|
|
{
|
2024-07-17 22:11:45 -05:00
|
|
|
return self::$auth->good();
|
2024-07-11 17:21:33 -05:00
|
|
|
}
|
2024-07-15 19:54:50 -05:00
|
|
|
|
2024-07-17 22:11:45 -05:00
|
|
|
public function route(string $uri, string $module): App
|
2024-07-15 19:54:50 -05:00
|
|
|
{
|
2024-07-17 22:11:45 -05:00
|
|
|
$this->routes[$uri] = $module;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle(string $uri): App
|
|
|
|
{
|
|
|
|
if (!isset($this->routes[$uri])) return self::fourOhFour($uri);
|
|
|
|
$this->routes[$uri]::handle();
|
|
|
|
return $this;
|
2024-07-15 19:54:50 -05:00
|
|
|
}
|
2024-07-17 12:51:50 -05:00
|
|
|
|
|
|
|
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;
|
2024-07-17 22:11:45 -05:00
|
|
|
|
|
|
|
return null;
|
2024-07-17 12:51:50 -05:00
|
|
|
}
|
|
|
|
|
2024-07-17 18:41:34 -05:00
|
|
|
public function cleanup()
|
2024-07-17 12:51:50 -05:00
|
|
|
{
|
|
|
|
// clean up flash messages
|
2024-07-17 18:41:34 -05:00
|
|
|
$_SESSION['flash'] = [];
|
2024-07-17 22:11:45 -05:00
|
|
|
self::$flashes = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fourOhFour(string $uri): void
|
|
|
|
{
|
|
|
|
http_response_code(404);
|
|
|
|
echo "404: $uri";
|
2024-07-17 12:51:50 -05:00
|
|
|
}
|
2024-07-11 17:21:33 -05:00
|
|
|
}
|