|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the BEAR.QueryRepository package. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace BEAR\QueryRepository; |
|
8
|
|
|
|
|
9
|
|
|
use BEAR\RepositoryModule\Annotation\Cacheable; |
|
10
|
|
|
use BEAR\RepositoryModule\Annotation\ExpiryConfig; |
|
11
|
|
|
use BEAR\RepositoryModule\Annotation\Storage; |
|
12
|
|
|
use BEAR\Resource\AbstractUri; |
|
13
|
|
|
use BEAR\Resource\ResourceObject; |
|
14
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
15
|
|
|
use Doctrine\Common\Cache\Cache; |
|
16
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
|
17
|
|
|
|
|
18
|
|
|
class QueryRepository implements QueryRepositoryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
const ETAG_BY_URI = 'etag-by-uri'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CacheProvider |
|
24
|
|
|
*/ |
|
25
|
|
|
private $kvs; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Reader |
|
29
|
|
|
*/ |
|
30
|
|
|
private $reader; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $expiry; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var EtagSetterInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $setEtag; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param EtagSetterInterface $setEtag |
|
44
|
|
|
* @param CacheProvider $kvs |
|
45
|
|
|
* @param Reader $reader |
|
46
|
|
|
* @param string $expiry |
|
47
|
|
|
* |
|
48
|
|
|
* @Storage("kvs") |
|
49
|
|
|
* @ExpiryConfig("expiry") |
|
50
|
|
|
*/ |
|
51
|
15 |
|
public function __construct( |
|
52
|
|
|
EtagSetterInterface $setEtag, |
|
53
|
|
|
Cache $kvs, |
|
54
|
|
|
Reader $reader, |
|
55
|
|
|
$expiry |
|
56
|
|
|
) { |
|
57
|
15 |
|
$this->setEtag = $setEtag; |
|
58
|
15 |
|
$this->reader = $reader; |
|
59
|
15 |
|
$this->kvs = $kvs; |
|
|
|
|
|
|
60
|
15 |
|
$this->expiry = $expiry; |
|
|
|
|
|
|
61
|
15 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
13 |
|
public function put(ResourceObject $ro) |
|
67
|
|
|
{ |
|
68
|
13 |
|
$this->setEtag->__invoke($ro); |
|
69
|
13 |
|
if (isset($ro->headers['ETag'])) { |
|
70
|
13 |
|
$this->updateEtagDatabase($ro); |
|
71
|
|
|
} |
|
72
|
|
|
/* @var $cacheable Cacheable */ |
|
73
|
13 |
|
$cacheable = $this->getCacheable($ro); |
|
74
|
13 |
|
$lifeTime = $this->getExpiryTime($cacheable); |
|
75
|
13 |
|
if ($cacheable instanceof Cacheable && $cacheable->type === 'view') { |
|
|
|
|
|
|
76
|
1 |
|
if (! $ro->view) { |
|
77
|
|
|
// render |
|
78
|
1 |
|
$ro->view = $ro->toString(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
return $this->kvs->save((string) $ro->uri, [$ro->code, $ro->headers, $ro->body, $ro->view], $lifeTime); |
|
82
|
|
|
} |
|
83
|
|
|
// "value" cache type |
|
84
|
12 |
|
return $this->kvs->save((string) $ro->uri, [$ro->code, $ro->headers, $ro->body, null], $lifeTime); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
13 |
|
public function get(AbstractUri $uri) |
|
91
|
|
|
{ |
|
92
|
13 |
|
$data = $this->kvs->fetch((string) $uri); |
|
93
|
13 |
|
if ($data === false) { |
|
94
|
12 |
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
9 |
|
return $data; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
8 |
|
public function purge(AbstractUri $uri) |
|
104
|
|
|
{ |
|
105
|
8 |
|
$this->deleteEtagDatabase($uri); |
|
106
|
|
|
|
|
107
|
8 |
|
return $this->kvs->delete((string) $uri); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Delete etag in etag repository |
|
112
|
|
|
* |
|
113
|
|
|
* @param AbstractUri $uri |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function deleteEtagDatabase(AbstractUri $uri) |
|
116
|
|
|
{ |
|
117
|
8 |
|
$etagId = self::ETAG_BY_URI . (string) $uri; // invalidate etag |
|
118
|
8 |
|
$oldEtagKey = $this->kvs->fetch($etagId); |
|
119
|
|
|
|
|
120
|
8 |
|
$this->kvs->delete($oldEtagKey); |
|
121
|
8 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return Cacheable |
|
125
|
|
|
*/ |
|
126
|
13 |
|
private function getCacheable(ResourceObject $ro) |
|
127
|
|
|
{ |
|
128
|
13 |
|
if (isset($ro->classAnnotations)) { |
|
129
|
11 |
|
$annotations = unserialize($ro->classAnnotations); |
|
|
|
|
|
|
130
|
|
|
|
|
131
|
11 |
|
return $annotations[Cacheable::class]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
return $this->reader->getClassAnnotation(new \ReflectionClass($ro), Cacheable::class); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Update etag in etag repository |
|
139
|
|
|
* |
|
140
|
|
|
* @param ResourceObject $ro |
|
141
|
|
|
*/ |
|
142
|
13 |
|
private function updateEtagDatabase(ResourceObject $ro) |
|
143
|
|
|
{ |
|
144
|
13 |
|
$etag = $ro->headers['ETag']; |
|
145
|
13 |
|
$uri = (string) $ro->uri; |
|
146
|
13 |
|
$etagUri = self::ETAG_BY_URI . $uri; |
|
147
|
13 |
|
$oldEtag = $this->kvs->fetch($etagUri); |
|
148
|
13 |
|
if ($oldEtag) { |
|
149
|
7 |
|
$this->kvs->delete($oldEtag); |
|
150
|
|
|
} |
|
151
|
13 |
|
$etagId = HttpCache::ETAG_KEY . $etag; |
|
152
|
13 |
|
$this->kvs->save($etagId, $uri); // save etag |
|
153
|
13 |
|
$this->kvs->save($etagUri, $etagId); // save uri mapping etag |
|
154
|
13 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param Cacheable $cacheable |
|
158
|
|
|
* |
|
159
|
|
|
* @return int |
|
160
|
|
|
*/ |
|
161
|
14 |
|
private function getExpiryTime(Cacheable $cacheable = null) |
|
162
|
|
|
{ |
|
163
|
14 |
|
if ($cacheable === null) { |
|
164
|
1 |
|
return 0; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
13 |
|
return $cacheable->expirySecond ? $cacheable->expirySecond : $this->expiry[$cacheable->expiry]; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.