comparison vendor/zendframework/zend-diactoros/src/Response/SapiEmitterTrait.php @ 0:c75dbcec494b

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