Skip to content

Commit 9cb1047

Browse files
committed
refactor: improve by shouldDisableToolbar to check header values
1 parent 02a2fcf commit 9cb1047

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

app/Config/Toolbar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Toolbar extends BaseConfig
137137
* Any request containing one of the following headers is treated as a
138138
* client-managed or partial request, and the Debug Toolbar injection is skipped.
139139
*
140-
* @var list<string>
140+
* @var array<string, string|null>
141141
*/
142142
public array $disableOnHeaders = [
143143
'X-Requested-With' => 'xmlhttprequest', // AJAX requests

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
__DIR__ . '/system/HTTP/Response.php',
109109
],
110110

111+
// Exclude test file because `is_cli()` is mocked and Rector might remove needed parameters.
111112
RemoveExtraParametersRector::class => [
112113
__DIR__ . '/tests/system/Debug/ToolbarTest.php',
113114
],

system/Debug/Toolbar.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Toolbar
4646
* Indicates if the current request is a custom AJAX-like request
4747
* (HTMX, Unpoly, Turbo, etc.) that expects clean HTML fragments.
4848
*/
49-
protected bool $isCustomAjax = false;
49+
protected bool $isDisabled = false;
5050

5151
/**
5252
* Collectors to be used and displayed.
@@ -413,11 +413,8 @@ public function prepare(?RequestInterface $request = null, ?ResponseInterface $r
413413

414414
$format = $response->getHeaderLine('content-type');
415415

416-
foreach ($config->disableOnHeaders as $header) {
417-
if ($request->hasHeader($header)) {
418-
$this->isCustomAjax = true;
419-
break;
420-
}
416+
if ($this->shouldDisableToolbar($request, $config->disableOnHeaders)) {
417+
$this->isDisabled = true;
421418
}
422419

423420
// Non-HTML formats should not include the debugbar
@@ -558,4 +555,35 @@ protected function format(string $data, string $format = 'html'): string
558555

559556
return $output;
560557
}
558+
559+
/**
560+
* Determine if the toolbar should be disabled based on the request headers.
561+
*
562+
* This method allows checking both the presence of headers and their expected values.
563+
*
564+
* @param array<string, string|null> $headersToDisableToolbar
565+
*
566+
* @return bool True if any header condition matches; false otherwise.
567+
*/
568+
private function shouldDisableToolbar(IncomingRequest $request, array $headersToDisableToolbar): bool
569+
{
570+
foreach ($headersToDisableToolbar as $headerName => $expectedValue) {
571+
if (! $request->hasHeader($headerName)) {
572+
continue; // header not present, skip
573+
}
574+
575+
// If expectedValue is null, only presence is enough
576+
if ($expectedValue === null) {
577+
return true;
578+
}
579+
580+
$headerValue = strtolower($request->getHeaderLine($headerName));
581+
582+
if ($headerValue === strtolower($expectedValue)) {
583+
return true;
584+
}
585+
}
586+
587+
return false;
588+
}
561589
}

0 commit comments

Comments
 (0)