1
This commit is contained in:
@@ -6,81 +6,20 @@ namespace App\Config;
|
||||
|
||||
final class ConfigLoader
|
||||
{
|
||||
private ConfigValidator $validator;
|
||||
private string $baseDir;
|
||||
private array $config;
|
||||
|
||||
/** @var array<string,array<string,mixed>> */
|
||||
private array $cache = [];
|
||||
|
||||
public function __construct(ConfigValidator $validator, ?string $baseDir = null)
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->baseDir = $baseDir ?? dirname(__DIR__, 3) . '/config';
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function planetClasses(): array
|
||||
{
|
||||
return $this->load('planet_classes.json', function (array $config): void {
|
||||
$this->validator->validatePlanetClasses($config, 'planet_classes.json');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function races(): array
|
||||
{
|
||||
return $this->load('races.json', function (array $config): void {
|
||||
$this->validator->validateRaces($config, 'races.json');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function blueprintsBuildings(): array
|
||||
{
|
||||
return $this->load('blueprints_buildings.json', function (array $config): void {
|
||||
$this->validator->validateBlueprintsBuildings($config, 'blueprints_buildings.json');
|
||||
});
|
||||
return $this->config['blueprints_buildings'] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
public function avatars(): array
|
||||
public function autoCostWeights(): array
|
||||
{
|
||||
return $this->load('avatars.json', function (array $config): void {
|
||||
$this->validator->validateAvatars($config, 'avatars.json');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(array<string,mixed>):void $validate
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private function load(string $file, callable $validate): array
|
||||
{
|
||||
if (isset($this->cache[$file])) {
|
||||
return $this->cache[$file];
|
||||
}
|
||||
$path = rtrim($this->baseDir, '/') . '/' . $file;
|
||||
if (!file_exists($path)) {
|
||||
throw new \RuntimeException("Config-Datei nicht gefunden: {$path}");
|
||||
}
|
||||
$raw = file_get_contents($path);
|
||||
if ($raw === false) {
|
||||
throw new \RuntimeException("Config-Datei konnte nicht gelesen werden: {$path}");
|
||||
}
|
||||
$data = json_decode($raw, true, flags: JSON_THROW_ON_ERROR);
|
||||
if (!is_array($data)) {
|
||||
throw new \RuntimeException("Config-Datei muss ein JSON-Objekt sein: {$path}");
|
||||
}
|
||||
$validate($data);
|
||||
$this->cache[$file] = $data;
|
||||
return $data;
|
||||
return $this->config['auto_cost_weights'] ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user