56 lines
1.9 KiB
PHP
56 lines
1.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
final class PhpMyAdminRuntimeConfig
|
|
{
|
|
private const FILE_NAME = 'config.json';
|
|
|
|
public static function sync(Settings $settings, string $pluginErrorLog = ''): void
|
|
{
|
|
$runtimeDir = rtrim($settings->adminerSsoDir(), '/');
|
|
if ($runtimeDir === '') {
|
|
return;
|
|
}
|
|
|
|
if (!is_dir($runtimeDir) && !@mkdir($runtimeDir, 0755, true) && !is_dir($runtimeDir)) {
|
|
self::log($pluginErrorLog, 'Cannot create phpMyAdmin runtime dir: ' . $runtimeDir);
|
|
return;
|
|
}
|
|
|
|
$payload = [
|
|
'version' => 1,
|
|
'phpmyadmin_url_path' => $settings->adminerUrlPath(),
|
|
'phpmyadmin_public_base_url' => $settings->adminerPublicBaseUrl(),
|
|
'ticket_dir' => $runtimeDir . '/tickets',
|
|
'host' => 'dynamic:' . basename($settings->mysqlCredentialsFile()),
|
|
'credentials_file' => $settings->mysqlCredentialsFile(),
|
|
'client_bin_dir' => $settings->mysqlClientBinDir(),
|
|
'updated_at' => time(),
|
|
];
|
|
|
|
$encoded = json_encode($payload, JSON_UNESCAPED_SLASHES);
|
|
if (!is_string($encoded) || $encoded === '') {
|
|
self::log($pluginErrorLog, 'Failed to encode phpMyAdmin runtime config JSON.');
|
|
return;
|
|
}
|
|
|
|
$targetPath = $runtimeDir . '/' . self::FILE_NAME;
|
|
if (@file_put_contents($targetPath, $encoded) === false) {
|
|
self::log($pluginErrorLog, 'Failed to write phpMyAdmin runtime config: ' . $targetPath);
|
|
return;
|
|
}
|
|
|
|
@chmod($targetPath, 0644);
|
|
}
|
|
|
|
private static function log(string $pluginErrorLog, string $message): void
|
|
{
|
|
if ($pluginErrorLog === '') {
|
|
return;
|
|
}
|
|
|
|
$line = sprintf("[%s] PHPMYADMIN_RUNTIME_CONFIG %s\n", date('c'), $message);
|
|
@error_log($line, 3, $pluginErrorLog);
|
|
}
|
|
}
|