-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[VarDumper] Only select HtmlDumper
if Accept
header is set with non-cli SAPI
#58070
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
base: 7.3
Are you sure you want to change the base?
Conversation
The implementation does not seem to do what you describe. It still uses the HtmlDumper for all non-CLI SAPIs but uses it also for some cases of CLI SAPIs. |
I'm not sure to get what you mean. The goal is to use CliDumper if we're in a CLI env, just as before. But if we're in another env or the Accept header is present with If you debug a JSON API, you don't want to HTML output because it messes with the output. |
This is not what your new condition does |
I'm sorry, I don't get where the condition is wrong 😕 Could you maybe precisely point at the mistake please?
It is the current behavior. All this PR does is "forcing" the HTML output in case of the Accept header. |
@alexandre-daubois in your condition, anything that is not a CLI SAPI will still use the HtmlDumper. what you changed is that you use the HtmlDumper in more cases, not less cases. |
d44dea6
to
fd73973
Compare
Alright, I think I figured out the problem, thank you for your explanation. Is it better now? If the header is present with the good value, we chose HtmlDumper. Otherwise, we fallback on CliDumper (just like proposed in #54772 (comment) actually). At this point, I'm not 100% sure if this should be considered a bug or a feature 🤔 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To me, we should keep CliDumper as the best guess for CLI sapis instead of trying to read $_SERVER['HTTP_ACCEPT']
for them
I would treat it as a feature. |
13dbdcc
to
21042df
Compare
Accept: text/html
is setAccept
header is set with non-cli SAPI
21042df
to
6144432
Compare
It is now updated with your suggestions, thank you! |
I would recommend to create a new "plain text only" dumper, and use that as the default fallback (if no accept html header is present) |
You are right, the CliDumper does not output anything if it is not in a terminal. I tried to see what would be the result of CliDumper in Chrome dev tool and it is just empty. Thus the problem I reported in 54772 (and probably the one described in 49754 as well) is not solved by using CLiDumper instead of HtmlDumper. A plain text dumper is indeed necessary. |
@pierresh I think that your case could (and should) be solved by launching a local VarDumper server so the output doesn't messes up with your API response |
a dd() will (and should) always mess up the output, no one excepts valid formatted json output if they call dd(), else you would return a new JsonResponse() instead of calling dd(). Also when debugging production / staging / docker etc, its not always possible to launch a VarDumper server. |
I'm not convinced that using I think that the conversation is moving away from the original purpose of the PR. Perhaps it's a good idea to continue the discussion in the respective issues to avoid any confusion? 🙂 |
6144432
to
52d377f
Compare
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? null; | ||
if (null === $acceptHeader || str_contains($acceptHeader, 'text/html') || str_contains($acceptHeader, '*/*')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure why we should use html when */*
is found but not html
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? null; | |
if (null === $acceptHeader || str_contains($acceptHeader, 'text/html') || str_contains($acceptHeader, '*/*')) { | |
if (str_contains($_SERVER['HTTP_ACCEPT'] ?? 'html', 'html')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the header is present and we reach this condition, we are necessarily in a SAPI context other than CLI. So I'm thinking that it's still worth rendering the HTML if the client sends "accept all". It seems more interesting to me than using CliDumper with the various escape characters that won't really work outside of a terminal. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used by Chrome dev tools (accept: application/json, text/plain, */*
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, updated 👍
52d377f
to
27d141c
Compare
27d141c
to
ca1dea4
Compare
Does this really work on Symfony apps? Don't we register another handler that'd also need a similar logic, but that'd use the request_stack instead of the globals to get the accept header? |
I tried with a new Symfony app and a dummy controller like the following: class HomeController
{
#[Route(path: '/')]
public function home(): Response
{
dd(PHP_SAPI);
}
} It dumped the value in the CLI, and dumped as HTML in the browser when adding |
Accept
header is set with non-cli SAPIHtmlDumper
if Accept
header is set with non-cli SAPI
dump()
anddd()
are very practical but sometimes a little too verbose, typically when debugging an JSON API for example. This change will only enable HtmlDumper when theAccept
header exists and contains eithertext/html
or*/*
in non-cli SAPI env.