1 | <?php |
||
15 | class Cache |
||
16 | { |
||
17 | /** |
||
18 | * @var CacheItemPoolInterface The cache implementation used |
||
19 | */ |
||
20 | private $cache; |
||
21 | |||
22 | /** |
||
23 | * @var CacheUtil |
||
24 | */ |
||
25 | private $cacheUtil; |
||
26 | |||
27 | /** |
||
28 | * @var CacheControl |
||
29 | */ |
||
30 | private $cacheControl; |
||
31 | |||
32 | /** |
||
33 | * Set the psr-6 cache pool. |
||
34 | * |
||
35 | * @param CacheItemPoolInterface $cache |
||
36 | */ |
||
37 | public function __construct(CacheItemPoolInterface $cache) |
||
38 | { |
||
39 | $this->cache = $cache; |
||
40 | $this->cacheUtil = new CacheUtil(); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Set a cache-control header to all responses. |
||
45 | * |
||
46 | * @param string|CacheControl $cacheControl |
||
47 | * |
||
48 | * @return self |
||
49 | */ |
||
50 | public function cacheControl($cacheControl) |
||
51 | { |
||
52 | if (!($cacheControl instanceof CacheControl)) { |
||
53 | $cacheControl = RequestCacheControl::fromString($cacheControl); |
||
54 | } |
||
55 | |||
56 | $this->cacheControl = $cacheControl; |
||
57 | |||
58 | return $this; |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * Execute the middleware. |
||
63 | * |
||
64 | * @param RequestInterface $request |
||
65 | * @param ResponseInterface $response |
||
66 | * @param callable $next |
||
67 | * |
||
68 | * @return ResponseInterface |
||
69 | */ |
||
70 | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
||
71 | { |
||
72 | $key = $this->getCacheKey($request); |
||
73 | $item = $this->cache->getItem($key); |
||
74 | |||
75 | //If it's cached |
||
76 | if ($item->isHit()) { |
||
77 | $headers = $item->get(); |
||
78 | $cachedResponse = $response->withStatus(304); |
||
79 | |||
80 | foreach ($headers as $name => $header) { |
||
81 | $cachedResponse = $cachedResponse->withHeader($name, $header); |
||
82 | } |
||
83 | |||
84 | if ($this->cacheUtil->isNotModified($request, $cachedResponse)) { |
||
85 | return $cachedResponse; |
||
86 | } |
||
87 | |||
88 | $this->cache->deleteItem($key); |
||
89 | } |
||
90 | |||
91 | $response = $next($request, $response); |
||
92 | |||
93 | //Add cache-control header |
||
94 | if ($this->cacheControl && !$response->hasHeader('Cache-Control')) { |
||
95 | $response = $this->cacheUtil->withCacheControl($response, $this->cacheControl); |
||
96 | } |
||
97 | |||
98 | //Add Last-Modified header |
||
99 | if (!$response->hasHeader('Last-Modified')) { |
||
100 | $response = $this->cacheUtil->withLastModified($response, time()); |
||
101 | } |
||
102 | |||
103 | //Save in the cache |
||
104 | if ($this->cacheUtil->isCacheable($response)) { |
||
105 | $item->set($response->getHeaders()); |
||
106 | $item->expiresAfter($this->cacheUtil->getLifetime($response)); |
||
107 | |||
108 | $this->cache->save($item); |
||
109 | } |
||
110 | |||
111 | return $response; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Returns the id used to cache a request. |
||
116 | * |
||
117 | * @param RequestInterface $request |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | private function getCacheKey(RequestInterface $request) |
||
125 | } |
||
126 |