composer package updates
[openemr.git] / vendor / zendframework / zend-diactoros / src / Response / SapiEmitterTrait.php
bloba0c1107bbda67cda487bc81ab6e99d9d8f5011ef
1 <?php
2 /**
3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6 */
8 namespace Zend\Diactoros\Response;
10 use Psr\Http\Message\ResponseInterface;
11 use RuntimeException;
13 use function ob_get_length;
14 use function ob_get_level;
15 use function sprintf;
16 use function str_replace;
17 use function ucwords;
19 /**
20 * @deprecated since 1.8.0. The package zendframework/zend-httphandlerrunner
21 * now provides this functionality.
23 trait SapiEmitterTrait
25 /**
26 * Checks to see if content has previously been sent.
28 * If either headers have been sent or the output buffer contains content,
29 * raises an exception.
31 * @throws RuntimeException if headers have already been sent.
32 * @throws RuntimeException if output is present in the output buffer.
34 private function assertNoPreviousOutput()
36 if (headers_sent()) {
37 throw new RuntimeException('Unable to emit response; headers already sent');
40 if (ob_get_level() > 0 && ob_get_length() > 0) {
41 throw new RuntimeException('Output has been emitted previously; cannot emit response');
45 /**
46 * Emit the status line.
48 * Emits the status line using the protocol version and status code from
49 * the response; if a reason phrase is available, it, too, is emitted.
51 * It is important to mention that this method should be called after
52 * `emitHeaders()` in order to prevent PHP from changing the status code of
53 * the emitted response.
55 * @param ResponseInterface $response
57 * @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders()
59 private function emitStatusLine(ResponseInterface $response)
61 $reasonPhrase = $response->getReasonPhrase();
62 $statusCode = $response->getStatusCode();
64 header(sprintf(
65 'HTTP/%s %d%s',
66 $response->getProtocolVersion(),
67 $statusCode,
68 ($reasonPhrase ? ' ' . $reasonPhrase : '')
69 ), true, $statusCode);
72 /**
73 * Emit response headers.
75 * Loops through each header, emitting each; if the header value
76 * is an array with multiple values, ensures that each is sent
77 * in such a way as to create aggregate headers (instead of replace
78 * the previous).
80 * @param ResponseInterface $response
82 private function emitHeaders(ResponseInterface $response)
84 $statusCode = $response->getStatusCode();
86 foreach ($response->getHeaders() as $header => $values) {
87 $name = $this->filterHeader($header);
88 $first = $name === 'Set-Cookie' ? false : true;
89 foreach ($values as $value) {
90 header(sprintf(
91 '%s: %s',
92 $name,
93 $value
94 ), $first, $statusCode);
95 $first = false;
101 * Filter a header name to wordcase
103 * @param string $header
104 * @return string
106 private function filterHeader($header)
108 $filtered = str_replace('-', ' ', $header);
109 $filtered = ucwords($filtered);
110 return str_replace(' ', '-', $filtered);