Skip to content
Merged
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 support for the `QUERY` HTTP method in the profiler
* Add support for Server-Sent Events / `EventSource` requests in the debug toolbar

7.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
use Symfony\Bundle\FullStack;
use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\EventStreamResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ServerEvent;
use Symfony\Component\HttpFoundation\Session\Flash\AutoExpireFlashBag;
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
Expand Down Expand Up @@ -115,6 +117,27 @@ public function onKernelResponse(ResponseEvent $event): void
$response->headers->remove('Location');
}

if ($response->headers->has('X-Debug-Token') && $response instanceof EventStreamResponse) {
$callback = $response->getCallback();
$response->setCallback(static function () use ($callback, $response) {
$response->sendEvent(new ServerEvent(
[
$response->headers->get('X-Debug-Token') ?? '',
$response->headers->get('X-Debug-Token-Link') ?? '',
],
'symfony:debug:started',
));
try {
$callback();
} catch (\Throwable $e) {
$response->sendEvent(new ServerEvent('error', 'symfony:debug:error'));
throw $e;
} finally {
$response->sendEvent(new ServerEvent('-', 'symfony:debug:finished'));
}
});
}

if (self::DISABLED === $this->mode
|| !$response->headers->has('X-Debug-Token')
|| $response->isRedirection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,51 @@
};

{% if excluded_ajax_paths is defined %}
if (window.EventSource) {
var oldEventSource = window.EventSource;
window.EventSource = function (url, options) {
var es = new oldEventSource(url, options);
if (!url.match(new RegExp({{ excluded_ajax_paths|json_encode|raw }}))) {
var stackElement = {
error: false,
url: url,
method: 'GET',
type: 'event-stream',
start: new Date()
};

var idx = requestStack.push(stackElement) - 1;
startAjaxRequest(idx);
addEventListener(es, 'error', function () {
stackElement.error = true;
finishAjaxRequest(idx);
});
addEventListener(es, 'open', function () {
stackElement.statusCode = 200;
stackElement.toolbarReplaceFinished = false;
stackElement.toolbarReplace = true;
});
addEventListener(es, 'symfony:debug:started', function (event) {
var items = event.data.split('\n');
stackElement.profile = items[0];
stackElement.profilerUrl = items[1];
});
addEventListener(es, 'symfony:debug:error', function (event) {
stackElement.error = true;
stackElement.statusCode = event.data;
finishAjaxRequest(idx);
});
addEventListener(es, 'symfony:debug:finished', function () {
stackElement.duration = new Date() - stackElement.start;
stackElement.toolbarReplaceFinished = false;
stackElement.toolbarReplace = true;
finishAjaxRequest(idx);
});
}

return es;
};
}
if (window.fetch && window.fetch.polyfill === undefined) {
var oldFetch = window.fetch;
window.fetch = function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\WebProfilerBundle\Csp\ContentSecurityPolicyHandler;
use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener;
use Symfony\Component\HttpFoundation\EventStreamResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ServerEvent;
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -401,6 +403,88 @@ public function testAjaxReplaceHeaderOnEnabledAndXHRButPreviouslySet()
$this->assertSame('0', $response->headers->get('Symfony-Debug-Toolbar-Replace'));
}

public function testEventStreamResponseHasDebugEvents()
{
if (!class_exists(EventStreamResponse::class)) {
self::markTestSkipped('This test requires symfony/http-foundation >= 7.3');
}

$request = new Request();
$response = new EventStreamResponse(
fn () => yield new ServerEvent('some data'),
headers: [
'X-Debug-Token' => 'aabbcc',
'X-Debug-Token-Link' => 'test://foobar',
],
);
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock());

$listener->onKernelResponse($event);

$this->expectOutputString(
<<<'EVENTSTREAM'
event: symfony:debug:started
data: aabbcc
data: test://foobar

data: some data

event: symfony:debug:finished
data: -


EVENTSTREAM
);
$response->send(false);
}

public function testEventStreamResponseHasDebugEventForException()
{
if (!class_exists(EventStreamResponse::class)) {
self::markTestSkipped('This test requires symfony/http-foundation >= 7.3');
}

$request = new Request();
$response = new EventStreamResponse(
function () {
yield new ServerEvent('some data');
throw new \RuntimeException('Something went wrong');
},
headers: [
'X-Debug-Token' => 'aabbcc',
'X-Debug-Token-Link' => 'test://foobar',
],
);
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock());

$listener->onKernelResponse($event);

$this->expectOutputString(
<<<'EVENTSTREAM'
event: symfony:debug:started
data: aabbcc
data: test://foobar

data: some data

event: symfony:debug:error
data: error

event: symfony:debug:finished
data: -


EVENTSTREAM
);
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Something went wrong');
$response->send(false);
}

protected function getTwigMock($render = 'WDT')
{
$templating = $this->createMock(Environment::class);
Expand Down
Loading