1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache\Middlewares; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use Spatie\ResponseCache\ResponseCache; |
9
|
|
|
use Spatie\ResponseCache\Events\CacheMissed; |
10
|
|
|
use Spatie\ResponseCache\Replacers\Replacer; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Spatie\ResponseCache\Events\ResponseCacheHit; |
13
|
|
|
|
14
|
|
|
class CacheResponse |
15
|
|
|
{ |
16
|
|
|
/** @var \Spatie\ResponseCache\ResponseCache */ |
17
|
|
|
protected $responseCache; |
18
|
|
|
|
19
|
|
|
public function __construct(ResponseCache $responseCache) |
20
|
|
|
{ |
21
|
|
|
$this->responseCache = $responseCache; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function handle(Request $request, Closure $next, ...$args): Response |
25
|
|
|
{ |
26
|
|
|
$lifetimeInSeconds = $this->getLifetime($args); |
27
|
|
|
$tags = $this->getTags($args); |
28
|
|
|
|
29
|
|
|
if ($this->responseCache->enabled($request)) { |
30
|
|
|
if ($this->responseCache->hasBeenCached($request, $tags)) { |
31
|
|
|
event(new ResponseCacheHit($request)); |
32
|
|
|
|
33
|
|
|
$response = $this->responseCache->getCachedResponseFor($request, $tags); |
34
|
|
|
|
35
|
|
|
$this->getReplacers()->each(function (Replacer $replacer) use ($response) { |
36
|
|
|
$replacer->replaceInCachedResponse($response); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
return $response; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$response = $next($request); |
44
|
|
|
|
45
|
|
|
if ($this->responseCache->enabled($request)) { |
46
|
|
|
if ($this->responseCache->shouldCache($request, $response)) { |
47
|
|
|
$this->makeReplacementsAndCacheResponse($request, $response, $lifetimeInSeconds, $tags); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
event(new CacheMissed($request)); |
52
|
|
|
|
53
|
|
|
return $response; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function makeReplacementsAndCacheResponse( |
57
|
|
|
Request $request, |
58
|
|
|
Response $response, |
59
|
|
|
?int $lifetimeInSeconds = null, |
60
|
|
|
array $tags = [] |
61
|
|
|
): void { |
62
|
|
|
$cachedResponse = clone $response; |
63
|
|
|
|
64
|
|
|
$this->getReplacers()->each(function (Replacer $replacer) use ($cachedResponse) { |
65
|
|
|
$replacer->prepareResponseToCache($cachedResponse); |
66
|
|
|
}); |
67
|
|
|
|
68
|
|
|
$this->responseCache->cacheResponse($request, $cachedResponse, $lifetimeInSeconds, $tags); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getReplacers(): Collection |
72
|
|
|
{ |
73
|
|
|
return collect(config('responsecache.replacers')) |
74
|
|
|
->map(function (string $replacerClass) { |
75
|
|
|
return app($replacerClass); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getLifetime(array $args): ?int |
80
|
|
|
{ |
81
|
|
|
if (count($args) >= 1 && is_numeric($args[0])) { |
82
|
|
|
return (int) $args[0]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function getTags(array $args): array |
89
|
|
|
{ |
90
|
|
|
$tags = $args; |
91
|
|
|
|
92
|
|
|
if (count($args) >= 1 && is_numeric($args[0])) { |
93
|
|
|
$tags = array_slice($args, 1); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return array_filter($tags); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|