1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TusPhp\Cache; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
|
7
|
|
|
class FileStore extends AbstractCache |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
protected $cacheDir; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
protected $cacheFile; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* FileStore constructor. |
17
|
|
|
*/ |
18
|
1 |
|
public function __construct() |
19
|
|
|
{ |
20
|
1 |
|
$this->setCacheDir(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . '.cache' . DIRECTORY_SEPARATOR); |
21
|
1 |
|
$this->setCacheFile('tus_php.cache'); |
22
|
1 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Set cache dir. |
26
|
|
|
* |
27
|
|
|
* @param string $path |
28
|
|
|
* |
29
|
|
|
* @return self |
30
|
|
|
*/ |
31
|
1 |
|
public function setCacheDir(string $path) : self |
32
|
|
|
{ |
33
|
1 |
|
$this->cacheDir = $path; |
34
|
|
|
|
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get cache dir. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
1 |
|
public function getCacheDir() : string |
44
|
|
|
{ |
45
|
1 |
|
return $this->cacheDir; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set cache file. |
50
|
|
|
* |
51
|
|
|
* @param string $file |
52
|
|
|
* |
53
|
|
|
* @return self |
54
|
|
|
*/ |
55
|
1 |
|
public function setCacheFile(string $file) : self |
56
|
|
|
{ |
57
|
1 |
|
$this->cacheFile = $file; |
58
|
|
|
|
59
|
1 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get cache file. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
3 |
|
public function getCacheFile() : string |
68
|
|
|
{ |
69
|
3 |
|
return $this->cacheDir . $this->cacheFile; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Create cache dir if not exists. |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
2 |
|
protected function createCacheDir() |
78
|
|
|
{ |
79
|
2 |
|
if ( ! file_exists($this->cacheDir)) { |
80
|
2 |
|
mkdir($this->cacheDir); |
81
|
|
|
} |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Create cache file and add required meta. |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
2 |
|
protected function createCacheFile() |
90
|
|
|
{ |
91
|
2 |
|
$this->createCacheDir(); |
92
|
|
|
|
93
|
2 |
|
$cacheFilePath = $this->getCacheFile(); |
94
|
|
|
|
95
|
2 |
|
if ( ! file_exists($cacheFilePath)) { |
96
|
2 |
|
touch($cacheFilePath); |
97
|
|
|
} |
98
|
2 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
7 |
|
public function get(string $key, bool $withExpired = false) |
104
|
|
|
{ |
105
|
7 |
|
$contents = $this->getCacheContents(); |
106
|
|
|
|
107
|
7 |
|
if (empty($contents[$key])) { |
108
|
2 |
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
if ($withExpired) { |
112
|
1 |
|
return $contents[$key]; |
113
|
|
|
} |
114
|
|
|
|
115
|
5 |
|
return $this->isValid($key) ? $contents[$key] : null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritDoc} |
120
|
|
|
*/ |
121
|
7 |
|
public function set(string $key, $value) |
122
|
|
|
{ |
123
|
7 |
|
$cacheFile = $this->getCacheFile(); |
124
|
|
|
|
125
|
7 |
|
if ( ! file_exists($cacheFile) || ! $this->isValid($key)) { |
126
|
7 |
|
$this->createCacheFile(); |
127
|
|
|
} |
128
|
|
|
|
129
|
7 |
|
$contents = json_decode(file_get_contents($cacheFile), true) ?? []; |
130
|
|
|
|
131
|
7 |
|
if ( ! empty($contents[$key]) && is_array($value)) { |
132
|
3 |
|
$contents[$key] = $value + $contents[$key]; |
133
|
|
|
} else { |
134
|
7 |
|
$contents[$key] = $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
7 |
|
return file_put_contents($cacheFile, json_encode($contents)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritDoc} |
142
|
|
|
*/ |
143
|
1 |
|
public function delete(string $key) |
144
|
|
|
{ |
145
|
1 |
|
$contents = $this->getCacheContents(); |
146
|
|
|
|
147
|
1 |
|
if (isset($contents[$key])) { |
148
|
1 |
|
unset($contents[$key]); |
149
|
|
|
|
150
|
1 |
|
return false !== file_put_contents($this->getCacheFile(), json_encode($contents)); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
return false; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* {@inheritDoc} |
158
|
|
|
*/ |
159
|
2 |
|
public function keys() : array |
160
|
|
|
{ |
161
|
2 |
|
$contents = $this->getCacheContents(); |
162
|
|
|
|
163
|
2 |
|
if (is_array($contents)) { |
164
|
1 |
|
return array_keys($this->getCacheContents()); |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return []; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Check if cache is still valid. |
172
|
|
|
* |
173
|
|
|
* @param string $key |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
3 |
|
public function isValid(string $key) : bool |
178
|
|
|
{ |
179
|
3 |
|
$meta = $this->getCacheContents()[$key] ?? []; |
180
|
|
|
|
181
|
3 |
|
if (empty($meta['expires_at'])) { |
182
|
1 |
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
2 |
|
return Carbon::now() < Carbon::createFromFormat(self::RFC_7231, $meta['expires_at']); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get cache contents. |
190
|
|
|
* |
191
|
|
|
* @return array|bool |
192
|
|
|
*/ |
193
|
3 |
|
public function getCacheContents() |
194
|
|
|
{ |
195
|
3 |
|
$cacheFile = $this->getCacheFile(); |
196
|
|
|
|
197
|
3 |
|
if ( ! file_exists($cacheFile)) { |
198
|
1 |
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
2 |
|
return json_decode(file_get_contents($cacheFile), true) ?? []; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|