1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Roave\DoctrineSimpleCache; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Cache\Cache as DoctrineCache; |
7
|
|
|
use Doctrine\Common\Cache\ClearableCache; |
8
|
|
|
use Doctrine\Common\Cache\MultiGetCache; |
9
|
|
|
use Doctrine\Common\Cache\MultiPutCache; |
10
|
|
|
use Psr\SimpleCache\CacheInterface as PsrCache; |
11
|
|
|
|
12
|
|
|
final class SimpleCacheAdapter implements PsrCache |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DoctrineCache|ClearableCache|MultiGetCache|MultiPutCache |
16
|
|
|
*/ |
17
|
|
|
private $doctrineCache; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param DoctrineCache $doctrineCache |
21
|
|
|
* @throws \Roave\DoctrineSimpleCache\CacheException |
22
|
|
|
*/ |
23
|
36 |
|
public function __construct(DoctrineCache $doctrineCache) |
24
|
|
|
{ |
25
|
36 |
|
$this->doctrineCache = $doctrineCache; |
26
|
|
|
|
27
|
36 |
|
if (!$this->doctrineCache instanceof ClearableCache) { |
28
|
1 |
|
throw CacheException::fromNonClearableCache($this->doctrineCache); |
29
|
|
|
} |
30
|
35 |
|
if (!$this->doctrineCache instanceof MultiGetCache) { |
31
|
1 |
|
throw CacheException::fromNonMultiGetCache($this->doctrineCache); |
32
|
|
|
} |
33
|
34 |
|
if (!$this->doctrineCache instanceof MultiPutCache) { |
34
|
1 |
|
throw CacheException::fromNonMultiPutCache($this->doctrineCache); |
35
|
|
|
} |
36
|
33 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param mixed $key |
40
|
|
|
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException |
41
|
|
|
*/ |
42
|
30 |
|
private function validateKey($key) : void |
43
|
|
|
{ |
44
|
30 |
|
if (!is_string($key)) { |
45
|
7 |
|
throw Exception\InvalidArgumentException::fromInvalidType($key); |
46
|
|
|
} |
47
|
|
|
|
48
|
23 |
|
if ('' === $key) { |
49
|
1 |
|
throw Exception\InvalidArgumentException::fromEmptyKey(); |
50
|
|
|
} |
51
|
|
|
|
52
|
22 |
|
if (preg_match('/[' . preg_quote('{}()/\@:', '/') . ']/', $key)) { |
53
|
10 |
|
throw Exception\InvalidArgumentException::fromInvalidKeyCharacters($key); |
54
|
|
|
} |
55
|
12 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $keys |
59
|
|
|
* @return array |
60
|
|
|
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException |
61
|
|
|
*/ |
62
|
25 |
|
private function filterValidateMultipleKeys($keys) : array |
63
|
|
|
{ |
64
|
25 |
|
if ($keys instanceof \Traversable) { |
65
|
2 |
|
$keys = iterator_to_array($keys); |
66
|
|
|
} |
67
|
|
|
|
68
|
25 |
|
if (!is_array($keys)) { |
69
|
1 |
|
throw Exception\InvalidArgumentException::fromNonIterableKeys($keys); |
70
|
|
|
} |
71
|
|
|
|
72
|
24 |
|
array_map([$this, 'validateKey'], $keys); |
73
|
|
|
|
74
|
6 |
|
return $keys; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
2 |
|
public function get($key, $default = null) |
81
|
|
|
{ |
82
|
2 |
|
$this->validateKey($key); |
83
|
|
|
|
84
|
2 |
|
$value = $this->doctrineCache->fetch($key); |
|
|
|
|
85
|
2 |
|
if ($value === false) { |
86
|
1 |
|
return $default; |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return $value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
2 |
|
public function set($key, $value, $ttl = null) : bool |
96
|
|
|
{ |
97
|
2 |
|
$this->validateKey($key); |
98
|
2 |
|
return $this->doctrineCache->save($key, $value, $ttl); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritDoc} |
103
|
|
|
*/ |
104
|
3 |
|
public function delete($key) : bool |
105
|
|
|
{ |
106
|
3 |
|
$this->validateKey($key); |
107
|
3 |
|
return $this->doctrineCache->delete($key); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
1 |
|
public function clear() : bool |
114
|
|
|
{ |
115
|
1 |
|
return $this->doctrineCache->deleteAll(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array|\Traversable $keys |
120
|
|
|
* @param mixed $default |
121
|
|
|
* @return array |
122
|
|
|
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException |
123
|
|
|
*/ |
124
|
23 |
|
public function getMultiple($keys, $default = null) |
125
|
|
|
{ |
126
|
23 |
|
$keys = $this->filterValidateMultipleKeys($keys); |
127
|
4 |
|
return array_merge(array_fill_keys($keys, $default), $this->doctrineCache->fetchMultiple($keys)); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param array|\Traversable $values |
132
|
|
|
* @param null|int|\DateInterval $ttl |
133
|
|
|
* @return bool |
134
|
|
|
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException |
135
|
|
|
*/ |
136
|
3 |
|
public function setMultiple($values, $ttl = null) : bool |
137
|
|
|
{ |
138
|
3 |
|
if (!$values instanceof \Traversable && !is_array($values)) { |
139
|
1 |
|
throw Exception\InvalidArgumentException::fromNonIterableKeys($values); |
140
|
|
|
} |
141
|
|
|
|
142
|
2 |
|
foreach ($values as $k => $v) { |
143
|
2 |
|
$this->validateKey($k); |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
return $this->doctrineCache->saveMultiple($values, $ttl); |
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param array|\Traversable $keys |
151
|
|
|
* @return bool |
152
|
|
|
* @throws \Roave\DoctrineSimpleCache\Exception\InvalidArgumentException |
153
|
|
|
*/ |
154
|
2 |
|
public function deleteMultiple($keys) : bool |
155
|
|
|
{ |
156
|
2 |
|
$keys = $this->filterValidateMultipleKeys($keys); |
157
|
|
|
|
158
|
2 |
|
$success = true; |
159
|
2 |
|
foreach ($keys as $key) { |
160
|
2 |
|
if (!$this->delete($key)) { |
161
|
2 |
|
$success = false; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
2 |
|
return $success; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritDoc} |
170
|
|
|
*/ |
171
|
1 |
|
public function has($key) : bool |
172
|
|
|
{ |
173
|
1 |
|
$this->validateKey($key); |
174
|
1 |
|
return $this->doctrineCache->contains($key); |
|
|
|
|
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: