1 | <?php |
||
12 | class Response { |
||
13 | /** |
||
14 | * Create a new response object |
||
15 | * |
||
16 | * @return ResponseInterface |
||
17 | */ |
||
18 | 1 | public static function response() { |
|
21 | |||
22 | /** |
||
23 | * Send output based on a response object |
||
24 | * @credit modified version of slimphp/slim - Slim/App.php |
||
25 | * |
||
26 | * @codeCoverageIgnore |
||
27 | * @param ResponseInterface $response |
||
28 | * @return null |
||
29 | */ |
||
30 | public static function respond( ResponseInterface $response ) { |
||
31 | if ( ! headers_sent() ) { |
||
32 | static::sendHeaders( $response ); |
||
33 | } |
||
34 | static::sendBody( $response ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Send a request's headers to the client |
||
39 | * |
||
40 | * @param ResponseInterface $response |
||
41 | * @return null |
||
42 | */ |
||
43 | protected static function sendHeaders( ResponseInterface $response ) { |
||
59 | |||
60 | /** |
||
61 | * Return a response's body stream so it is ready to be read |
||
62 | * |
||
63 | * @param ResponseInterface $response |
||
64 | * @return ResponseInterface |
||
|
|||
65 | */ |
||
66 | protected static function getBody( ResponseInterface $response ) { |
||
73 | |||
74 | /** |
||
75 | * Return a response's body's content length |
||
76 | * |
||
77 | * @param ResponseInterface $response |
||
78 | * @return integer |
||
79 | */ |
||
80 | protected static function getBodyContentLength( ResponseInterface $response ) { |
||
88 | |||
89 | /** |
||
90 | * Send a request's body to the client |
||
91 | * |
||
92 | * @param ResponseInterface $response |
||
93 | * @return null |
||
94 | */ |
||
95 | protected static function sendBody( ResponseInterface $response, $chunk_size = 4096 ) { |
||
114 | |||
115 | /** |
||
116 | * Return a cloned response with the passed string as the body |
||
117 | * |
||
118 | * @param ResponseInterface $response |
||
119 | * @param string $output |
||
120 | * @return ResponseInterface |
||
121 | */ |
||
122 | public static function output( ResponseInterface $response, $output ) { |
||
126 | |||
127 | /** |
||
128 | * Resolve a template or a template array to an absolute filepath |
||
129 | * |
||
130 | * @param string|string[] $templates |
||
131 | * @return string |
||
132 | */ |
||
133 | protected static function resolveTemplate( $templates ) { |
||
144 | |||
145 | /** |
||
146 | * Resolve the first existing absolute template filepath from an array of template filepaths |
||
147 | * |
||
148 | * @param string[] $templates |
||
149 | * @return string |
||
150 | */ |
||
151 | protected static function resolveTemplateFromFilesystem( $templates ) { |
||
159 | |||
160 | /** |
||
161 | * Return a cloned response, resolving and rendering a template as the body |
||
162 | * |
||
163 | * @param ResponseInterface $response |
||
164 | * @param string|string[] $templates |
||
165 | * @param array $context |
||
166 | * @return ResponseInterface |
||
167 | */ |
||
168 | public static function template( ResponseInterface $response, $templates, $context = array() ) { |
||
178 | |||
179 | /** |
||
180 | * Return a cloned response, json encoding the passed data as the body |
||
181 | * |
||
182 | * @param ResponseInterface $response |
||
183 | * @param mixed $data |
||
184 | * @return ResponseInterface |
||
185 | */ |
||
186 | public static function json( ResponseInterface $response, $data ) { |
||
191 | |||
192 | /** |
||
193 | * Return a cloned response, with location and status headers |
||
194 | * |
||
195 | * @param ResponseInterface $response |
||
196 | * @param string $url |
||
197 | * @param integer $status |
||
198 | * @return ResponseInterface |
||
199 | */ |
||
200 | public static function redirect( ResponseInterface $response, $url, $status = 302 ) { |
||
205 | |||
206 | /** |
||
207 | * Return a cloned response, with status headers and rendering a suitable template as the body |
||
208 | * |
||
209 | * @param ResponseInterface $response |
||
210 | * @param integer $status |
||
211 | * @return ResponseInterface |
||
212 | */ |
||
213 | public static function error( ResponseInterface $response, $status ) { |
||
222 | } |
||
223 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.