feat: configure the game from env variables

This commit is contained in:
Emmanuel BENOîT 2025-01-02 14:41:59 +01:00
parent bc5a70c4c6
commit 8ffcccb1e8
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
5 changed files with 39 additions and 8 deletions
game/scripts/lib

View file

@ -12,16 +12,33 @@ class xml_config {
private static $games = null;
private static $defGame = null;
private static function readFromEnv(string $varName, string $default): string {
if ($varName == '') {
return $default;
}
$fileVarName = $varName . '_FILE';
$fileName = getenv($fileVarName);
if ($fileName !== false) {
$value = @file_get_contents($fileName);
} else {
$value = getenv($varName);
}
return $value ?: $default;
}
private static function parseMainParams($root) {
$node = $root->firstChild;
while ($node) {
if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == 'Param') {
$aName = $node->getAttribute('name');
$aValue = $node->getAttribute('value');
if ($aName == "") {
if ($aName == '') {
l::warn("CONFIG: a main parameter is missing a 'name' attribute");
} elseif (!isset(xml_config::$mGame->params[$aName])) {
$aValue = $node->getAttribute('value');
$aFromEnv = $node->getAttribute('from-env');
$aValue = self::readFromEnv($aFromEnv, $aValue);
xml_config::$mGame->params[$aName] = $aValue;
} else {
l::notice("CONFIG: duplicate main parameter '$aName'");