1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TusPhp; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
6
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request as HttpRequest; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response as HttpResponse; |
9
|
|
|
|
10
|
|
|
class Response |
11
|
|
|
{ |
12
|
|
|
/** @var HttpResponse */ |
13
|
|
|
protected $response; |
14
|
|
|
|
15
|
|
|
/** @var bool */ |
16
|
|
|
protected $createOnly = true; |
17
|
|
|
|
18
|
|
|
/** @var array */ |
19
|
|
|
protected $headers = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Response constructor. |
23
|
|
|
*/ |
24
|
|
|
public function __construct() |
25
|
|
|
{ |
26
|
|
|
$this->response = new HttpResponse; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set create only. |
31
|
|
|
* |
32
|
|
|
* @param bool $state |
33
|
|
|
* |
34
|
|
|
* @return self |
35
|
|
|
*/ |
36
|
|
|
public function createOnly(bool $state) : self |
37
|
|
|
{ |
38
|
|
|
$this->createOnly = $state; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set headers. |
45
|
|
|
* |
46
|
|
|
* @param array $headers |
47
|
|
|
* |
48
|
|
|
* @return Response |
49
|
|
|
*/ |
50
|
|
|
public function setHeaders(array $headers) : self |
51
|
|
|
{ |
52
|
|
|
$this->headers += $headers; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Replace headers. |
59
|
|
|
* |
60
|
|
|
* @param array $headers |
61
|
|
|
* |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
|
|
public function replaceHeaders(array $headers) : self |
65
|
|
|
{ |
66
|
|
|
$this->headers = $headers; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get global headers. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getHeaders() : array |
77
|
|
|
{ |
78
|
|
|
return $this->headers; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get create only. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function getCreateOnly() : bool |
87
|
|
|
{ |
88
|
|
|
return $this->createOnly; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create and send a response. |
93
|
|
|
* |
94
|
|
|
* @param mixed $content Response data. |
95
|
|
|
* @param int $status Http status code. |
96
|
|
|
* @param array $headers Headers. |
97
|
|
|
* |
98
|
|
|
* @return HttpResponse |
99
|
|
|
*/ |
100
|
|
|
public function send($content, int $status = HttpResponse::HTTP_OK, array $headers = []) : HttpResponse |
101
|
|
|
{ |
102
|
|
|
$headers = \array_merge($this->headers, $headers); |
103
|
|
|
|
104
|
|
|
if (\is_array($content)) { |
105
|
|
|
$content = \json_encode($content); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$response = $this->response->create($content, $status, $headers); |
109
|
|
|
|
110
|
|
|
return $this->createOnly ? $response : $response->send(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create a new file download response. |
115
|
|
|
* |
116
|
|
|
* @param \SplFileInfo|string $file |
117
|
|
|
* @param string $name |
118
|
|
|
* @param array $headers |
119
|
|
|
* @param string|null $disposition |
120
|
|
|
* |
121
|
|
|
* @return BinaryFileResponse |
122
|
|
|
*/ |
123
|
|
|
public function download( |
124
|
|
|
$file, |
125
|
|
|
string $name = null, |
126
|
|
|
array $headers = [], |
127
|
|
|
string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT |
128
|
|
|
) : BinaryFileResponse { |
129
|
|
|
$response = new BinaryFileResponse($file, HttpResponse::HTTP_OK, $headers, true, $disposition); |
130
|
|
|
|
131
|
|
|
$response->prepare(HttpRequest::createFromGlobals()); |
132
|
|
|
|
133
|
|
|
if ( ! \is_null($name)) { |
134
|
|
|
$response = $response->setContentDisposition( |
135
|
|
|
$disposition, |
136
|
|
|
$name |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->createOnly ? $response : $response->send(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|