61 lines
1.8 KiB
PHP
61 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace BattleForge\Http\Handlers;
|
|
|
|
use BattleForge\Http\Request;
|
|
use BattleForge\Http\Response;
|
|
|
|
final class GetHomePage
|
|
{
|
|
public function __construct(private readonly string $scenariosDir = '')
|
|
{
|
|
}
|
|
|
|
/** @param array<string, string> $params */
|
|
public function handle(Request $request, array $params): Response
|
|
{
|
|
$csrf = $request->cookies['__csrf'] ?? '';
|
|
$bundled = $this->loadBundledScenarios();
|
|
|
|
ob_start();
|
|
require_once __DIR__ . '/../../Views/layout.php';
|
|
require __DIR__ . '/../../Views/home.php';
|
|
$body = (string) ob_get_clean();
|
|
|
|
return Response::html(200, $body);
|
|
}
|
|
|
|
/** @return list<array{id: string, name: string, summary: string}> */
|
|
private function loadBundledScenarios(): array
|
|
{
|
|
if ($this->scenariosDir === '' || !is_file($this->scenariosDir . '/manifest.json')) {
|
|
return [];
|
|
}
|
|
$decoded = json_decode((string) file_get_contents($this->scenariosDir . '/manifest.json'), true);
|
|
if (!is_array($decoded)) {
|
|
return [];
|
|
}
|
|
$entries = [];
|
|
foreach ($decoded as $id => $entry) {
|
|
if (!is_string($id) || !preg_match('/^[a-z0-9-]+$/', $id) || !is_array($entry)) {
|
|
continue;
|
|
}
|
|
if (!isset($entry['name'], $entry['summary'])) {
|
|
continue;
|
|
}
|
|
if (!is_file($this->scenariosDir . '/' . $id . '.json')) {
|
|
continue;
|
|
}
|
|
$entries[] = [
|
|
'id' => $id,
|
|
'name' => (string) $entry['name'],
|
|
'summary' => (string) $entry['summary'],
|
|
];
|
|
}
|
|
usort($entries, static fn (array $a, array $b): int => strcmp($a['id'], $b['id']));
|
|
return $entries;
|
|
}
|
|
}
|