Skip to content

[WebProfilerBundle] add debugbar on StreamedResponse #58789

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

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `ajax_replace` option for replacing toolbar on AJAX requests
* Show debug bar when using a streamed response
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Show debug bar when using a streamed response
* Add support for streamed responses in the debug toolbar


7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
Expand Down Expand Up @@ -110,7 +111,14 @@ public function onKernelResponse(ResponseEvent $event): void
$session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
}

$response->setContent($this->twig->render('@WebProfiler/Profiler/toolbar_redirect.html.twig', ['location' => $response->headers->get('Location'), 'host' => $request->getSchemeAndHttpHost()]));
if ($response instanceof StreamedResponse) {
$twig = $this->twig;
$response->setCallback(function () use ($twig, $request, $response): void {
echo $twig->render('@WebProfiler/Profiler/toolbar_redirect.html.twig', ['location' => $response->headers->get('Location'), 'host' => $request->getSchemeAndHttpHost()]);
});
} else {
$response->setContent($this->twig->render('@WebProfiler/Profiler/toolbar_redirect.html.twig', ['location' => $response->headers->get('Location'), 'host' => $request->getSchemeAndHttpHost()]));
}
$response->setStatusCode(200);
$response->headers->remove('Location');
}
Expand All @@ -133,26 +141,51 @@ public function onKernelResponse(ResponseEvent $event): void
*/
protected function injectToolbar(Response $response, Request $request, array $nonces): void
{
$content = $response->getContent();
$pos = strripos($content, '</body>');

if (false !== $pos) {
$toolbar = "\n".str_replace("\n", '', $this->twig->render(
'@WebProfiler/Profiler/toolbar_js.html.twig',
[
'full_stack' => class_exists(FullStack::class),
'excluded_ajax_paths' => $this->excludedAjaxPaths,
'token' => $response->headers->get('X-Debug-Token'),
'request' => $request,
'csp_script_nonce' => $nonces['csp_script_nonce'] ?? null,
'csp_style_nonce' => $nonces['csp_style_nonce'] ?? null,
]
))."\n";
$content = substr($content, 0, $pos).$toolbar.substr($content, $pos);
$response->setContent($content);
if ($response instanceof StreamedResponse) {
$callback = $response->getCallback();
$toolbarHTMLContent = "\n".str_replace("\n", '', $this->getToolbarHTML($request, $response->headers->get('X-Debug-Token'), $nonces))."\n";
$injectedCallback = static function () use ($toolbarHTMLContent, $callback): void {
ob_start(function (string $buffer, int $phase) use ($toolbarHTMLContent): string {
$pos = strripos($buffer, '</body>');
if (false !== $pos) {
$buffer = substr($buffer, 0, $pos).$toolbarHTMLContent.substr($buffer, $pos);
}

return $buffer;
}, 8); // length of '</body>'

($callback)();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
($callback)();
$callback();

ob_end_flush();
};
$response->setCallback($injectedCallback);
} else {
$content = $response->getContent();
$pos = strripos($content, '</body>');

if (false !== $pos) {
$toolbar = "\n".str_replace("\n", '', $this->getToolbarHTML($request, $response->headers->get('X-Debug-Token'), $nonces))."\n";

$content = substr($content, 0, $pos).$toolbar.substr($content, $pos);
$response->setContent($content);
}
}
}

private function getToolbarHTML(Request $request, ?string $debugToken, array $nonces): string
{
return $this->twig->render(
'@WebProfiler/Profiler/toolbar_js.html.twig',
[
'full_stack' => class_exists(FullStack::class),
'excluded_ajax_paths' => $this->excludedAjaxPaths,
'token' => $debugToken,
'request' => $request,
'csp_script_nonce' => $nonces['csp_script_nonce'] ?? null,
'csp_style_nonce' => $nonces['csp_style_nonce'] ?? null,
]
);
}

public static function getSubscribedEvents(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WebDebugToolbarListenerTest extends TestCase
/**
* @dataProvider getInjectToolbarTests
*/
public function testInjectToolbar($content, $expected)
public function testInjectToolbar(string $content, string $expected)
{
$listener = new WebDebugToolbarListener($this->getTwigMock());
$m = new \ReflectionMethod($listener, 'injectToolbar');
Expand Down Expand Up @@ -60,7 +60,7 @@ public static function getInjectToolbarTests()
/**
* @dataProvider provideRedirects
*/
public function testHtmlRedirectionIsIntercepted($statusCode)
public function testHtmlRedirectionIsIntercepted(int $statusCode)
{
$response = new Response('Some content', $statusCode);
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
Expand All @@ -75,7 +75,7 @@ public function testHtmlRedirectionIsIntercepted($statusCode)

public function testNonHtmlRedirectionIsNotIntercepted()
{
$response = new Response('Some content', '301');
$response = new Response('Some content', 301);
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
$event = new ResponseEvent($this->createMock(Kernel::class), new Request([], [], ['_format' => 'json']), HttpKernelInterface::MAIN_REQUEST, $response);

Expand Down Expand Up @@ -136,7 +136,7 @@ public function testToolbarIsNotInjectedOnContentDispositionAttachment()
*
* @dataProvider provideRedirects
*/
public function testToolbarIsNotInjectedOnRedirection($statusCode)
public function testToolbarIsNotInjectedOnRedirection(int $statusCode)
{
$response = new Response('<html><head></head><body></body></html>', $statusCode);
$response->headers->set('X-Debug-Token', 'xxxxxxxx');
Expand Down Expand Up @@ -417,7 +417,7 @@ public function testAjaxReplaceHeaderOnEnabledAndXHRButPreviouslySet()
$this->assertSame('0', $response->headers->get('Symfony-Debug-Toolbar-Replace'));
}

protected function getTwigMock($render = 'WDT')
protected function getTwigMock(string $render = 'WDT')
{
$templating = $this->createMock(Environment::class);
$templating->expects($this->any())
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy