HtmlOutput.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Michel\Debug\Output\BacktraceOutput;
  3. use Michel\Debug\Output\OutputInterface;
  4. final class HtmlOutput implements OutputInterface
  5. {
  6. private $output;
  7. public function __construct(callable $output = null)
  8. {
  9. if ($output === null) {
  10. $output = function (string $dumped) {
  11. echo $dumped;
  12. };
  13. }
  14. $this->output = $output;
  15. }
  16. public function print($value): void
  17. {
  18. if (!is_array($value)) {
  19. return;
  20. }
  21. $html[] = '<style>';
  22. $html[] = file_get_contents(dirname(__DIR__, 3) . '/resources/css/backtrace.css');
  23. $html[] = '</style>';
  24. $html[] = '<div class="__beautify-backtrace-container">';
  25. $html[] = sprintf(
  26. '<div class="__beautify-backtrace-title">Backtrace — last %d call(s)</div>' . PHP_EOL . PHP_EOL,
  27. count($value)
  28. );
  29. foreach ($value as $i => $entry) {
  30. $file = $entry['file'] ?? '[internal]';
  31. $line = $entry['line'] ?? '-';
  32. $function = ($entry['class'] ?? '') . ($entry['type'] ?? '') . $entry['function'];
  33. $html[] = sprintf(
  34. '<div class="__beautify-backtrace-dumper">#%d - %s: Called from %s:%s</div>',
  35. $i + 1,
  36. htmlspecialchars($function, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'),
  37. htmlspecialchars($file, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'),
  38. htmlspecialchars($line, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8')
  39. );
  40. }
  41. $html[] = '</div>';
  42. $outputCallback = $this->output;
  43. $outputCallback(implode('', $html));
  44. }
  45. }