This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
/
CacheMiddleware.php
192 lines (164 loc) · 4.41 KB
/
CacheMiddleware.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
//
// +---------------------------------------------------------------------+
// | CODE INC. SOURCE CODE |
// +---------------------------------------------------------------------+
// | Copyright (c) 2017 - Code Inc. SAS - All Rights Reserved. |
// | Visit https://www.codeinc.fr for more information about licensing. |
// +---------------------------------------------------------------------+
// | NOTICE: All information contained herein is, and remains the |
// | property of Code Inc. SAS. The intellectual and technical concepts |
// | contained herein are proprietary to Code Inc. SAS are protected by |
// | trade secret or copyright law. Dissemination of this information or |
// | reproduction of this material is strictly forbidden unless prior |
// | written permission is obtained from Code Inc. SAS. |
// +---------------------------------------------------------------------+
//
// Author: Joan Fabrégat <joan@codeinc.fr>
// Date: 04/03/2018
// Time: 08:38
// Project: HttpHeadersMiddleware
//
declare(strict_types = 1);
namespace CodeInc\HttpHeadersMiddleware;
use CodeInc\HttpHeadersMiddleware\Tests\CacheMiddlewareTest;
use CodeInc\Psr7Responses\NotModifiedResponse;
use Micheh\Cache\CacheUtil;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
/**
* Class CacheMiddleware
*
* @uses CacheUtil
* @see CacheMiddlewareTest
* @package CodeInc\HttpHeadersMiddleware
* @author Joan Fabrégat <joan@codeinc.fr>
* @license MIT <https://github.com/CodeIncHQ/HttpHeadersMiddleware/blob/master/LICENSE>
* @link https://github.com/CodeIncHQ/HttpHeadersMiddleware
*/
class CacheMiddleware implements MiddlewareInterface
{
/**
* @var \DateTime|null
*/
private $lastModified;
/**
* @var string|null
*/
private $etag;
/**
* @var bool
*/
private $public;
/**
* @var int
*/
private $maxAge;
/**
* CacheMiddleware constructor.
*
* @param bool $public
* @param int $maxAge
* @param \DateTime|null $lastModified
* @param null|string $etag
*/
public function __construct(bool $public = false, int $maxAge = 600, ?\DateTime $lastModified = null,
?string $etag = null)
{
$this->public = $public;
$this->maxAge = $maxAge;
if ($lastModified) {
$this->setLastModified($lastModified);
}
if ($etag) {
$this->setEtag($etag);
}
}
/**
* Defines if the cache is public
*
* @param bool $isPublic
*/
public function setPublic(bool $isPublic):void
{
$this->public = $isPublic;
}
/**
* @return bool
*/
public function isPublic():bool
{
return $this->public;
}
/**
* @param int $maxAge
*/
public function setMaxAge(int $maxAge):void
{
$this->maxAge = $maxAge;
}
/**
* @return int
*/
public function getMaxAge():int
{
return $this->maxAge;
}
/**
* @param \DateTime|null $lastModified
*/
public function setLastModified(?\DateTime $lastModified):void
{
$this->lastModified = $lastModified;
}
/**
* @return \DateTime|null
*/
public function getLastModified():?\DateTime
{
return $this->lastModified;
}
/**
* @param null|string $etag
*/
public function setEtag(?string $etag):void
{
$this->etag = $etag;
}
/**
* @return null|string
*/
public function getEtag():?string
{
return $this->etag;
}
/**
* @inheritdoc
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request,
RequestHandlerInterface $handler):ResponseInterface
{
// processing
$response = $handler->handle($request);
// adding cache headers
$cacheUtils = new CacheUtil();
$response = $cacheUtils->withCache($response, $this->public, $this->maxAge);
// adding eTag
if ($this->etag) {
$response = $cacheUtils->withETag($response, $this->etag);
}
// adding last modified
if ($this->lastModified) {
$response = $cacheUtils->withLastModified($response, $this->lastModified);
if ($cacheUtils->isNotModified($request, $response)) {
return new NotModifiedResponse();
}
}
return $response;
}
}