Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@12
|
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@12
|
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 namespace Zend\Diactoros\Response;
|
Chris@0
|
9
|
Chris@0
|
10 use Psr\Http\Message\ResponseInterface;
|
Chris@12
|
11 use RuntimeException;
|
Chris@0
|
12
|
Chris@0
|
13 trait SapiEmitterTrait
|
Chris@0
|
14 {
|
Chris@0
|
15 /**
|
Chris@12
|
16 * Checks to see if content has previously been sent.
|
Chris@0
|
17 *
|
Chris@12
|
18 * If either headers have been sent or the output buffer contains content,
|
Chris@12
|
19 * raises an exception.
|
Chris@12
|
20 *
|
Chris@12
|
21 * @throws RuntimeException if headers have already been sent.
|
Chris@12
|
22 * @throws RuntimeException if output is present in the output buffer.
|
Chris@0
|
23 */
|
Chris@12
|
24 private function assertNoPreviousOutput()
|
Chris@0
|
25 {
|
Chris@12
|
26 if (headers_sent()) {
|
Chris@12
|
27 throw new RuntimeException('Unable to emit response; headers already sent');
|
Chris@0
|
28 }
|
Chris@0
|
29
|
Chris@12
|
30 if (ob_get_level() > 0 && ob_get_length() > 0) {
|
Chris@12
|
31 throw new RuntimeException('Output has been emitted previously; cannot emit response');
|
Chris@12
|
32 }
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * Emit the status line.
|
Chris@0
|
37 *
|
Chris@0
|
38 * Emits the status line using the protocol version and status code from
|
Chris@0
|
39 * the response; if a reason phrase is available, it, too, is emitted.
|
Chris@0
|
40 *
|
Chris@12
|
41 * It is important to mention that this method should be called after
|
Chris@12
|
42 * `emitHeaders()` in order to prevent PHP from changing the status code of
|
Chris@12
|
43 * the emitted response.
|
Chris@12
|
44 *
|
Chris@0
|
45 * @param ResponseInterface $response
|
Chris@12
|
46 *
|
Chris@12
|
47 * @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders()
|
Chris@0
|
48 */
|
Chris@0
|
49 private function emitStatusLine(ResponseInterface $response)
|
Chris@0
|
50 {
|
Chris@0
|
51 $reasonPhrase = $response->getReasonPhrase();
|
Chris@12
|
52 $statusCode = $response->getStatusCode();
|
Chris@12
|
53
|
Chris@0
|
54 header(sprintf(
|
Chris@0
|
55 'HTTP/%s %d%s',
|
Chris@0
|
56 $response->getProtocolVersion(),
|
Chris@12
|
57 $statusCode,
|
Chris@0
|
58 ($reasonPhrase ? ' ' . $reasonPhrase : '')
|
Chris@12
|
59 ), true, $statusCode);
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Emit response headers.
|
Chris@0
|
64 *
|
Chris@0
|
65 * Loops through each header, emitting each; if the header value
|
Chris@0
|
66 * is an array with multiple values, ensures that each is sent
|
Chris@0
|
67 * in such a way as to create aggregate headers (instead of replace
|
Chris@0
|
68 * the previous).
|
Chris@0
|
69 *
|
Chris@0
|
70 * @param ResponseInterface $response
|
Chris@0
|
71 */
|
Chris@0
|
72 private function emitHeaders(ResponseInterface $response)
|
Chris@0
|
73 {
|
Chris@12
|
74 $statusCode = $response->getStatusCode();
|
Chris@12
|
75
|
Chris@0
|
76 foreach ($response->getHeaders() as $header => $values) {
|
Chris@0
|
77 $name = $this->filterHeader($header);
|
Chris@0
|
78 $first = $name === 'Set-Cookie' ? false : true;
|
Chris@0
|
79 foreach ($values as $value) {
|
Chris@0
|
80 header(sprintf(
|
Chris@0
|
81 '%s: %s',
|
Chris@0
|
82 $name,
|
Chris@0
|
83 $value
|
Chris@12
|
84 ), $first, $statusCode);
|
Chris@0
|
85 $first = false;
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Filter a header name to wordcase
|
Chris@0
|
92 *
|
Chris@0
|
93 * @param string $header
|
Chris@0
|
94 * @return string
|
Chris@0
|
95 */
|
Chris@0
|
96 private function filterHeader($header)
|
Chris@0
|
97 {
|
Chris@0
|
98 $filtered = str_replace('-', ' ', $header);
|
Chris@0
|
99 $filtered = ucwords($filtered);
|
Chris@0
|
100 return str_replace(' ', '-', $filtered);
|
Chris@0
|
101 }
|
Chris@0
|
102 }
|