1 | <?php |
||
13 | class Response { |
||
14 | /** |
||
15 | * Create a new response object |
||
16 | * |
||
17 | * @return ResponseInterface |
||
18 | */ |
||
19 | 1 | public static function response() { |
|
22 | |||
23 | /** |
||
24 | * Send output based on a response object |
||
25 | * @credit modified version of slimphp/slim - Slim/App.php |
||
26 | * |
||
27 | * @codeCoverageIgnore |
||
28 | * @param ResponseInterface $response |
||
29 | * @return null |
||
30 | */ |
||
31 | public static function respond( ResponseInterface $response ) { |
||
37 | |||
38 | /** |
||
39 | * Send a request's headers to the client |
||
40 | * |
||
41 | * @codeCoverageIgnore |
||
42 | * @param ResponseInterface $response |
||
43 | * @return null |
||
44 | */ |
||
45 | protected static function sendHeaders( ResponseInterface $response ) { |
||
61 | |||
62 | /** |
||
63 | * Return a response's body stream so it is ready to be read |
||
64 | * |
||
65 | * @codeCoverageIgnore |
||
66 | * @param ResponseInterface $response |
||
67 | * @return \Psr\Http\Message\StreamInterface |
||
68 | */ |
||
69 | protected static function getBody( ResponseInterface $response ) { |
||
76 | |||
77 | /** |
||
78 | * Return a response's body's content length |
||
79 | * |
||
80 | * @codeCoverageIgnore |
||
81 | * @param ResponseInterface $response |
||
82 | * @return integer |
||
83 | */ |
||
84 | protected static function getBodyContentLength( ResponseInterface $response ) { |
||
98 | |||
99 | /** |
||
100 | * Send a request's body to the client |
||
101 | * |
||
102 | * @codeCoverageIgnore |
||
103 | * @param ResponseInterface $response |
||
104 | * @return null |
||
105 | */ |
||
106 | protected static function sendBody( ResponseInterface $response, $chunk_size = 4096 ) { |
||
125 | |||
126 | /** |
||
127 | * Return a cloned response with the passed string as the body |
||
128 | * |
||
129 | * @param ResponseInterface $response |
||
130 | * @param string $output |
||
131 | * @return ResponseInterface |
||
132 | */ |
||
133 | 1 | public static function output( ResponseInterface $response, $output ) { |
|
137 | |||
138 | /** |
||
139 | * Resolve a template or a template array to an absolute filepath |
||
140 | * |
||
141 | * @param string|string[] $templates |
||
142 | * @return string |
||
143 | */ |
||
144 | 2 | protected static function resolveTemplate( $templates ) { |
|
155 | |||
156 | /** |
||
157 | * Resolve the first existing absolute template filepath from an array of template filepaths |
||
158 | * |
||
159 | * @param string[] $templates |
||
160 | * @return string |
||
161 | */ |
||
162 | 2 | protected static function resolveTemplateFromFilesystem( $templates ) { |
|
170 | |||
171 | /** |
||
172 | * Return a cloned response, resolving and rendering a template as the body |
||
173 | * |
||
174 | * @param ResponseInterface $response |
||
175 | * @param string|string[] $templates |
||
176 | * @param array $context |
||
177 | * @return ResponseInterface |
||
178 | */ |
||
179 | 2 | public static function template( ResponseInterface $response, $templates, $context = array() ) { |
|
180 | 2 | $template = static::resolveTemplate( $templates ); |
|
181 | |||
182 | 2 | if ( ! $template ) { |
|
183 | 1 | throw new Exception( 'Could not resolve template.' ); |
|
184 | } |
||
185 | |||
186 | 1 | $engine = Framework::resolve( 'framework.templating.engine' ); |
|
187 | 1 | $html = $engine->render( $template, $context ); |
|
188 | |||
189 | 1 | $response = $response->withHeader( 'Content-Type', 'text/html' ); |
|
190 | 1 | $response = $response->withBody( Psr7\stream_for( $html ) ); |
|
191 | 1 | return $response; |
|
192 | } |
||
193 | |||
194 | /** |
||
195 | * Return a cloned response, json encoding the passed data as the body |
||
196 | * |
||
197 | * @param ResponseInterface $response |
||
198 | * @param mixed $data |
||
199 | * @return ResponseInterface |
||
200 | */ |
||
201 | 1 | public static function json( ResponseInterface $response, $data ) { |
|
206 | |||
207 | /** |
||
208 | * Return a cloned response, with location and status headers |
||
209 | * |
||
210 | * @param ResponseInterface $response |
||
211 | * @param string $url |
||
212 | * @param integer $status |
||
213 | * @return ResponseInterface |
||
214 | */ |
||
215 | 2 | public static function redirect( ResponseInterface $response, $url, $status = 302 ) { |
|
220 | |||
221 | /** |
||
222 | * Return a cloned response, with location header equal to the current url and status header |
||
223 | * |
||
224 | * @param ResponseInterface $response |
||
225 | * @param \Obsidian\Request $request |
||
226 | * @param integer $status |
||
227 | * @return ResponseInterface |
||
228 | */ |
||
229 | public static function reload( ResponseInterface $response, $request, $status = 302 ) { |
||
232 | |||
233 | /** |
||
234 | * Return a cloned response, with status headers and rendering a suitable template as the body |
||
235 | * |
||
236 | * @param ResponseInterface $response |
||
237 | * @param integer $status |
||
238 | * @return ResponseInterface |
||
239 | */ |
||
240 | 1 | public static function error( ResponseInterface $response, $status ) { |
|
249 | } |
||
250 |