|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr7Middlewares\Utils; |
|
6
|
|
|
use Psr\Http\Message\RequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use DateTimeImmutable; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Middleware to send Expire header. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Expire |
|
14
|
|
|
{ |
|
15
|
|
|
private $expires = [ |
|
16
|
|
|
'text/css' => '+1 year', |
|
17
|
|
|
'application/atom+xml' => '+1 hour', |
|
18
|
|
|
'application/rdf+xml' => '+1 hour', |
|
19
|
|
|
'application/rss+xml' => '+1 hour', |
|
20
|
|
|
'application/json' => '+0 seconds', |
|
21
|
|
|
'application/ld+json' => '+0 seconds', |
|
22
|
|
|
'application/schema+json' => '+0 seconds', |
|
23
|
|
|
'application/vnd.geo+json' => '+0 seconds', |
|
24
|
|
|
'application/xml' => '+0 seconds', |
|
25
|
|
|
'text/xml' => '+0 seconds', |
|
26
|
|
|
'image/vnd.microsoft.icon' => '+1 week', |
|
27
|
|
|
'image/x-icon' => '+1 week', |
|
28
|
|
|
'text/html' => '+0 seconds', |
|
29
|
|
|
'application/javascript' => '+1 year', |
|
30
|
|
|
'application/x-javascript' => '+1 year', |
|
31
|
|
|
'text/javascript' => '+1 year', |
|
32
|
|
|
'application/manifest+json' => '+1 week', |
|
33
|
|
|
'application/x-web-app-manifest+json' => '+0 seconds', |
|
34
|
|
|
'text/cache-manifest' => '+0 seconds', |
|
35
|
|
|
'audio/ogg' => '+1 month', |
|
36
|
|
|
'image/bmp' => '+1 month', |
|
37
|
|
|
'image/gif' => '+1 month', |
|
38
|
|
|
'image/jpeg' => '+1 month', |
|
39
|
|
|
'image/png' => '+1 month', |
|
40
|
|
|
'image/svg+xml' => '+1 month', |
|
41
|
|
|
'image/webp' => '+1 month', |
|
42
|
|
|
'video/mp4' => '+1 month', |
|
43
|
|
|
'video/ogg' => '+1 month', |
|
44
|
|
|
'video/webm' => '+1 month', |
|
45
|
|
|
'application/vnd.ms-fontobject' => '+1 month', |
|
46
|
|
|
'font/eot' => '+1 month', |
|
47
|
|
|
'font/opentype' => '+1 month', |
|
48
|
|
|
'application/x-font-ttf' => '+1 month', |
|
49
|
|
|
'application/font-woff' => '+1 month', |
|
50
|
|
|
'application/x-font-woff' => '+1 month', |
|
51
|
|
|
'font/woff' => '+1 month', |
|
52
|
|
|
'application/font-woff2' => '+1 month', |
|
53
|
|
|
'text/x-cross-domain-policy' => '+1 week', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Add a new expire. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $mime |
|
60
|
|
|
* @param string $expire |
|
61
|
|
|
*/ |
|
62
|
|
|
public function addExpire($mime, $expire) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->expires[$mime] = $expire; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Execute the middleware. |
|
69
|
|
|
* |
|
70
|
|
|
* @param RequestInterface $request |
|
71
|
|
|
* @param ResponseInterface $response |
|
72
|
|
|
* @param callable $next |
|
73
|
|
|
* |
|
74
|
|
|
* @return ResponseInterface |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
|
77
|
|
|
{ |
|
78
|
|
|
$response = $next($request, $response); |
|
79
|
|
|
|
|
80
|
|
|
$mime = Utils\Helpers::getMimeType($response); |
|
81
|
|
|
|
|
82
|
|
|
if (isset($this->expires[$mime])) { |
|
83
|
|
|
$cacheControl = $response->getHeaderLine('Cache-Control') ?: ''; |
|
84
|
|
|
|
|
85
|
|
|
if (stripos($cacheControl, 'max-age') === false) { |
|
86
|
|
|
$expire = new DateTimeImmutable($this->expires[$mime]); |
|
87
|
|
|
|
|
88
|
|
|
$cacheControl .= ' max-age='.($expire->getTimestamp() - time()); |
|
89
|
|
|
|
|
90
|
|
|
return $response |
|
91
|
|
|
->withHeader('Cache-Control', trim($cacheControl)) |
|
92
|
|
|
->withHeader('Expires', $expire->format('D, d M Y H:i:s').' GMT'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $response; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|