forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandler.php
More file actions
232 lines (183 loc) · 5.41 KB
/
FileHandler.php
File metadata and controls
232 lines (183 loc) · 5.41 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Cache\Handlers;
use CodeIgniter\Cache\Exceptions\CacheException;
use CodeIgniter\I18n\Time;
use Config\Cache;
use Throwable;
/**
* File system cache handler
*
* @see \CodeIgniter\Cache\Handlers\FileHandlerTest
*/
class FileHandler extends BaseHandler
{
/**
* Maximum key length.
*/
public const MAX_KEY_LENGTH = 255;
/**
* Where to store cached files on the disk.
*
* @var string
*/
protected $path;
/**
* Mode for the stored files.
* Must be chmod-safe (octal).
*
* @var int
*
* @see https://www.php.net/manual/en/function.chmod.php
*/
protected $mode;
/**
* Note: Use `CacheFactory::getHandler()` to instantiate.
*
* @throws CacheException
*/
public function __construct(Cache $config)
{
$options = [
...['storePath' => WRITEPATH . 'cache', 'mode' => 0640],
...$config->file,
];
$this->path = $options['storePath'] !== '' ? $options['storePath'] : WRITEPATH . 'cache';
$this->path = rtrim($this->path, '\\/') . '/';
if (! is_really_writable($this->path)) {
throw CacheException::forUnableToWrite($this->path);
}
$this->mode = $options['mode'];
$this->prefix = $config->prefix;
helper('filesystem');
}
public function initialize(): void
{
}
public function get(string $key): mixed
{
$key = static::validateKey($key, $this->prefix);
$data = $this->getItem($key);
return is_array($data) ? $data['data'] : null;
}
public function save(string $key, mixed $value, int $ttl = 60): bool
{
$key = static::validateKey($key, $this->prefix);
$contents = [
'time' => Time::now()->getTimestamp(),
'ttl' => $ttl,
'data' => $value,
];
if (write_file($this->path . $key, serialize($contents))) {
try {
chmod($this->path . $key, $this->mode);
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
log_message('debug', 'Failed to set mode on cache file: ' . $e);
// @codeCoverageIgnoreEnd
}
return true;
}
return false;
}
public function delete(string $key): bool
{
$key = static::validateKey($key, $this->prefix);
return is_file($this->path . $key) && unlink($this->path . $key);
}
public function deleteMatching(string $pattern): int
{
$deleted = 0;
foreach (glob($this->path . $pattern, GLOB_NOSORT) as $filename) {
if (is_file($filename) && @unlink($filename)) {
$deleted++;
}
}
return $deleted;
}
public function increment(string $key, int $offset = 1): bool|int
{
$prefixedKey = static::validateKey($key, $this->prefix);
$tmp = $this->getItem($prefixedKey);
if ($tmp === false) {
$tmp = ['data' => 0, 'ttl' => 60];
}
['data' => $value, 'ttl' => $ttl] = $tmp;
if (! is_int($value)) {
return false;
}
$value += $offset;
return $this->save($key, $value, $ttl) ? $value : false;
}
public function decrement(string $key, int $offset = 1): bool|int
{
return $this->increment($key, -$offset);
}
public function clean(): bool
{
return delete_files($this->path, false, true);
}
public function getCacheInfo(): array
{
return get_dir_file_info($this->path);
}
public function getMetaData(string $key): ?array
{
$key = static::validateKey($key, $this->prefix);
if (false === $data = $this->getItem($key)) {
return null;
}
return [
'expire' => $data['ttl'] > 0 ? $data['time'] + $data['ttl'] : null,
'mtime' => filemtime($this->path . $key),
'data' => $data['data'],
];
}
public function isSupported(): bool
{
return is_writable($this->path);
}
/**
* Does the heavy lifting of actually retrieving the file and
* verifying its age.
*
* @return array{data: mixed, ttl: int, time: int}|false
*/
protected function getItem(string $filename): array|false
{
if (! is_file($this->path . $filename)) {
return false;
}
$content = @file_get_contents($this->path . $filename);
if ($content === false) {
return false;
}
try {
$data = unserialize($content);
} catch (Throwable) {
return false;
}
if (! is_array($data)) {
return false;
}
if (! isset($data['ttl']) || ! is_int($data['ttl'])) {
return false;
}
if (! isset($data['time']) || ! is_int($data['time'])) {
return false;
}
if ($data['ttl'] > 0 && Time::now()->getTimestamp() > $data['time'] + $data['ttl']) {
@unlink($this->path . $filename);
return false;
}
return $data;
}
}