1
|
|
|
<?php namespace Comodojo\Dispatcher\Response; |
2
|
|
|
|
3
|
|
|
use \Comodojo\Dispatcher\Components\AbstractModel; |
4
|
|
|
use \Comodojo\Dispatcher\Request\Model as Request; |
5
|
|
|
use \Comodojo\Dispatcher\Router\Route; |
6
|
|
|
use \Comodojo\Foundation\Timing\TimingTrait; |
7
|
|
|
use \Comodojo\Foundation\Base\Configuration; |
8
|
|
|
use \Comodojo\Cookies\CookieManager; |
9
|
|
|
use \Psr\Log\LoggerInterface; |
10
|
|
|
use \Serializable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @package Comodojo Dispatcher |
14
|
|
|
* @author Marco Giovinazzi <[email protected]> |
15
|
|
|
* @author Marco Castiello <[email protected]> |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* LICENSE: |
19
|
|
|
* |
20
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
21
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
22
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
23
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
24
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
25
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
26
|
|
|
* THE SOFTWARE. |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
class Model extends AbstractModel implements Serializable { |
30
|
|
|
|
31
|
|
|
use TimingTrait; |
32
|
|
|
|
33
|
|
|
protected static $no_content_statuses = [100, 101, 102, 204, 304]; |
34
|
|
|
|
35
|
|
|
protected static $cacheable_methods = ['GET', 'HEAD', 'POST', 'PUT']; |
36
|
|
|
|
37
|
|
|
protected static $cacheable_statuses = [200, 203, 300, 301, 302, 404, 410]; |
38
|
|
|
|
39
|
|
|
protected $headers; |
40
|
|
|
|
41
|
|
|
protected $cookies; |
42
|
|
|
|
43
|
|
|
protected $status; |
44
|
|
|
|
45
|
|
|
protected $content; |
46
|
|
|
|
47
|
|
|
protected $location; |
48
|
|
|
|
49
|
|
|
protected $preprocessor; |
50
|
|
|
|
51
|
7 |
|
public function __construct(Configuration $configuration, LoggerInterface $logger) { |
52
|
|
|
|
53
|
7 |
|
parent::__construct($configuration, $logger); |
54
|
|
|
|
55
|
7 |
|
$this->setHeaders(new Headers()) |
56
|
7 |
|
->setCookies(new CookieManager()) |
57
|
7 |
|
->setStatus(new Status()) |
58
|
7 |
|
->setContent(new Content()) |
59
|
7 |
|
->setLocation(new Location()) |
60
|
7 |
|
->setPreprocessor(new Preprocessor()) |
61
|
7 |
|
->setTiming(); |
62
|
|
|
|
63
|
7 |
|
} |
64
|
|
|
|
65
|
6 |
|
public function getHeaders() { |
66
|
|
|
|
67
|
6 |
|
return $this->headers; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
7 |
|
public function setHeaders(Headers $headers) { |
72
|
|
|
|
73
|
7 |
|
$this->headers = $headers; |
74
|
|
|
|
75
|
7 |
|
return $this; |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
6 |
|
public function getCookies() { |
80
|
|
|
|
81
|
6 |
|
return $this->cookies; |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
public function setCookies(CookieManager $cookies) { |
86
|
|
|
|
87
|
7 |
|
$this->cookies = $cookies; |
88
|
|
|
|
89
|
7 |
|
return $this; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
9 |
|
public function getStatus() { |
94
|
|
|
|
95
|
9 |
|
return $this->status; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
public function setStatus(Status $status) { |
100
|
|
|
|
101
|
7 |
|
$this->status = $status; |
102
|
|
|
|
103
|
7 |
|
return $this; |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
9 |
|
public function getContent() { |
108
|
|
|
|
109
|
9 |
|
return $this->content; |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
7 |
|
public function setContent(Content $content) { |
114
|
|
|
|
115
|
7 |
|
$this->content = $content; |
116
|
|
|
|
117
|
7 |
|
return $this; |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
public function getLocation() { |
122
|
|
|
|
123
|
3 |
|
return $this->location; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
7 |
|
public function setLocation(Location $location) { |
128
|
|
|
|
129
|
7 |
|
$this->location = $location; |
130
|
|
|
|
131
|
7 |
|
return $this; |
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Get the current preprocessor |
137
|
|
|
* |
138
|
|
|
* @return Preprocessor |
139
|
|
|
*/ |
140
|
33 |
|
public function getPreprocessor() { |
141
|
|
|
|
142
|
33 |
|
return $this->preprocessor; |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the current preprocessor |
148
|
|
|
* |
149
|
|
|
* @param Preprocessor $preprocessor |
150
|
|
|
* The preprocessor to use |
151
|
|
|
* @return Model |
152
|
|
|
*/ |
153
|
7 |
|
public function setPreprocessor(Preprocessor $preprocessor) { |
154
|
|
|
|
155
|
7 |
|
$this->preprocessor = $preprocessor; |
156
|
|
|
|
157
|
7 |
|
return $this; |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function serialize() { |
162
|
|
|
|
163
|
|
|
return serialize($this->export()); |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function unserialize($data) { |
168
|
|
|
|
169
|
|
|
$this->import(unserialize($data)); |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
public function export() { |
174
|
|
|
|
175
|
|
|
return (object)[ |
176
|
1 |
|
'headers' => $this->getHeaders(), |
177
|
1 |
|
'cookies' => $this->getCookies()->getAll(), |
178
|
1 |
|
'status' => $this->getStatus(), |
179
|
1 |
|
'content' => $this->getContent(), |
180
|
1 |
|
'location' => $this->getLocation() |
181
|
1 |
|
]; |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
1 |
|
public function import($data) { |
186
|
|
|
|
187
|
1 |
|
if (isset($data->headers)) $this->setHeaders($data->headers); |
188
|
1 |
|
if (isset($data->status)) $this->setStatus($data->status); |
189
|
1 |
|
if (isset($data->content)) $this->setContent($data->content); |
190
|
1 |
|
if (isset($data->location)) $this->setLocation($data->location); |
191
|
|
|
|
192
|
1 |
|
if (isset($data->cookies) && is_array($data->cookies)) { |
193
|
1 |
|
$cookies = $this->getCookies(); |
194
|
1 |
|
foreach ($data->cookies as $name => $cookie) $cookies->add($cookie); |
195
|
1 |
|
} |
196
|
|
|
|
197
|
1 |
|
} |
198
|
|
|
|
199
|
2 |
|
public function consolidate(Request $request, Route $route=null) { |
200
|
|
|
|
201
|
2 |
|
$status = $this->getStatus()->get(); |
202
|
2 |
|
$preprocessor = $this->getPreprocessor()->get($status); |
203
|
2 |
|
$preprocessor->consolidate($this); |
204
|
|
|
|
205
|
2 |
|
if ($route != null) { |
|
|
|
|
206
|
|
|
$this->setClientCache($request, $route); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
// extra checks |
210
|
2 |
|
$content = $this->getContent(); |
211
|
2 |
|
$headers = $this->getHeaders(); |
212
|
|
|
|
213
|
2 |
|
if ((string)$request->getMethod() == 'HEAD' && !in_array($status, self::$no_content_statuses)) { |
|
|
|
|
214
|
|
|
$length = $content->length(); |
215
|
|
|
$content->set(null); |
216
|
|
|
if ($length) { |
217
|
|
|
$headers->set('Content-Length', $length); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
2 |
|
if ($headers->get('Transfer-Encoding') != null) { |
|
|
|
|
222
|
|
|
$headers->delete('Content-Length'); |
223
|
|
|
} |
224
|
|
|
|
225
|
2 |
|
if ((string)$request->getVersion() == '1.0' && false !== strpos($headers->get('Cache-Control'), 'no-cache')) { |
|
|
|
|
226
|
|
|
$headers->set('pragma', 'no-cache'); |
227
|
|
|
$headers->set('expires', -1); |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
} |
231
|
|
|
|
232
|
|
|
private function setClientCache(Request $request, Route $route) { |
233
|
|
|
|
234
|
|
|
$cache = strtoupper($route->getParameter('cache')); |
235
|
|
|
$ttl = (int)$route->getParameter('ttl'); |
236
|
|
|
|
237
|
|
|
if ( |
238
|
|
|
($cache == 'CLIENT' || $cache == 'BOTH') && |
|
|
|
|
239
|
|
|
in_array((string)$request->getMethod(), self::$cacheable_methods) && |
240
|
|
|
in_array($this->getStatus()->get(), self::$cacheable_statuses) |
241
|
|
|
// @TODO: here we should also check for Cache-Control no-store or private; |
242
|
|
|
// the cache layer will be improoved in future versions. |
243
|
|
|
) { |
244
|
|
|
|
245
|
|
|
$headers = $this->getHeaders(); |
246
|
|
|
$timestamp = (int) $this->getTime()->format('U')+$ttl; |
247
|
|
|
|
248
|
|
|
if ($ttl > 0) { |
249
|
|
|
|
250
|
|
|
$headers->set("Cache-Control", "max-age=".$ttl.", must-revalidate"); |
|
|
|
|
251
|
|
|
$headers->set("Expires", gmdate("D, d M Y H:i:s", $timestamp)." GMT"); |
|
|
|
|
252
|
|
|
|
253
|
|
|
} else { |
254
|
|
|
|
255
|
|
|
$headers->set("Cache-Control", "no-cache, must-revalidate"); |
|
|
|
|
256
|
|
|
$headers->set("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); |
|
|
|
|
257
|
|
|
|
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|