1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix\Cache\PsrCache; |
14
|
|
|
|
15
|
|
|
use Psr\Cache\CacheItemInterface as ItemInterface; |
16
|
|
|
use Psr\Cache\CacheItemPoolInterface as ItemPoolInterface; |
17
|
|
|
|
18
|
|
|
class Item implements ItemInterface |
19
|
|
|
{ |
20
|
|
|
const DEFAULT_EXPIRATION = 'now +1 year'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The pool that the item belongs to. |
24
|
|
|
* @var ItemPoolInterface |
25
|
|
|
*/ |
26
|
|
|
// protected $pool; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The cache key for the item. |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $key; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The raw (unserialized) cached value. |
36
|
|
|
* @var mixed |
37
|
|
|
*/ |
38
|
|
|
protected $value; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Wether the item has been saved to the cache yet. |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
protected $hit = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The expiration date. |
48
|
|
|
* @var \DateTime |
49
|
|
|
*/ |
50
|
|
|
protected $expiration; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructs a new Item. |
54
|
|
|
* You should never use this directly. It is used internally to create items |
55
|
|
|
* from the pool. |
56
|
|
|
* @param string $key The item key |
57
|
|
|
* @param mixed $value The item value (unserialized) |
58
|
|
|
* @param \DateTime|integer|null $ttl |
59
|
|
|
* @param bool $hit Was this item retrived from cache? |
60
|
|
|
*/ |
61
|
1003 |
|
public function __construct($key, $value = null, $ttl = null, $hit = false) |
62
|
|
|
{ |
63
|
1003 |
|
if (strpbrk($key, '{}()/\@:')) { |
64
|
34 |
|
throw new InvalidArgumentException( |
65
|
|
|
'Item key contains an invalide character.' . $key |
66
|
34 |
|
); |
67
|
|
|
} |
68
|
1003 |
|
$this->key = $key; |
69
|
1003 |
|
$this->value = $value; |
70
|
1003 |
|
$this->hit = $hit; |
71
|
1003 |
|
$this->expiresAt($ttl); |
|
|
|
|
72
|
1003 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
425 |
|
public function getKey() |
78
|
|
|
{ |
79
|
425 |
|
return $this->key; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
459 |
|
public function get() |
86
|
|
|
{ |
87
|
459 |
|
return $this->hit ? $this->value : null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
391 |
|
public function set($value = null) |
94
|
|
|
{ |
95
|
391 |
|
$this->value = $value; |
96
|
391 |
|
$this->hit = false; // TODO: check wether we should we do this? |
97
|
|
|
|
98
|
391 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
459 |
|
public function isHit() |
105
|
|
|
{ |
106
|
459 |
|
return $this->hit; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @deprecated |
111
|
|
|
* @codeCoverageIgnore |
112
|
|
|
*/ |
113
|
|
|
public function exists() |
114
|
|
|
{ |
115
|
|
|
return $this->hit; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @deprecated |
120
|
|
|
* @codeCoverageIgnore |
121
|
|
|
*/ |
122
|
|
|
public function isRegenerating() |
123
|
|
|
{ |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @deprecated |
129
|
|
|
* @see Item::expiresAt() |
130
|
|
|
* @codeCoverageIgnore |
131
|
|
|
*/ |
132
|
|
|
public function setExpiration($ttl = null) |
133
|
|
|
{ |
134
|
|
|
if (is_int($expiration)) { |
|
|
|
|
135
|
|
|
$ttl = new \DateTime('now +' . $ttl . ' seconds'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->expiresAt($ttl); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
272 |
|
public function getExpiration() |
145
|
|
|
{ |
146
|
272 |
|
return $this->expiration; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Returns the time to live in second. |
151
|
|
|
* |
152
|
|
|
* @return integer |
153
|
|
|
*/ |
154
|
459 |
|
public function getTtlInSecond() |
155
|
|
|
{ |
156
|
459 |
|
return $this->expiration->format('U') - time(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Sets the cache hit for this item. |
161
|
|
|
* |
162
|
|
|
* @param boolean $hit |
163
|
|
|
* @return static |
164
|
|
|
* The invoked object. |
165
|
|
|
*/ |
166
|
391 |
|
public function setHit($hit) |
167
|
|
|
{ |
168
|
391 |
|
$this->hit = (bool) $hit; |
169
|
|
|
|
170
|
391 |
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
1003 |
View Code Duplication |
public function expiresAt($expiration = null) |
|
|
|
|
177
|
|
|
{ |
178
|
1003 |
|
if ($expiration instanceof \DateTime) { |
179
|
68 |
|
$this->expiration = $expiration; |
180
|
|
|
|
181
|
1003 |
|
} elseif (is_int($expiration)) { |
182
|
221 |
|
$this->expiration = new \DateTime( |
183
|
221 |
|
'now +' . $expiration . ' seconds' |
184
|
221 |
|
); |
185
|
|
|
|
186
|
1003 |
|
} elseif (null === $expiration) { |
187
|
1003 |
|
$this->expiration = new \DateTime(self::DEFAULT_EXPIRATION); |
188
|
|
|
|
189
|
1003 |
|
} else { |
190
|
|
|
|
191
|
34 |
|
throw new InvalidArgumentException( |
192
|
|
|
'Integer or \DateTime object expected.' |
193
|
34 |
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
1003 |
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* {@inheritdoc} |
201
|
|
|
*/ |
202
|
170 |
View Code Duplication |
public function expiresAfter($time) |
|
|
|
|
203
|
|
|
{ |
204
|
170 |
|
if ($time instanceof \DateInterval) { |
205
|
34 |
|
$this->expiration = new \DateTime(); |
206
|
34 |
|
$this->expiration->add($time); |
207
|
|
|
|
208
|
170 |
|
} elseif (is_int($time)) { |
209
|
68 |
|
$this->expiration = new \DateTime('now +' . $time . ' seconds'); |
210
|
|
|
|
211
|
136 |
|
} elseif (null === $time) { |
212
|
34 |
|
$this->expiration = new \DateTime(self::DEFAULT_EXPIRATION); |
213
|
|
|
|
214
|
34 |
|
} else { |
215
|
|
|
|
216
|
34 |
|
throw new InvalidArgumentException( |
217
|
|
|
'Integer or \DateInterval object expected.' |
218
|
34 |
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
136 |
|
return $this; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
} |
225
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.