1 | <?php |
||
12 | class Response { |
||
13 | /** |
||
14 | * Create a new response object |
||
15 | * |
||
16 | * @return Psr7Response |
||
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 ) { |
||
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( $response ) { |
||
59 | |||
60 | /** |
||
61 | * Send a request's body to the client |
||
62 | * |
||
63 | * @param ResponseInterface $response |
||
64 | * @return null |
||
65 | */ |
||
66 | protected static function sendBody( $response, $chunk_size = 4096 ) { |
||
91 | |||
92 | /** |
||
93 | * Return a cloned response with the passed string as the body |
||
94 | * |
||
95 | * @param Psr7Response $response |
||
96 | * @param string $output |
||
97 | * @return Psr7Response |
||
98 | */ |
||
99 | public static function output( Psr7Response $response, $output ) { |
||
103 | |||
104 | /** |
||
105 | * Resolve a template or a template array to an absolute filepath |
||
106 | * |
||
107 | * @param string|string[] $templates |
||
108 | * @return string |
||
109 | */ |
||
110 | protected static function resolveTemplate( $templates ) { |
||
111 | $templates = is_array( $templates ) ? $templates : [$templates]; |
||
112 | $template = locate_template( $templates, false ); |
||
113 | |||
114 | // locate_template failed to find the template - test if a valid absolute path was passed |
||
115 | if ( ! $template ) { |
||
116 | foreach ( $templates as $tpl ) { |
||
117 | if ( file_exists( $tpl ) ) { |
||
118 | $template = $tpl; |
||
119 | break; |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | |||
124 | return $template; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Return a cloned response, resolving and rendering a template as the body |
||
129 | * |
||
130 | * @param Psr7Response $response |
||
131 | * @param string|string[] $templates |
||
132 | * @param array $context |
||
133 | * @return Psr7Response |
||
134 | */ |
||
135 | public static function template( Psr7Response $response, $templates, $context = array() ) { |
||
145 | |||
146 | /** |
||
147 | * Return a cloned response, json encoding the passed array as the body |
||
148 | * |
||
149 | * @param Psr7Response $response |
||
150 | * @param array $data |
||
151 | * @return Psr7Response |
||
152 | */ |
||
153 | public static function json( Psr7Response $response, $data ) { |
||
158 | |||
159 | /** |
||
160 | * Return a cloned response, with location and status headers |
||
161 | * |
||
162 | * @param Psr7Response $response |
||
163 | * @param string $url |
||
164 | * @param integer $status |
||
165 | * @return Psr7Response |
||
166 | */ |
||
167 | public static function redirect( Psr7Response $response, $url, $status = 302 ) { |
||
172 | |||
173 | /** |
||
174 | * Return a cloned response, with status headers and rendering a suitable template as the body |
||
175 | * |
||
176 | * @param Psr7Response $response |
||
177 | * @param integer $status |
||
178 | * @return Psr7Response |
||
179 | */ |
||
180 | public static function error( Psr7Response $response, $status ) { |
||
189 | } |
||
190 |