diff --git a/composer.json b/composer.json index 77bcca9..fa110d3 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": "^7.3 || ^8.0", - "maximebf/debugbar": "^1.4", + "maximebf/debugbar": "1.22.3", "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0", "psr/container-implementation": "^1.0 || ^2.0", diff --git a/src/PhpDebugBarMiddleware.php b/src/PhpDebugBarMiddleware.php index 8d4a892..71b0f34 100644 --- a/src/PhpDebugBarMiddleware.php +++ b/src/PhpDebugBarMiddleware.php @@ -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 @@ -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 = '
%sResponse:
%s%s'; $escapedOutResponseBody = htmlspecialchars($outResponseBody); @@ -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()) {