61 lines
3.6 KiB
PHP
61 lines
3.6 KiB
PHP
<?php
|
|
/** preland#11: владелец21.10 согласовал только requisites из текущей zoomzilla.ru. */
|
|
declare(strict_types=1);
|
|
|
|
function replaceRequisites(string $raw, string $encoded): string
|
|
{
|
|
$depth = 0; $length = strlen($raw);
|
|
for ($i = 0; $i < $length; $i++) {
|
|
if ($raw[$i] === '"') {
|
|
$start = $i++;
|
|
for (; $i < $length; $i++) {
|
|
if ($raw[$i] === '\\') { $i++; continue; }
|
|
if ($raw[$i] === '"') break;
|
|
}
|
|
if ($depth !== 1 || json_decode(substr($raw, $start, $i - $start + 1)) !== 'requisites') continue;
|
|
$cursor = $i + 1;
|
|
while ($cursor < $length && ctype_space($raw[$cursor])) $cursor++;
|
|
if (($raw[$cursor] ?? '') !== ':') continue;
|
|
$cursor++;
|
|
while ($cursor < $length && ctype_space($raw[$cursor])) $cursor++;
|
|
$valueStart = $cursor; $nested = 0; $quoted = false;
|
|
for (; $cursor < $length; $cursor++) {
|
|
$char = $raw[$cursor];
|
|
if ($quoted) {
|
|
if ($char === '\\') $cursor++;
|
|
elseif ($char === '"') $quoted = false;
|
|
continue;
|
|
}
|
|
if ($char === '"') { $quoted = true; continue; }
|
|
if (($char === ',' || $char === '}') && $nested === 0) break;
|
|
if ($char === '[' || $char === '{') $nested++;
|
|
elseif ($char === ']' || $char === '}') $nested--;
|
|
}
|
|
return substr($raw, 0, $valueStart) . $encoded . substr($raw, $cursor);
|
|
}
|
|
if ($raw[$i] === '{' || $raw[$i] === '[') $depth++;
|
|
elseif ($raw[$i] === '}' || $raw[$i] === ']') $depth--;
|
|
}
|
|
$end = strrpos($raw, '}');
|
|
$prefix = substr($raw, 0, $end);
|
|
return $prefix . (count(json_decode($raw, true)) ? ',' : '') . '"requisites":' . $encoded . substr($raw, $end);
|
|
}
|
|
|
|
try {
|
|
if ($argc !== 2) throw new RuntimeException('Требуется путь к текущему host config');
|
|
$path = $argv[1];
|
|
$raw = file_get_contents($path);
|
|
$config = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
|
if (!is_array($config) || !str_starts_with(ltrim($raw), '{')) throw new RuntimeException('Требуется объект config');
|
|
$expected = json_decode(file_get_contents(dirname(__DIR__) . '/release/requisites.json'), true, 512, JSON_THROW_ON_ERROR);
|
|
if (!is_array($expected) || !$expected || array_filter($expected, static fn($line) => !is_string($line) || trim($line) === '')) throw new RuntimeException('Некорректный согласованный источник requisites');
|
|
if (($config['requisites'] ?? null) === $expected) { echo "requisites актуальны: конфиг не изменён\n"; exit(0); }
|
|
$patched = replaceRequisites($raw, json_encode($expected, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR));
|
|
$actual = json_decode($patched, true, 512, JSON_THROW_ON_ERROR);
|
|
$config['requisites'] = $expected;
|
|
// JSON-порядок ключей не меняет контракт, остальные значения должны совпасть.
|
|
if ($actual != $config) throw new RuntimeException('Изменились несогласованные поля');
|
|
if (file_put_contents($path, $patched, LOCK_EX) === false) throw new RuntimeException('Запись config не удалась');
|
|
echo "Изменён только requisites по решению владельца preland#11\n"; exit(10);
|
|
} catch (Throwable $error) { fwrite(STDERR, $error->getMessage() . "\n"); exit(2); }
|