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