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
|
2 |
|
public function __construct() |
25
|
|
|
{ |
26
|
2 |
|
$this->response = new HttpResponse; |
27
|
2 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set create only. |
31
|
|
|
* |
32
|
|
|
* @param bool $state |
33
|
|
|
* |
34
|
|
|
* @return self |
35
|
|
|
*/ |
36
|
1 |
|
public function createOnly(bool $state) : self |
37
|
|
|
{ |
38
|
1 |
|
$this->createOnly = $state; |
39
|
|
|
|
40
|
1 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set headers. |
45
|
|
|
* |
46
|
|
|
* @param array $headers |
47
|
|
|
* |
48
|
|
|
* @return Response |
49
|
|
|
*/ |
50
|
2 |
|
public function setHeaders(array $headers) : self |
51
|
|
|
{ |
52
|
2 |
|
$this->headers += $headers; |
53
|
|
|
|
54
|
2 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Replace headers. |
59
|
|
|
* |
60
|
|
|
* @param array $headers |
61
|
|
|
* |
62
|
|
|
* @return Response |
63
|
|
|
*/ |
64
|
1 |
|
public function replaceHeaders(array $headers) : self |
65
|
|
|
{ |
66
|
1 |
|
$this->headers = $headers; |
67
|
|
|
|
68
|
1 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get global headers. |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
2 |
|
public function getHeaders() : array |
77
|
|
|
{ |
78
|
2 |
|
return $this->headers; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get create only. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
1 |
|
public function getCreateOnly() : bool |
87
|
|
|
{ |
88
|
1 |
|
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
|
2 |
|
public function send($content, int $status = HttpResponse::HTTP_OK, array $headers = []) : HttpResponse |
101
|
|
|
{ |
102
|
2 |
|
$headers = array_merge($this->headers, $headers); |
103
|
|
|
|
104
|
2 |
|
if (\is_array($content)) { |
105
|
1 |
|
$content = json_encode($content); |
106
|
|
|
} |
107
|
|
|
|
108
|
2 |
|
$response = $this->response::create($content, $status, $headers); |
|
|
|
|
109
|
|
|
|
110
|
2 |
|
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
|
3 |
|
public function download( |
124
|
|
|
$file, |
125
|
|
|
string $name = null, |
126
|
|
|
array $headers = [], |
127
|
|
|
string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT |
128
|
|
|
) : BinaryFileResponse { |
129
|
3 |
|
$response = new BinaryFileResponse($file, HttpResponse::HTTP_OK, $headers, true, $disposition); |
130
|
|
|
|
131
|
2 |
|
$response->prepare(HttpRequest::createFromGlobals()); |
132
|
|
|
|
133
|
2 |
|
if ($name !== null) { |
134
|
1 |
|
$response = $response->setContentDisposition( |
135
|
1 |
|
$disposition, |
136
|
1 |
|
$name |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
return $this->createOnly ? $response : $response->send(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.