}
return $f;
}
+
+ /**
+ * @param $filePath string
+ * @param $array array
+ * @return void
+ * @see https://www.faqforge.com/php/how-to-write-an-array-to-an-env-file-using-php/
+ */
+ public static function arrayToEnvFile($filePath, $array)
+ {
+ $content = '';
+
+ foreach ($array as $key => $value) {
+ // Ensure the key is a string and the value is scalar (not an array, object, etc.)
+ if (!is_string($key) || !is_scalar($value)) {
+ throw new InvalidArgumentException("Invalid key or value type.");
+ }
+
+ // Escape if needed and format the line
+ $key = strtoupper($key); // Optional: convert keys to uppercase
+ $value = strval($value); // Convert the value to a string
+ $value = strpos($value, ' ') !== false ? '"' . $value . '"' : $value; // Enclose the value in quotes if it contains spaces
+ $content .= "{$key}={$value}\n";
+ }
+
+ // Write to file
+ if (file_put_contents($filePath, $content) === false) {
+ throw new RuntimeException("Failed to write to the file at $filePath.");
+ }
+ }
}
\ No newline at end of file