<?php
/**
* Header Metadata Instrumentation
*
* This function instruments the headers metadata of a local utility
* with synchronous execution. It adds a tracing mechanism to log
* header information during the execution of the utility.
*
* @param callable $utility_function The callable (function) to instrument.
* @param string $log_file (optional) The file to log header metadata to. Defaults to 'header_log.txt'.
* @return callable The instrumented function.
*/
function instrumentHeaders(callable $utility_function, string $log_file = 'header_log.txt'): callable
{
// Define a function to log header metadata
$log_header_metadata = function () use ($log_file) {
$headers = getallheaders();
if ($headers) {
$log_string = "--- Header Metadata ---\n";
foreach ($headers as $header => $value) {
$log_string .= "$header: $value\n";
}
$log_string .= "--- End Header Metadata ---\n";
// Write to log file
file_put_contents($log_file, $log_string, FILE_APPEND);
} else {
$log_string = "--- No Headers Found ---\n";
file_put_contents($log_file, $log_string, FILE_APPEND);
}
};
// Wrap the utility function
return function () use ($utility_function, $log_header_metadata) {
// Log header metadata before execution
$log_header_metadata();
// Execute the original utility function
$utility_function();
// Log header metadata after execution
$log_header_metadata();
};
}
// Example Usage (for testing):
/*
function myUtilityFunction() {
// Simulate some work
sleep(1);
header('Content-Type: application/json');
echo json_encode(['message' => 'Utility executed']);
}
$instrumentedFunction = instrumentHeaders('myUtilityFunction', 'my_header_log.txt');
$instrumentedFunction(); // Execute the instrumented function
*/
?>
Add your comment