serendipity_hq/component-var-dumper-f

Adds functions dumpf() and ddf() to Symfony's VarDumper component to be used in CLI scripts to dump to a file.

2.1.4 2024-02-24 15:49 UTC

README

Serendipity HQ VarDumper F

Adds functions `dumpf()` and `ddf()` to [Symfony's VarDumper component](https://symfony.com/doc/current/components/var_dumper.html) that write the dump to a file.

Supports:

Tested with:

Current Status

Coverage Maintainability Rating Quality Gate Status Reliability Rating Security Rating Technical Debt Vulnerabilities

Phan PHPStan PSalm PHPUnit Composer PHP CS Fixer Rector

Do you like this library?
LEAVE A ★

or run
composer global require symfony/thanks && composer thanks
to say thank you to all libraries you use in your current project, this included!

Install VarDumper CLI to HTML via Composer

$ composer require serendipity_hq/component-var-dumper-f --dev

This library follows the http://semver.org/ versioning conventions.

Usage

The library provides two functions: dumpf() and ddf().

They work exactly like the Symfony's VarDumper built ones dump() and dd(), with those specifities:

  1. Both dumpf() and ddf() can be used only in scripts that run in the command line;
  2. Both requires the first parameter is null or an array with some options.

The simplest usage is this:

$var = [
    'a simple string' => "in an array of 5 elements",
    'a float' => 1.0,
    'an integer' => 1,
    'a boolean' => true,
    'an empty array' => [],
];
dumpf(null, $var);
ddf(null, $var);

The result will be an HTML file with a random name that contains the dump of $var.

Opening the file you will see

For more examples of outputs, consult the Symfony's VarDumper documentation about Dump Examples and Output.

Specifying the dumped file

You can specify the file of the dump passing its full path name as first argument:

dumpf('~/path/to/your/folder/filename', $var);
ddf('~/path/to/your/folder/filename', $var);

You can use a name like filename or like filename.html: the resulting file will always have one .html extension (filename.html).

Passing options

Both functions accepts as first argument an array.

The passed array can contain any of the options that you can pass to a Symfony's VarDumper Cloner (reference).

The only additional option you can pass is the file, whit which you can specify the name of the dumped file:

dumpf(['file' => 'dump_var', 'maxDepth' => 2], $var);
ddf(['file' => 'dump_var.html', 'maxDepth' => 5], $var);

In both cases, the file dump_var.html will be generated.

What happens if the file already exists

The new dump will be added to the already existing file.

So, if you call dumpf() three times in your CLI script, then the resulting file dump will have three dumps in it.

If you run again the script, the resulting file dump will now contain six dumps in it: the three from the first run and the three from the second run.

To get a fresh dump, simply remove the dumped files.

Do you like this library?
LEAVE A ★

or run
composer global require symfony/thanks && composer thanks
to say thank you to all libraries you use in your current project, this included!