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
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected $isHit = false; |
60
|
|
|
|
61
|
|
|
/******************** |
62
|
|
|
* |
63
|
|
|
* PSR-6 Methods |
64
|
|
|
* |
65
|
|
|
*******************/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getKey() |
71
|
|
|
{ |
72
|
|
|
return $this->key; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public function get() |
79
|
|
|
{ |
80
|
|
|
return $this->data; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param mixed $value |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function set($value) |
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* The user set a value, |
91
|
|
|
* therefore there is no need to |
92
|
|
|
* fetch from source anymore |
93
|
|
|
*/ |
94
|
|
|
$this->fetched = true; |
95
|
|
|
$this->data = $value; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return bool |
102
|
|
|
* @throws \InvalidArgumentException |
103
|
|
|
*/ |
104
|
|
|
public function isHit() |
105
|
|
|
{ |
106
|
|
|
return $this->isHit; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param bool $isHit |
111
|
|
|
* @return $this |
112
|
|
|
* @throws \InvalidArgumentException |
113
|
|
|
*/ |
114
|
|
|
public function setHit($isHit) |
115
|
|
|
{ |
116
|
|
|
if (is_bool($isHit)) { |
117
|
|
|
$this->isHit = $isHit; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} else { |
121
|
|
|
throw new \InvalidArgumentException('$isHit must be a boolean'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param \DateTimeInterface $expiration |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
public function expiresAt($expiration) |
130
|
|
|
{ |
131
|
|
|
if ($expiration instanceof \DateTimeInterface) { |
132
|
|
|
$this->expirationDate = $expiration; |
|
|
|
|
133
|
|
|
} else { |
134
|
|
|
throw new \InvalidArgumentException('$expiration must be an object implementing the DateTimeInterface'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets the expiration time for this cache item. |
142
|
|
|
* |
143
|
|
|
* @param int|\DateInterval $time |
144
|
|
|
* The period of time from the present after which the item MUST be considered |
145
|
|
|
* expired. An integer parameter is understood to be the time in seconds until |
146
|
|
|
* expiration. If null is passed explicitly, a default value MAY be used. |
147
|
|
|
* If none is set, the value should be stored permanently or for as long as the |
148
|
|
|
* implementation allows. |
149
|
|
|
* |
150
|
|
|
* @return static |
151
|
|
|
* The called object. |
152
|
|
|
* |
153
|
|
|
* @deprecated Use CacheItemInterface::expiresAfter() instead |
154
|
|
|
*/ |
155
|
|
|
public function touch($time) |
156
|
|
|
{ |
157
|
|
|
trigger_error('touch() is deprecated and will be removed in the next major release, use CacheItemInterface::expiresAfter() instead'); |
158
|
|
|
|
159
|
|
|
return $this->expiresAfter($time); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param \DateInterval|int $time |
164
|
|
|
* @return $this |
165
|
|
|
* @throws \InvalidArgumentException |
166
|
|
|
*/ |
167
|
|
|
public function expiresAfter($time) |
168
|
|
|
{ |
169
|
|
|
if (is_numeric($time)) { |
170
|
|
|
if ($time <= 0) { |
171
|
|
|
/** |
172
|
|
|
* 5 years, however memcached or memory cached will gone when u restart it |
173
|
|
|
* just recommended for sqlite. files |
174
|
|
|
*/ |
175
|
|
|
$time = 30 * 24 * 3600 * 5; |
176
|
|
|
} |
177
|
|
|
$this->expirationDate = (new \DateTime())->add(new \DateInterval(sprintf('PT%dS', $time))); |
178
|
|
|
} else if ($time instanceof \DateInterval) { |
179
|
|
|
$this->expirationDate = (new \DateTime())->add($time); |
180
|
|
|
} else { |
181
|
|
|
throw new \InvalidArgumentException('Invalid date format'); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/******************** |
188
|
|
|
* |
189
|
|
|
* PSR-6 Extended Methods |
190
|
|
|
* |
191
|
|
|
*******************/ |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getEncodedKey() |
197
|
|
|
{ |
198
|
|
|
return md5($this->getKey()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return mixed |
203
|
|
|
*/ |
204
|
|
|
public function getUncommittedData() |
205
|
|
|
{ |
206
|
|
|
return $this->data; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return \DateTimeInterface |
211
|
|
|
*/ |
212
|
|
|
public function getExpirationDate() |
213
|
|
|
{ |
214
|
|
|
return $this->expirationDate; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return int |
219
|
|
|
*/ |
220
|
|
|
public function getTtl() |
221
|
|
|
{ |
222
|
|
|
$ttl = $this->expirationDate->getTimestamp() - time(); |
223
|
|
|
if ($ttl > 2592000) { |
224
|
|
|
$ttl = time() + $ttl; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $ttl; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @return bool |
232
|
|
|
*/ |
233
|
|
|
public function isExpired() |
234
|
|
|
{ |
235
|
|
|
return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp(); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param int $step |
240
|
|
|
* @return $this |
241
|
|
|
* @throws \InvalidArgumentException |
242
|
|
|
*/ |
243
|
|
View Code Duplication |
public function increment($step = 1) |
244
|
|
|
{ |
245
|
|
|
if (is_int($step)) { |
246
|
|
|
$this->fetched = true; |
247
|
|
|
$this->data += $step; |
248
|
|
|
} else { |
249
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param int $step |
257
|
|
|
* @return $this |
258
|
|
|
* @throws \InvalidArgumentException |
259
|
|
|
*/ |
260
|
|
View Code Duplication |
public function decrement($step = 1) |
261
|
|
|
{ |
262
|
|
|
if (is_int($step)) { |
263
|
|
|
$this->fetched = true; |
264
|
|
|
$this->data -= $step; |
265
|
|
|
} else { |
266
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param array|string $data |
274
|
|
|
* @return $this |
275
|
|
|
* @throws \InvalidArgumentException |
276
|
|
|
*/ |
277
|
|
View Code Duplication |
public function append($data) |
278
|
|
|
{ |
279
|
|
|
if (is_array($this->data)) { |
280
|
|
|
array_push($this->data, $data); |
281
|
|
|
} else if (is_string($data)) { |
282
|
|
|
$this->data .= (string) $data; |
283
|
|
|
} else { |
284
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param array|string $data |
293
|
|
|
* @return $this |
294
|
|
|
* @throws \InvalidArgumentException |
295
|
|
|
*/ |
296
|
|
View Code Duplication |
public function prepend($data) |
297
|
|
|
{ |
298
|
|
|
if (is_array($this->data)) { |
299
|
|
|
array_unshift($this->data, $data); |
300
|
|
|
} else if (is_string($data)) { |
301
|
|
|
$this->data = (string) $data . $this->data; |
302
|
|
|
} else { |
303
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $this; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param $tagName |
311
|
|
|
* @return $this |
312
|
|
|
* @throws \InvalidArgumentException |
313
|
|
|
*/ |
314
|
|
|
public function addTag($tagName) |
315
|
|
|
{ |
316
|
|
|
if (is_string($tagName)) { |
317
|
|
|
$this->tags = array_unique(array_merge($this->tags, [$tagName])); |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} else { |
321
|
|
|
throw new \InvalidArgumentException('$tagName must be a string'); |
322
|
|
|
} |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* @param array $tagNames |
327
|
|
|
* @return $this |
328
|
|
|
*/ |
329
|
|
|
public function addTags(array $tagNames) |
330
|
|
|
{ |
331
|
|
|
foreach ($tagNames as $tagName) { |
332
|
|
|
$this->addTag($tagName); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param array $tags |
340
|
|
|
* @return $this |
341
|
|
|
* @throws \InvalidArgumentException |
342
|
|
|
*/ |
343
|
|
|
public function setTags(array $tags) |
344
|
|
|
{ |
345
|
|
|
if (count($tags)) { |
346
|
|
|
if (array_filter($tags, 'is_string')) { |
347
|
|
|
$this->tags = $tags; |
348
|
|
|
} else { |
349
|
|
|
throw new \InvalidArgumentException('$tagName must be an array of string'); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return array |
358
|
|
|
*/ |
359
|
|
|
public function getTags() |
360
|
|
|
{ |
361
|
|
|
return $this->tags; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return string |
366
|
|
|
*/ |
367
|
|
|
public function getTagsAsString($separator = ', ') |
368
|
|
|
{ |
369
|
|
|
return implode($separator, $this->tags); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @param $tagName |
374
|
|
|
* @return $this |
375
|
|
|
*/ |
376
|
|
|
public function removeTag($tagName) |
377
|
|
|
{ |
378
|
|
|
if (($key = array_search($tagName, $this->tags)) !== false) { |
379
|
|
|
unset($this->tags[ $key ]); |
380
|
|
|
$this->removedTags[] = $tagName; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @param array $tagNames |
388
|
|
|
* @return $this |
389
|
|
|
*/ |
390
|
|
|
public function removeTags(array $tagNames) |
391
|
|
|
{ |
392
|
|
|
foreach ($tagNames as $tagName) { |
393
|
|
|
$this->removeTag($tagName); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
return $this; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return array |
401
|
|
|
*/ |
402
|
|
|
public function getRemovedTags() |
403
|
|
|
{ |
404
|
|
|
return array_diff($this->removedTags, $this->tags); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Return the data as a well-formatted string. |
409
|
|
|
* Any scalar value will be casted to an array |
410
|
|
|
* @param int $option json_encode() options |
411
|
|
|
* @param int $depth json_encode() depth |
412
|
|
|
* @return string |
413
|
|
|
*/ |
414
|
|
|
public function getDataAsJsonString($option = 0, $depth = 512) |
415
|
|
|
{ |
416
|
|
|
$data = $this->get(); |
417
|
|
|
|
418
|
|
|
if (is_object($data) || is_array($data)) { |
419
|
|
|
$data = json_encode($data, $option, $depth); |
420
|
|
|
} else { |
421
|
|
|
$data = json_encode([$data], $option, $depth); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
return json_encode($data, $option, $depth); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Implements \JsonSerializable interface |
429
|
|
|
* @return mixed |
430
|
|
|
*/ |
431
|
|
|
public function jsonSerialize() |
432
|
|
|
{ |
433
|
|
|
return $this->get(); |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Prevent recursions for Debug (php 5.6+) |
438
|
|
|
* @return array |
439
|
|
|
*/ |
440
|
|
|
final public function __debugInfo() |
441
|
|
|
{ |
442
|
|
|
$info = get_object_vars($this); |
443
|
|
|
$info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
444
|
|
|
|
445
|
|
|
return (array) $info; |
446
|
|
|
} |
447
|
|
|
} |
448
|
|
|
|
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.