a0c1107bbda67cda487bc81ab6e99d9d8f5011ef
[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-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  */
7
8 namespace Zend\Diactoros\Response;
9
10 use Psr\Http\Message\ResponseInterface;
11 use RuntimeException;
12
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;
18
19 /**
20  * @deprecated since 1.8.0. The package zendframework/zend-httphandlerrunner
21  *     now provides this functionality.
22  */
23 trait SapiEmitterTrait
24 {
25     /**
26      * Checks to see if content has previously been sent.
27      *
28      * If either headers have been sent or the output buffer contains content,
29      * raises an exception.
30      *
31      * @throws RuntimeException if headers have already been sent.
32      * @throws RuntimeException if output is present in the output buffer.
33      */
34     private function assertNoPreviousOutput()
35     {
36         if (headers_sent()) {
37             throw new RuntimeException('Unable to emit response; headers already sent');
38         }
39
40         if (ob_get_level() > 0 && ob_get_length() > 0) {
41             throw new RuntimeException('Output has been emitted previously; cannot emit response');
42         }
43     }
44
45     /**
46      * Emit the status line.
47      *
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.
50      *
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.
54      *
55      * @param ResponseInterface $response
56      *
57      * @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders()
58      */
59     private function emitStatusLine(ResponseInterface $response)
60     {
61         $reasonPhrase = $response->getReasonPhrase();
62         $statusCode   = $response->getStatusCode();
63
64         header(sprintf(
65             'HTTP/%s %d%s',
66             $response->getProtocolVersion(),
67             $statusCode,
68             ($reasonPhrase ? ' ' . $reasonPhrase : '')
69         ), true, $statusCode);
70     }
71
72     /**
73      * Emit response headers.
74      *
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).
79      *
80      * @param ResponseInterface $response
81      */
82     private function emitHeaders(ResponseInterface $response)
83     {
84         $statusCode = $response->getStatusCode();
85
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;
96             }
97         }
98     }
99
100     /**
101      * Filter a header name to wordcase
102      *
103      * @param string $header
104      * @return string
105      */
106     private function filterHeader($header)
107     {
108         $filtered = str_replace('-', ' ', $header);
109         $filtered = ucwords($filtered);
110         return str_replace(' ', '-', $filtered);
111     }
112 }