1. <?php
  2. /**
  3. * Reloads dataset configurations, supporting older versions.
  4. *
  5. * @param string $config_file Path to the configuration file.
  6. * @param string $version Optional: Configuration version to load. If not provided, loads the latest.
  7. * @return array|false An array containing the configuration data, or false on failure.
  8. */
  9. function reloadDatasetConfig(string $config_file, string $version = ''): array|false
  10. {
  11. try {
  12. // Determine the configuration file path based on the specified version.
  13. $file_path = $version ? $config_file . '_' . $version . '.json' : $config_file . '_latest.json';
  14. // Check if the configuration file exists.
  15. if (!file_exists($file_path)) {
  16. error_log("Dataset configuration file not found: " . $file_path);
  17. return false;
  18. }
  19. // Load the configuration from the JSON file.
  20. $config_data = json_decode(file_get_contents($file_path), true);
  21. // Check for JSON decoding errors.
  22. if (json_last_error() !== JSON_ERROR_NONE) {
  23. error_log("Error decoding JSON from file: " . $file_path . ". Error: " . json_last_error_msg());
  24. return false;
  25. }
  26. return $config_data;
  27. } catch (Exception $e) {
  28. error_log("An unexpected error occurred: " . $e->getMessage());
  29. return false;
  30. }
  31. }
  32. // Example usage (replace with your actual file path)
  33. //$config = reloadDatasetConfig('datasets/config');
  34. //if ($config) {
  35. // // Use the loaded configuration data
  36. // print_r($config);
  37. //} else {
  38. // echo "Failed to reload dataset configuration.";
  39. //}
  40. ?>

Add your comment