Skip to content

Commit 2821e9f

Browse files
committed
Make StreamFactory optional
This means that we don't break BC.
1 parent 71b139e commit 2821e9f

File tree

2 files changed

+38
-27
lines changed

2 files changed

+38
-27
lines changed

src/Cache.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,23 @@ class Cache implements MiddlewareInterface
4747
protected $mustRevalidate;
4848

4949
/**
50-
* @var StreamFactoryInterface
50+
* @var StreamFactoryInterface|null
5151
*/
5252
protected $streamFactory;
5353

5454
/**
5555
* Create new HTTP cache
5656
*
57-
* @param string $type The cache type: "public" or "private"
58-
* @param int $maxAge The maximum age of client-side cache
59-
* @param bool $mustRevalidate must-revalidate
57+
* @param string $type The cache type: "public" or "private"
58+
* @param int $maxAge The maximum age of client-side cache
59+
* @param bool $mustRevalidate must-revalidate
60+
* @param StreamFactoryInterface|null $streamFactory Will clear body of a 304 if provided
6061
*/
6162
public function __construct(
62-
StreamFactoryInterface $streamFactory,
6363
string $type = 'private',
6464
int $maxAge = 86400,
65-
bool $mustRevalidate = false
65+
bool $mustRevalidate = false,
66+
?StreamFactoryInterface $streamFactory = null
6667
) {
6768
$this->type = $type;
6869
$this->maxAge = $maxAge;
@@ -112,8 +113,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
112113
if ($ifNoneMatch) {
113114
$etagList = preg_split('@\s*,\s*@', $ifNoneMatch);
114115
if (is_array($etagList) && (in_array($etag, $etagList) || in_array('*', $etagList))) {
115-
return $response->withStatus(304)
116-
->withBody($this->streamFactory->createStream(''));
116+
$response = $response->withStatus(304);
117+
if ($this->streamFactory) {
118+
$response = $response->withBody($this->streamFactory->createStream(''));
119+
}
120+
return $response;
117121
}
118122
}
119123
}
@@ -130,8 +134,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
130134
$ifModifiedSince = $request->getHeaderLine('If-Modified-Since');
131135

132136
if ($ifModifiedSince && $lastModified <= strtotime($ifModifiedSince)) {
133-
return $response->withStatus(304)
134-
->withBody($this->streamFactory->createStream(''));
137+
$response = $response->withStatus(304);
138+
if ($this->streamFactory) {
139+
$response = $response->withBody($this->streamFactory->createStream(''));
140+
}
141+
return $response;
135142
}
136143
}
137144

tests/CacheTest.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222

2323
class CacheTest extends TestCase
2424
{
25-
private function createCache(string $type = 'privte', int $maxAge = 86400, bool $mustRevalidate = false): Cache
25+
private function createCache(string $type, int $maxAge, bool $mustRevalidate, bool $withStreamFactory): Cache
2626
{
27-
return new Cache(new StreamFactory(), $type, $maxAge, $mustRevalidate);
27+
$streamFactory = $withStreamFactory ? new StreamFactory() : null;
28+
return new Cache($type, $maxAge, $mustRevalidate, $streamFactory);
2829
}
2930

30-
3131
public function requestFactory(): ServerRequestInterface
3232
{
3333
$serverRequestFactory = new ServerRequestFactory();
@@ -71,7 +71,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
7171

7272
public function testCacheControlHeader()
7373
{
74-
$cache = $this->createCache('public', 86400);
74+
$cache = $this->createCache('public', 86400, false, false);
7575
$req = $this->requestFactory();
7676

7777
$res = $cache->process($req, $this->createRequestHandler(null));
@@ -83,7 +83,7 @@ public function testCacheControlHeader()
8383

8484
public function testCacheControlHeaderWithMustRevalidate()
8585
{
86-
$cache = $this->createCache('private', 86400, true);
86+
$cache = $this->createCache('private', 86400, true, false);
8787
$req = $this->requestFactory();
8888

8989
$res = $cache->process($req, $this->createRequestHandler(null));
@@ -95,7 +95,7 @@ public function testCacheControlHeaderWithMustRevalidate()
9595

9696
public function testCacheControlHeaderWithZeroMaxAge()
9797
{
98-
$cache = $this->createCache('private', 0, false);
98+
$cache = $this->createCache('private', 0, false, false);
9999
$req = $this->requestFactory();
100100

101101
$res = $cache->process($req, $this->createRequestHandler(null));
@@ -107,7 +107,7 @@ public function testCacheControlHeaderWithZeroMaxAge()
107107

108108
public function testCacheControlHeaderDoesNotOverrideExistingHeader()
109109
{
110-
$cache = $this->createCache('public', 86400);
110+
$cache = $this->createCache('public', 86400, false, false);
111111
$req = $this->requestFactory();
112112

113113
$res = $this->createResponse()->withHeader('Cache-Control', 'no-cache,no-store');
@@ -123,22 +123,24 @@ public function testLastModifiedWithCacheHit()
123123
$now = time();
124124
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
125125
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now + 86400);
126-
$cache = $this->createCache('public', 86400);
126+
$cache = $this->createCache('public', 86400, false, false);
127127

128128
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
129129

130130
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
131+
$res->getBody()->write('payload data');
131132
$res = $cache->process($req, $this->createRequestHandler($res));
132133

133134
$this->assertEquals(304, $res->getStatusCode());
135+
self::assertSame('payload data', (string) $res->getBody());
134136
}
135137

136138
public function testLastModifiedWithCacheHitAndNewerDate()
137139
{
138140
$now = time();
139141
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
140142
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now + 172800); // <-- Newer date
141-
$cache = $this->createCache('public', 86400);
143+
$cache = $this->createCache('public', 86400, false, false);
142144
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
143145

144146
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
@@ -152,7 +154,7 @@ public function testLastModifiedWithCacheHitAndOlderDate()
152154
$now = time();
153155
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
154156
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now); // <-- Older date
155-
$cache = $this->createCache('public', 86400);
157+
$cache = $this->createCache('public', 86400, false, false);
156158
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
157159

158160
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
@@ -166,7 +168,7 @@ public function testLastModifiedWithCacheMiss()
166168
$now = time();
167169
$lastModified = gmdate('D, d M Y H:i:s T', $now + 86400);
168170
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now - 86400);
169-
$cache = $this->createCache('public', 86400);
171+
$cache = $this->createCache('public', 86400, false, false);
170172
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
171173

172174
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);
@@ -179,20 +181,22 @@ public function testETagWithCacheHit()
179181
{
180182
$etag = 'abc';
181183
$ifNoneMatch = 'abc';
182-
$cache = $this->createCache('public', 86400);
184+
$cache = $this->createCache('public', 86400, false, false);
183185
$req = $this->requestFactory()->withHeader('If-None-Match', $ifNoneMatch);
184186

185187
$res = $this->createResponse()->withHeader('Etag', $etag);
188+
$res->getBody()->write('payload data');
186189
$res = $cache->process($req, $this->createRequestHandler($res));
187190

188191
$this->assertEquals(304, $res->getStatusCode());
192+
self::assertSame('payload data', (string) $res->getBody());
189193
}
190194

191195
public function testETagWithCacheMiss()
192196
{
193197
$etag = 'abc';
194198
$ifNoneMatch = 'xyz';
195-
$cache = $this->createCache('public', 86400);
199+
$cache = $this->createCache('public', 86400, false, false);
196200
$req = $this->requestFactory()->withHeader('If-None-Match', $ifNoneMatch);
197201

198202
$res = $this->createResponse()->withHeader('Etag', $etag);
@@ -201,10 +205,10 @@ public function testETagWithCacheMiss()
201205
$this->assertEquals(200, $res->getStatusCode());
202206
}
203207

204-
public function testETagReturnsNoBodyOnCacheHit(): void
208+
public function testETagReturnsNoBodyOnCacheHitWhenAStreamFactoryIsProvided(): void
205209
{
206210
$etag = 'abc';
207-
$cache = $this->createCache();
211+
$cache = $this->createCache('private', 86400, false, true);
208212
$req = $this->requestFactory()->withHeader('If-None-Match', $etag);
209213

210214
$res = $this->createResponse()->withHeader('Etag', $etag);
@@ -215,12 +219,12 @@ public function testETagReturnsNoBodyOnCacheHit(): void
215219
self::assertSame('', (string) $res->getBody());
216220
}
217221

218-
public function testLastModifiedReturnsNoBodyOnCacheHit(): void
222+
public function testLastModifiedReturnsNoBodyOnCacheHitWhenAStreamFactoryIsProvided(): void
219223
{
220224
$now = time() + 86400;
221225
$lastModified = gmdate('D, d M Y H:i:s T', $now);
222226
$ifModifiedSince = gmdate('D, d M Y H:i:s T', $now);
223-
$cache = $this->createCache();
227+
$cache = $this->createCache('private', 86400, false, true);
224228

225229
$req = $this->requestFactory()->withHeader('If-Modified-Since', $ifModifiedSince);
226230
$res = $this->createResponse()->withHeader('Last-Modified', $lastModified);

0 commit comments

Comments
 (0)