Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit a22b273

Browse files
committed
Updates documentation of emitters
- Ensures they do not discuss output buffering. - Adds section on `SapiStreamEmitter`.
1 parent 755ded6 commit a22b273

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

doc/book/emitting-responses.md

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
If you are using a non-SAPI PHP implementation and wish to use the `Server` class, or if you do not
44
want to use the `Server` implementation but want to emit a response, this package provides an
55
interface, `Zend\Diactoros\Response\EmitterInterface`, defining a method `emit()` for emitting the
6-
response. A single implementation is currently available, `Zend\Diactoros\Response\SapiEmitter`,
7-
which will use the native PHP functions `header()` and `echo` in order to emit the response. If you
8-
are using a non-SAPI implementation, you will need to create your own `EmitterInterface`
9-
implementation.
6+
response.
7+
8+
Diactoros provides two implementations currently, both for working with
9+
traditional Server API (SAPI) implementations: `Zend\Diactoros\Response\SapiEmitter`
10+
and `Zend\Diactoros\Response\SapiStreamEmitter`. Each uses the native `header()`
11+
PHP function to emit headers, and `echo()` to emit the response body.
12+
13+
If you are using a non-SAPI implementation, you will need to create your own
14+
`EmitterInterface` implementation.
1015

1116
For example, the `SapiEmitter` implementation of the `EmitterInterface` can be used thus:
1217

@@ -16,3 +21,43 @@ $response->getBody()->write("some content\n");
1621
$emitter = new Zend\Diactoros\Response\SapiEmitter();
1722
$emitter->emit($response);
1823
```
24+
25+
## Emitting ranges of streamed files
26+
27+
The `SapiStreamEmitter` is useful when you want to emit a `Content-Range`. As an
28+
example, to stream a range of bytes from a file to a client, the client can pass
29+
the following header:
30+
31+
```http
32+
Range: bytes=1024-2047
33+
```
34+
35+
Your application would then populate the response with a `Content-Range` header:
36+
37+
```php
38+
$range = $request->getHeaderLine('range');
39+
$range = str_replace('=', ' ', $range);
40+
41+
$body = new Stream($pathToFile);
42+
$size = $body->getSize();
43+
$range .= '/' . $size;
44+
45+
$response = new Response($body);
46+
$response = $response->withHeader('Content-Range', $range);
47+
```
48+
49+
> Note: you will likely want to ensure the range specified falls within the
50+
> content size of the streamed body!
51+
52+
The `SapiStreamEmitter` detects the `Content-Range` header and emits only the
53+
bytes specified.
54+
55+
```php
56+
$emitter = new SapiStreamEmitter();
57+
$emitter->emit($response);
58+
```
59+
60+
The `SapiStreamEmitter` may be used in place of the `SapiEmitter`, even when not
61+
sending files. However, unlike the `SapiEmitter`, it will emit a chunk of
62+
content at a time instead of the full content at once, which could lead to
63+
performance overhead. The default chunk size is 8192 bytes.

0 commit comments

Comments
 (0)