1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Http\Factory; |
3
|
|
|
|
4
|
|
|
use Closure; |
5
|
|
|
use Generator; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use JsonSerializable; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\StreamInterface; |
10
|
|
|
use Psr\Http\Message\UriInterface; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Traversable; |
13
|
|
|
use Wandu\Http\Psr\Response; |
14
|
|
|
use Wandu\Http\Psr\Stream\IteratorStream; |
15
|
|
|
use Wandu\Http\Psr\Stream\ResourceStream; |
16
|
|
|
use Wandu\Http\Psr\Stream\StringStream; |
17
|
|
|
|
18
|
|
|
class ResponseFactory |
19
|
|
|
{ |
20
|
|
|
/** @var \Wandu\Http\Factory\ResponseFactory */ |
21
|
|
|
public static $instance; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param mixed $contents |
25
|
|
|
* @param int $status |
26
|
|
|
* @param array $headers |
27
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
28
|
|
|
*/ |
29
|
19 |
|
public function auto($contents = null, $status = 200, array $headers = []) |
30
|
|
|
{ |
31
|
19 |
|
if ($contents instanceof ResponseInterface) { |
32
|
3 |
|
return $contents; |
33
|
|
|
} |
34
|
18 |
|
if ($contents instanceof StreamInterface || !isset($contents)) { |
35
|
6 |
|
return $this->create($contents, $status, $headers); |
36
|
|
|
} |
37
|
14 |
|
while (is_callable($contents)) { |
38
|
3 |
|
$nextResponse = call_user_func($contents); |
39
|
3 |
|
$contents = $nextResponse; |
40
|
|
|
} |
41
|
|
|
// int, float, boolean, string |
42
|
14 |
|
if (is_scalar($contents)) { |
43
|
9 |
|
if ($contents === true) { |
44
|
2 |
|
$contents = 'true'; |
45
|
8 |
|
} elseif ($contents === false) { |
46
|
2 |
|
$contents = 'false'; |
47
|
|
|
} |
48
|
9 |
|
return $this->string((string)$contents, $status, $headers); |
49
|
|
|
} |
50
|
6 |
|
if (is_array($contents)) { |
51
|
2 |
|
return $this->json($contents, $status, $headers); |
52
|
|
|
} |
53
|
5 |
|
if (is_object($contents)) { |
54
|
4 |
|
if ($contents instanceof Traversable) { |
55
|
2 |
|
return $this->iterator($contents, $status, $headers); |
56
|
2 |
|
} elseif ($contents instanceof JsonSerializable) { |
|
|
|
|
57
|
1 |
|
return $this->json($contents, $status, $headers); |
58
|
2 |
|
} elseif (method_exists($contents, '__toString')) { |
59
|
1 |
|
return $this->string($contents, $status, $headers); |
|
|
|
|
60
|
|
|
} else { |
61
|
1 |
|
return $this->json($contents, $status, $headers); |
62
|
|
|
} |
63
|
|
|
} |
64
|
2 |
|
if (is_resource($contents)) { |
65
|
2 |
|
return $this->resource($contents, $status, $headers); |
66
|
|
|
} |
67
|
|
|
throw new RuntimeException('unsupported type of response.'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \Psr\Http\Message\StreamInterface $stream |
72
|
|
|
* @param int $status |
73
|
|
|
* @param array $headers |
74
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
75
|
|
|
*/ |
76
|
31 |
|
public function create(StreamInterface $stream = null, $status = 200, array $headers = []) |
77
|
|
|
{ |
78
|
31 |
|
return new Response($status, $stream, $headers, '', '1.1'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Closure $area |
83
|
|
|
* @param int $status |
84
|
|
|
* @param array $headers |
85
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
86
|
|
|
*/ |
87
|
2 |
|
public function capture(Closure $area, $status = 200, array $headers = []) |
88
|
|
|
{ |
89
|
2 |
|
ob_start(); |
90
|
2 |
|
$area(); |
91
|
2 |
|
$contents = ob_get_contents(); |
92
|
2 |
|
ob_end_clean(); |
93
|
2 |
|
return $this->string($contents, $status, $headers); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $contents |
98
|
|
|
* @param int $status |
99
|
|
|
* @param array $headers |
100
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
101
|
|
|
*/ |
102
|
17 |
|
public function string(string $contents, $status = 200, array $headers = []) |
103
|
|
|
{ |
104
|
17 |
|
return $this->create(new StringStream($contents), $status, $headers); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $data |
109
|
|
|
* @param int $status |
110
|
|
|
* @param array $headers |
111
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
112
|
|
|
*/ |
113
|
5 |
|
public function json($data = [], $status = 200, array $headers = []) |
114
|
|
|
{ |
115
|
5 |
|
return $this->create(new StringStream(json_encode($data)), $status, $headers) |
116
|
5 |
|
->withHeader('Content-Type', 'application/json'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $file |
121
|
|
|
* @param string $name |
122
|
|
|
* @param array $headers |
123
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
124
|
|
|
*/ |
125
|
|
|
public function download($file, $name = null, array $headers = []) |
126
|
|
|
{ |
127
|
|
|
if (!is_file($file)) { |
128
|
|
|
new InvalidArgumentException("\"{$file}\" is not a file."); |
129
|
|
|
} |
130
|
|
|
return $this->create(new StringStream(file_get_contents($file)), 200, $headers) |
131
|
|
|
->withHeader('Pragma', 'public') |
132
|
|
|
->withHeader('Expires', '0') |
133
|
|
|
->withHeader('Content-Type', 'application/octet-stream') |
134
|
|
|
->withHeader( |
135
|
|
|
'Content-Disposition', |
136
|
|
|
'attachment; filename=' . (isset($name) ? $name : basename($file)) |
137
|
|
|
) |
138
|
|
|
->withHeader('Content-Transfer-Encoding', 'binary') |
139
|
|
|
->withHeader('Content-Length', filesize($file) . ''); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param \Psr\Http\Message\UriInterface|string $path |
144
|
|
|
* @param int $status |
145
|
|
|
* @param array $headers |
146
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
147
|
|
|
*/ |
148
|
2 |
|
public function redirect($path, $status = 302, $headers = []) |
149
|
|
|
{ |
150
|
2 |
|
if ($path instanceof UriInterface) { |
151
|
|
|
$path = $path->__toString(); |
152
|
|
|
} |
153
|
2 |
|
return $this->create(null, $status, $headers) |
154
|
2 |
|
->withStatus($status) |
155
|
2 |
|
->withAddedHeader('Location', $path); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @deprecated use iterator |
160
|
|
|
* |
161
|
|
|
* @param \Generator $generator |
162
|
|
|
* @param int $status |
163
|
|
|
* @param array $headers |
164
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
165
|
|
|
*/ |
166
|
|
|
public function generator(Generator $generator, $status = 200, array $headers = []) |
167
|
|
|
{ |
168
|
|
|
return $this->create(new Iteratorstream($generator), $status, $headers); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param \Traversable $iterator |
173
|
|
|
* @param int $status |
174
|
|
|
* @param array $headers |
175
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
176
|
|
|
*/ |
177
|
4 |
|
public function iterator(Traversable $iterator, $status = 200, array $headers = []) |
178
|
|
|
{ |
179
|
4 |
|
return $this->create(new IteratorStream($iterator), $status, $headers); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param resource $resource |
184
|
|
|
* @param int $status |
185
|
|
|
* @param array $headers |
186
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
187
|
|
|
*/ |
188
|
2 |
|
public function resource($resource, $status = 200, array $headers = []) |
189
|
|
|
{ |
190
|
2 |
|
return $this->create(new ResourceStream($resource), $status, $headers); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|