|
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
|
|
|
* Duree de vie du cache. |
|
27
|
|
|
* |
|
28
|
|
|
* @var int seconds |
|
29
|
|
|
*/ |
|
30
|
|
|
private int $ttl = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param bool|list<string> $cacheQueryString S'il faut prendre en compte la chaîne de requête URL lors de la génération des fichiers de cache de sortie. |
|
|
|
|
|
|
34
|
|
|
* Les options valides sont : |
|
35
|
|
|
* false = Désactivé |
|
36
|
|
|
* true = Activé, prend en compte tous les paramètres de requête. |
|
37
|
|
|
* Veuillez noter que cela peut entraîner de nombreux fichiers de cache générés encore et encore pour la même page. |
|
38
|
|
|
* array('q') = Activé, mais ne prend en compte que la liste spécifiée de paramètres de requête. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(private CacheInterface $cache, private array|bool $cacheQueryString = false) |
|
41
|
|
|
{ |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function setTtl(int $ttl): self |
|
45
|
|
|
{ |
|
46
|
|
|
$this->ttl = $ttl; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Génère la clé de cache à utiliser à partir de la requête actuelle. |
|
53
|
|
|
* |
|
54
|
|
|
* @internal à des fins de test uniquement |
|
55
|
|
|
*/ |
|
56
|
|
|
public function generateCacheKey(RequestInterface $request): string |
|
57
|
|
|
{ |
|
58
|
|
|
$uri = $request->getUri(); |
|
59
|
|
|
|
|
60
|
|
|
$query = ''; |
|
61
|
|
|
if ($this->cacheQueryString !== false) { |
|
62
|
|
|
parse_str($uri->getQuery(), $queryParams); |
|
63
|
|
|
|
|
64
|
|
|
if (is_array($this->cacheQueryString)) { |
|
65
|
|
|
// Filtrer seulement les paramètres spécifiés |
|
66
|
|
|
$queryParams = array_intersect_key($queryParams, array_flip($this->cacheQueryString)); |
|
67
|
|
|
} |
|
68
|
|
|
// Trier pour garantir l'ordre et éviter les doublons avec paramètres dans un ordre différent |
|
69
|
|
|
ksort($queryParams); |
|
70
|
|
|
$query = http_build_query($queryParams); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return md5($uri->withFragment('')->withQuery($query)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Met en cache la réponse. |
|
78
|
|
|
*/ |
|
79
|
|
|
public function make(ServerRequestInterface $request, ResponseInterface $response): bool |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->ttl === 0) { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$headers = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach (array_keys($response->getHeaders()) as $header) { |
|
88
|
|
|
$headers[$header] = $response->getHeaderLine($header); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->cache->set( |
|
92
|
|
|
$this->generateCacheKey($request), |
|
93
|
|
|
serialize(['headers' => $headers, 'output' => $response->getBody()->getContents()]), |
|
94
|
|
|
$this->ttl |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Obtient la réponse mise en cache pour la demande. |
|
100
|
|
|
*/ |
|
101
|
|
|
public function get(ServerRequestInterface $request, ResponseInterface $response): ?ResponseInterface |
|
102
|
|
|
{ |
|
103
|
|
|
if ($cachedResponse = $this->cache->get($this->generateCacheKey($request))) { |
|
104
|
|
|
if (!is_string($cachedResponse)) { |
|
105
|
|
|
throw new Exception('Données de cache corrompues'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$cachedResponse = unserialize($cachedResponse, ['allowed_classes' => false]); |
|
109
|
|
|
|
|
110
|
|
|
if (!is_array($cachedResponse) || !isset($cachedResponse['output'], $cachedResponse['headers'])) { |
|
111
|
|
|
throw new Exception('Structure de cache invalide'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Validation des headers |
|
115
|
|
|
if (!is_array($cachedResponse['headers'])) { |
|
116
|
|
|
throw new Exception('Headers de cache invalides'); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$headers = $cachedResponse['headers']; |
|
120
|
|
|
$output = $cachedResponse['output']; |
|
121
|
|
|
|
|
122
|
|
|
// Effacer tous les en-têtes par défaut |
|
123
|
|
|
foreach (array_keys($response->getHeaders()) as $key) { |
|
124
|
|
|
$response = $response->withoutHeader($key); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// Définir les en-têtes mis en cache |
|
128
|
|
|
foreach ($headers as $name => $value) { |
|
129
|
|
|
$response = $response->withHeader($name, $value); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $response->withBody(to_stream($output)); |
|
|
|
|
|
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
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