Skip to content

Commit d83167d

Browse files
committed
feature symfony#58287 [WebProfilerBundle] Render the toolbar stylesheet (smnandre)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [WebProfilerBundle] Render the toolbar stylesheet | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | Fix #... | License | MIT Render the (static) toolbar stylesheet separately from the (dynamic) toolbar content. (avoid the 20ko inlined CSS injection on every page) Commits ------- c36fcff [WebProfilerBundle] Render the toolbar stylesheet
2 parents 6e9c993 + c36fcff commit d83167d

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,27 @@ public function toolbarAction(Request $request, ?string $token = null): Response
162162
]);
163163
}
164164

165+
/**
166+
* Renders the Web Debug Toolbar stylesheet.
167+
*
168+
* @throws NotFoundHttpException
169+
*/
170+
public function toolbarStylesheetAction(): Response
171+
{
172+
$this->denyAccessIfProfilerDisabled();
173+
174+
$this->cspHandler?->disableCsp();
175+
176+
return new Response(
177+
$this->twig->render('@WebProfiler/Profiler/toolbar.css.twig'),
178+
200,
179+
[
180+
'Content-Type' => 'text/css',
181+
'Cache-Control' => 'max-age=600, private',
182+
],
183+
);
184+
}
185+
165186
/**
166187
* Renders the profiler search bar.
167188
*
@@ -383,6 +404,9 @@ protected function getTemplateManager(): TemplateManager
383404
return $this->templateManager ??= new TemplateManager($this->profiler, $this->twig, $this->templates);
384405
}
385406

407+
/**
408+
* @throws NotFoundHttpException
409+
*/
386410
private function denyAccessIfProfilerDisabled(): void
387411
{
388412
if (null === $this->profiler) {

src/Symfony/Bundle/WebProfilerBundle/Resources/config/routing/wdt.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
55
xsi:schemaLocation="http://symfony.com/schema/routing https://symfony.com/schema/routing/routing-1.0.xsd">
66

7+
<route id="_wdt_stylesheet" path="/styles.css">
8+
<default key="_controller">web_profiler.controller.profiler::toolbarStylesheetAction</default>
9+
</route>
10+
711
<route id="_wdt" path="/{token}">
812
<default key="_controller">web_profiler.controller.profiler::toolbarAction</default>
913
</route>

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_js.html.twig

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
}) }}
1010
</div>
1111

12-
<style{% if csp_style_nonce %} nonce="{{ csp_style_nonce }}"{% endif %}>
13-
{{ include('@WebProfiler/Profiler/toolbar.css.twig') }}
14-
</style>
12+
<link rel="stylesheet"{% if csp_style_nonce %} nonce="{{ csp_style_nonce }}"{% endif %} href="{{ url('_wdt_stylesheet') }}" />
1513

1614
{# CAUTION: the contents of this file are processed by Twig before loading
1715
them as JavaScript source code. Always use '/*' comments instead

src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/ProfilerControllerTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,33 @@ public function testToolbarActionWithEmptyToken($token)
137137
$this->assertEquals(200, $response->getStatusCode());
138138
}
139139

140+
public function testToolbarStylesheetActionWithProfilerDisabled()
141+
{
142+
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
143+
$twig = $this->createMock(Environment::class);
144+
145+
$controller = new ProfilerController($urlGenerator, null, $twig, []);
146+
147+
$this->expectException(NotFoundHttpException::class);
148+
$this->expectExceptionMessage('The profiler must be enabled.');
149+
150+
$controller->toolbarStylesheetAction();
151+
}
152+
153+
public function testToolbarStylesheetAction()
154+
{
155+
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
156+
$twig = $this->createMock(Environment::class);
157+
$profiler = $this->createMock(Profiler::class);
158+
159+
$controller = new ProfilerController($urlGenerator, $profiler, $twig, []);
160+
161+
$response = $controller->toolbarStylesheetAction();
162+
$this->assertSame(200, $response->getStatusCode());
163+
$this->assertSame('text/css', $response->headers->get('Content-Type'));
164+
$this->assertSame('max-age=600, private', $response->headers->get('Cache-Control'));
165+
}
166+
140167
public static function getEmptyTokenCases()
141168
{
142169
return [

0 commit comments

Comments
 (0)