1. <?php
  2. /**
  3. * Instruments query strings for internal tooling with error logging.
  4. *
  5. * @param string $callback The function to be called for each query string.
  6. * @param array $query_strings An array of query strings to process.
  7. * @return array An array of results from the callback function.
  8. */
  9. function instrumentQueryStrings(callable $callback, array $query_strings): array
  10. {
  11. $results = [];
  12. foreach ($query_strings as $query_string) {
  13. try {
  14. // Log the query string.
  15. error_log("Processing query string: " . $query_string);
  16. // Call the callback function with the query string.
  17. $result = $callback($query_string);
  18. $results[] = $result;
  19. } catch (Exception $e) {
  20. // Log the error.
  21. error_log("Error processing query string: " . $query_string . ". Error: " . $e->getMessage());
  22. $results[] = ['error' => $e->getMessage()]; // Store error in results
  23. }
  24. }
  25. return $results;
  26. }
  27. // Example usage (for testing)
  28. if (isset($_GET)) {
  29. $query_strings = $_GET;
  30. $callback = function ($query_string) {
  31. // Simulate some processing.
  32. return "Processed: " . $query_string;
  33. };
  34. $results = instrumentQueryStrings($callback, $query_strings);
  35. //Output the results (for testing)
  36. print_r($results);
  37. }
  38. ?>

Add your comment