-
-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathFileStore.php
More file actions
306 lines (256 loc) · 6.54 KB
/
FileStore.php
File metadata and controls
306 lines (256 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<?php
namespace TusPhp\Cache;
use TusPhp\File;
use Carbon\Carbon;
use TusPhp\Config;
class FileStore extends AbstractCache
{
/** @var int */
public const LOCK_NONE = 0;
/** @var string */
protected $cacheDir;
/** @var string */
protected $cacheFile;
/**
* FileStore constructor.
*
* @param string|null $cacheDir
* @param string|null $cacheFile
*/
public function __construct(?string $cacheDir = null, ?string $cacheFile = null)
{
$cacheDir = $cacheDir ?? Config::get('file.dir');
$cacheFile = $cacheFile ?? Config::get('file.name');
$this->setCacheDir($cacheDir);
$this->setCacheFile($cacheFile);
}
/**
* Set cache dir.
*
* @param string $path
*
* @return self
*/
public function setCacheDir(string $path): self
{
$this->cacheDir = $path;
return $this;
}
/**
* Get cache dir.
*
* @return string
*/
public function getCacheDir(): string
{
return $this->cacheDir;
}
/**
* Set cache file.
*
* @param string $file
*
* @return self
*/
public function setCacheFile(string $file): self
{
$this->cacheFile = $file;
return $this;
}
/**
* Get cache file.
*
* @return string
*/
public function getCacheFile(): string
{
return $this->cacheDir . $this->cacheFile;
}
/**
* Create cache dir if not exists.
*
* @return void
*/
protected function createCacheDir()
{
if ( ! file_exists($this->cacheDir)) {
mkdir($this->cacheDir);
}
}
/**
* Create a cache file.
*
* @return void
*/
protected function createCacheFile()
{
$this->createCacheDir();
$cacheFilePath = $this->getCacheFile();
if ( ! file_exists($cacheFilePath)) {
touch($cacheFilePath);
}
}
/**
* {@inheritDoc}
*/
public function get(string $key, bool $withExpired = false)
{
$key = $this->getActualCacheKey($key);
$contents = $this->getCacheContents();
if (empty($contents[$key])) {
return null;
}
if ($withExpired) {
return $contents[$key];
}
return $this->isValid($key) ? $contents[$key] : null;
}
/**
* @param string $path
* @param int $type
* @param callable|null $cb
*
* @return mixed
*/
protected function lock(string $path, int $type = LOCK_SH, ?callable $cb = null, $fopenType = FILE::READ_BINARY)
{
$out = false;
$handle = @fopen($path, $fopenType);
if (false === $handle) {
return $out;
}
try {
if (flock($handle, $type)) {
clearstatcache(true, $path);
$out = $cb($handle);
}
} finally {
flock($handle, LOCK_UN);
fclose($handle);
}
return $out;
}
/**
* Get contents of a file with shared access.
*
* @param string $path
*
* @return string
*/
public function sharedGet(string $path): string
{
return $this->lock($path, LOCK_SH, function ($handle) use ($path) {
$fstat = fstat($handle);
$size = $fstat ? $fstat['size'] : 1;
$contents = fread($handle, $size ?: 1);
if (false === $contents) {
return '';
}
return $contents;
});
}
/**
* Write the contents of a file with exclusive lock.
*
* @param string $path
* @param string $contents
* @param int $lock
*
* @return int|false
*/
public function put(string $path, string $contents, int $lock = LOCK_EX)
{
return file_put_contents($path, $contents, $lock);
}
/**
* {@inheritDoc}
*/
public function set(string $key, $value)
{
$cacheKey = $this->getActualCacheKey($key);
$cacheFile = $this->getCacheFile();
if ( ! file_exists($cacheFile) || ! $this->isValid($cacheKey)) {
$this->createCacheFile();
}
return $this->lock($cacheFile, LOCK_EX, function ($handle) use ($cacheKey, $cacheFile, $value) {
$size = fstat($handle)['size'];
$contents = fread($handle, $size ?: 1) ?? '';
$contents = json_decode($contents, true) ?? [];
if ( ! empty($contents[$cacheKey]) && \is_array($value)) {
$contents[$cacheKey] = $value + $contents[$cacheKey];
} else {
$contents[$cacheKey] = $value;
}
ftruncate($handle, 0);
return fwrite($handle, json_encode($contents));
}, FILE::APPEND_WRITE);
}
/**
* {@inheritDoc}
*/
public function delete(string $key): bool
{
$cacheKey = $this->getActualCacheKey($key);
$contents = $this->getCacheContents();
if (isset($contents[$cacheKey])) {
unset($contents[$cacheKey]);
return false !== $this->put($this->getCacheFile(), json_encode($contents));
}
return false;
}
/**
* {@inheritDoc}
*/
public function keys(): array
{
$contents = $this->getCacheContents();
if (\is_array($contents)) {
return array_keys($contents);
}
return [];
}
/**
* Check if cache is still valid.
*
* @param string $key
*
* @return bool
*/
public function isValid(string $key): bool
{
$key = $this->getActualCacheKey($key);
$meta = $this->getCacheContents()[$key] ?? [];
if (empty($meta['expires_at'])) {
return false;
}
return Carbon::now() < Carbon::createFromFormat(self::RFC_7231, $meta['expires_at']);
}
/**
* Get cache contents.
*
* @return array|bool
*/
public function getCacheContents()
{
$cacheFile = $this->getCacheFile();
if ( ! file_exists($cacheFile)) {
return false;
}
return json_decode($this->sharedGet($cacheFile), true) ?? [];
}
/**
* Get actual cache key with prefix.
*
* @param string $key
*
* @return string
*/
public function getActualCacheKey(string $key): string
{
$prefix = $this->getPrefix();
if (false === strpos($key, $prefix)) {
$key = $prefix . $key;
}
return $key;
}
}