diff --git a/src/Debug/Middleware/DebugHeaders.php b/src/Debug/Middleware/DebugHeaders.php index b5d00b4..722bd65 100644 --- a/src/Debug/Middleware/DebugHeaders.php +++ b/src/Debug/Middleware/DebugHeaders.php @@ -9,7 +9,7 @@ use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Yiisoft\Router\UrlGeneratorInterface; -use Yiisoft\Yii\Debug\DebuggerIdGenerator; +use Yiisoft\Yii\Debug\Debugger; /** * Adds debug headers to response. Information from these headers may be used to request information about @@ -17,17 +17,24 @@ */ final class DebugHeaders implements MiddlewareInterface { - public function __construct(private DebuggerIdGenerator $idGenerator, private UrlGeneratorInterface $urlGenerator) - { + public function __construct( + private Debugger $debugger, + private UrlGeneratorInterface $urlGenerator, + ) { } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); - $link = $this->urlGenerator->generate('debug/api/view', ['id' => $this->idGenerator->getId()]); + + if (!$this->debugger->isActive()) { + return $response; + } + + $link = $this->urlGenerator->generate('debug/api/view', ['id' => $this->debugger->getId()]); return $response - ->withHeader('X-Debug-Id', $this->idGenerator->getId()) + ->withHeader('X-Debug-Id', $this->debugger->getId()) ->withHeader('X-Debug-Link', $link); } } diff --git a/tests/Unit/Debug/Middleware/DebugHeadersTest.php b/tests/Unit/Debug/Middleware/DebugHeadersTest.php index f86459a..ceae107 100644 --- a/tests/Unit/Debug/Middleware/DebugHeadersTest.php +++ b/tests/Unit/Debug/Middleware/DebugHeadersTest.php @@ -9,9 +9,11 @@ use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; use Psr\Http\Server\RequestHandlerInterface; +use stdClass; use Yiisoft\Router\UrlGeneratorInterface; use Yiisoft\Yii\Debug\Api\Debug\Middleware\DebugHeaders; -use Yiisoft\Yii\Debug\DebuggerIdGenerator; +use Yiisoft\Yii\Debug\Debugger; +use Yiisoft\Yii\Debug\Storage\MemoryStorage; final class DebugHeadersTest extends TestCase { @@ -21,10 +23,12 @@ public function testHeaders(): void $urlGenerator->method('generate')->willReturnCallback( fn (string $route, array $parameters) => $route . '?' . http_build_query($parameters) ); - $idGenerator = new DebuggerIdGenerator(); - $expectedId = $idGenerator->getId(); - $middleware = new DebugHeaders($idGenerator, $urlGenerator); + $debugger = new Debugger(new MemoryStorage(), []); + $debugger->startup(new stdClass()); + $expectedId = $debugger->getId(); + + $middleware = new DebugHeaders($debugger, $urlGenerator); $response = $middleware->process(new ServerRequest(), $this->createRequestHandler()); $this->assertSame($expectedId, $response->getHeaderLine('X-Debug-Id'));