1414
1515class SapiEmitter implements EmitterInterface
1616{
17+ use SapiEmitterTrait;
18+
1719 /**
1820 * Emits a response for a PHP SAPI environment.
1921 *
@@ -29,96 +31,21 @@ public function emit(ResponseInterface $response, $maxBufferLevel = null)
2931 throw new RuntimeException ('Unable to emit response; headers already sent ' );
3032 }
3133
32- if (! $ response ->hasHeader ('Content-Length ' )) {
33- // PSR-7 indicates int OR null for the stream size; for null values,
34- // we will not auto-inject the Content-Length.
35- if (null !== $ response ->getBody ()->getSize ()) {
36- $ response = $ response ->withHeader ('Content-Length ' , (string ) $ response ->getBody ()->getSize ());
37- }
38- }
34+ $ response = $ this ->injectContentLength ($ response );
3935
4036 $ this ->emitStatusLine ($ response );
4137 $ this ->emitHeaders ($ response );
42- $ this ->emitBody ($ response , $ maxBufferLevel );
43- }
44-
45- /**
46- * Emit the status line.
47- *
48- * Emits the status line using the protocol version and status code from
49- * the response; if a reason phrase is availble, it, too, is emitted.
50- *
51- * @param ResponseInterface $response
52- */
53- private function emitStatusLine (ResponseInterface $ response )
54- {
55- $ reasonPhrase = $ response ->getReasonPhrase ();
56- header (sprintf (
57- 'HTTP/%s %d%s ' ,
58- $ response ->getProtocolVersion (),
59- $ response ->getStatusCode (),
60- ($ reasonPhrase ? ' ' . $ reasonPhrase : '' )
61- ));
62- }
63-
64- /**
65- * Emit response headers.
66- *
67- * Loops through each header, emitting each; if the header value
68- * is an array with multiple values, ensures that each is sent
69- * in such a way as to create aggregate headers (instead of replace
70- * the previous).
71- *
72- * @param ResponseInterface $response
73- */
74- private function emitHeaders (ResponseInterface $ response )
75- {
76- foreach ($ response ->getHeaders () as $ header => $ values ) {
77- $ name = $ this ->filterHeader ($ header );
78- $ first = true ;
79- foreach ($ values as $ value ) {
80- header (sprintf (
81- '%s: %s ' ,
82- $ name ,
83- $ value
84- ), $ first );
85- $ first = false ;
86- }
87- }
38+ $ this ->flush ($ maxBufferLevel );
39+ $ this ->emitBody ($ response );
8840 }
8941
9042 /**
9143 * Emit the message body.
9244 *
93- * Loops through the output buffer, flushing each, before emitting
94- * the response body using `echo()`.
95- *
9645 * @param ResponseInterface $response
97- * @param int $maxBufferLevel Flush up to this buffer level.
9846 */
99- private function emitBody (ResponseInterface $ response, $ maxBufferLevel )
47+ private function emitBody (ResponseInterface $ response )
10048 {
101- if (null === $ maxBufferLevel ) {
102- $ maxBufferLevel = ob_get_level ();
103- }
104-
105- while (ob_get_level () > $ maxBufferLevel ) {
106- ob_end_flush ();
107- }
108-
10949 echo $ response ->getBody ();
11050 }
111-
112- /**
113- * Filter a header name to wordcase
114- *
115- * @param string $header
116- * @return string
117- */
118- private function filterHeader ($ header )
119- {
120- $ filtered = str_replace ('- ' , ' ' , $ header );
121- $ filtered = ucwords ($ filtered );
122- return str_replace (' ' , '- ' , $ filtered );
123- }
12451}
0 commit comments