Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[WebProfilerBundle] Extend web profiler listener & config for replace on ajax requests #59123

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

Merged
merged 1 commit into from
Jan 15, 2025
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
7 changes: 6 additions & 1 deletion 7 src/Symfony/Bundle/WebProfilerBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Add `ajax_replace` option for replacing toolbar on AJAX requests

7.2
---

Expand Down Expand Up @@ -65,7 +70,7 @@ CHANGELOG
-----

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

4.0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ public function getConfigTreeBuilder(): TreeBuilder

$treeBuilder->getRootNode()
->children()
->booleanNode('toolbar')->defaultFalse()->end()
->arrayNode('toolbar')
->info('Profiler toolbar configuration')
->canBeEnabled()
->children()
->booleanNode('ajax_replace')
chr-hertel marked this conversation as resolved.
Show resolved Hide resolved
->defaultFalse()
->info('Replace toolbar on AJAX requests')
->end()
->end()
->end()
->booleanNode('intercept_redirects')->defaultFalse()->end()
->scalarNode('excluded_ajax_paths')->defaultValue('^/((index|app(_[\w]+)?)\.php/)?_wdt')->end()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public function load(array $configs, ContainerBuilder $container): void
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('profiler.php');

if ($config['toolbar'] || $config['intercept_redirects']) {
if ($config['toolbar']['enabled'] || $config['intercept_redirects']) {
$loader->load('toolbar.php');
$container->getDefinition('web_profiler.debug_toolbar')->replaceArgument(4, $config['excluded_ajax_paths']);
$container->getDefinition('web_profiler.debug_toolbar')->replaceArgument(7, $config['toolbar']['ajax_replace']);
$container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']);
$container->setParameter('web_profiler.debug_toolbar.mode', $config['toolbar'] ? WebDebugToolbarListener::ENABLED : WebDebugToolbarListener::DISABLED);
$container->setParameter('web_profiler.debug_toolbar.mode', $config['toolbar']['enabled'] ? WebDebugToolbarListener::ENABLED : WebDebugToolbarListener::DISABLED);
}

$container->getDefinition('debug.file_link_formatter')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(
private string $excludedAjaxPaths = '^/bundles|^/_wdt',
private ?ContentSecurityPolicyHandler $cspHandler = null,
private ?DumpDataCollector $dumpDataCollector = null,
private bool $ajaxReplace = false,
) {
}

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

// do not capture redirects or modify XML HTTP Requests
if ($request->isXmlHttpRequest()) {
if (self::ENABLED === $this->mode && $this->ajaxReplace && !$response->headers->has('Symfony-Debug-Toolbar-Replace')) {
$response->headers->set('Symfony-Debug-Toolbar-Replace', '1');
chr-hertel marked this conversation as resolved.
Show resolved Hide resolved
}

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

<xsd:complexType name="config">
<xsd:attribute name="toolbar" type="xsd:boolean" />
<xsd:sequence>
<xsd:element name="toolbar" type="toolbar" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="intercept-redirects" type="xsd:boolean" />
</xsd:complexType>

<xsd:complexType name="toolbar">
<xsd:attribute name="enabled" type="xsd:boolean" />
<xsd:attribute name="ajax-replace" type="xsd:boolean" />
</xsd:complexType>
</xsd:schema>
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
abstract_arg('paths that should be excluded from the AJAX requests shown in the toolbar'),
service('web_profiler.csp.handler'),
service('data_collector.dump')->ignoreOnInvalid(),
abstract_arg('whether to replace toolbar on AJAX requests or not'),
])
->tag('kernel.event_subscriber')
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,46 @@ public static function getDebugModes()
'options' => [],
'expectedResult' => [
'intercept_redirects' => false,
'toolbar' => false,
'toolbar' => [
'enabled' => false,
'ajax_replace' => false,
],
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
],
],
[
'options' => ['toolbar' => true],
'expectedResult' => [
'intercept_redirects' => false,
'toolbar' => true,
'toolbar' => [
'enabled' => true,
'ajax_replace' => false,
],
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
],
],
[
'options' => ['excluded_ajax_paths' => 'test'],
'expectedResult' => [
'intercept_redirects' => false,
'toolbar' => false,
'toolbar' => [
'enabled' => false,
'ajax_replace' => false,
],
'excluded_ajax_paths' => 'test',
],
],
[
'options' => ['toolbar' => ['ajax_replace' => true]],
'expectedResult' => [
'intercept_redirects' => false,
'toolbar' => [
'enabled' => true,
'ajax_replace' => true,
],
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
],
],
];
}

Expand All @@ -78,15 +98,21 @@ public static function getInterceptRedirectsConfiguration()
'interceptRedirects' => true,
'expectedResult' => [
'intercept_redirects' => true,
'toolbar' => false,
'toolbar' => [
'enabled' => false,
'ajax_replace' => false,
],
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
],
],
[
'interceptRedirects' => false,
'expectedResult' => [
'intercept_redirects' => false,
'toolbar' => false,
'toolbar' => [
'enabled' => false,
'ajax_replace' => false,
],
'excluded_ajax_paths' => '^/((index|app(_[\w]+)?)\.php/)?_wdt',
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,66 @@ public function testNullContentTypeWithNoDebugEnv()
$this->expectNotToPerformAssertions();
}

public function testAjaxReplaceHeaderOnDisabledToolbar()
{
$response = new Response();
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::DISABLED, null, '', null, null, true);
$listener->onKernelResponse($event);

$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
}

public function testAjaxReplaceHeaderOnDisabledReplace()
{
$response = new Response();
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null);
$listener->onKernelResponse($event);

$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
}

public function testAjaxReplaceHeaderOnEnabledAndNonXHR()
{
$response = new Response();
$event = new ResponseEvent($this->createMock(Kernel::class), new Request(), HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null, true);
$listener->onKernelResponse($event);

$this->assertFalse($response->headers->has('Symfony-Debug-Toolbar-Replace'));
}

public function testAjaxReplaceHeaderOnEnabledAndXHR()
{
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$response = new Response();
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null, true);
$listener->onKernelResponse($event);

$this->assertSame('1', $response->headers->get('Symfony-Debug-Toolbar-Replace'));
}

public function testAjaxReplaceHeaderOnEnabledAndXHRButPreviouslySet()
{
$request = new Request();
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$response = new Response();
$response->headers->set('Symfony-Debug-Toolbar-Replace', '0');
$event = new ResponseEvent($this->createMock(Kernel::class), $request, HttpKernelInterface::MAIN_REQUEST, $response);

$listener = new WebDebugToolbarListener($this->getTwigMock(), false, WebDebugToolbarListener::ENABLED, null, '', null, null, true);
$listener->onKernelResponse($event);

$this->assertSame('0', $response->headers->get('Symfony-Debug-Toolbar-Replace'));
}

protected function getTwigMock($render = 'WDT')
{
$templating = $this->createMock(Environment::class);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.