Skip to content

Commit fb213e9

Browse files
committed
Merge branch 'hotfix/1.0.2' into develop
2 parents 9a084fd + 51d7617 commit fb213e9

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ For the Symfony 4/Flex structure, you need to adjust your `index.php` like this:
6565

6666
// public/index.php
6767
$kernel = new Kernel($env, $debug);
68-
$kernel = new HttpCache($kernel, new Psr6Store(['cache_directory' => $kernel->getCacheDir()]));
68+
$kernel = new HttpCache(
69+
$kernel,
70+
new Psr6Store(['cache_directory' => $kernel->getCacheDir()]),
71+
null,
72+
['debug' => $debug]
73+
);
6974
```
7075

7176
That's it, that's all there is to do. The `Psr6Store` will automatically

src/Psr6Store.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
1818
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
1919
use Symfony\Component\Cache\PruneableInterface;
20-
use Symfony\Component\HttpFoundation\HeaderBag;
2120
use Symfony\Component\HttpFoundation\Request;
2221
use Symfony\Component\HttpFoundation\Response;
2322
use Symfony\Component\Lock\Exception\LockReleasingException;
@@ -120,7 +119,7 @@ public function __construct(array $options = [])
120119
}
121120

122121
return new TagAwareAdapter(
123-
new FilesystemAdapter('http-cache-tags', 0, $options['cache_directory'])
122+
new FilesystemAdapter('http_cache', 0, $options['cache_directory'])
124123
);
125124
})->setAllowedTypes('cache', AdapterInterface::class);
126125

@@ -167,7 +166,7 @@ public function lookup(Request $request)
167166
// Otherwise we have to see if Vary headers match
168167
$varyKeyRequest = $this->getVaryKey(
169168
$responseData['vary'],
170-
$request->headers
169+
$request
171170
);
172171

173172
if ($varyKeyRequest === $varyKeyResponse) {
@@ -218,7 +217,7 @@ public function write(Request $request, Response $response)
218217
}
219218

220219
// Add or replace entry with current Vary header key
221-
$entries[$this->getVaryKey($response->getVary(), $response->headers)] = [
220+
$entries[$this->getVaryKey($response->getVary(), $request)] = [
222221
'vary' => $response->getVary(),
223222
'headers' => $headers,
224223
'status' => $response->getStatusCode(),
@@ -400,12 +399,12 @@ public function getCacheKey(Request $request)
400399
}
401400

402401
/**
403-
* @param array $vary
404-
* @param HeaderBag $headerBag
402+
* @param array $vary
403+
* @param Request $request
405404
*
406405
* @return string
407406
*/
408-
public function getVaryKey(array $vary, HeaderBag $headerBag)
407+
public function getVaryKey(array $vary, Request $request)
409408
{
410409
if (0 === \count($vary)) {
411410
return self::NON_VARYING_KEY;
@@ -416,7 +415,7 @@ public function getVaryKey(array $vary, HeaderBag $headerBag)
416415
$hashData = '';
417416

418417
foreach ($vary as $headerName) {
419-
$hashData .= $headerName.':'.$headerBag->get($headerName);
418+
$hashData .= $headerName.':'.$request->headers->get($headerName);
420419
}
421420

422421
return hash('sha256', $hashData);

tests/Psr6StoreTest.php

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -332,34 +332,36 @@ public function testLookupWithEmptyCache()
332332
public function testLookupWithVaryResponse()
333333
{
334334
$request = Request::create('https://foobar.com/');
335-
$response = new Response('hello world', 200, ['Vary' => 'Foobar', 'Foobar' => 'whatever']);
335+
$request->headers->set('Foobar', 'whatever');
336+
$response = new Response('hello world', 200, ['Vary' => 'Foobar']);
336337

337338
$this->store->write($request, $response);
338339

340+
$request = Request::create('https://foobar.com/');
339341
$result = $this->store->lookup($request);
340-
341342
$this->assertNull($result);
342343

343344
$request = Request::create('https://foobar.com/');
344345
$request->headers->set('Foobar', 'whatever');
345-
346346
$result = $this->store->lookup($request);
347-
348347
$this->assertSame(200, $result->getStatusCode());
349348
$this->assertSame('hello world', $result->getContent());
350349
$this->assertSame('Foobar', $result->headers->get('Vary'));
351-
$this->assertSame('whatever', $result->headers->get('Foobar'));
352350
}
353351

354352
public function testLookupWithMultipleVaryResponse()
355353
{
356-
$request = Request::create('https://foobar.com/');
357-
$response1 = new Response('should be whatever 1', 200, ['Vary' => 'Foobar', 'Foobar' => 'whatever1']);
358-
$response2 = new Response('should be whatever 2', 200, ['Vary' => 'Foobar', 'Foobar' => 'whatever2']);
354+
$jsonRequest = Request::create('https://foobar.com/');
355+
$jsonRequest->headers->set('Accept', 'application/json');
356+
$htmlRequest = Request::create('https://foobar.com/');
357+
$htmlRequest->headers->set('Accept', 'text/html');
358+
359+
$jsonResponse = new Response('{}', 200, ['Vary' => 'Accept', 'Content-Type' => 'application/json']);
360+
$htmlResponse = new Response('<html></html>', 200, ['Vary' => 'Accept', 'Content-Type' => 'text/html']);
359361

360362
// Fill cache
361-
$this->store->write($request, $response1);
362-
$this->store->write($request, $response2);
363+
$this->store->write($jsonRequest, $jsonResponse);
364+
$this->store->write($htmlRequest, $htmlResponse);
363365

364366
// Should return null because no header provided
365367
$request = Request::create('https://foobar.com/');
@@ -368,27 +370,27 @@ public function testLookupWithMultipleVaryResponse()
368370

369371
// Should return null because header provided but non matching content
370372
$request = Request::create('https://foobar.com/');
371-
$request->headers->set('Foobar', 'whatever3');
373+
$request->headers->set('Accept', 'application/xml');
372374
$result = $this->store->lookup($request);
373375
$this->assertNull($result);
374376

375-
// Should return $response1
377+
// Should return a JSON response
376378
$request = Request::create('https://foobar.com/');
377-
$request->headers->set('Foobar', 'whatever1');
379+
$request->headers->set('Accept', 'application/json');
378380
$result = $this->store->lookup($request);
379381
$this->assertSame(200, $result->getStatusCode());
380-
$this->assertSame('should be whatever 1', $result->getContent());
381-
$this->assertSame('Foobar', $result->headers->get('Vary'));
382-
$this->assertSame('whatever1', $result->headers->get('Foobar'));
382+
$this->assertSame('{}', $result->getContent());
383+
$this->assertSame('Accept', $result->headers->get('Vary'));
384+
$this->assertSame('application/json', $result->headers->get('Content-Type'));
383385

384-
// Should return $response2
386+
// Should return an HTML response
385387
$request = Request::create('https://foobar.com/');
386-
$request->headers->set('Foobar', 'whatever2');
388+
$request->headers->set('Accept', 'text/html');
387389
$result = $this->store->lookup($request);
388390
$this->assertSame(200, $result->getStatusCode());
389-
$this->assertSame('should be whatever 2', $result->getContent());
390-
$this->assertSame('Foobar', $result->headers->get('Vary'));
391-
$this->assertSame('whatever2', $result->headers->get('Foobar'));
391+
$this->assertSame('<html></html>', $result->getContent());
392+
$this->assertSame('Accept', $result->headers->get('Vary'));
393+
$this->assertSame('text/html', $result->headers->get('Content-Type'));
392394
}
393395

394396
public function testInvalidate()

0 commit comments

Comments
 (0)