Mercurial > hg > isophonics-drupal-site
comparison vendor/zendframework/zend-diactoros/src/Response/SapiEmitterTrait.php @ 12:7a779792577d
Update Drupal core to v8.4.5 (via Composer)
author | Chris Cannam |
---|---|
date | Fri, 23 Feb 2018 15:52:07 +0000 |
parents | 4c8ae668cc8c |
children | c2387f117808 |
comparison
equal
deleted
inserted
replaced
11:bfffd8d7479a | 12:7a779792577d |
---|---|
1 <?php | 1 <?php |
2 /** | 2 /** |
3 * Zend Framework (http://framework.zend.com/) | 3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository |
4 * | 4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com) |
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 | 5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License |
8 */ | 6 */ |
9 | 7 |
10 namespace Zend\Diactoros\Response; | 8 namespace Zend\Diactoros\Response; |
11 | 9 |
12 use Psr\Http\Message\ResponseInterface; | 10 use Psr\Http\Message\ResponseInterface; |
11 use RuntimeException; | |
13 | 12 |
14 trait SapiEmitterTrait | 13 trait SapiEmitterTrait |
15 { | 14 { |
16 /** | 15 /** |
17 * Inject the Content-Length header if is not already present. | 16 * Checks to see if content has previously been sent. |
18 * | 17 * |
19 * @param ResponseInterface $response | 18 * If either headers have been sent or the output buffer contains content, |
20 * @return ResponseInterface | 19 * raises an exception. |
20 * | |
21 * @throws RuntimeException if headers have already been sent. | |
22 * @throws RuntimeException if output is present in the output buffer. | |
21 */ | 23 */ |
22 private function injectContentLength(ResponseInterface $response) | 24 private function assertNoPreviousOutput() |
23 { | 25 { |
24 if (! $response->hasHeader('Content-Length')) { | 26 if (headers_sent()) { |
25 // PSR-7 indicates int OR null for the stream size; for null values, | 27 throw new RuntimeException('Unable to emit response; headers already sent'); |
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 } | 28 } |
31 | 29 |
32 return $response; | 30 if (ob_get_level() > 0 && ob_get_length() > 0) { |
31 throw new RuntimeException('Output has been emitted previously; cannot emit response'); | |
32 } | |
33 } | 33 } |
34 | 34 |
35 /** | 35 /** |
36 * Emit the status line. | 36 * Emit the status line. |
37 * | 37 * |
38 * Emits the status line using the protocol version and status code from | 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. | 39 * the response; if a reason phrase is available, it, too, is emitted. |
40 * | 40 * |
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. | |
44 * | |
41 * @param ResponseInterface $response | 45 * @param ResponseInterface $response |
46 * | |
47 * @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders() | |
42 */ | 48 */ |
43 private function emitStatusLine(ResponseInterface $response) | 49 private function emitStatusLine(ResponseInterface $response) |
44 { | 50 { |
45 $reasonPhrase = $response->getReasonPhrase(); | 51 $reasonPhrase = $response->getReasonPhrase(); |
52 $statusCode = $response->getStatusCode(); | |
53 | |
46 header(sprintf( | 54 header(sprintf( |
47 'HTTP/%s %d%s', | 55 'HTTP/%s %d%s', |
48 $response->getProtocolVersion(), | 56 $response->getProtocolVersion(), |
49 $response->getStatusCode(), | 57 $statusCode, |
50 ($reasonPhrase ? ' ' . $reasonPhrase : '') | 58 ($reasonPhrase ? ' ' . $reasonPhrase : '') |
51 )); | 59 ), true, $statusCode); |
52 } | 60 } |
53 | 61 |
54 /** | 62 /** |
55 * Emit response headers. | 63 * Emit response headers. |
56 * | 64 * |
61 * | 69 * |
62 * @param ResponseInterface $response | 70 * @param ResponseInterface $response |
63 */ | 71 */ |
64 private function emitHeaders(ResponseInterface $response) | 72 private function emitHeaders(ResponseInterface $response) |
65 { | 73 { |
74 $statusCode = $response->getStatusCode(); | |
75 | |
66 foreach ($response->getHeaders() as $header => $values) { | 76 foreach ($response->getHeaders() as $header => $values) { |
67 $name = $this->filterHeader($header); | 77 $name = $this->filterHeader($header); |
68 $first = $name === 'Set-Cookie' ? false : true; | 78 $first = $name === 'Set-Cookie' ? false : true; |
69 foreach ($values as $value) { | 79 foreach ($values as $value) { |
70 header(sprintf( | 80 header(sprintf( |
71 '%s: %s', | 81 '%s: %s', |
72 $name, | 82 $name, |
73 $value | 83 $value |
74 ), $first); | 84 ), $first, $statusCode); |
75 $first = false; | 85 $first = false; |
76 } | 86 } |
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 } | 87 } |
95 } | 88 } |
96 | 89 |
97 /** | 90 /** |
98 * Filter a header name to wordcase | 91 * Filter a header name to wordcase |