1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
|
8
|
|
|
final class SessionCacheConfiguration implements CacheConfigurationInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var CacheConfigurationInterface |
12
|
|
|
*/ |
13
|
|
|
private $cacheConfiguration; |
14
|
|
|
|
15
|
4 |
|
public function __construct(CacheConfigurationInterface $cacheConfiguration) |
16
|
|
|
{ |
17
|
4 |
|
$this->cacheConfiguration = $cacheConfiguration; |
18
|
4 |
|
} |
19
|
|
|
|
20
|
2 |
|
public function requestIsCacheable(ServerRequestInterface $request): bool |
21
|
|
|
{ |
22
|
|
|
if ( |
23
|
2 |
|
$request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME) !== null && |
24
|
2 |
|
$request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME)->isActive() === true |
25
|
|
|
) { |
26
|
1 |
|
return false; |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
return $this->cacheConfiguration->requestIsCacheable($request); |
30
|
|
|
} |
31
|
|
|
|
32
|
2 |
|
public function responseIsCacheable(ServerRequestInterface $request, ResponseInterface $response): bool |
33
|
|
|
{ |
34
|
|
|
if ( |
35
|
2 |
|
$request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME) !== null && |
36
|
2 |
|
$request->getAttribute(SessionMiddleware::ATTRIBUTE_NAME)->isActive() === true |
37
|
|
|
) { |
38
|
1 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $this->cacheConfiguration->responseIsCacheable($request, $response); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function cacheKey(ServerRequestInterface $request): string |
45
|
|
|
{ |
46
|
|
|
return $this->cacheConfiguration->cacheKey($request); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function cacheTtl(ServerRequestInterface $request, ResponseInterface $response): ?int |
50
|
|
|
{ |
51
|
|
|
return $this->cacheConfiguration->cacheTtl($request, $response); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function cacheEncode(ResponseInterface $response): string |
55
|
|
|
{ |
56
|
|
|
return $this->cacheConfiguration->cacheEncode($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function cacheDecode(string $response): ResponseInterface |
60
|
|
|
{ |
61
|
|
|
return $this->cacheConfiguration->cacheDecode($response); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|