1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of phpFastCache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Bundle\Response; |
17
|
|
|
|
18
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
19
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
|
23
|
|
|
class CacheableResponse |
24
|
|
|
{ |
25
|
|
|
const RESPONSE_PREFIX = '__CACH_RESP__'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ExtendedCacheItemPoolInterface |
29
|
|
|
*/ |
30
|
|
|
protected $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ExtendedCacheItemPoolInterface |
34
|
|
|
*/ |
35
|
|
|
protected $cacheInstance; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* CachePromise constructor. |
39
|
|
|
* @param ExtendedCacheItemPoolInterface $cacheInstance |
40
|
|
|
*/ |
41
|
|
|
public function __construct(ExtendedCacheItemPoolInterface $cacheInstance, Request $request) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->cacheInstance = $cacheInstance; |
44
|
|
|
$this->request = $request; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $cacheKey |
49
|
|
|
* @param int|\DateInterval $expiresAfter |
50
|
|
|
* @param callable $callback |
51
|
|
|
* @return mixed |
52
|
|
|
* @throws PhpfastcacheLogicException |
53
|
|
|
*/ |
54
|
|
|
public function getResponse(string $cacheKey, $expiresAfter = null, callable $callback): Response |
55
|
|
|
{ |
56
|
|
|
$cacheKey = self::RESPONSE_PREFIX . $cacheKey; |
57
|
|
|
$cacheItem = $this->cacheInstance->getItem($cacheKey); |
58
|
|
|
$cacheResponse = $cacheItem->get(); |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* No isHit() test here as we directly |
62
|
|
|
* test if the cached response object |
63
|
|
|
* is effectively a "Response" object |
64
|
|
|
*/ |
65
|
|
|
if (!($cacheResponse instanceof Response)) { |
66
|
|
|
$response = $callback(); |
67
|
|
|
if($response instanceof Response){ |
68
|
|
|
$cacheItem->expiresAfter($expiresAfter); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Alter response header to set |
72
|
|
|
* cache/expiration directives |
73
|
|
|
*/ |
74
|
|
|
$response->setExpires($cacheItem->getExpirationDate()); |
75
|
|
|
$response->setSharedMaxAge($cacheItem->getTtl()); |
76
|
|
|
$response->headers->addCacheControlDirective('must-revalidate', true); |
77
|
|
|
$response->setEtag(md5($response->getContent())); |
78
|
|
|
$response->setPublic(); |
79
|
|
|
|
80
|
|
|
$cacheItem->set($response); |
81
|
|
|
$this->cacheInstance->save($cacheItem); |
82
|
|
|
$cacheResponse = $response; |
83
|
|
|
}else{ |
84
|
|
|
throw new PhpfastcacheLogicException('Your callback response MUST return a valid Symfony HTTP Foundation Response object'); |
85
|
|
|
} |
86
|
|
|
}else{ |
87
|
|
|
$cacheResponse->isNotModified($this->request); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $cacheResponse; |
91
|
|
|
} |
92
|
|
|
} |