Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Diactoros\Response;
|
Chris@0
|
11
|
Chris@0
|
12 use Psr\Http\Message\ResponseInterface;
|
Chris@0
|
13
|
Chris@0
|
14 trait SapiEmitterTrait
|
Chris@0
|
15 {
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Inject the Content-Length header if is not already present.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @param ResponseInterface $response
|
Chris@0
|
20 * @return ResponseInterface
|
Chris@0
|
21 */
|
Chris@0
|
22 private function injectContentLength(ResponseInterface $response)
|
Chris@0
|
23 {
|
Chris@0
|
24 if (! $response->hasHeader('Content-Length')) {
|
Chris@0
|
25 // PSR-7 indicates int OR null for the stream size; for null values,
|
Chris@0
|
26 // we will not auto-inject the Content-Length.
|
Chris@0
|
27 if (null !== $response->getBody()->getSize()) {
|
Chris@0
|
28 return $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
|
Chris@0
|
29 }
|
Chris@0
|
30 }
|
Chris@0
|
31
|
Chris@0
|
32 return $response;
|
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@0
|
41 * @param ResponseInterface $response
|
Chris@0
|
42 */
|
Chris@0
|
43 private function emitStatusLine(ResponseInterface $response)
|
Chris@0
|
44 {
|
Chris@0
|
45 $reasonPhrase = $response->getReasonPhrase();
|
Chris@0
|
46 header(sprintf(
|
Chris@0
|
47 'HTTP/%s %d%s',
|
Chris@0
|
48 $response->getProtocolVersion(),
|
Chris@0
|
49 $response->getStatusCode(),
|
Chris@0
|
50 ($reasonPhrase ? ' ' . $reasonPhrase : '')
|
Chris@0
|
51 ));
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * Emit response headers.
|
Chris@0
|
56 *
|
Chris@0
|
57 * Loops through each header, emitting each; if the header value
|
Chris@0
|
58 * is an array with multiple values, ensures that each is sent
|
Chris@0
|
59 * in such a way as to create aggregate headers (instead of replace
|
Chris@0
|
60 * the previous).
|
Chris@0
|
61 *
|
Chris@0
|
62 * @param ResponseInterface $response
|
Chris@0
|
63 */
|
Chris@0
|
64 private function emitHeaders(ResponseInterface $response)
|
Chris@0
|
65 {
|
Chris@0
|
66 foreach ($response->getHeaders() as $header => $values) {
|
Chris@0
|
67 $name = $this->filterHeader($header);
|
Chris@0
|
68 $first = $name === 'Set-Cookie' ? false : true;
|
Chris@0
|
69 foreach ($values as $value) {
|
Chris@0
|
70 header(sprintf(
|
Chris@0
|
71 '%s: %s',
|
Chris@0
|
72 $name,
|
Chris@0
|
73 $value
|
Chris@0
|
74 ), $first);
|
Chris@0
|
75 $first = false;
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 /**
|
Chris@0
|
81 * Loops through the output buffer, flushing each, before emitting
|
Chris@0
|
82 * the response.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param int|null $maxBufferLevel Flush up to this buffer level.
|
Chris@0
|
85 */
|
Chris@0
|
86 private function flush($maxBufferLevel = null)
|
Chris@0
|
87 {
|
Chris@0
|
88 if (null === $maxBufferLevel) {
|
Chris@0
|
89 $maxBufferLevel = ob_get_level();
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 while (ob_get_level() > $maxBufferLevel) {
|
Chris@0
|
93 ob_end_flush();
|
Chris@0
|
94 }
|
Chris@0
|
95 }
|
Chris@0
|
96
|
Chris@0
|
97 /**
|
Chris@0
|
98 * Filter a header name to wordcase
|
Chris@0
|
99 *
|
Chris@0
|
100 * @param string $header
|
Chris@0
|
101 * @return string
|
Chris@0
|
102 */
|
Chris@0
|
103 private function filterHeader($header)
|
Chris@0
|
104 {
|
Chris@0
|
105 $filtered = str_replace('-', ' ', $header);
|
Chris@0
|
106 $filtered = ucwords($filtered);
|
Chris@0
|
107 return str_replace(' ', '-', $filtered);
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|