$a !== '--check' && $a !== $argv[0])); $check = in_array('--check', $argv, true); $defsDir = $args[0] ?? (getenv('RESPONDAGE_VARIANTS_DIR') ?: __DIR__); $motherConfig = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config.xml'; foreach ([$motherConfig] as $f) { if (!is_file($f)) { fwrite(STDERR, "Required file not found: $f\n"); exit(1); } } if (!is_dir($defsDir)) { fwrite(STDERR, "Definitions dir not found: $defsDir\n"); exit(1); } $doc = new DOMDocument(); if (!$doc->load($motherConfig)) { fwrite(STDERR, "Could not parse $motherConfig\n"); exit(1); } $formats = option_format_map($doc); $jsons = glob(rtrim($defsDir, '/\\') . DIRECTORY_SEPARATOR . '*.json'); if (empty($jsons)) { fwrite(STDERR, "No *.json files in $defsDir\n"); exit(1); } $fail = 0; foreach ($jsons as $jsonFile) { $definition = json_decode(file_get_contents($jsonFile), true); if (!is_array($definition)) { fwrite(STDERR, "Invalid JSON in $jsonFile — skipped\n"); $fail++; continue; } // merge: mother order first (hints refreshed), then legacy keys kept at the end $merged = []; foreach ($formats as $k => $hint) { $merged[$k] = $hint; } foreach (($definition['_formats'] ?? []) as $k => $hint) { if (!array_key_exists($k, $merged)) { $merged[$k] = $hint; // legacy key no longer in the mother config } } $definition['_formats'] = $merged; $out = json_encode($definition, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . "\n"; $current = str_replace("\r\n", "\n", file_get_contents($jsonFile)); if ($check) { if ($current !== $out) { echo "STALE: " . basename($jsonFile) . " — run: php variants/document_options.php\n"; $fail++; } else { echo "OK: " . basename($jsonFile) . " (" . count($merged) . " format hints)\n"; } continue; } file_put_contents($jsonFile, $out); echo "Wrote _formats in " . basename($jsonFile) . ": " . count($merged) . " hints\n"; } exit($fail > 0 ? 1 : 0);