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