Files
da-postgresql/exec/lib/AdminerRuntimeConfig.php
T
2026-07-04 16:00:04 +02:00

62 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
final class AdminerRuntimeConfig
{
private const FILE_NAME = 'config.json';
public static function sync(Settings $settings, string $pluginErrorLog = ''): void
{
$runtimeDir = rtrim($settings->adminerSsoDir(), '/');
if ($runtimeDir === '' || !is_dir($runtimeDir)) {
return;
}
if (!is_writable($runtimeDir)) {
self::log($pluginErrorLog, 'Adminer runtime dir is not writable: ' . $runtimeDir);
return;
}
$payload = [
'version' => 1,
'adminer_public' => ($settings->enableAdminer() && $settings->adminerPublic()),
'updated_at' => time(),
];
$encoded = json_encode($payload, JSON_UNESCAPED_SLASHES);
if (!is_string($encoded) || $encoded === '') {
self::log($pluginErrorLog, 'Failed to encode Adminer runtime config JSON.');
return;
}
$targetPath = $runtimeDir . '/' . self::FILE_NAME;
$tempPath = $runtimeDir . '/.' . self::FILE_NAME . '.' . getmypid() . '.tmp';
$written = @file_put_contents($tempPath, $encoded, LOCK_EX);
if ($written === false) {
self::log($pluginErrorLog, 'Failed to write temporary Adminer runtime config: ' . $tempPath);
return;
}
@chmod($tempPath, 0644);
if (!@rename($tempPath, $targetPath)) {
@unlink($tempPath);
self::log($pluginErrorLog, 'Failed to replace Adminer runtime config: ' . $targetPath);
return;
}
@chmod($targetPath, 0644);
}
private static function log(string $pluginErrorLog, string $message): void
{
if ($pluginErrorLog === '') {
return;
}
$line = sprintf("[%s] ADMINER_RUNTIME_CONFIG %s\n", date('c'), $message);
@error_log($line, 3, $pluginErrorLog);
}
}