dump/src/Dump.php

54 lines
1.5 KiB
PHP

<?php declare(strict_types=1);
namespace Dump;
class Dump {
/**
* Latest stack trace found from the debugger
* @var
*/
protected $trace = null;
/**
* @var mixed
*/
protected $content = null;
public function __construct(...$content) {
$this->trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2] ?? [
"line" => -1,
"file" => "unknown",
];
$this->content = $content;
}
public function renderArray() {
return [
'content' => $this->content,
'file' => $this->trace['file'] ?? '?',
'line' => $this->trace['line'] ?? '?',
];
}
public function renderHtml() {
$data = $this->getDumpContent();
$this->trace['file'] ??= "?";
$this->trace['line'] ??= "?";
return <<<HTML
<pre style='-webkit-overflow-scrolling: touch;background-color: #f5f5f5;color: #4a4a4a;font-size: 12px;line-height:18px;overflow-x: auto;padding: 0.5rem 1.5rem;word-wrap: normal;white-space: pre;margin:0.33rem 0;position: relative;z-index:9000000000;'>
<div style='color:#9f9f9f;font-size:10px;' title='{$this->trace['file']}'>[ file: {$this->trace['file']}:{$this->trace['line']} ]</div>
$data
</pre>
HTML;
}
protected function getDumpContent() {
ob_start();
var_dump(...($this->content !== [] ? $this->content : [null]));
return ob_get_clean();
}
}