Download the PHP package gerardpastor/dumper without Composer
On this page you can find all versions of the php package gerardpastor/dumper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gerardpastor/dumper
More information about gerardpastor/dumper
Files in gerardpastor/dumper
Package dumper
Short Description A simple PHP var dumper
License MIT
Homepage https://github.com/gerardpastor/dumper
Informations about the package dumper
Dumper
Dumper is a simple and ready to use PHP var dumper.
Essentially, its a print_r()
and var_dump()
replacement.
Usage
Install the latest version using composer require gerardpastor/dumper
Enabling all dumping functions is as easy as calling enable()
method on the
main Dumper
class:
The first parameter determines what output to use. This can be a predefined
string or an instance of OutputInterface
.
By default, Dumper includes 3 outputs: dummy, browser and console.
- browser: Dumps variables to browser (uses HTML formatter)
- console: Dumps variables to console (uses Console formatter)
- dummy: Dumps nothing (used for production environment)
Dumping vars
Dumper defines 3 dumping functions:
dumpVars
: Dumps each var in $vars
dump
: Dump $var
dumpAll
: Dump each argument
All of this functions starting with "e" dumps and ends up the execution.
Dumping backtrace
You can dump current debug backtrace with dumpBacktrace()
:
Raw var_dump
Aditionally, Dumper provides rawDump
function that does a native var_dump
inside a <pre>
tag.
Configuration
You can configure some default parameters on Dumper.
Accessing Dumper Instance
To configure Dumper you must acces to its instance.
You can access dumper instance when call enable()
or by calling
getInstance()
when Dumper is already enabled.
Setting default max dumping deep
You can set the default max dumping deep by passing to the VarParser
:
You can override this value in any call to dump
or dumpVars
as the second
argument:
Limiting the number of stack frames in backtrace dumping
By default, Dumper dumps all stack frames in backtrace. You can limit this
number globally by passing to the BacktraceFactory
:
You can override this value in any call to dumpBacktrace
as the first
argument:
Adding excludes to backtrace
Dumper exculdes all namespaces and directories from Dumper, but you can add your
own by passing to the BacktraceFactory
:
Disabling global functions
You can disable the definition of Dumper as global functions by passing
false
as the second argument when calling enable()
.
Then, you can still access dumper functions by calling directly on a dumper instance:
You can enable this global functions at any time by calling
defineGlobalFunctions()
:
Var Tokenizers
Dumper uses tokenizers to convert any variable into a string.
A tokenizer receives a variable and returns a TokenStream
(that is a
collection of Token
)
Dumper provides tokenizers to parse the most generic variable types:
- ObjectTokenizer: Parses an object
- ArrayTokenizer: Parses an array
- StringTokenizer: Parses an string
- GenericTokenizer: Parses any variable (in a very simple way)
Tokenizer has an accept($var)
and a getConfidence()
method.
The parser will use the tokenizer with higher confidence from those which accepted the given variable.
Custom Tokenizers
You can add more specific or sophisticated parsing by adding custom tokenizers.
To do that, you must create a class that implements TokenizerInterface
and
pass to the VarParser
:
Afterwards, pass to VarParser
:
Take a look at provided tokenizers for more specific examples.
Outputs
Dumper can use diferent outputs to show variables to user.
The provided Outputs are:
- BrowserOutput: Prints parsed result to the browser (like
var_dump
) - ConsoleOutput: Prints parsed result to the system console (
php://stdout
,php://output
) - NullOutput: Prints nothing. Can be used to prevent dumping any variable in a production environment.
Using custom Outputs
You can provide your own output by extending Output
class:
And then use it:
Take a look at provided outputs for more specific examples.
Formatters
An Output use a Formatter to format the response. The provided Outputs are:
- HtmlFormatter: Formats result to HTML code.
- ConsoleFormatter: Formats result to console code.
- PlainFormatter: Only format chars like new lines or indentions.
Using custom Formatters
You can provide your own formatter by implementing FormatterInterface
interface:
And then use it:
Take a look at provided formatters for more specific examples.
Using Dumper as Object
The enable()
method simply loads a default configuration, but you can
instantiate Dumper manually, without using enable()
:
If you want the global functions to use your own instance, call setInstance()
on
Dumper
: