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 = $this->expirationDate->add(new \DateInterval(sprintf('PT%dS', $time))); |
178
|
|
|
} else if ($time instanceof \DateInterval) { |
179
|
|
|
$this->expirationDate = $this->expirationDate->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
|
|
|
/** |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function getUncommittedData() |
198
|
|
|
{ |
199
|
|
|
return $this->data; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return \DateTimeInterface |
204
|
|
|
*/ |
205
|
|
|
public function getExpirationDate() |
206
|
|
|
{ |
207
|
|
|
return $this->expirationDate; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return int |
212
|
|
|
*/ |
213
|
|
|
public function getTtl() |
214
|
|
|
{ |
215
|
|
|
$ttl = $this->expirationDate->getTimestamp() - time(); |
216
|
|
|
if ($ttl > 2592000) { |
217
|
|
|
$ttl = time() + $ttl; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $ttl; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
|
|
public function isExpired() |
227
|
|
|
{ |
228
|
|
|
return $this->expirationDate->getTimestamp() < (new \DateTime())->getTimestamp(); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param int $step |
233
|
|
|
* @return $this |
234
|
|
|
* @throws \InvalidArgumentException |
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function increment($step = 1) |
|
|
|
|
237
|
|
|
{ |
238
|
|
|
if (is_int($step)) { |
239
|
|
|
$this->fetched = true; |
240
|
|
|
$this->data += $step; |
241
|
|
|
} else { |
242
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param int $step |
250
|
|
|
* @return $this |
251
|
|
|
* @throws \InvalidArgumentException |
252
|
|
|
*/ |
253
|
|
View Code Duplication |
public function decrement($step = 1) |
|
|
|
|
254
|
|
|
{ |
255
|
|
|
if (is_int($step)) { |
256
|
|
|
$this->fetched = true; |
257
|
|
|
$this->data -= $step; |
258
|
|
|
} else { |
259
|
|
|
throw new \InvalidArgumentException('$step must be numeric.'); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param array|string $data |
267
|
|
|
* @return $this |
268
|
|
|
* @throws \InvalidArgumentException |
269
|
|
|
*/ |
270
|
|
View Code Duplication |
public function append($data) |
|
|
|
|
271
|
|
|
{ |
272
|
|
|
if (is_array($this->data)) { |
273
|
|
|
array_push($this->data, $data); |
274
|
|
|
} else if (is_string($data)) { |
275
|
|
|
$this->data .= (string) $data; |
276
|
|
|
} else { |
277
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $this; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param array|string $data |
286
|
|
|
* @return $this |
287
|
|
|
* @throws \InvalidArgumentException |
288
|
|
|
*/ |
289
|
|
View Code Duplication |
public function prepend($data) |
|
|
|
|
290
|
|
|
{ |
291
|
|
|
if (is_array($this->data)) { |
292
|
|
|
array_unshift($this->data, $data); |
293
|
|
|
} else if (is_string($data)) { |
294
|
|
|
$this->data = (string) $data . $this->data; |
295
|
|
|
} else { |
296
|
|
|
throw new \InvalidArgumentException('$data must be either array nor string.'); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
return $this; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param $tagName |
304
|
|
|
* @return $this |
305
|
|
|
* @throws \InvalidArgumentException |
306
|
|
|
*/ |
307
|
|
|
public function addTag($tagName) |
308
|
|
|
{ |
309
|
|
|
if (is_string($tagName)) { |
310
|
|
|
$this->tags = array_unique(array_merge($this->tags, [$tagName])); |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} else { |
314
|
|
|
throw new \InvalidArgumentException('$tagName must be a string'); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @param array $tagNames |
320
|
|
|
* @return $this |
321
|
|
|
*/ |
322
|
|
|
public function addTags(array $tagNames) |
323
|
|
|
{ |
324
|
|
|
foreach ($tagNames as $tagName) { |
325
|
|
|
$this->addTag($tagName); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
return $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @param array $tags |
333
|
|
|
* @return $this |
334
|
|
|
* @throws \InvalidArgumentException |
335
|
|
|
*/ |
336
|
|
|
public function setTags(array $tags) |
337
|
|
|
{ |
338
|
|
|
if (count($tags)) { |
339
|
|
|
if (array_filter($tags, 'is_string')) { |
340
|
|
|
$this->tags = $tags; |
341
|
|
|
} else { |
342
|
|
|
throw new \InvalidArgumentException('$tagName must be an array of string'); |
343
|
|
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $this; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return array |
351
|
|
|
*/ |
352
|
|
|
public function getTags() |
353
|
|
|
{ |
354
|
|
|
return $this->tags; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* @return string |
359
|
|
|
*/ |
360
|
|
|
public function getTagsAsString($separator = ', ') |
361
|
|
|
{ |
362
|
|
|
return implode($separator, $this->tags); |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* @param $tagName |
367
|
|
|
* @return $this |
368
|
|
|
*/ |
369
|
|
|
public function removeTag($tagName) |
370
|
|
|
{ |
371
|
|
|
if (($key = array_search($tagName, $this->tags)) !== false) { |
372
|
|
|
unset($this->tags[ $key ]); |
373
|
|
|
$this->removedTags[] = $tagName; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
return $this; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @param array $tagNames |
381
|
|
|
* @return $this |
382
|
|
|
*/ |
383
|
|
|
public function removeTags(array $tagNames) |
384
|
|
|
{ |
385
|
|
|
foreach ($tagNames as $tagName) { |
386
|
|
|
$this->removeTag($tagName); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
return $this; |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* @return array |
394
|
|
|
*/ |
395
|
|
|
public function getRemovedTags() |
396
|
|
|
{ |
397
|
|
|
return array_diff($this->removedTags, $this->tags); |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Return the data as a well-formatted string. |
402
|
|
|
* Any scalar value will be casted to an array |
403
|
|
|
* @param int $option json_encode() options |
404
|
|
|
* @param int $depth json_encode() depth |
405
|
|
|
* @return string |
406
|
|
|
*/ |
407
|
|
|
public function getDataAsJsonString($option = 0, $depth = 512) |
408
|
|
|
{ |
409
|
|
|
$data = $this->get(); |
410
|
|
|
|
411
|
|
|
if (is_object($data) || is_array($data)) { |
412
|
|
|
$data = json_encode($data, $option, $depth); |
413
|
|
|
} else { |
414
|
|
|
$data = json_encode([$data], $option, $depth); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return json_encode($data, $option, $depth); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @throws \RuntimeException |
422
|
|
|
*/ |
423
|
|
|
/* final public function __sleep() |
|
|
|
|
424
|
|
|
{ |
425
|
|
|
$info = get_object_vars($this); |
426
|
|
|
$info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
427
|
|
|
|
428
|
|
|
return (array) $info; |
429
|
|
|
}*/ |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Prevent recursions for Debug (php 5.6+) |
433
|
|
|
* @return array |
434
|
|
|
*/ |
435
|
|
|
final public function __debugInfo() |
436
|
|
|
{ |
437
|
|
|
$info = get_object_vars($this); |
438
|
|
|
$info[ 'driver' ] = 'object(' . get_class($info[ 'driver' ]) . ')'; |
439
|
|
|
|
440
|
|
|
return (array) $info; |
441
|
|
|
} |
442
|
|
|
} |
443
|
|
|
|
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.