1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\QueryRepository; |
6
|
|
|
|
7
|
|
|
use BEAR\QueryRepository\Exception\ExpireAtKeyNotExists; |
8
|
|
|
use BEAR\RepositoryModule\Annotation\Cacheable; |
9
|
|
|
use BEAR\RepositoryModule\Annotation\HttpCache; |
|
|
|
|
10
|
|
|
use BEAR\Resource\AbstractUri; |
11
|
|
|
use BEAR\Resource\ResourceObject; |
12
|
|
|
use Doctrine\Common\Annotations\Reader; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
|
15
|
|
|
use function is_array; |
16
|
|
|
use function sprintf; |
17
|
|
|
use function strtotime; |
18
|
|
|
use function time; |
19
|
|
|
|
20
|
|
|
final class QueryRepository implements QueryRepositoryInterface |
21
|
|
|
{ |
22
|
|
|
public function __construct( |
23
|
|
|
private readonly RepositoryLoggerInterface $logger, |
24
|
|
|
private readonly HeaderSetter $headerSetter, |
25
|
|
|
private readonly ResourceStorageInterface $storage, |
26
|
|
|
private readonly Reader $reader, |
27
|
|
|
private readonly Expiry $expiry, |
|
|
|
|
28
|
|
|
) { |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
public function put(ResourceObject $ro) |
35
|
|
|
{ |
36
|
|
|
$this->logger->log('put-query-repository uri:%s', $ro->uri); |
37
|
29 |
|
$this->storage->deleteEtag($ro->uri); |
38
|
|
|
$ro->toString(); |
39
|
|
|
$cacheable = $this->getCacheableAnnotation($ro); |
40
|
|
|
$httpCache = $this->getHttpCacheAnnotation($ro); |
41
|
|
|
$ttl = $this->getExpiryTime($ro, $cacheable); |
42
|
|
|
($this->headerSetter)($ro, $ttl, $httpCache); |
43
|
29 |
|
if (isset($ro->headers[Header::ETAG])) { |
44
|
29 |
|
$etag = $ro->headers[Header::ETAG]; |
45
|
29 |
|
$surrogateKeys = $ro->headers[Header::SURROGATE_KEY] ?? ''; |
46
|
29 |
|
$this->storage->saveEtag($ro->uri, $etag, $surrogateKeys, $ttl); |
47
|
29 |
|
} |
48
|
|
|
|
49
|
|
|
if ($cacheable instanceof Cacheable && $cacheable->type === 'view') { |
50
|
|
|
return $this->storage->saveView($ro, $ttl); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->storage->saveValue($ro, $ttl); |
54
|
27 |
|
} |
55
|
|
|
|
56
|
27 |
|
/** |
57
|
27 |
|
* {@inheritDoc} |
58
|
27 |
|
*/ |
59
|
27 |
|
public function get(AbstractUri $uri): ResourceState|null |
60
|
27 |
|
{ |
61
|
26 |
|
$state = $this->storage->get($uri); |
62
|
26 |
|
|
63
|
|
|
if (! $state instanceof ResourceState) { |
64
|
26 |
|
return null; |
65
|
26 |
|
} |
66
|
2 |
|
|
67
|
|
|
$state->headers[Header::AGE] = (string) (time() - (int) strtotime($state->headers[Header::LAST_MODIFIED])); |
68
|
|
|
|
69
|
24 |
|
return $state; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
25 |
|
public function purge(AbstractUri $uri) |
76
|
|
|
{ |
77
|
25 |
|
$this->logger->log('purge-query-repository uri:%s', $uri); |
78
|
25 |
|
|
79
|
23 |
|
return $this->storage->deleteEtag($uri); |
80
|
|
|
} |
81
|
15 |
|
|
82
|
15 |
|
private function getHttpCacheAnnotation(ResourceObject $ro): HttpCache|null |
83
|
|
|
{ |
84
|
15 |
|
return $this->reader->getClassAnnotation(new ReflectionClass($ro), HttpCache::class); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getCacheableAnnotation(ResourceObject $ro): Cacheable|null |
88
|
|
|
{ |
89
|
|
|
return $this->reader->getClassAnnotation(new ReflectionClass($ro), Cacheable::class); |
90
|
12 |
|
} |
91
|
|
|
|
92
|
12 |
|
private function getExpiryTime(ResourceObject $ro, Cacheable|null $cacheable = null): int |
93
|
|
|
{ |
94
|
12 |
|
if ($cacheable === null) { |
95
|
|
|
return 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($cacheable->expiryAt !== '') { |
99
|
|
|
return $this->getExpiryAtSec($ro, $cacheable); |
100
|
27 |
|
} |
101
|
|
|
|
102
|
27 |
|
return $cacheable->expirySecond ?: $this->expiry->getTime($cacheable->expiry); |
103
|
27 |
|
} |
104
|
27 |
|
|
105
|
|
|
private function getExpiryAtSec(ResourceObject $ro, Cacheable $cacheable): int |
106
|
|
|
{ |
107
|
|
|
if (! is_array($ro->body) || ! isset($ro->body[$cacheable->expiryAt])) { |
108
|
|
|
$msg = sprintf('%s::%s', $ro::class, $cacheable->expiryAt); |
109
|
|
|
|
110
|
|
|
throw new ExpireAtKeyNotExists($msg); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @var string $expiryAt */ |
114
|
|
|
$expiryAt = $ro->body[$cacheable->expiryAt]; |
115
|
27 |
|
|
116
|
|
|
return (int) strtotime($expiryAt) - time(); |
117
|
27 |
|
} |
118
|
|
|
} |
119
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: