<?php
/**
* Reloads dataset configurations, supporting older versions.
*
* @param string $config_file Path to the configuration file.
* @param string $version Optional: Configuration version to load. If not provided, loads the latest.
* @return array|false An array containing the configuration data, or false on failure.
*/
function reloadDatasetConfig(string $config_file, string $version = ''): array|false
{
try {
// Determine the configuration file path based on the specified version.
$file_path = $version ? $config_file . '_' . $version . '.json' : $config_file . '_latest.json';
// Check if the configuration file exists.
if (!file_exists($file_path)) {
error_log("Dataset configuration file not found: " . $file_path);
return false;
}
// Load the configuration from the JSON file.
$config_data = json_decode(file_get_contents($file_path), true);
// Check for JSON decoding errors.
if (json_last_error() !== JSON_ERROR_NONE) {
error_log("Error decoding JSON from file: " . $file_path . ". Error: " . json_last_error_msg());
return false;
}
return $config_data;
} catch (Exception $e) {
error_log("An unexpected error occurred: " . $e->getMessage());
return false;
}
}
// Example usage (replace with your actual file path)
//$config = reloadDatasetConfig('datasets/config');
//if ($config) {
// // Use the loaded configuration data
// print_r($config);
//} else {
// echo "Failed to reload dataset configuration.";
//}
?>
Add your comment