|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Http\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use Closure; |
|
5
|
|
|
use Generator; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Psr\Http\Message\StreamInterface; |
|
8
|
|
|
use Psr\Http\Message\UriInterface; |
|
9
|
|
|
use Wandu\Http\Psr\Response; |
|
10
|
|
|
use Wandu\Http\Psr\Stream\GeneratorStream; |
|
11
|
|
|
use Wandu\Http\Psr\Stream\StringStream; |
|
12
|
|
|
|
|
13
|
|
|
class ResponseFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param \Psr\Http\Message\StreamInterface|string $content |
|
17
|
|
|
* @param int $status |
|
18
|
|
|
* @param array $headers |
|
19
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
20
|
|
|
*/ |
|
21
|
41 |
|
public function create($content = null, $status = 200, array $headers = []) |
|
22
|
|
|
{ |
|
23
|
41 |
|
if (isset($content) && !($content instanceof StreamInterface)) { |
|
24
|
39 |
|
$content = new StringStream($content); |
|
25
|
39 |
|
} |
|
26
|
41 |
|
return new Response($status, $content, $headers, '', '1.1'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param \Closure $area |
|
31
|
|
|
* @param int $status |
|
32
|
|
|
* @param array $headers |
|
33
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
public function capture(Closure $area, $status = 200, array $headers = []) |
|
36
|
|
|
{ |
|
37
|
|
|
ob_start(); |
|
38
|
|
|
$area(); |
|
39
|
|
|
$contents = ob_get_contents(); |
|
40
|
|
|
ob_end_clean(); |
|
41
|
|
|
return $this->create($contents, $status, $headers); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string|array $data |
|
46
|
|
|
* @param int $status |
|
47
|
|
|
* @param array $headers |
|
48
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
49
|
|
|
*/ |
|
50
|
2 |
|
public function json($data = [], $status = 200, array $headers = []) |
|
51
|
|
|
{ |
|
52
|
2 |
|
return $this->create(json_encode($data), $status, $headers) |
|
53
|
2 |
|
->withHeader('Content-Type', 'application/json'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $file |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @param array $headers |
|
60
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
61
|
|
|
*/ |
|
62
|
|
|
public function download($file, $name = null, array $headers = []) |
|
63
|
|
|
{ |
|
64
|
|
|
if (!is_file($file)) { |
|
65
|
|
|
new InvalidArgumentException("\"{$file}\" is not a file."); |
|
66
|
|
|
} |
|
67
|
|
|
return $this->create(file_get_contents($file), 200, $headers) |
|
68
|
|
|
->withHeader('Pragma', 'public') |
|
69
|
|
|
->withHeader('Expires', '0') |
|
70
|
|
|
->withHeader('Content-Type', 'application/octet-stream') |
|
71
|
|
|
->withHeader( |
|
72
|
|
|
'Content-Disposition', |
|
73
|
|
|
'attachment; filename=' . (isset($name) ? $name : basename($file)) |
|
74
|
|
|
) |
|
75
|
|
|
->withHeader('Content-Transfer-Encoding', 'binary') |
|
76
|
|
|
->withHeader('Content-Length', filesize($file) . ''); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param \Psr\Http\Message\UriInterface|string $path |
|
81
|
|
|
* @param int $status |
|
82
|
|
|
* @param array $headers |
|
83
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function redirect($path, $status = 302, $headers = []) |
|
86
|
|
|
{ |
|
87
|
|
|
if ($path instanceof UriInterface) { |
|
88
|
|
|
$path = $path->__toString(); |
|
89
|
|
|
} |
|
90
|
|
|
return $this->create(null, $status, $headers) |
|
91
|
|
|
->withStatus($status) |
|
92
|
|
|
->withAddedHeader('Location', $path); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param \Generator $generator |
|
97
|
|
|
* @param int $status |
|
98
|
|
|
* @param array $headers |
|
99
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
100
|
|
|
*/ |
|
101
|
2 |
|
public function generator(Generator $generator, $status = 200, array $headers = []) |
|
102
|
|
|
{ |
|
103
|
2 |
|
return $this->create(new GeneratorStream($generator), $status, $headers); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|