|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CarbonFramework; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7; |
|
6
|
|
|
use GuzzleHttp\Psr7\Response as Psr7Response; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A collection of tools for the creation of responses |
|
11
|
|
|
*/ |
|
12
|
|
|
class Response { |
|
13
|
|
|
/** |
|
14
|
|
|
* Create a new response object |
|
15
|
|
|
* |
|
16
|
|
|
* @return Psr7Response |
|
17
|
|
|
*/ |
|
18
|
1 |
|
public static function response() { |
|
|
|
|
|
|
19
|
1 |
|
return new Psr7Response(); |
|
20
|
|
|
} |
|
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( $response ) { |
|
44
|
|
|
// Status |
|
45
|
|
|
header( sprintf( |
|
46
|
|
|
'HTTP/%s %s %s', |
|
47
|
|
|
$response->getProtocolVersion(), |
|
48
|
|
|
$response->getStatusCode(), |
|
49
|
|
|
$response->getReasonPhrase() |
|
50
|
|
|
) ); |
|
51
|
|
|
|
|
52
|
|
|
// Headers |
|
53
|
|
|
foreach ( $response->getHeaders() as $name => $values ) { |
|
54
|
|
|
foreach ( $values as $value ) { |
|
55
|
|
|
header( sprintf( '%s: %s', $name, $value ), false ); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
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 ) { |
|
67
|
|
|
$body = $response->getBody(); |
|
68
|
|
|
if ( $body->isSeekable() ) { |
|
69
|
|
|
$body->rewind(); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$content_length = $response->getHeaderLine( 'Content-Length' ); |
|
73
|
|
|
if ( ! $content_length ) { |
|
74
|
|
|
$content_length = $body->getSize(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$content_left = $content_length ? $content_length : -1; |
|
78
|
|
|
$amount_to_read = $content_left > -1 ? min( $chunk_size, $content_left ) : $chunk_size; |
|
79
|
|
|
while ( ! $body->eof() ) { |
|
80
|
|
|
echo $body->read( $amount_to_read ); |
|
81
|
|
|
|
|
82
|
|
|
if ( $content_left > -1 ) { |
|
83
|
|
|
$content_left -= $amount_to_read; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ( connection_status() != CONNECTION_NORMAL ) { |
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
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 ) { |
|
100
|
|
|
$response = $response->withBody( Psr7\stream_for( $output ) ); |
|
101
|
|
|
return $response; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Return a cloned response, resolving and rendering a template as the body |
|
106
|
|
|
* |
|
107
|
|
|
* @param Psr7Response $response |
|
108
|
|
|
* @param string|string[] $templates |
|
109
|
|
|
* @param array $context |
|
110
|
|
|
* @return Psr7Response |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function template( Psr7Response $response, $templates, $context = array() ) { |
|
113
|
|
|
$templates = is_array( $templates ) ? $templates : [$templates]; |
|
114
|
|
|
$template = locate_template( $templates, false ); |
|
115
|
|
|
|
|
116
|
|
|
// locate_template failed to find the template - test if a valid absolute path was passed |
|
117
|
|
|
if ( ! $template ) { |
|
118
|
|
|
foreach ( $templates as $tpl ) { |
|
119
|
|
|
if ( file_exists( $tpl ) ) { |
|
120
|
|
|
$template = $tpl; |
|
121
|
|
|
break; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$engine = Framework::resolve( 'framework.templating.engine' ); |
|
127
|
|
|
$html = $engine->render( $template, $context ); |
|
128
|
|
|
|
|
129
|
|
|
$response = $response->withHeader( 'Content-Type', 'text/html' ); |
|
130
|
|
|
$response = $response->withBody( Psr7\stream_for( $html ) ); |
|
131
|
|
|
return $response; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return a cloned response, json encoding the passed array as the body |
|
136
|
|
|
* |
|
137
|
|
|
* @param Psr7Response $response |
|
138
|
|
|
* @param array $data |
|
139
|
|
|
* @return Psr7Response |
|
140
|
|
|
*/ |
|
141
|
|
|
public static function json( Psr7Response $response, $data ) { |
|
142
|
|
|
$response = $response->withHeader( 'Content-Type', 'application/json' ); |
|
143
|
|
|
$response = $response->withBody( Psr7\stream_for( wp_json_encode( $data ) ) ); |
|
144
|
|
|
return $response; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Return a cloned response, with location and status headers |
|
149
|
|
|
* |
|
150
|
|
|
* @param Psr7Response $response |
|
151
|
|
|
* @param string $url |
|
152
|
|
|
* @param integer $status |
|
153
|
|
|
* @return Psr7Response |
|
154
|
|
|
*/ |
|
155
|
|
|
public static function redirect( Psr7Response $response, $url, $status = 302 ) { |
|
156
|
|
|
$response = $response->withStatus( $status ); |
|
157
|
|
|
$response = $response->withHeader( 'Location', $url ); |
|
158
|
|
|
return $response; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Return a cloned response, with status headers and rendering a suitable template as the body |
|
163
|
|
|
* |
|
164
|
|
|
* @param Psr7Response $response |
|
165
|
|
|
* @param integer $status |
|
166
|
|
|
* @return Psr7Response |
|
167
|
|
|
*/ |
|
168
|
|
|
public static function error( Psr7Response $response, $status ) { |
|
169
|
|
|
global $wp_query; |
|
|
|
|
|
|
170
|
|
|
if ( $status === 404 ) { |
|
171
|
|
|
$wp_query->set_404(); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
$response = $response->withStatus( $status ); |
|
175
|
|
|
return static::template( $response, array( $status . '.php', 'index.php' ) ); |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|