1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of phpFastCache. |
5
|
|
|
* |
6
|
|
|
* @license MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
9
|
|
|
* |
10
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpFastCache\Cache; |
16
|
|
|
|
17
|
|
|
use phpFastCache\Core\DriverAbstract; |
18
|
|
|
|
19
|
|
|
trait ItemBaseTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
protected $fetched = false; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var DriverAbstract |
28
|
|
|
*/ |
29
|
|
|
protected $driver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $key; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var mixed |
38
|
|
|
*/ |
39
|
|
|
protected $data; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \DateTime |
43
|
|
|
*/ |
44
|
|
|
protected $expirationDate; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $tags = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
protected $removedTags = []; |
55
|
|
|
|
56
|
|
|
/******************** |
57
|
|
|
* |
58
|
|
|
* PSR-6 Methods |
59
|
|
|
* |
60
|
|
|
*******************/ |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getKey() |
66
|
|
|
{ |
67
|
|
|
return $this->key; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return mixed |
72
|
|
|
* @throws \InvalidArgumentException |
73
|
|
|
*/ |
74
|
|
|
public function get() |
75
|
|
|
{ |
76
|
|
|
return $this->data; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param mixed $value |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function set($value) |
84
|
|
|
{ |
85
|
|
|
/** |
86
|
|
|
* The user set a value, |
87
|
|
|
* therefore there is no need to |
88
|
|
|
* fetch from source anymore |
89
|
|
|
*/ |
90
|
|
|
$this->fetched = true; |
91
|
|
|
$this->data = $value; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return bool |
98
|
|
|
* @throws \InvalidArgumentException |
99
|
|
|
*/ |
100
|
|
|
public function isHit() |
101
|
|
|
{ |
102
|
|
|
return $this->driver->driverIsHit($this); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param \DateTimeInterface $expiration |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function expiresAt($expiration) |
110
|
|
|
{ |
111
|
|
|
if ($expiration instanceof \DateTimeInterface) { |
112
|
|
|
$this->expirationDate = $expiration; |
|
|
|
|
113
|
|
|
} else { |
114
|
|
|
throw new \InvalidArgumentException('$expiration must be an object implementing the DateTimeInterface'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Sets the expiration time for this cache item. |
122
|
|
|
* |
123
|
|
|
* @param int|\DateInterval $time |
124
|
|
|
* The period of time from the present after which the item MUST be considered |
125
|
|
|
* expired. An integer parameter is understood to be the time in seconds until |
126
|
|
|
* expiration. If null is passed explicitly, a default value MAY be used. |
127
|
|
|
* If none is set, the value should be stored permanently or for as long as the |
128
|
|
|
* implementation allows. |
129
|
|
|
* |
130
|
|
|
* @return static |
131
|
|
|
* The called object. |
132
|
|
|
* |
133
|
|
|
* @deprecated Use CacheItemInterface::expiresAfter() instead |
134
|
|
|
*/ |
135
|
|
|
public function touch($time) |
136
|
|
|
{ |
137
|
|
|
trigger_error('touch() is deprecated and will be removed in the next major release, use CacheItemInterface::expiresAfter() instead'); |
138
|
|
|
|
139
|
|
|
return $this->expiresAfter($time); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param \DateInterval|int $time |
144
|
|
|
* @return $this |
145
|
|
|
* @throws \InvalidArgumentException |
146
|
|
|
*/ |
147
|
|
|
public function expiresAfter($time) |
148
|
|
|
{ |
149
|
|
|
if (is_numeric($time)) { |
150
|
|
|
if ($time <= 0) { |
151
|
|
|
/** |
152
|
|
|
* 5 years, however memcached or memory cached will gone when u restart it |
153
|
|
|
* just recommended for sqlite. files |
154
|
|
|
*/ |
155
|
|
|
$time = 30 * 24 * 3600 * 5; |
156
|
|
|
} |
157
|
|
|
$this->expirationDate = $this->expirationDate->add(new \DateInterval(sprintf('PT%dS', $time))); |
158
|
|
|
} else if ($time instanceof \DateInterval) { |
159
|
|
|
$this->expirationDate = $this->expirationDate->add($time); |
160
|
|
|
} else { |
161
|
|
|
throw new \InvalidArgumentException('Invalid date format'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/******************** |
168
|
|
|
* |
169
|
|
|
* PSR-6 Extended Methods |
170
|
|
|
* |
171
|
|
|
*******************/ |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return mixed |
176
|
|
|
*/ |
177
|
|
|
public function getUncommittedData() |
178
|
|
|
{ |
179
|
|
|
return $this->data; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return \DateTimeInterface |
184
|
|
|
*/ |
185
|
|
|
public function getExpirationDate() |
186
|
|
|
{ |
187
|
|
|
return $this->expirationDate; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return int |
192
|
|
|
*/ |
193
|
|
|
public function getTtl() |
194
|
|
|
{ |
195
|
|
|
$ttl = $this->expirationDate->getTimestamp() - time(); |
196
|
|
|
if ($ttl > 2592000) { |
197
|
|
|
$ttl = time() + $ttl; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return $ttl; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function isExpired() |
207
|
|
|
{ |
208
|
|
|
return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param int $step |
213
|
|
|
* @return $this |
214
|
|
|
* @throws \InvalidArgumentException |
215
|
|
|
*/ |
216
|
|
View Code Duplication |
public function increment($step = 1) |
|
|
|
|
217
|
|
|
{ |
218
|
|
|
if (is_int($step)) { |
219
|
|
|
$this->fetched = true; |
220
|
|
|
$this->data += $step; |
221
|
|
|
} else { |
222
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @param int $step |
230
|
|
|
* @return $this |
231
|
|
|
* @throws \InvalidArgumentException |
232
|
|
|
*/ |
233
|
|
View Code Duplication |
public function decrement($step = 1) |
|
|
|
|
234
|
|
|
{ |
235
|
|
|
if (is_int($step)) { |
236
|
|
|
$this->fetched = true; |
237
|
|
|
$this->data -= $step; |
238
|
|
|
} else { |
239
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @param array|string $data |
247
|
|
|
* @return $this |
248
|
|
|
* @throws \InvalidArgumentException |
249
|
|
|
*/ |
250
|
|
View Code Duplication |
public function append($data) |
|
|
|
|
251
|
|
|
{ |
252
|
|
|
if (is_array($this->data)) { |
253
|
|
|
array_push($this->data, $data); |
254
|
|
|
} else if (is_string($data)) { |
255
|
|
|
$this->data .= (string) $data; |
256
|
|
|
} else { |
257
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return $this; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param array|string $data |
266
|
|
|
* @return $this |
267
|
|
|
* @throws \InvalidArgumentException |
268
|
|
|
*/ |
269
|
|
View Code Duplication |
public function prepend($data) |
|
|
|
|
270
|
|
|
{ |
271
|
|
|
if (is_array($this->data)) { |
272
|
|
|
array_unshift($this->data, $data); |
273
|
|
|
} else if (is_string($data)) { |
274
|
|
|
$this->data = (string) $data . $this->data; |
275
|
|
|
} else { |
276
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @param $tagName |
284
|
|
|
* @return $this |
285
|
|
|
* @throws \InvalidArgumentException |
286
|
|
|
*/ |
287
|
|
|
public function addTag($tagName) |
288
|
|
|
{ |
289
|
|
|
if (is_string($tagName)) { |
290
|
|
|
$this->tags = array_unique(array_merge($this->tags, [$tagName])); |
291
|
|
|
|
292
|
|
|
return $this; |
293
|
|
|
} else { |
294
|
|
|
throw new \InvalidArgumentException('$tagName must be a string'); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @param array $tagNames |
300
|
|
|
* @return $this |
301
|
|
|
*/ |
302
|
|
|
public function addTags(array $tagNames) |
303
|
|
|
{ |
304
|
|
|
foreach ($tagNames as $tagName) { |
305
|
|
|
$this->addTag($tagName); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return $this; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @param array $tags |
313
|
|
|
* @return $this |
314
|
|
|
* @throws \InvalidArgumentException |
315
|
|
|
*/ |
316
|
|
|
public function setTags(array $tags) |
317
|
|
|
{ |
318
|
|
|
if (count($tags)) { |
319
|
|
|
if (array_filter($tags, 'is_string')) { |
320
|
|
|
$this->tags = $tags; |
321
|
|
|
} else { |
322
|
|
|
throw new \InvalidArgumentException('$tagName must be an array of string'); |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return $this; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return array |
331
|
|
|
*/ |
332
|
|
|
public function getTags() |
333
|
|
|
{ |
334
|
|
|
return $this->tags; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @return string |
339
|
|
|
*/ |
340
|
|
|
public function getTagsAsString($separator = ', ') |
341
|
|
|
{ |
342
|
|
|
return implode($separator, $this->tags); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* @param $tagName |
347
|
|
|
* @return $this |
348
|
|
|
*/ |
349
|
|
|
public function removeTag($tagName) |
350
|
|
|
{ |
351
|
|
|
if (($key = array_search($tagName, $this->tags)) !== false) { |
352
|
|
|
unset($this->tags[ $key ]); |
353
|
|
|
$this->removedTags[] = $tagName; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
return $this; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @param array $tagNames |
361
|
|
|
* @return $this |
362
|
|
|
*/ |
363
|
|
|
public function removeTags(array $tagNames) |
364
|
|
|
{ |
365
|
|
|
foreach ($tagNames as $tagName) { |
366
|
|
|
$this->removeTag($tagName); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
return $this; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return array |
374
|
|
|
*/ |
375
|
|
|
public function getRemovedTags() |
376
|
|
|
{ |
377
|
|
|
return array_diff($this->removedTags, $this->tags); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @throws \RuntimeException |
382
|
|
|
*/ |
383
|
|
|
/* final public function __sleep() |
|
|
|
|
384
|
|
|
{ |
385
|
|
|
$info = get_object_vars($this); |
386
|
|
|
$info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
387
|
|
|
|
388
|
|
|
return (array) $info; |
389
|
|
|
}*/ |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Prevent recursions for Debug (php 5.6+) |
393
|
|
|
* @return array |
394
|
|
|
*/ |
395
|
|
|
final public function __debugInfo() |
396
|
|
|
{ |
397
|
|
|
$info = get_object_vars($this); |
398
|
|
|
$info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
399
|
|
|
|
400
|
|
|
return (array) $info; |
401
|
|
|
} |
402
|
|
|
} |
403
|
|
|
|
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.