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