3
3
If you are using a non-SAPI PHP implementation and wish to use the ` Server ` class, or if you do not
4
4
want to use the ` Server ` implementation but want to emit a response, this package provides an
5
5
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.
10
15
11
16
For example, the ` SapiEmitter ` implementation of the ` EmitterInterface ` can be used thus:
12
17
@@ -16,3 +21,43 @@ $response->getBody()->write("some content\n");
16
21
$emitter = new Zend\Diactoros\Response\SapiEmitter();
17
22
$emitter->emit($response);
18
23
```
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