# DeferredCapture

> Holds the pending capture finalizer until the application reaches its shutdown phase.

[API reference](/docs/api/index.md) › [Yii3 Debug](/docs/extensions/yii3-debug/reference/index.md) › [Capture](/docs/extensions/yii3-debug/reference/capture/index.md) › DeferredCapture

| final class | `Yii3\Debug\Capture\DeferredCapture` |
| --- | --- |
| Source | [`src/Capture/DeferredCapture.php`](https://github.com/yii3/debug/blob/main/src/Capture/DeferredCapture.php#L23) |

Holds the pending capture finalizer until the application reaches its shutdown phase.

Writing the capture from the middleware misses everything produced after the pipeline returns: a lazily rendered
response body, log and profiler flushes, and the events dispatched around emission. Deferring the finalizer to
`Yiisoft\Yii\Http\Event\ApplicationShutdown` keeps those in the snapshot, and the registered PHP shutdown fallback
still writes it when the application never dispatches that event.

While the request handler runs, [`arm()`](/docs/extensions/yii3-debug/reference/capture/deferredcapture/index.md#method-arm) keeps a second, separate fallback for the capture in progress, so a
script that ends inside the handler, through `exit`, `dd()`, or a fatal error, still writes it, as the Yii2 logger
flush does at shutdown.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a deferred finalizer using the supplied shutdown registrar or PHP's default registrar. | `DeferredCapture` |
| [`arm()`](#method-arm) | Stores the fallback that writes the capture in progress when the script ends before the request handler returns. | `DeferredCapture` |
| [`cancel()`](#method-cancel) | Drops the pending finalizer without running it. | `DeferredCapture` |
| [`defer()`](#method-defer) | Stores the finalizer to run at shutdown, finalizing a capture still pending from an earlier request. | `DeferredCapture` |
| [`disarm()`](#method-disarm) | Drops the fallback [`arm()`](/docs/extensions/yii3-debug/reference/capture/deferredcapture/index.md#method-arm) stored, once the request handler returned or failed. | `DeferredCapture` |
| [`finalize()`](#method-finalize) | Runs the pending finalizer exactly once, clearing it first so a second call stays a no-op. | `DeferredCapture` |
| [`isPending()`](#method-isPending) | Reports whether a deferred finalizer is waiting to run. | `DeferredCapture` |

## Method details

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

```php
public function __construct(Closure(callable(): void): void|null $shutdownRegistrar = null)
```

Creates a deferred finalizer using the supplied shutdown registrar or PHP's default registrar.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$shutdownRegistrar` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(callable(): void): void\|null` | Registrar receiving the shutdown fallback, or `null` to register it with PHP itself. |

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

```php
public function arm(Closure(): void $fallback): void
```

Stores the fallback that writes the capture in progress when the script ends before the request handler returns.

The fallback is kept apart from the pending finalizer, so a [`defer()`](/docs/extensions/yii3-debug/reference/capture/deferredcapture/index.md#method-defer) or [`finalize()`](/docs/extensions/yii3-debug/reference/capture/deferredcapture/index.md#method-finalize) reached while the
handler runs never writes the capture in progress; only the PHP shutdown fallback runs it.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$fallback` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(): void` | Finalizer writing the capture of the request being handled. |

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

```php
public function cancel(): void
```

Drops the pending finalizer without running it.

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

```php
public function defer(Closure(): void $finalizer): void
```

Stores the finalizer to run at shutdown, finalizing a capture still pending from an earlier request.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$finalizer` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(): void` | Finalizer writing the capture. |

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

```php
public function disarm(): void
```

Drops the fallback [`arm()`](/docs/extensions/yii3-debug/reference/capture/deferredcapture/index.md#method-arm) stored, once the request handler returned or failed.

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

```php
public function finalize(): void
```

Runs the pending finalizer exactly once, clearing it first so a second call stays a no-op.

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

```php
public function isPending(): bool
```

Reports whether a deferred finalizer is waiting to run.

**Returns**

`bool`: `true` when a finalizer is waiting to run; `false` otherwise.