<?php
/**
* Logs DOM element operations for staging environments.
*
* @param string $action The action performed (e.g., 'create', 'update', 'delete').
* @param DOMElement $element The DOM element involved.
* @param array $attributes An associative array of attributes.
* @param string $value The new value of an attribute (if applicable).
* @param string $comment An optional comment describing the operation.
*/
function logDomOperation(string $action, DOMElement $element, array $attributes = [], string $value = '', string $comment = ''): void
{
// Define the log file path. Consider using a configurable path.
$logFilePath = 'dom_operations.log';
// Format the log entry.
$logEntry = date('Y-m-d H:i:s') . " - Action: $action\n";
$logEntry .= " Element ID: " . $element->id . "\n";
$logEntry .= " Element Tag: " . $element->tagName . "\n";
if (!empty($attributes)) {
$logEntry .= " Attributes:\n";
foreach ($attributes as $attribute => $attributeValue) {
$logEntry .= " $attribute: $attributeValue\n";
}
}
if (!empty($value)) {
$logEntry .= " New Value: $value\n";
}
if (!empty($comment)) {
$logEntry .= " Comment: $comment\n";
}
$logEntry .= "----------------------------------------\n";
// Append the log entry to the file.
file_put_contents($logFilePath, $logEntry, FILE_APPEND);
}
// Example usage (for testing):
/*
$element = new DOMElement('div');
$element->id = 'myDiv';
$element->setAttribute('class', 'staging-element');
$element->setAttribute('data-value', 'initial');
logDomOperation('create', $element, ['class' => 'staging-element', 'data-value' => 'initial'], 'new value', 'Created a staging div element.');
$element->setAttribute('class', 'updated-class');
logDomOperation('update', $element, ['class' => 'staging-element' => 'updated-class'], 'updated class', 'Updated the class attribute.');
$element->removeAttribute('data-value');
logDomOperation('delete', $element, ['data-value'], 'data-value', 'Deleted the data-value attribute.');
*/
?>
Add your comment