Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
AJAX request should not render the initialization code.
handler Ajax Request.
  • Loading branch information
mostafasy authored Aug 7, 2023
commit 5affbfea88e81ab961191083438b103b6cbe08e0
16 changes: 9 additions & 7 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ public function process(ServerRequest $request, RequestHandler $handler): Respon

$response = $handler->handle($request);

if ($this->shouldReturnResponse($request, $response)) {
$isAjax = strtolower($request->getHeaderLine('X-Requested-With')) === 'xmlhttprequest';

if ($this->shouldReturnResponse($request, $response) && ! $isAjax) {
return $response;
}

if ($this->isHtmlResponse($response)) {
return $this->attachDebugBarToHtmlResponse($response);
return $this->attachDebugBarToHtmlResponse($response,$isAjax);
}

return $this->prepareHtmlResponseWithDebugBar($response);
return $this->prepareHtmlResponseWithDebugBar($response,$isAjax);
}

public function __invoke(ServerRequest $request, Response $response, callable $next): Response
Expand Down Expand Up @@ -106,10 +108,10 @@ private function shouldReturnResponse(ServerRequest $request, Response $response
return $isForceDisable || (!$isForceEnable && ($this->isRedirect($response) || !$this->isHtmlAccepted($request)));
}

private function prepareHtmlResponseWithDebugBar(Response $response): Response
private function prepareHtmlResponseWithDebugBar(Response $response,bool $isAjax): Response
{
$head = $this->debugBarRenderer->renderHead();
$body = $this->debugBarRenderer->render();
$body = $this->debugBarRenderer->render(!$isAjax);
$outResponseBody = $this->serializeResponse($response);
$template = '<html><head>%s</head><body><h1>DebugBar</h1><p>Response:</p><pre>%s</pre>%s</body></html>';
$escapedOutResponseBody = htmlspecialchars($outResponseBody);
Expand All @@ -122,10 +124,10 @@ private function prepareHtmlResponseWithDebugBar(Response $response): Response
->withAddedHeader('Content-type', 'text/html');
}

private function attachDebugBarToHtmlResponse(Response $response): Response
private function attachDebugBarToHtmlResponse(Response $response,bool $isAjax): Response
{
$head = $this->debugBarRenderer->renderHead();
$body = $this->debugBarRenderer->render();
$body = $this->debugBarRenderer->render(!$isAjax);
$responseBody = $response->getBody();

if (! $responseBody->eof() && $responseBody->isSeekable()) {
Expand Down