1. <?php
  2. /**
  3. * Logs DOM element operations for staging environments.
  4. *
  5. * @param string $action The action performed (e.g., 'create', 'update', 'delete').
  6. * @param DOMElement $element The DOM element involved.
  7. * @param array $attributes An associative array of attributes.
  8. * @param string $value The new value of an attribute (if applicable).
  9. * @param string $comment An optional comment describing the operation.
  10. */
  11. function logDomOperation(string $action, DOMElement $element, array $attributes = [], string $value = '', string $comment = ''): void
  12. {
  13. // Define the log file path. Consider using a configurable path.
  14. $logFilePath = 'dom_operations.log';
  15. // Format the log entry.
  16. $logEntry = date('Y-m-d H:i:s') . " - Action: $action\n";
  17. $logEntry .= " Element ID: " . $element->id . "\n";
  18. $logEntry .= " Element Tag: " . $element->tagName . "\n";
  19. if (!empty($attributes)) {
  20. $logEntry .= " Attributes:\n";
  21. foreach ($attributes as $attribute => $attributeValue) {
  22. $logEntry .= " $attribute: $attributeValue\n";
  23. }
  24. }
  25. if (!empty($value)) {
  26. $logEntry .= " New Value: $value\n";
  27. }
  28. if (!empty($comment)) {
  29. $logEntry .= " Comment: $comment\n";
  30. }
  31. $logEntry .= "----------------------------------------\n";
  32. // Append the log entry to the file.
  33. file_put_contents($logFilePath, $logEntry, FILE_APPEND);
  34. }
  35. // Example usage (for testing):
  36. /*
  37. $element = new DOMElement('div');
  38. $element->id = 'myDiv';
  39. $element->setAttribute('class', 'staging-element');
  40. $element->setAttribute('data-value', 'initial');
  41. logDomOperation('create', $element, ['class' => 'staging-element', 'data-value' => 'initial'], 'new value', 'Created a staging div element.');
  42. $element->setAttribute('class', 'updated-class');
  43. logDomOperation('update', $element, ['class' => 'staging-element' => 'updated-class'], 'updated class', 'Updated the class attribute.');
  44. $element->removeAttribute('data-value');
  45. logDomOperation('delete', $element, ['data-value'], 'data-value', 'Deleted the data-value attribute.');
  46. */
  47. ?>

Add your comment