1. <?php
  2. /**
  3. * Header Metadata Instrumentation
  4. *
  5. * This function instruments the headers metadata of a local utility
  6. * with synchronous execution. It adds a tracing mechanism to log
  7. * header information during the execution of the utility.
  8. *
  9. * @param callable $utility_function The callable (function) to instrument.
  10. * @param string $log_file (optional) The file to log header metadata to. Defaults to 'header_log.txt'.
  11. * @return callable The instrumented function.
  12. */
  13. function instrumentHeaders(callable $utility_function, string $log_file = 'header_log.txt'): callable
  14. {
  15. // Define a function to log header metadata
  16. $log_header_metadata = function () use ($log_file) {
  17. $headers = getallheaders();
  18. if ($headers) {
  19. $log_string = "--- Header Metadata ---\n";
  20. foreach ($headers as $header => $value) {
  21. $log_string .= "$header: $value\n";
  22. }
  23. $log_string .= "--- End Header Metadata ---\n";
  24. // Write to log file
  25. file_put_contents($log_file, $log_string, FILE_APPEND);
  26. } else {
  27. $log_string = "--- No Headers Found ---\n";
  28. file_put_contents($log_file, $log_string, FILE_APPEND);
  29. }
  30. };
  31. // Wrap the utility function
  32. return function () use ($utility_function, $log_header_metadata) {
  33. // Log header metadata before execution
  34. $log_header_metadata();
  35. // Execute the original utility function
  36. $utility_function();
  37. // Log header metadata after execution
  38. $log_header_metadata();
  39. };
  40. }
  41. // Example Usage (for testing):
  42. /*
  43. function myUtilityFunction() {
  44. // Simulate some work
  45. sleep(1);
  46. header('Content-Type: application/json');
  47. echo json_encode(['message' => 'Utility executed']);
  48. }
  49. $instrumentedFunction = instrumentHeaders('myUtilityFunction', 'my_header_log.txt');
  50. $instrumentedFunction(); // Execute the instrumented function
  51. */
  52. ?>

Add your comment