1. <?php
  2. /**
  3. * Aggregates command-line options with configuration file values for maintenance tasks.
  4. *
  5. * @param array $argv Command-line arguments.
  6. * @param string $config_file Path to the configuration file.
  7. * @return array Aggregated options.
  8. */
  9. function aggregateOptions(array $argv, string $config_file): array
  10. {
  11. $options = [];
  12. // Parse command-line arguments
  13. $option_args = array_slice($argv, 1); // Skip script name
  14. foreach ($option_args as $arg) {
  15. if (strpos($arg, '=')) {
  16. list($option, $value) = explode('=', $arg, 2);
  17. $option = strtolower($option); // Convert to lowercase for consistency
  18. $options[$option] = $value;
  19. }
  20. }
  21. // Load configuration file
  22. if (file_exists($config_file)) {
  23. $config = require $config_file;
  24. } else {
  25. // Handle missing config file (e.g., return empty array or throw an error)
  26. error_log("Configuration file not found: $config_file");
  27. return $options; //Return only command line args
  28. }
  29. // Merge command-line options with configuration values
  30. foreach ($config as $key => $value) {
  31. if (isset($options[$key])) {
  32. // Command-line option takes precedence
  33. $options[$key] = $options[$key];
  34. } else {
  35. $options[$key] = $value;
  36. }
  37. }
  38. return $options;
  39. }
  40. // Example usage (for testing - remove or modify as needed)
  41. if (count($argv) > 1) {
  42. $argv = $argv; //Ensure $argv is passed correctly
  43. $config_file = 'config.php'; // Replace with your config file path
  44. $aggregated_options = aggregateOptions($argv, $config_file);
  45. print_r($aggregated_options);
  46. }
  47. ?>

Add your comment