8000 feat: extend web profiler config for replace on ajax requests · symfony/symfony@bb0c996 · GitHub
[go: up one dir, main page]

Skip to content

Commit bb0c996

Browse files
committed
feat: extend web profiler config for replace on ajax requests
1 parent c98b02a commit bb0c996

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed

src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.3
5+
---
6+
7+
* Add configuration flag for replacing toolbar on AJAX requests
8+
49
7.2
510
---
611

@@ -65,7 +70,7 @@ CHANGELOG
6570
-----
6671

6772
* added information about orphaned events
68-
* made the toolbar auto-update with info from ajax reponses when they set the
73+
* made the toolbar auto-update with info from ajax responses when they set the
6974
`Symfony-Debug-Toolbar-Replace header` to `1`
7075

7176
4.0.0

src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function getConfigTreeBuilder(): TreeBuilder
3434
$treeBuilder->getRootNode()
3535
->children()
3636
->booleanNode('toolbar')->defaultFalse()->end()
37+
->booleanNode('ajax_replace')->defaultFalse()->end()
3738
->booleanNode('intercept_redirects')->defaultFalse()->end()
3839
->scalarNode('excluded_ajax_paths')->defaultValue('^/((index|app(_[\w]+)?)\.php/)?_wdt')->end()
3940
->end()

src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function load(array $configs, ContainerBuilder $container): void
4949
if ($config['toolbar'] || $config['intercept_redirects']) {
5050
$loader->load('toolbar.php');
5151
$container->getDefinition('web_profiler.debug_toolbar')->replaceArgument(4, $config['excluded_ajax_paths']);
52+
$container->getDefinition('web_profiler.debug_toolbar')->replaceArgument(7, $config['ajax_replace']);
5253
$container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']);
5354
$container->setParameter('web_profiler.debug_toolbar.mode', $config['toolbar'] ? WebDebugToolbarListener::ENABLED : WebDebugToolbarListener:: 8000 DISABLED);
5455
}

src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
private string $excludedAjaxPaths = '^/bundles|^/_wdt',
4949
private ?ContentSecurityPolicyHandler $cspHandler = null,
5050
private ?DumpDataCollector $dumpDataCollector = null,
51+
private bool $ajaxReplace = false,
5152
) {
5253
}
5354

@@ -96,6 +97,10 @@ public function onKernelResponse(ResponseEvent $event): void
9697

9798
// do not capture redirects or modify XML HTTP Requests
9899
if ($request->isXmlHttpRequest()) {
100+
if (self::ENABLED === $this->mode && $this->ajaxReplace) {
101+
$response->headers->set('Symfony-Debug-Toolbar-Replace', '1');
102+
}
103+
99104
return;
100105
}
101106

src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
abstract_arg('paths that should be excluded from the AJAX requests shown in the toolbar'),
2626
service('web_profiler.csp.handler'),
2727
service('data_collector.dump')->ignoreOnInvalid(),
28+
abstract_arg('whether to replace toolbar on AJAX requests or not'),
2829
])
2930
->tag('kernel.event_subscriber')
3031
;

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static function getDebugModes()
3838
'intercept_redirects' => false,
3939
'toolbar' => false,
4040
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
41+
'ajax_replace' => false,
4142
],
4243
],
4344
[
@@ -46,6 +47,7 @@ public static function getDebugModes()
4647
'intercept_redirects' => false,
4748
'toolbar' => true,
4849
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
50+
'ajax_replace' => false,
4951
],
5052
],
5153
[
@@ -54,6 +56,16 @@ public static function getDebugModes()
5456
'intercept_redirects' => false,
5557
'toolbar' => false,
5658
'excluded_ajax_paths' => 'test',
59+
'ajax_replace' => false,
60+
],
61+
],
62+
[
63+
'options' => ['ajax_replace' => true],
64+
'expectedResult' => [
65+
'intercept_redirects' => false,
66+
'toolbar' => false,
67+
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
68+
'ajax_replace' => true,
5769
],
5870
],
5971
];
@@ -80,6 +92,7 @@ public static function getInterceptRedirectsConfiguration()
8092
'intercept_redirects' => true,
8193
'toolbar' => false,
8294
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
95+
'ajax_replace' => false,
8396
],
8497
],
8598
[
@@ -88,6 +101,7 @@ public static function getInterceptRedirectsConfiguration()
88101
'intercept_redirects' => false,
89102
'toolbar' => false,
90103
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
104+
'ajax_replace' => false,
91105
],
92106
],
93107
];

src/Symfony/Bundle/WebProfilerBundle/Tests/EventListener/WebDebugToolbarListenerTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,52 @@ public function testNullContentTypeWithNoDebugEnv()
357357
$this->expectNotToPerformAssertions();
358358
}
359359

360+
public function testAjaxReplaceHeaderOnDisabledToolbar()
361+
{
362+
$response = new Response();
363+
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);
364+
365+
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::DISABLED, null, '', null, null, true);
366+
$listener->onKernelResponse($event);
367+
368+
$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
369+
}
370+
371+
public function testAjaxReplaceHeaderOnDisabledReplace()
372+
{
373+
$response = new Response();
374+
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);
375+
376+
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null);
377+
$listener->onKernelResponse($event);
378+
379+
$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
380+
}
381+
382+
public function testAjaxReplaceHeaderOnEnabledAndNonXHR()
383+
{
384+
$response = new Response();
385+
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);
386+
387+
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null, true);
388+
$listener->onKernelResponse($event);
389+
390+
$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
391+
}
392+
393+
public function testAjaxReplaceHeaderOnEnabledAndXHR()
394+
{
395+
$request = new Request();
396+
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
397+
$response = new Response();
398+
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);
399+
400+
$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null, true);
401+
$listener->onKernelResponse($event);
402+
403+
$this->assertEquals('1', $response->headers->get('Symfony-Debug-Toolbar-Replace'));
404+
}
405+
360406
protected function getTwigMock($render = 'WDT')
361407
{
362408
$templating = $this->createMock(Environment::class);

0 commit comments

Comments
 (0)
0