feat: add GetBundledScenarios handler with manifest-driven list

This commit is contained in:
Keith Solomon
2026-07-25 23:37:27 -05:00
parent c2f9e0a970
commit 1f9e76afef
2 changed files with 153 additions and 0 deletions
+51
View File
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
namespace BattleForge\Http\Handlers;
use BattleForge\Http\Request;
use BattleForge\Http\Response;
final class GetBundledScenarios
{
private const FILENAME_PATTERN = '/^[a-z0-9-]+\.json$/';
public function __construct(private readonly string $scenariosDir)
{
}
/** @param array<string, string> $params */
public function handle(Request $request, array $params): Response
{
$manifestPath = $this->scenariosDir . '/manifest.json';
$manifest = [];
if (is_file($manifestPath)) {
$decoded = json_decode((string) file_get_contents($manifestPath), true);
if (is_array($decoded)) {
$manifest = $decoded;
}
}
$entries = [];
foreach (scandir($this->scenariosDir) ?: [] as $file) {
if (!preg_match(self::FILENAME_PATTERN, $file)) {
continue;
}
$id = substr($file, 0, -5);
$entry = $manifest[$id] ?? null;
if (!is_array($entry) || !isset($entry['name'], $entry['summary'])) {
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 Response::json(200, $entries);
}
}