Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[VarDumper] New env var to select the dump format #25125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 4, 2018

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Nov 22, 2017

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR symfony/symfony-docs#10275

This PR introduces a new environment variable that can be used to force dump() to generate HTML even in CLI, or CLI even in a web context.
It allows to dump large objects when debugging a command, to open the resulting HTML in a browser, and to benefit of the nice JS UI (folded by default, search engine...).

Example usage:

VAR_DUMPER_FORMAT=html vendor/bin/behat > tmp.html; open -a firefox tmp.html
VAR_DUMPER_FORMAT=cli vendor/bin/behat > tmp.txt

@ogizanagi
Copy link
Contributor

You might have missed #23831 :)
Unless you think it's another convenient approach. But it mixes real output & dumps in CLI and #23831 answers other user-cases.

@dunglas
Copy link
Member Author

dunglas commented Nov 23, 2017

Indeed I've missed #23831 and it looks very interesting but I don't think that it addresses the same use case.
This patch is intended for quick & dirty debugging, for instance for a command written in PHP not using Symfony at all, you just install var_dumper (or have it installed) globally and you can still get the HTML output by just prepending an env var.

it mixes real output

Yes it's intended (just as do the CLI formatter by default).

@ogizanagi
Copy link
Contributor

ogizanagi commented Nov 23, 2017

It's always quick & dirty ;) Just trying to make it a bit more clean.
With #23831, you won't even need an env var. Just run your command and if the server is up it'll collect dumps.
Next steps for #23831 could be to provide a simple ServerDumper::register(string $host = null, bool $swallow = true) static method executing the manual wiring described in the PR description with default values so you just have to add one line to your app, and ship the symfony/var-dumper component with a simple script allowing to start the server.

But of course I also understand how a simple env var to prepend is convenient for punctual dumps, so I'm not against this feature at all.

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Nov 23, 2017
@theofidry
Copy link
Contributor

@dunglas I also think you can replace /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome tmp.html by open tmp.html which will use the default browser ;)

@dunglas
Copy link
Member Author

dunglas commented Nov 23, 2017

@theofidry unfortunately I can't because my default browser is Firefox, and Firefox is very bad at reusing an existing instance (it usually ends up by an error).

@ogizanagi
Copy link
Contributor

ogizanagi commented Nov 23, 2017

open -a "Google Chrome" tmp.html

? 😄

@dunglas
Copy link
Member Author

dunglas commented Nov 23, 2017

@ogizanagi great thanks! This one works with Firefox too (but why, it's another topic).

@dunglas
Copy link
Member Author

dunglas commented Dec 31, 2017

Are you ok to merge this one @symfony/deciders? I miss it almost every day 😄.

@@ -29,7 +29,7 @@ public static function dump($var)
{
if (null === self::$handler) {
$cloner = new VarCloner();
$dumper = in_array(PHP_SAPI, array('cli', 'phpdbg')) ? new CliDumper() : new HtmlDumper();
$dumper = in_array(PHP_SAPI, array('cli', 'phpdbg')) && !($_SERVER['VAR_DUMPER_HTML'] ?? false) ? new CliDumper() : new HtmlDumper();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could work both ways: forcing CLI instead of HTML, for API responses

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@lyrixx
Copy link
Member

lyrixx commented Mar 22, 2018

Note: it's related to #23442

@nicolas-grekas nicolas-grekas modified the milestones: 4.1, next Apr 20, 2018
@dunglas dunglas changed the title [VarDumper] Allow to dump HTML from the CLI [VarDumper] New env var to select the dump format Jun 30, 2018
@dunglas
Copy link
Member Author

dunglas commented Jun 30, 2018

PR tweaked to be able to select any format, env var renamed to VAR_DUMPER_FORMAT.

@lyrixx
Copy link
Member

lyrixx commented Jul 2, 2018

I thinks I only works when using the dumper directly.
In a symfony app, the dumper is configured in the bundle.
IMHO this line should be updated too:

if (!\in_array(PHP_SAPI, array('cli', 'phpdbg'), true) && stripos($h[$i], 'html')) {

@dunglas
Copy link
Member Author

dunglas commented Jul 13, 2018

@lyrixx done

@fabpot
Copy link
Member

fabpot commented Sep 4, 2018

Thank you @dunglas.

@fabpot fabpot merged commit 536125a into symfony:master Sep 4, 2018
fabpot added a commit that referenced this pull request Sep 4, 2018
…glas)

This PR was squashed before being merged into the 4.2-dev branch (closes #25125).

Discussion
----------

[VarDumper] New env var to select the dump format

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      |  no
| New feature?  |  yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | todo

This PR introduces a new environment variable that can be used to force `dump()` to generate HTML even in CLI, or CLI even in a web context.
It allows to dump large objects when debugging a command, to open the resulting HTML in a browser, and to benefit of the nice JS UI (folded by default, search engine...).

Example usage:

    VAR_DUMPER_FORMAT=html vendor/bin/behat > tmp.html; open -a firefox tmp.html
    VAR_DUMPER_FORMAT=cli vendor/bin/behat > tmp.txt

Commits
-------

536125a [VarDumper] New env var to select the dump format
@javiereguiluz
Copy link
Member

I've created symfony/symfony-docs#10275 to document this new feature. But please, don't forget to create a PR, or at least an issue, for every new feature. Thanks!

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
This was referenced Nov 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants