$values */ public static function set(string $path, array $values): void { $content = is_file($path) ? (string) file_get_contents($path) : ''; foreach ($values as $key => $value) { $line = $key.'='.self::format($value); $pattern = '/^#?\s*'.preg_quote($key, '/').'=.*$/m'; $content = preg_match($pattern, $content) ? preg_replace($pattern, str_replace(['\\', '$'], ['\\\\', '\\$'], $line), $content, 1) : rtrim($content, "\n")."\n".$line."\n"; } AtomicFile::write($path, $content); } private static function format(string|int|bool|null $value): string { if ($value === null) { return ''; } if (is_bool($value)) { return $value ? 'true' : 'false'; } $value = (string) $value; if ($value === '' || preg_match('/^[A-Za-z0-9_.:\/@+=-]+$/', $value)) { return $value; } return '"'.str_replace(['\\', '"', '$'], ['\\\\', '\\"', '\\$'], $value).'"'; } }