3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros\Response;
10 use Psr\Http\Message\ResponseInterface;
13 trait SapiEmitterTrait
16 * Checks to see if content has previously been sent.
18 * If either headers have been sent or the output buffer contains content,
19 * raises an exception.
21 * @throws RuntimeException if headers have already been sent.
22 * @throws RuntimeException if output is present in the output buffer.
24 private function assertNoPreviousOutput()
27 throw new RuntimeException('Unable to emit response; headers already sent');
30 if (ob_get_level() > 0 && ob_get_length() > 0) {
31 throw new RuntimeException('Output has been emitted previously; cannot emit response');
36 * Emit the status line.
38 * Emits the status line using the protocol version and status code from
39 * the response; if a reason phrase is available, it, too, is emitted.
41 * It is important to mention that this method should be called after
42 * `emitHeaders()` in order to prevent PHP from changing the status code of
43 * the emitted response.
45 * @param ResponseInterface $response
47 * @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders()
49 private function emitStatusLine(ResponseInterface $response)
51 $reasonPhrase = $response->getReasonPhrase();
52 $statusCode = $response->getStatusCode();
56 $response->getProtocolVersion(),
58 ($reasonPhrase ? ' ' . $reasonPhrase : '')
59 ), true, $statusCode);
63 * Emit response headers.
65 * Loops through each header, emitting each; if the header value
66 * is an array with multiple values, ensures that each is sent
67 * in such a way as to create aggregate headers (instead of replace
70 * @param ResponseInterface $response
72 private function emitHeaders(ResponseInterface $response)
74 $statusCode = $response->getStatusCode();
76 foreach ($response->getHeaders() as $header => $values) {
77 $name = $this->filterHeader($header);
78 $first = $name === 'Set-Cookie' ? false : true;
79 foreach ($values as $value) {
84 ), $first, $statusCode);
91 * Filter a header name to wordcase
93 * @param string $header
96 private function filterHeader($header)
98 $filtered = str_replace('-', ' ', $header);
99 $filtered = ucwords($filtered);
100 return str_replace(' ', '-', $filtered);