Files
preland/bin/ensure-focus-config.php
T

69 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Точечно добавляет новую UTM focus в рабочий конфиг приленда на хостинге.
*
* Значения уже существующих полей конфигурации намеренно не нормализуются и
* не переписываются: владелец настраивает их непосредственно на стенде или
* production. Скрипт возвращает 0, когда focus уже существует, и 10, когда
* ключ добавлен и config.json требуется загрузить обратно.
*
* @usage php bin/ensure-focus-config.php /path/to/config.json
*/
declare(strict_types=1);
if ($argc !== 2) {
fwrite(STDERR, "Использование: php bin/ensure-focus-config.php /path/to/config.json\n");
exit(2);
}
$path = $argv[1];
$raw = @file_get_contents($path);
if ($raw === false) {
fwrite(STDERR, "Не удалось прочитать config.json.\n");
exit(2);
}
try {
$config = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
fwrite(STDERR, "config.json содержит некорректный JSON: {$exception->getMessage()}\n");
exit(2);
}
if (!is_array($config) || !isset($config['utm']) || !is_array($config['utm'])) {
fwrite(STDERR, "В config.json отсутствует объект utm.\n");
exit(2);
}
if (array_key_exists('focus', $config['utm'])) {
fwrite(STDOUT, "focus уже настроен: конфиг не изменён.\n");
exit(0);
}
$config['utm']['focus'] = [
'weights' => [
'center' => 0,
'face' => 1,
],
'default' => 'face',
];
try {
$encoded = json_encode(
$config,
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR
) . "\n";
} catch (JsonException $exception) {
fwrite(STDERR, "Не удалось сериализовать config.json: {$exception->getMessage()}\n");
exit(2);
}
if (file_put_contents($path, $encoded, LOCK_EX) === false) {
fwrite(STDERR, "Не удалось записать обновлённый config.json.\n");
exit(2);
}
fwrite(STDOUT, "Добавлен только utm.focus (face включён по умолчанию).\n");
exit(10);