# SapiEmitter

> Emits PSR-7 responses through PHP SAPI.

[API reference](/docs/api/index.md) › [PSR bridge](/docs/extensions/psr-bridge/reference/index.md) › [emitter](/docs/extensions/psr-bridge/reference/emitter/index.md) › SapiEmitter

| final class | `yii2\extensions\psrbridge\emitter\SapiEmitter` |
| --- | --- |
| Source | [`src/emitter/SapiEmitter.php`](https://github.com/yii2-extensions/psr-bridge/blob/main/src/emitter/SapiEmitter.php#L21) |

Emits PSR-7 responses through PHP SAPI.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Configures the maximum bytes read in each body-emission chunk. | `SapiEmitter` |
| [`emit()`](#method-emit) | Emits status, headers, and optionally a readable body through PHP SAPI. | `SapiEmitter` |

## Method details

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

```php
public function __construct(int $bufferLength = 8192)
```

Configures the maximum bytes read in each body-emission chunk.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$bufferLength` | `int` | Positive chunk size in bytes; defaults to 8192. |

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): when the buffer length is less than 1.

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

```php
public function emit(Psr\Http\Message\ResponseInterface $response, bool $body = true): void
```

Emits status, headers, and optionally a readable body through PHP SAPI.

Suppresses the body for statuses recognized by [`HttpNoBodyStatus`](/docs/extensions/psr-bridge/reference/emitter/httpnobodystatus/index.md). The caller must pass `false` for
HEAD because this method has no originating request. Reads are bounded by the configured chunk size;
after-send lifecycle processing remains the caller's responsibility.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | `Psr\Http\Message\ResponseInterface` | Response to emit. |
| `$body` | `bool` | Whether to emit the body when allowed by status code. |

**Throws**

- [`yii2\extensions\psrbridge\exception\HeadersAlreadySentException`](/docs/extensions/psr-bridge/reference/exception/headersalreadysentexception/index.md): when PHP has already sent headers.
- [`yii2\extensions\psrbridge\exception\OutputAlreadySentException`](/docs/extensions/psr-bridge/reference/exception/outputalreadysentexception/index.md): when the current output buffer contains bytes.
- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): when reading or positioning the body stream fails.