1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\QueryRepository\Exception\ResourceStorageUnserializeException; |
8
|
|
|
use BEAR\RepositoryModule\Annotation\EtagPool; |
9
|
|
|
use BEAR\Resource\LoggerInterface; |
10
|
|
|
use Error; |
11
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
12
|
|
|
use Ray\Di\Di\Inject; |
13
|
|
|
use Ray\Di\Exception\Unbound; |
14
|
|
|
use Ray\Di\InjectorInterface; |
15
|
|
|
use Ray\PsrCacheModule\Annotation\Shared; |
16
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
17
|
|
|
use Symfony\Component\Cache\Adapter\TagAwareAdapter; |
18
|
|
|
|
19
|
|
|
use function assert; |
20
|
|
|
|
21
|
|
|
trait ResourceStorageCacheableTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ?InjectorInterface |
25
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
26
|
|
|
*/ |
27
|
|
|
protected $injector; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @Inject |
31
|
|
|
*/ |
32
|
|
|
#[Inject] |
33
|
|
|
final public function setInjector(InjectorInterface $injector): void |
34
|
|
|
{ |
35
|
|
|
$this->injector = $injector; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} |
40
|
|
|
*/ |
41
|
|
|
final public function __serialize(): array |
42
|
|
|
{ |
43
|
|
|
assert($this->injector instanceof InjectorInterface); |
44
|
|
|
|
45
|
|
|
return [ |
46
|
|
|
'logger' => $this->logger, |
47
|
|
|
'purger' => $this->purger, |
48
|
|
|
'uriTag' => $this->uriTag, |
49
|
|
|
'saver' => $this->saver, |
50
|
|
|
'knownTagTtl' => $this->knownTagTtl, |
51
|
|
|
'injector' => $this->injector, |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data |
57
|
|
|
*/ |
58
|
|
|
final public function __unserialize(array $data): void |
59
|
|
|
{ |
60
|
|
|
try { |
61
|
|
|
$this->unserialize($data); |
62
|
|
|
} catch (Error $e) { |
63
|
|
|
throw new ResourceStorageUnserializeException($e->getMessage()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array{logger: RepositoryLoggerInterface, purger: PurgerInterface, uriTag: UriTagInterface, saver: ResourceStorageSaver, knownTagTtl: float, injector: InjectorInterface} $data |
69
|
|
|
*/ |
70
|
|
|
private function unserialize(array $data): void |
71
|
|
|
{ |
72
|
|
|
$this->logger = $data['logger']; |
|
|
|
|
73
|
|
|
$this->purger = $data['purger']; |
|
|
|
|
74
|
|
|
$this->uriTag = $data['uriTag']; |
|
|
|
|
75
|
|
|
$this->saver = $data['saver']; |
|
|
|
|
76
|
|
|
$pool = $data['injector']->getInstance(CacheItemPoolInterface::class, Shared::class); |
77
|
|
|
try { |
78
|
|
|
$maybeEtagPool = $data['injector']->getInstance(CacheItemPoolInterface::class, EtagPool::class); |
79
|
|
|
} catch (Unbound $e) { |
80
|
|
|
$maybeEtagPool = null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
assert($pool instanceof AdapterInterface); |
84
|
|
|
/** @psalm-suppress all */ |
85
|
|
|
$etagPool = $maybeEtagPool instanceof AdapterInterface ? $maybeEtagPool : $pool; |
86
|
|
|
$this->roPool = new TagAwareAdapter($pool, $etagPool, $data['knownTagTtl']); |
|
|
|
|
87
|
|
|
$this->etagPool = new TagAwareAdapter($etagPool, $etagPool, $data['knownTagTtl']); |
|
|
|
|
88
|
|
|
$this->knownTagTtl = $data['knownTagTtl']; |
|
|
|
|
89
|
|
|
$this->injector = $data['injector']; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|