A smarter alternative to var_dump for PHP5.6+
The goal is to provide a friendly debug tool that is framework independent and works across multiple environments.
This project is still in beta. Backwards incompatible changes might be introduced during this time.
composer require --dev bonemeijer/smartdump
By default, SmartDump is configured to detect where you are debugging from and will output either plaintext or html. For example: when you dump from the commandline, a plaintext output is used. But when you are debugging from a browser, an interactive HTML output is used.
There is a main SmartDump
class, which provides a friendly global interface to debug a variable anywhere
in your code. But don't worry, you can use and configure your own instances too.
\SmartDump\SmartDump::dump($variable);
Even easier are the shortcut functions. These live in the global namespace so you don't have to do any imports when you quickly want to debug a variable.
Available functions:
smartdump($variable)
- shortcut for\SmartDump\SmartDump::dump($variable);
smartdump_text($variable)
- dump plaintext to outputsmartdump_html($variable)
- dump simplehtml to outputsmartdump_text_stream
- dump plaintext to a file or urld($variabe)
- dump a context aware format to outputo($variabe)
- dump a context aware format to output, clear output buffer and exit()
TIP - the shortcut function smartdump()
uses the global SmartDump class, so you can configure the desired
behaviour through the SmartDump class.
SmartDump will internally convert any passed variable to a Node using a NodeFactory. These Nodes will then be passed to a Dumper, which will accept Formatter to render an output format.
A Node is basically an internal representation of a variable. The DefaultNodeFactory
has support for
all regular PHP variable types like booleans, strings, integers, floats, arrays, objects, resources and
NULL values.
A formatter formats the Node to output that can be handles by a Dumper. At this time there are only string type formatters, which will render a Node to a string.
By default, the ContextAwareStringFormatter
will be used, which will return a formatter based on where
you are debugging from.
Available formatters:
PlainTextStringFormatter
- formats to plaintext, for use in commandline and other text based environmentsDomStringFormatter
- can use aMarkup
document to output HTML (or someday XML or any other markup language)ContextAwareStringFormatter
- switches between plaintext and dom based on the current environmentCallBackStringFormatter
- accepts callbacks to determine which formatter is used
To globally use a different formatter for all SmartDump::dump()
calls:
\SmartDump\SmartDump::setFormatter(
new \SmartDump\Formatter\StringFormatter\PlainTextStringFormatter()
);
A Dumper will determine how the formatted node will be handled. By default, the OutputDumper
is used,
which will just echo the output to the screen.
Available dumpers:
OutputDumper
- simply echo's the outputStreamDumper
- dumps the formatted output to a file or stream
To globally use a different dumper for all SmartDump::dump()
calls:
\SmartDump\SmartDump::setDumper(
new \SmartDump\Dumper\StreamDumper('output.txt')
);
Don't. Don't deploy debug tools. Which is why I recommend using the --dev
flag during installation.
SmartDump uses SemVer for versioning. For the versions available, see the tags on this repository.
- Maurice Bonemeijer - Initial work - Bonemeijer
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details