# RequestCaptureMiddleware

> Captures debug snapshots and injects the toolbar into eligible HTML responses.

[API reference](/docs/api/index.md) › [Yii3 Debug](/docs/extensions/yii3-debug/reference/index.md) › [Middleware](/docs/extensions/yii3-debug/reference/middleware/index.md) › RequestCaptureMiddleware

| final readonly class | `Yii3\Debug\Middleware\RequestCaptureMiddleware` |
| --- | --- |
| Implements | `Psr\Http\Server\MiddlewareInterface` |
| Source | [`src/Middleware/RequestCaptureMiddleware.php`](https://github.com/yii3/debug/blob/main/src/Middleware/RequestCaptureMiddleware.php#L43) |

Captures debug snapshots and injects the toolbar into eligible HTML responses.

Requests targeting the debugger pass through uncaptured, so [`DebugRouteMiddleware`](/docs/extensions/yii3-debug/reference/middleware/debugroutemiddleware/index.md) can serve them whichever of
the two runs first, and an application that answers the debugger paths itself can register this middleware alone.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates request-capture middleware with toolbar rendering, storage, policy, and optional collectors. | `RequestCaptureMiddleware` |
| [`process()`](#method-process) | Captures the request, then injects the toolbar into an eligible HTML response. | `RequestCaptureMiddleware` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(Yii3\Debug\Web\ToolbarRenderer $renderer, Psr\Http\Message\StreamFactoryInterface $streamFactory, PHPForge\Debug\Storage\SnapshotStore $store, Yiisoft\NetworkUtilities\IpRanges $allowedIpRanges, Yii3\Debug\Middleware\ToolbarOptions $options, PHPForge\Debug\Capture\CapturePolicy $capturePolicy, PHPForge\Debug\Collector\CollectorCoordinator|null $collectorCoordinator, Yii3\Debug\Capture\DeferredCapture|null $deferredCapture)
```

Creates request-capture middleware with toolbar rendering, storage, policy, and optional collectors.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$renderer` | [`Yii3\Debug\Web\ToolbarRenderer`](/docs/extensions/yii3-debug/reference/web/toolbarrenderer/index.md) | Renderer producing the toolbar markup injected into the response. |
| `$streamFactory` | `Psr\Http\Message\StreamFactoryInterface` | Factory building the rewritten response body. |
| `$store` | `PHPForge\Debug\Storage\SnapshotStore` | Store the capture is written to. |
| `$allowedIpRanges` | `Yiisoft\NetworkUtilities\IpRanges` | Ranges allowed to reach the debugger. |
| `$options` | [`ToolbarOptions`](/docs/extensions/yii3-debug/reference/middleware/toolbaroptions/index.md) | Settings carrying the route prefix, the history size, the excessive-caller threshold, and the toolbar presentation. |
| `$capturePolicy` | [`CapturePolicy`](https://github.com/php-forge/debug-core/blob/b70459f033a1d7a1c5edf7951029aad2061986dd/src/Capture/CapturePolicy.php) | Policy deciding which values are persisted and which are redacted. |
| `$collectorCoordinator` | `PHPForge\Debug\Collector\CollectorCoordinator\|null` | Coordinator owning the collectors, or `null` to capture nothing but the request summary. |
| `$deferredCapture` | [`Yii3\Debug\Capture\DeferredCapture`](/docs/extensions/yii3-debug/reference/capture/deferredcapture/index.md)`\|null` | Holder of the pending finalizer, or `null` to finalize inside the pipeline. Deferral requires a coordinator; without one the capture is finalized immediately. |

### process() <span id="method-process"></span>

```php
public function process(Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Server\RequestHandlerInterface $handler): Psr\Http\Message\ResponseInterface
```

Captures the request, then injects the toolbar into an eligible HTML response.

A capture still pending from an earlier request is written before anything else, debugger pages and requests
from a disallowed address included: a worker that never dispatched the shutdown event stops the collectors
before serving them, so their activity stays out of that capture, and the capture reaches history at once. A
failing finalization propagates from the request that triggered it. [`DebugRouteMiddleware`](/docs/extensions/yii3-debug/reference/middleware/debugroutemiddleware/index.md) applies the
same rule, so the guarantee holds whichever of the two runs first.

Requests targeting the debugger itself and requests from a disallowed address pass through untouched.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$request` | `Psr\Http\Message\ServerRequestInterface` | Request reaching the middleware. |
| `$handler` | `Psr\Http\Server\RequestHandlerInterface` | Next handler in the middleware stack. |

**Returns**

`Psr\Http\Message\ResponseInterface`: Response, with the toolbar injected when the request qualifies.

**Throws**

- [`Throwable`](https://www.php.net/manual/en/class.throwable.php): when the pending capture or the request handler fails.