utm ?? null) instanceof stdClass) throw new RuntimeException('В config.json отсутствует объект utm.'); $hasCons = property_exists($config->utm, 'cons'); $cons = $hasCons ? $config->utm->cons : (object) ['weights' => (object) ['1' => 100, '2' => 0], 'default' => '2']; if (!$cons instanceof stdClass || !(is_array($cons->weights ?? null) || ($cons->weights ?? null) instanceof stdClass) || !(is_string($cons->default ?? null) || is_int($cons->default ?? null)) || !preg_match('/^[1-9][0-9]*$/D', (string) $cons->default) || (float) $cons->default > 2147483647) { throw new RuntimeException('Существующий utm.cons некорректен; автоматическая перезапись запрещена.'); } if (!property_exists($config->utm, 'chb') && $hasCons) { fwrite(STDOUT, "cons уже настроен: config не изменён.\n"); exit(0); } unset($config->utm->chb); $config->utm->cons = $cons; $encoded = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n"; if (file_put_contents($path, $encoded, LOCK_EX) === false) throw new RuntimeException('Не удалось записать config.json.'); fwrite(STDOUT, "Мигрированы только utm.chb/cons; остальные поля и существующие настройки cons сохранены.\n"); exit(10); } catch (Throwable $error) { fwrite(STDERR, $error->getMessage() . "\n"); exit(2); }