<?php
/**
* Compresses configuration files for a quick prototype.
* Handles potential errors gracefully.
*
* @param array $config_array An associative array representing the configuration.
* @param string $output_file The path to the output file.
* @return bool True on success, false on failure.
*/
function compactConfig(array $config_array, string $output_file): bool
{
try {
$compacted_config = [];
foreach ($config_array as $key => $value) {
// Handle different data types
if (is_array($value)) {
$compacted_config[$key] = compactConfig($value, 'nested_' . $key); // Recursive call for nested arrays
} else {
$compacted_config[$key] = $value;
}
}
// Output to file
file_put_contents($output_file, json_encode($compacted_config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); // Use JSON for compact representation
return true;
} catch (Exception $e) {
error_log("Error compacting config: " . $e->getMessage()); // Log errors
return false;
}
}
//Example Usage (for testing)
/*
$config = [
'database' => [
'host' => 'localhost',
'user' => 'admin',
'password' => 'password',
],
'api' => [
'url' => 'https://api.example.com',
'timeout' => 10,
],
'debug' => true,
'nested_array' => [ 1 => 'value1', 2 => 'value2' ],
];
$output_file = 'compacted_config.json';
if (compactConfig($config, $output_file)) {
echo "Configuration compacted to " . $output_file . "\n";
} else {
echo "Configuration compacting failed.\n";
}
*/
?>
Add your comment