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