1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cache; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Psr\Http\Message\RequestInterface; |
|
|
|
|
16
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
|
|
|
17
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
|
|
|
18
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Web Page Caching |
22
|
|
|
*/ |
23
|
|
|
final class ResponseCache |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Cache time to live. |
27
|
|
|
* |
28
|
|
|
* @var int seconds |
29
|
|
|
*/ |
30
|
|
|
private int $ttl = 0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param bool|string[] $cacheQueryString Whether to take the URL query string into consideration when generating output cache files. |
34
|
|
|
* Valid options are: |
35
|
|
|
* false = Disabled |
36
|
|
|
* true = Enabled, take all query parameters into account. |
37
|
|
|
* Please be aware that this may result in numerous cache |
38
|
|
|
* files generated for the same page over and over again. |
39
|
|
|
* array('q') = Enabled, but only take into account the specified list of query parameters. |
40
|
|
|
*/ |
41
|
|
|
public function __construct(private CacheInterface $cache, private bool|array $cacheQueryString = false) |
42
|
|
|
{ |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function setTtl(int $ttl): self |
46
|
|
|
{ |
47
|
|
|
$this->ttl = $ttl; |
48
|
|
|
|
49
|
|
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Generates the cache key to use from the current request. |
54
|
|
|
* |
55
|
|
|
* @internal for testing purposes only |
56
|
|
|
*/ |
57
|
|
|
public function generateCacheKey(RequestInterface $request): string |
58
|
|
|
{ |
59
|
|
|
$uri = clone $request->getUri(); |
60
|
|
|
|
61
|
|
|
// @todo implementation de la recuperation des queriestring |
62
|
|
|
/* $query = $this->cacheQueryString |
63
|
|
|
? $uri->getQuery(is_array($this->cacheQueryString) ? ['only' => $this->cacheQueryString] : []) |
64
|
|
|
: ''; */ |
65
|
|
|
|
66
|
|
|
$query = ''; |
67
|
|
|
|
68
|
|
|
return md5($uri->withFragment('')->withQuery($query)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Caches the response. |
73
|
|
|
*/ |
74
|
|
|
public function make(ServerRequestInterface $request, ResponseInterface $response): bool |
75
|
|
|
{ |
76
|
|
|
if ($this->ttl === 0) { |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$headers = []; |
81
|
|
|
|
82
|
|
|
foreach (array_keys($response->getHeaders()) as $header) { |
83
|
|
|
$headers[$header] = $response->getHeaderLine($header); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->cache->set( |
87
|
|
|
$this->generateCacheKey($request), |
88
|
|
|
serialize(['headers' => $headers, 'output' => $response->getBody()->getContents()]), |
89
|
|
|
$this->ttl |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the cached response for the request. |
95
|
|
|
*/ |
96
|
|
|
public function get(ServerRequestInterface $request, ResponseInterface $response): ?ResponseInterface |
97
|
|
|
{ |
98
|
|
|
if ($cachedResponse = $this->cache->get($this->generateCacheKey($request))) { |
99
|
|
|
$cachedResponse = unserialize($cachedResponse); |
100
|
|
|
|
101
|
|
|
if (! is_array($cachedResponse) || ! isset($cachedResponse['output']) || ! isset($cachedResponse['headers'])) { |
102
|
|
|
throw new Exception('Erreur lors de la désérialisation du cache de page'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$headers = $cachedResponse['headers']; |
106
|
|
|
$output = $cachedResponse['output']; |
107
|
|
|
|
108
|
|
|
// Effacer tous les en-têtes par défaut |
109
|
|
|
foreach (array_keys($response->getHeaders()) as $key) { |
110
|
|
|
$response = $response->withoutHeader($key); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// Définir les en-têtes mis en cache |
114
|
|
|
foreach ($headers as $name => $value) { |
115
|
|
|
$response = $response->withHeader($name, $value); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $response->withBody(to_stream($output)); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths