52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?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);
|
|
}
|
|
}
|