|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Ctw\Middleware\PageCacheMiddleware; |
|
5
|
|
|
|
|
6
|
|
|
use Ctw\Middleware\PageCacheMiddleware\IdGenerator\IdGeneratorInterface; |
|
7
|
|
|
use Ctw\Middleware\PageCacheMiddleware\Strategy\StrategyInterface; |
|
8
|
|
|
use Laminas\Cache\Storage\Adapter\AbstractAdapter as StorageAdapter; |
|
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
|
11
|
|
|
|
|
12
|
|
|
abstract class AbstractPageCacheMiddleware implements MiddlewareInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected const STATUS_HIT = 'Hit'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected const STATUS_MISS = 'Miss'; |
|
23
|
|
|
|
|
24
|
|
|
private StorageAdapter $storageAdapter; |
|
25
|
|
|
|
|
26
|
|
|
private IdGeneratorInterface $idGenerator; |
|
27
|
|
|
|
|
28
|
|
|
private StrategyInterface $strategy; |
|
29
|
|
|
|
|
30
|
|
|
private bool $enabled; |
|
31
|
|
|
|
|
32
|
|
|
public function getStorageAdapter(): StorageAdapter |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->storageAdapter; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function setStorageAdapter(StorageAdapter $storageAdapter): self |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->storageAdapter = $storageAdapter; |
|
40
|
|
|
|
|
41
|
2 |
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getIdGenerator(): IdGeneratorInterface |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->idGenerator; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function setIdGenerator(IdGeneratorInterface $idGenerator): self |
|
50
|
|
|
{ |
|
51
|
2 |
|
$this->idGenerator = $idGenerator; |
|
52
|
|
|
|
|
53
|
2 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
public function getStrategy(): StrategyInterface |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->strategy; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
2 |
|
public function setStrategy(StrategyInterface $strategy): self |
|
62
|
|
|
{ |
|
63
|
2 |
|
$this->strategy = $strategy; |
|
64
|
|
|
|
|
65
|
2 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
public function getEnabled(): bool |
|
69
|
|
|
{ |
|
70
|
2 |
|
return $this->enabled; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function setEnabled(bool $enabled): self |
|
74
|
|
|
{ |
|
75
|
2 |
|
$this->enabled = $enabled; |
|
76
|
|
|
|
|
77
|
2 |
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
protected function shouldCache(ServerRequestInterface $request): bool |
|
81
|
|
|
{ |
|
82
|
2 |
|
if (!$this->getEnabled()) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
2 |
|
$strategy = $this->getStrategy(); |
|
87
|
|
|
|
|
88
|
2 |
|
return $strategy->shouldCache($request); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|