Dumper
in package
A class that dumps variables and exception in a nice formatting.
Tags
Table of Contents
- VAR_DUMP_CONVERSIONS = [ // replace unnecessary line breaks after arrow with spaces only '/(=>)\s*(.+)/' => ' $1 $2', // replace opening curly brace with opening square bracket '/{\n/' => "[ ", // replace closing curly brace with closing square bracket '/}\n/' => "] ", // replace multiline empty square brackets with single line square brackets '/\[\n\s*\]/' => "[]", // add comma to all line endings except the ones wrapped in double quotes and the ones preceded by opening brackets '/(?<!\[)\n(?=([^"]*["][^"]*["])*[^"]*$)/' => ", ", // add object type info as comment after array opening bracket '/&?(object\(.+\))(#\d+) \(\d+\) (\[)/' => '/* $1 [SPL-ID: $2] */ $3', // add resource type info as comment in a single line '/&?(resource\(\d+\) ([\w ]+) \((\w+)\))(,)*/' => '/* $1 */ "$3"$4', // remove the type hint and variable length for strings, and arrays at the beginning of line '/^&?(?:string|array|\w+)(\(.+\)) /m' => '', // remove the type hint and variable length for strings, and arrays after arrow '/(=>) &?(?:string|array|\w+)(\(.+\)) ([\["])/' => '$1 $3', // replace bool($var), int($var), float($var) with $var '/&?(?:bool|int|float)\((.+?)\)/' => '$1', // replace NULL with null '/NULL/' => 'null', // replace all backslashes with escaped backslashes '/(\\)/' => '\\$1', // replace all single quotes with an escaped single quotes '/(')/' => '\\$1', // replace private visibility with a better formatted one '/\["(.+?)":"(.+)":(private)\]/' => '["$1":$3($2)]', // replace key with visibility in double quotes in square brackets with key in single quotes and add visibility as comment '/\["(.+?)":(.+?)\] (=>) (.+)/' => "'$1' $3 /* $2 */ $4", // replace key in double quotes in square brackets with key in single quotes '/\["(.*)"\] (=>)/' => "'$1' $2", // replace numeric key in square brackets with key '/\[(-?\d+)\] (=>)/' => '$1 $2', // replace string opening double quotes with single quotes '/(=>)([ ]\/\*.*\*\/)? "/' => "$1$2 '", // replace string closing double quotes with single quotes '/(.+)"(,)( \/\/.*)?\n/' => "$1'$2$3 ", // replace double quotes at the beginning of line with single quotes '/^"/m' => "'", // combine consequent comments with semicolon '/[ ]\*\/ \/\*[ ]/' => '; ', // replace *RECURSION* with __RECURSION__ '/\*(RECURSION)\*/' => '__$1__', ]
- Regular expressions to transform `var_dump()` result from var dump syntax to a valid square brackets array (`[]`).
- VAR_EXPORT_CONVERSIONS = [ // replace array construct opening alone '/array \(/' => '[', // replace array construct opening inside a function call '/(\()array\(/' => '$1[', // replace array construct opening for stdClass '/\(object\) array\(/' => '(object)[', // replace array construct closing not part of a string '/\)(\))(?=([^']*'[^']*')*[^']*$)/' => ']$1', // replace array construct closing alone '/^([ ]*)\)(,?)$/m' => '$1]$2', // replace array construct closing inside a function call '/(\n)([ ]*)\]\)/' => '$1$2])', // replace array key with nested array '/([ ]*)('[^']+') => ([\['])/' => '$1$2 => $3', // replace array construct/bracket opening after arrow with newline and spaces '/=>[ ]?\n[ ]+(\[|\()/' => '=> $1', // replace any valid php after arrow with a newline and spaces '/=>[ ]?\n[ ]+([a-zA-Z0-9_\x7f-\xff])/' => '=> $1', // replace empty array brackets array with a newline and spaces '/\[[ ]?\n[ ]*\]/' => '[]', // replace NULL with null '/NULL/' => 'null', ]
- Regular expressions to transform `var_export()` result from array construct (`array()`) to valid square brackets array (`[]`).
- $accentColor : string
- Accent color of exceptions page and dump block.
- $contrastColor : string
- Contrast color of exceptions page and dump block.
- $styles : array<string|int, mixed>
- Dumper CSS styles.
- $syntaxHighlightColors : array<string|int, mixed>
- Colors of syntax tokens.
- $syntaxHighlightStyles : array<string|int, mixed>
- Additional CSS styling of syntax tokens.
- $useVarDump : bool
- Whether or not to use `var_dump()` instead of `var_export()` to dump the variables.
- $syntaxHighlightTokens : array<string|int, mixed>
- PHP highlighting syntax tokens.
- dd() : void
- Dumps a variable and dies.
- dump() : void
- Dumps a variable in a nice HTML block with syntax highlighting.
- dumpException() : void
- Dumps an exception in a nice HTML page or as string and exits the script.
- exportExpression() : string
- Dumps an expression using `var_export()` or `var_dump()`.
- varDump() : string
- Returns dump of the passed variable using `var_dump()`.
- varExport() : string
- Returns dump of the passed variable using `var_export()`.
- exportExpressionWithSyntaxHighlighting() : string
- Dumps an expression using `var_export()` or `var_dump()` with syntax highlighting.
- getAnsiCodeFromHexColor() : int
- Converts a hex color to the closest standard ANSI color code.
- getDumpingBlocks() : void
- Returns an array containing HTML/ANSI wrapping blocks.
- getValidCallerTrace() : string
- Returns the last caller trace before `dd()` or `dump()` if the format of `file:line`.
- highlightFile() : string
- Highlights the passed file with the possibility to focus a specific line.
- isCli() : bool
- Checks whether the script is currently running in CLI mode or not.
- setSyntaxHighlighting() : void
- Sets PHP syntax highlighting colors according to current class state.
- tabulateStacktrace() : string
- Tabulates the passed stacktrace in an HTML table.
Constants
VAR_DUMP_CONVERSIONS
Regular expressions to transform `var_dump()` result from var dump syntax to a valid square brackets array (`[]`).
protected
array<string|int, mixed>
VAR_DUMP_CONVERSIONS
= [
// replace unnecessary line breaks after arrow with spaces only
'/(=>)\s*(.+)/' => ' $1 $2',
// replace opening curly brace with opening square bracket
'/{\n/' => "[
",
// replace closing curly brace with closing square bracket
'/}\n/' => "]
",
// replace multiline empty square brackets with single line square brackets
'/\[\n\s*\]/' => "[]",
// add comma to all line endings except the ones wrapped in double quotes and the ones preceded by opening brackets
'/(?<!\[)\n(?=([^"]*["][^"]*["])*[^"]*$)/' => ",
",
// add object type info as comment after array opening bracket
'/&?(object\(.+\))(#\d+) \(\d+\) (\[)/' => '/* $1 [SPL-ID: $2] */ $3',
// add resource type info as comment in a single line
'/&?(resource\(\d+\) ([\w ]+) \((\w+)\))(,)*/' => '/* $1 */ "$3"$4',
// remove the type hint and variable length for strings, and arrays at the beginning of line
'/^&?(?:string|array|\w+)(\(.+\)) /m' => '',
// remove the type hint and variable length for strings, and arrays after arrow
'/(=>) &?(?:string|array|\w+)(\(.+\)) ([\["])/' => '$1 $3',
// replace bool($var), int($var), float($var) with $var
'/&?(?:bool|int|float)\((.+?)\)/' => '$1',
// replace NULL with null
'/NULL/' => 'null',
// replace all backslashes with escaped backslashes
'/(\\)/' => '\\$1',
// replace all single quotes with an escaped single quotes
'/(')/' => '\\$1',
// replace private visibility with a better formatted one
'/\["(.+?)":"(.+)":(private)\]/' => '["$1":$3($2)]',
// replace key with visibility in double quotes in square brackets with key in single quotes and add visibility as comment
'/\["(.+?)":(.+?)\] (=>) (.+)/' => "'$1' $3 /* $2 */ $4",
// replace key in double quotes in square brackets with key in single quotes
'/\["(.*)"\] (=>)/' => "'$1' $2",
// replace numeric key in square brackets with key
'/\[(-?\d+)\] (=>)/' => '$1 $2',
// replace string opening double quotes with single quotes
'/(=>)([ ]\/\*.*\*\/)? "/' => "$1$2 '",
// replace string closing double quotes with single quotes
'/(.+)"(,)( \/\/.*)?\n/' => "$1'$2$3
",
// replace double quotes at the beginning of line with single quotes
'/^"/m' => "'",
// combine consequent comments with semicolon
'/[ ]\*\/ \/\*[ ]/' => '; ',
// replace *RECURSION* with __RECURSION__
'/\*(RECURSION)\*/' => '__$1__',
]
Tags
VAR_EXPORT_CONVERSIONS
Regular expressions to transform `var_export()` result from array construct (`array()`) to valid square brackets array (`[]`).
protected
array<string|int, mixed>
VAR_EXPORT_CONVERSIONS
= [
// replace array construct opening alone
'/array \(/' => '[',
// replace array construct opening inside a function call
'/(\()array\(/' => '$1[',
// replace array construct opening for stdClass
'/\(object\) array\(/' => '(object)[',
// replace array construct closing not part of a string
'/\)(\))(?=([^']*'[^']*')*[^']*$)/' => ']$1',
// replace array construct closing alone
'/^([ ]*)\)(,?)$/m' => '$1]$2',
// replace array construct closing inside a function call
'/(\n)([ ]*)\]\)/' => '$1$2])',
// replace array key with nested array
'/([ ]*)('[^']+') => ([\['])/' => '$1$2 => $3',
// replace array construct/bracket opening after arrow with newline and spaces
'/=>[ ]?\n[ ]+(\[|\()/' => '=> $1',
// replace any valid php after arrow with a newline and spaces
'/=>[ ]?\n[ ]+([a-zA-Z0-9_\x7f-\xff])/' => '=> $1',
// replace empty array brackets array with a newline and spaces
'/\[[ ]?\n[ ]*\]/' => '[]',
// replace NULL with null
'/NULL/' => 'null',
]
Tags
Properties
$accentColor
Accent color of exceptions page and dump block.
public
static string
$accentColor
= '#ff3a60'
$contrastColor
Contrast color of exceptions page and dump block.
public
static string
$contrastColor
= '#030035'
$styles
Dumper CSS styles.
public
static array<string|int, mixed>
$styles
= ['exceptionPage' => ":root{--light:#fff;--dark:#000;--accent-color:%accentColor%;--contrast-color:%contrastColor%;--font-normal:-apple-system,'Fira Sans',Ubuntu,Helvetica,Arial,sans-serif;--font-mono:'Fira Code','Ubuntu Mono',Courier,monospace;--font-base-size:16px;--container-width:85vw;--container-max-width:1364px}@media (max-width:992px){:root{--font-base-size:14px;--container-width:100%;--container-max-width:100vw}}*,::after,::before{box-sizing:border-box;scrollbar-width:thin;scrollbar-color:var(--accent-color) rgba(0,0,0,.15)}::-webkit-scrollbar{width:8px;height:8px;opacity:1;-webkit-appearance:none}::-webkit-scrollbar-thumb{background:var(--accent-color);border-radius:4px}::-webkit-scrollbar-track,::selection{background:rgba(0,0,0,.15)}body{background:var(--light);color:var(--dark);font-family:var(--font-normal);font-size:var(--font-base-size);line-height:1.5;margin:0}h1,h2,h3,h4,h5,h6{margin:0}h1{color:var(--accent-color);font-size:2rem}h2{color:var(--accent-color);font-size:1.75rem}h3{color:var(--light)}p{font-size:1rem;margin:1rem 0}a{color:var(--accent-color)}a:hover{text-decoration:underline}ul{padding:1.5rem 1rem;margin:1rem 0}li{white-space:pre;list-style-type:none}pre{white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word}.monospace,code{font-family:var(--font-mono);word-wrap:break-word;word-break:break-all}.container{width:var(--container-width);max-width:var(--container-max-width);min-height:100vh;background:var(--light);padding:7vh calc((var(--container-max-width) * .03)) 10vh;margin:0 auto;overflow:hidden}.capture-section,.info-section,.trace-section{margin-bottom:3rem}.message{background:var(--accent-color);color:var(--light);padding:2rem 1rem 1rem 1rem}.scrollable{overflow-x:scroll}.code{display:block;width:max-content;min-width:100%;background:var(--contrast-color);font-family:var(--font-mono);font-size:.875rem;margin:0;overflow-y:scroll;-ms-overflow-style:none;scrollbar-width:none;cursor:initial}.code::-webkit-scrollbar{display:none}.code *{background:0 0}.code-line{display:inline-block;width:calc(3ch + (2 * .75ch));background:rgba(255,255,255,.25);color:var(--light);text-align:right;padding:.25rem .75ch;margin:0 1.5ch 0 0;user-select:none}.code-line.exception-line{color:var(--accent-color);font-weight:700}.code-line.exception-line+code>span>span:not(:first-child){padding-bottom:3px;border-bottom:2px solid var(--accent-color)}.button{display:inline-block;vertical-align:baseline;background:var(--accent-color);color:var(--light);font-size:1rem;text-decoration:none;padding:.5rem 1rem;margin:0 0 1rem 0;border:none;border-radius:2.5rem;cursor:pointer}.button:hover{background:var(--contrast-color);text-decoration:inherit}.button:last-child{margin-bottom:0}.table{width:100%;border-collapse:collapse;border-spacing:0}.table .table-cell{padding:.75rem}.table .table-head .table-cell{background:var(--contrast-color);color:var(--light);text-align:left;padding-top:.75rem;padding-bottom:.75rem}.table-cell.compact{width:1%}.table-row{background:var(--light);border-top:1px solid rgba(0,0,0,.15)}.table .table-row:hover{background:rgba(0,0,0,.065)!important}.table .table-row.additional .table-cell{padding:0}.table .table-row.odd,.table .table-row.odd+.additional{background:var(--light)}.table .table-row.even,.table .table-row.even+.additional{background:rgba(0,0,0,.035)}.table .table-row.even+.additional,.table .table-row.odd+.additional{border-top:none}.pop-up{cursor:help}.line,.number{text-align:center}.class,.function{font-size:.875rem;font-weight:700}.arguments{white-space:nowrap}.argument{display:inline-block;background:rgba(0,0,0,.125);color:var(--accent-color);font-size:.875rem;font-style:italic;padding:.125rem .5rem;margin:0 .25rem 0 0;border-radius:2.5rem}.argument:hover{background:var(--accent-color);color:var(--contrast-color)}.accordion{cursor:pointer;position:relative}.accordion-summary{width:1.5rem;height:1.5rem;background:var(--accent-color);color:var(--light);line-height:1.5rem;text-align:center;list-style:none;border-radius:50%;position:absolute;top:-2.2925rem;left:1.425rem;user-select:none;cursor:pointer}.accordion-summary:hover{background:var(--contrast-color)}.accordion-details{padding:0}", 'traceBlock' => "background:#fff;color:%accentColor%;font-family:-apple-system,'Fira Sans',Ubuntu,Helvetica,Arial,sans-serif;font-size:12px;padding:4px 8px;margin-bottom:18px;", 'dumpBlock' => "display:table;background:%contrastColor%;color:#fff;font-family:'Fira Code','Ubuntu Mono',Courier,monospace;font-size:18px;padding:18px;margin-bottom:8px;", 'timeBlock' => "display:table;background:%accentColor%;color:#fff;font-family:'Fira Code','Ubuntu Mono',Courier,monospace;font-size:12px;font-weight:bold;padding:12px;margin-bottom:8px;", 'detailsBlock' => "background:%accentColor%;color:#fff;font-family:-apple-system,'Fira Sans',Ubuntu,Helvetica,Arial,sans-serif;font-size:12px;font-weight:bold;padding:12px;margin-bottom:8px;cursor:pointer;user-select:none;"]
The array contains styles for:
-
exceptionPage
-
traceBlock
-
dumpBlock
-
timeBlock
-
detailsBlock
Currently set dumper colors can be inject in CSS using the %accentColor%
and %contrastColor%
placeholders.
Tags
$syntaxHighlightColors
Colors of syntax tokens.
public
static array<string|int, mixed>
$syntaxHighlightColors
= ['comment' => '#aeaeae', 'keyword' => '#00bfff', 'string' => '#e4ba80', 'default' => '#e8703a', 'html' => '#ab8703']
$syntaxHighlightStyles
Additional CSS styling of syntax tokens.
public
static array<string|int, mixed>
$syntaxHighlightStyles
= ['comment' => 'font-weight: lighter;', 'keyword' => 'font-weight: bold;', 'string' => '', 'default' => '', 'html' => '']
$useVarDump
Whether or not to use `var_dump()` instead of `var_export()` to dump the variables.
public
static bool
$useVarDump
= false
NOTE: The dumper will always fall back to var_dump()
if var_export()
fails.
$syntaxHighlightTokens
PHP highlighting syntax tokens.
private
static array<string|int, mixed>
$syntaxHighlightTokens
= ['comment', 'keyword', 'string', 'default', 'html']
Methods
dd()
Dumps a variable and dies.
public
static dd(mixed ...$variable) : void
Parameters
- $variable : mixed
Tags
Return values
void —The result will simply get echoed.
dump()
Dumps a variable in a nice HTML block with syntax highlighting.
public
static dump(mixed ...$variable) : void
Parameters
- $variable : mixed
Return values
void —The result will simply get echoed.
dumpException()
Dumps an exception in a nice HTML page or as string and exits the script.
public
static dumpException(Throwable $exception) : void
Parameters
- $exception : Throwable
Tags
Return values
void —The result will be echoed as HTML page or a string representation of the exception if the interface is CLI.
exportExpression()
Dumps an expression using `var_export()` or `var_dump()`.
public
static exportExpression(mixed $expression) : string
Parameters
- $expression : mixed
Return values
string —varDump()
Returns dump of the passed variable using `var_dump()`.
protected
static varDump(mixed $variable) : string
Parameters
- $variable : mixed
Tags
Return values
string —varExport()
Returns dump of the passed variable using `var_export()`.
protected
static varExport(mixed $variable) : string
Parameters
- $variable : mixed
Tags
Return values
string —exportExpressionWithSyntaxHighlighting()
Dumps an expression using `var_export()` or `var_dump()` with syntax highlighting.
private
static exportExpressionWithSyntaxHighlighting(mixed $expression[, string|null $phpReplacement = '' ]) : string
Parameters
- $expression : mixed
- $phpReplacement : string|null = ''
-
<?php
replacement.
Return values
string —getAnsiCodeFromHexColor()
Converts a hex color to the closest standard ANSI color code.
private
static getAnsiCodeFromHexColor(string $color) : int
Standard ANSI colors include: black, red, green, yellow, blue, magenta, cyan and white.
Parameters
- $color : string
Return values
int —getDumpingBlocks()
Returns an array containing HTML/ANSI wrapping blocks.
private
static getDumpingBlocks() : void
Available blocks are: traceBlock
, dumpBlock
, timeBlock
, and detailsBlock
.
All this blocks will contain a placeholder for a *printf()
function to inject content.
Return values
void —getValidCallerTrace()
Returns the last caller trace before `dd()` or `dump()` if the format of `file:line`.
private
static getValidCallerTrace() : string
Return values
string —highlightFile()
Highlights the passed file with the possibility to focus a specific line.
private
static highlightFile(string $file[, int $line = null ]) : string
Parameters
- $file : string
-
The file to highlight.
- $line : int = null
-
The line to focus.
Tags
Return values
string —The hightailed file as HTML.
isCli()
Checks whether the script is currently running in CLI mode or not.
private
static isCli() : bool
Return values
bool —setSyntaxHighlighting()
Sets PHP syntax highlighting colors according to current class state.
private
static setSyntaxHighlighting() : void
Tags
Return values
void —tabulateStacktrace()
Tabulates the passed stacktrace in an HTML table.
private
static tabulateStacktrace(array<string|int, mixed> $trace) : string
Parameters
- $trace : array<string|int, mixed>
-
Exception stacktrace array.
Tags
Return values
string —The tabulated trace as HTML.