version 0.8.0

This commit is contained in:
danny 2026-09-18 06:46:56 +02:00
parent f22007003a
commit e5ca9ef194
95 changed files with 5601 additions and 4042 deletions

28
app/Core/Config.php Normal file
View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/**
* Zentrale Konfiguration. Lädt das Array aus /config/config.php
* (liegt außerhalb des Webroots) und stellt es global bereit.
*/
final class Config
{
/** @var array<string,mixed> */
private static array $data = [];
public static function load(string $file): void
{
self::$data = require $file;
}
public static function get(string $key, mixed $default = null): mixed
{
return self::$data[$key] ?? $default;
}
/** @return array<string,mixed> */
public static function all(): array
{
return self::$data;
}
}

34
app/Core/Controller.php Normal file
View file

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* Basis für Seiten-Controller. Rendert eine View – optional im gemeinsamen
* Layout (Startseite/Regeln/Legal) oder als eigenständiges Dokument
* (Spiel/Login/Profil/Admin, die eigene Nav + noindex mitbringen).
*/
abstract class Controller
{
/** @param array<string,mixed> $meta */
protected function view(string $page, array $meta = [], bool $layout = true): void
{
$viewFile = BSB_BASE . '/app/Views/pages/' . $page . '.php';
if (!is_file($viewFile)) {
http_response_code(500);
echo 'View fehlt: ' . htmlspecialchars($page, ENT_QUOTES);
return;
}
if (!$layout) {
require $viewFile;
return;
}
// Variablen für app/Views/layout.php
$title = (string) ($meta['title'] ?? 'Black Sheep Bluff');
$description = (string) ($meta['description'] ?? '');
$cssFiles = (array) ($meta['css'] ?? []);
$jsFiles = (array) ($meta['js'] ?? []);
$bodyClass = (string) ($meta['bodyClass'] ?? '');
require BSB_BASE . '/app/Views/layout.php';
}
}

24
app/Core/Database.php Normal file
View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
/**
* PDO-Singleton. Baut die Verbindung aus der Config (1:1 zur alten db()).
*/
final class Database
{
private static ?PDO $pdo = null;
public static function pdo(): PDO
{
if (self::$pdo === null) {
$c = Config::all();
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=%s', $c['host'], $c['dbname'], $c['charset']);
self::$pdo = new PDO($dsn, $c['user'], $c['pass'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
}
return self::$pdo;
}
}

46
app/Core/Kernel.php Normal file
View file

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
/**
* Anwendungs-Kernel: registriert den Autoloader, lädt Helfer/Config,
* startet die Session und übergibt an den Router.
*
* Bewusst KEIN Composer nötig – ein schlanker Klassen-Autoloader, der die
* Standard-Verzeichnisse nach <ClassName>.php durchsucht (Dateiname == Klassenname).
*/
final class Kernel
{
public static function boot(string $basePath): void
{
define('BSB_BASE', $basePath);
define('BSB_UPLOADS', $basePath . '/public/uploads');
spl_autoload_register(static function (string $class) use ($basePath): void {
static $dirs = [
'app/Core',
'app/Database',
'app/Services',
'app/Controllers/Pages',
'app/Controllers/Api',
];
foreach ($dirs as $dir) {
$file = $basePath . '/' . $dir . '/' . $class . '.php';
if (is_file($file)) {
require $file;
return;
}
}
});
require $basePath . '/app/Support/helpers.php';
require $basePath . '/app/Database/migrations.php'; // globale Migrations-Definitionen
Config::load($basePath . '/config/config.php');
session_start();
(new Router(
require $basePath . '/app/routes/web.php',
require $basePath . '/app/routes/api.php'
))->dispatch();
}
}

67
app/Core/Router.php Normal file
View file

@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/**
* Router: trennt Seiten- und API-Requests.
* / -> Seiten-Route '' -> [Controller, method]
* /regeln -> Seiten-Route 'regeln'
* /api/state -> API-Route 'state' -> [Controller, method]
* /api/auth/me -> API-Route 'auth/me'
* Unbekannte Seite -> 404-Route; unbekannte API -> JSON-Fehler.
*/
final class Router
{
/**
* @param array<string,array{0:class-string,1:string}> $web
* @param array<string,array{0:class-string,1:string}> $api
*/
public function __construct(private array $web, private array $api) {}
public function dispatch(): void
{
$path = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?? '/';
$slug = trim(rawurldecode($path), '/');
if ($slug === 'api' || str_starts_with($slug, 'api/')) {
$this->dispatchApi(ltrim(substr($slug, 3), '/'));
return;
}
$this->dispatchPage($slug);
}
private function dispatchApi(string $action): void
{
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-store');
try {
Migrator::run(Database::pdo(), (string) Config::get('dbname'));
} catch (Throwable $e) {
respond(['error' => 'DB-Verbindung fehlgeschlagen: ' . $e->getMessage()], 500);
}
if (!isset($this->api[$action])) {
respond(['error' => 'Unbekannte Aktion.'], 400);
}
[$class, $method] = $this->api[$action];
try {
(new $class())->$method();
} catch (Throwable $e) {
respond(['error' => 'Serverfehler: ' . $e->getMessage()], 500);
}
}
private function dispatchPage(string $slug): void
{
if (isset($this->web[$slug])) {
$route = $this->web[$slug];
} else {
http_response_code(404);
$route = $this->web['__404__'];
}
[$class, $method] = $route;
(new $class())->$method();
}
}