1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vakata\http; |
4
|
|
|
|
5
|
|
|
use Laminas\Diactoros\Stream; |
6
|
|
|
use Laminas\Diactoros\Response as PSRResponse; |
7
|
|
|
|
8
|
|
|
class Response extends PSRResponse |
9
|
|
|
{ |
10
|
|
|
protected $callback = null; |
11
|
|
|
|
12
|
1 |
|
public function __construct($status = 200, string $body = null, array $headers = []) |
13
|
|
|
{ |
14
|
1 |
|
if ($body !== null) { |
15
|
|
|
$temp = (new Stream('php://temp', 'wb+')); |
16
|
|
|
$temp->write($body); |
17
|
|
|
} else { |
18
|
1 |
|
$temp = 'php://memory'; |
19
|
|
|
} |
20
|
1 |
|
parent::__construct($temp, $status, $headers); |
21
|
1 |
|
} |
22
|
1 |
|
public function hasCache() |
23
|
|
|
{ |
24
|
1 |
|
return $this->hasHeader('Cache-Control') || |
25
|
1 |
|
$this->hasHeader('Expires') || |
26
|
1 |
|
$this->hasHeader('Last-Modified') || |
27
|
1 |
|
$this->hasHeader('ETag'); |
28
|
|
|
} |
29
|
|
|
/** |
30
|
|
|
* Make the response cacheable. |
31
|
|
|
* @param int|string $expires when should the request expire - either a timestamp or strtotime expression |
32
|
|
|
* @return self |
33
|
|
|
*/ |
34
|
1 |
|
public function cacheUntil($expires) |
35
|
|
|
{ |
36
|
1 |
|
if (!is_int($expires)) { |
37
|
1 |
|
$expires = strtotime($expires); |
38
|
|
|
} |
39
|
|
|
return $this |
40
|
1 |
|
->withHeader('Pragma', 'public') |
41
|
1 |
|
->withHeader('Cache-Control', 'maxage='.($expires - time())) |
42
|
1 |
|
->withHeader('Expires', gmdate('D, d M Y H:i:s', $expires).' GMT'); |
43
|
|
|
} |
44
|
|
|
/** |
45
|
|
|
* Prevent caching |
46
|
|
|
* |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
|
|
public function noCache() |
50
|
|
|
{ |
51
|
|
|
return $this |
52
|
|
|
->withHeader('Cache-Control', 'no-store, no-cache, must-revalidate') |
53
|
|
|
->withHeader('Expires', gmdate('D, d M Y H:i:s', 0).' GMT'); |
54
|
|
|
} |
55
|
1 |
|
public function setBody(string $body) |
56
|
|
|
{ |
57
|
1 |
|
$temp = (new Stream('php://temp', 'wb+')); |
58
|
1 |
|
$temp->write($body); |
59
|
1 |
|
return $this->withBody($temp); |
60
|
|
|
} |
61
|
|
|
/** |
62
|
|
|
* Set a cookie |
63
|
|
|
* @param string $name the cookie name |
64
|
|
|
* @param string $value the cookie value |
65
|
|
|
* @param string $extra optional extra params for the cookie (semicolon delimited) |
66
|
|
|
* @return self |
67
|
|
|
*/ |
68
|
|
|
public function withCookie($name, $value, $extra = '') |
69
|
|
|
{ |
70
|
|
|
return $this->withAddedHeader('Set-Cookie', $name . '=' . urlencode($value) . '; ' . $extra); |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* Expires an existing cookie |
74
|
|
|
* @param string $name the cookie name |
75
|
|
|
* @param string $extra optional extra params for the cookie (semicolon delimited) |
76
|
|
|
* @return self |
77
|
|
|
*/ |
78
|
|
|
public function expireCookie($name, $extra = '') |
79
|
|
|
{ |
80
|
|
|
$extra = implode('; ', array_filter([ $extra, 'Expires=' . date('r', 0) ])); |
81
|
|
|
return $this->withCookie($name, 'deleted', $extra); |
82
|
|
|
} |
83
|
1 |
|
public function setContentTypeByExtension(string $extension) |
84
|
|
|
{ |
85
|
1 |
|
switch (strtolower($extension)) { |
86
|
1 |
|
case "txt": |
87
|
1 |
|
case "text": |
88
|
|
|
$type = "text/plain; charset=UTF-8"; |
89
|
|
|
break; |
90
|
1 |
|
case "xml": |
91
|
1 |
|
case "xsl": |
92
|
|
|
$type = "text/xml; charset=UTF-8"; |
93
|
|
|
break; |
94
|
1 |
|
case "json": |
95
|
1 |
|
$type = "application/json; charset=UTF-8"; |
96
|
1 |
|
break; |
97
|
|
|
case "pdf": |
98
|
|
|
$type = "application/pdf"; |
99
|
|
|
break; |
100
|
|
|
case "exe": |
101
|
|
|
$type = "application/octet-stream"; |
102
|
|
|
break; |
103
|
|
|
case "zip": |
104
|
|
|
$type = "application/zip"; |
105
|
|
|
break; |
106
|
|
|
case "docx": |
107
|
|
|
$type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; |
108
|
|
|
break; |
109
|
|
|
case "doc": |
110
|
|
|
$type = "application/msword"; |
111
|
|
|
break; |
112
|
|
|
case "xlsx": |
113
|
|
|
$type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; |
114
|
|
|
break; |
115
|
|
|
case "xls": |
116
|
|
|
$type = "application/vnd.ms-excel"; |
117
|
|
|
break; |
118
|
|
|
case "ppt": |
119
|
|
|
$type = "application/vnd.ms-powerpoint"; |
120
|
|
|
break; |
121
|
|
|
case "pptx": |
122
|
|
|
$type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; |
123
|
|
|
break; |
124
|
|
|
case "gif": |
125
|
|
|
$type = "image/gif"; |
126
|
|
|
break; |
127
|
|
|
case "png": |
128
|
|
|
$type = "image/png"; |
129
|
|
|
break; |
130
|
|
|
case "mp3": |
131
|
|
|
$type = "audio/mpeg"; |
132
|
|
|
break; |
133
|
|
|
case "mp4": |
134
|
|
|
$type = "video/mpeg"; |
135
|
|
|
break; |
136
|
|
|
case "jpeg": |
137
|
|
|
case "jpg": |
138
|
|
|
$type = "image/jpeg"; |
139
|
|
|
break; |
140
|
|
|
case "html": |
141
|
|
|
case "php": |
142
|
|
|
case "htm": |
143
|
|
|
$type = "text/html; charset=UTF-8"; |
144
|
|
|
break; |
145
|
|
|
default: |
146
|
|
|
$type = "application/binary"; |
147
|
|
|
break; |
148
|
|
|
} |
149
|
1 |
|
return $this->withHeader('Content-Type', $type); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function withCallback(callable $callback = null) |
153
|
|
|
{ |
154
|
|
|
return $this->setBody('')->setCallback($callback); |
155
|
|
|
} |
156
|
|
|
protected function setCallback(callable $callback = null) |
157
|
|
|
{ |
158
|
|
|
$this->callback = $callback; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
public function hasCallback() |
162
|
|
|
{ |
163
|
|
|
return $this->callback !== null; |
164
|
|
|
} |
165
|
|
|
public function getCallback() |
166
|
|
|
{ |
167
|
|
|
return $this->callback; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|