Passed
Pull Request — master (#29)
by Alexander
01:55
created

FileCache::setFileMode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache\File;
6
7
use DateInterval;
8
use DateTime;
9
use Exception;
10
use Psr\SimpleCache\CacheInterface;
11
use Traversable;
12
13
use function array_keys;
14
use function array_map;
15
use function closedir;
16
use function dirname;
17
use function error_get_last;
18
use function filemtime;
19
use function fileowner;
20
use function file_exists;
21
use function fopen;
22
use function function_exists;
23
use function gettype;
24
use function is_dir;
25
use function is_file;
26
use function is_iterable;
27
use function is_string;
28
use function iterator_to_array;
29
use function opendir;
30
use function posix_geteuid;
31
use function random_int;
32
use function readdir;
33
use function rmdir;
34
use function serialize;
35
use function strncmp;
36
use function strpbrk;
37
use function substr;
38
use function unlink;
39
use function unserialize;
40
41
use const LOCK_EX;
42
use const LOCK_SH;
43
use const LOCK_UN;
44
45
/**
46
 * FileCache implements a cache handler using files.
47
 *
48
 * For each data value being cached, FileCache will store it in a separate file.
49
 * The cache files are placed under {@see FileCache::$cachePath}.
50
 * FileCache will perform garbage collection automatically to remove expired cache files.
51
 *
52
 * Please refer to {@see \Psr\SimpleCache\CacheInterface} for common cache operations that are supported by FileCache.
53
 */
54
final class FileCache implements CacheInterface
55
{
56
    private const TTL_INFINITY = 31536000; // 1 year
57
    private const EXPIRATION_EXPIRED = -1;
58
59
    /**
60
     * @var string The directory to store cache files. You may use path alias here.
61
     *
62
     * @see https://github.com/yiisoft/docs/blob/master/guide/en/concept/aliases.md
63
     */
64
    private string $cachePath;
65
66
    /**
67
     * @var string The cache file suffix. Defaults to '.bin'.
68
     */
69
    private string $fileSuffix = '.bin';
70
71
    /**
72
     * @var int|null The permission to be set for newly created cache files.
73
     * This value will be used by PHP chmod() function. No umask will be applied.
74
     * If not set, the permission will be determined by the current environment.
75
     */
76
    private ?int $fileMode = null;
77
78
    /**
79
     * @var int The permission to be set for newly created directories.
80
     * This value will be used by PHP chmod() function. No umask will be applied.
81
     * Defaults to 0775, meaning the directory is read-writable by owner and group,
82
     * but read-only for other users.
83
     */
84
    private int $dirMode = 0775;
85
86
    /**
87
     * @var int The level of sub-directories to store cache files. Defaults to 1.
88
     * If the system has huge number of cache files (e.g. one million), you may use a bigger value
89
     * (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
90
     * is not over burdened with a single directory having too many files.
91
     */
92
    private int $directoryLevel = 1;
93
94
    /**
95
     * @var int The probability (parts per million) that garbage collection (GC) should be performed
96
     * when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
97
     * This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
98
     */
99
    private int $gcProbability = 10;
100
101
    /**
102
     * @param string $cachePath The directory to store cache files.
103
     *
104
     * @see FileCache::$cachePath
105
     *
106
     * @throws CacheException If failed to create cache directory.
107
     */
108 171
    public function __construct(string $cachePath)
109
    {
110 171
        if (!$this->createDirectoryIfNotExists($cachePath)) {
111
            throw new CacheException("Failed to create cache directory \"{$cachePath}\".");
112
        }
113
114 171
        $this->cachePath = $cachePath;
115 171
    }
116
117 85
    public function get($key, $default = null)
118
    {
119 85
        $this->validateKey($key);
120 77
        $file = $this->getCacheFile($key);
121
122 77
        if (!$this->existsAndNotExpired($file) || ($filePointer = @fopen($file, 'rb')) === false) {
123 27
            return $default;
124
        }
125
126 64
        flock($filePointer, LOCK_SH);
127 64
        $value = stream_get_contents($filePointer);
128 64
        flock($filePointer, LOCK_UN);
129 64
        fclose($filePointer);
130
131 64
        return unserialize($value);
132
    }
133
134 97
    public function set($key, $value, $ttl = null): bool
135
    {
136 97
        $this->validateKey($key);
137 89
        $this->gc();
138 89
        $expiration = $this->ttlToExpiration($ttl);
139
140 89
        if ($expiration < 0) {
141 1
            return $this->delete($key);
142
        }
143
144 89
        $file = $this->getCacheFile($key);
145
146 89
        if ($this->directoryLevel > 0 && !$this->createDirectoryIfNotExists(dirname($file))) {
147
            return false;
148
        }
149
150
        // If ownership differs the touch call will fail, so we try to
151
        // rebuild the file from scratch by deleting it first
152
        // https://github.com/yiisoft/yii2/pull/16120
153 89
        if (function_exists('posix_geteuid') && is_file($file) && fileowner($file) !== posix_geteuid()) {
154
            @unlink($file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

154
            /** @scrutinizer ignore-unhandled */ @unlink($file);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
155
        }
156
157 89
        if (file_put_contents($file, serialize($value), LOCK_EX) === false) {
158
            return false;
159
        }
160
161 89
        if ($this->fileMode !== null) {
162 1
            chmod($file, $this->fileMode);
163
        }
164
165 89
        return touch($file, $expiration);
166
    }
167
168 22
    public function delete($key): bool
169
    {
170 22
        $this->validateKey($key);
171 14
        $file = $this->getCacheFile($key);
172
173 14
        if (!file_exists($file)) {
174
            return true;
175
        }
176
177 14
        return @unlink($file);
178
    }
179
180 12
    public function clear(): bool
181
    {
182 12
        $this->removeCacheFiles($this->cachePath, false);
183 12
        return true;
184
    }
185
186 23
    public function getMultiple($keys, $default = null): iterable
187
    {
188 23
        $keys = $this->iterableToArray($keys);
189 15
        $this->validateKeys($keys);
190 7
        $results = [];
191
192 7
        foreach ($keys as $key) {
193 7
            $results[$key] = $this->get($key, $default);
194
        }
195
196 7
        return $results;
197
    }
198
199 18
    public function setMultiple($values, $ttl = null): bool
200
    {
201 18
        $values = $this->iterableToArray($values);
202 10
        $this->validateKeys(array_map('strval', array_keys($values)));
203
204 10
        foreach ($values as $key => $value) {
205 10
            $this->set((string) $key, $value, $ttl);
206
        }
207
208 10
        return true;
209
    }
210
211 17
    public function deleteMultiple($keys): bool
212
    {
213 17
        $keys = $this->iterableToArray($keys);
214 9
        $this->validateKeys($keys);
215
216 1
        foreach ($keys as $key) {
217 1
            $this->delete($key);
218
        }
219
220 1
        return true;
221
    }
222
223 21
    public function has($key): bool
224
    {
225 21
        $this->validateKey($key);
226 13
        return $this->existsAndNotExpired($this->getCacheFile($key));
227
    }
228
229
    /**
230
     * @param string $fileSuffix The cache file suffix. Defaults to '.bin'.
231
     *
232
     * @return self
233
     */
234 1
    public function withFileSuffix(string $fileSuffix): self
235
    {
236 1
        $new = clone $this;
237 1
        $new->fileSuffix = $fileSuffix;
238 1
        return $new;
239
    }
240
241
    /**
242
     * @param int $fileMode The permission to be set for newly created cache files.
243
     * This value will be used by PHP chmod() function. No umask will be applied.
244
     * If not set, the permission will be determined by the current environment.
245
     *
246
     * @return self
247
     */
248 1
    public function withFileMode(int $fileMode): self
249
    {
250 1
        $new = clone $this;
251 1
        $new->fileMode = $fileMode;
252 1
        return $new;
253
    }
254
255
    /**
256
     * @param int $dirMode The permission to be set for newly created directories.
257
     * This value will be used by PHP chmod() function. No umask will be applied.
258
     * Defaults to 0775, meaning the directory is read-writable by owner and group, but read-only for other users.
259
     *
260
     * @return self
261
     */
262 1
    public function withDirMode(int $dirMode): self
263
    {
264 1
        $new = clone $this;
265 1
        $new->dirMode = $dirMode;
266 1
        return $new;
267
    }
268
269
    /**
270
     * @param int $directoryLevel The level of sub-directories to store cache files. Defaults to 1.
271
     * If the system has huge number of cache files (e.g. one million), you may use a bigger value
272
     * (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
273
     * is not over burdened with a single directory having too many files.
274
     *
275
     * @return self
276
     */
277 1
    public function withDirectoryLevel(int $directoryLevel): self
278
    {
279 1
        $new = clone $this;
280 1
        $new->directoryLevel = $directoryLevel;
281 1
        return $new;
282
    }
283
284
    /**
285
     * @param int $gcProbability The probability (parts per million) that garbage collection (GC) should
286
     * be performed when storing a piece of data in the cache. Defaults to 10, meaning 0.001% chance.
287
     * This number should be between 0 and 1000000. A value 0 means no GC will be performed at all.
288
     *
289
     * @return self
290
     */
291 1
    public function withGcProbability(int $gcProbability): self
292
    {
293 1
        $new = clone $this;
294 1
        $new->gcProbability = $gcProbability;
295 1
        return $new;
296
    }
297
298
    /**
299
     * Converts TTL to expiration
300
     *
301
     * @param DateInterval|int|null $ttl
302
     *
303
     * @return int
304
     */
305 92
    private function ttlToExpiration($ttl): int
306
    {
307 92
        $ttl = $this->normalizeTtl($ttl);
308
309 92
        if ($ttl === null) {
310 87
            return self::TTL_INFINITY + time();
311
        }
312
313 8
        if ($ttl <= 0) {
314 2
            return self::EXPIRATION_EXPIRED;
315
        }
316
317 6
        return $ttl + time();
318
    }
319
320
    /**
321
     * Normalizes cache TTL handling strings and {@see DateInterval} objects.
322
     *
323
     * @param DateInterval|int|string|null $ttl The raw TTL.
324
     *
325
     * @return int|null TTL value as UNIX timestamp or null meaning infinity
326
     */
327 98
    private function normalizeTtl($ttl): ?int
328
    {
329 98
        if ($ttl === null) {
330 88
            return null;
331
        }
332
333 13
        if ($ttl instanceof DateInterval) {
334 3
            return (new DateTime('@0'))->add($ttl)->getTimestamp();
335
        }
336
337 10
        return (int) $ttl;
338
    }
339
340
    /**
341
     * Ensures that the directory is created.
342
     *
343
     * @param string $path The path to the directory.
344
     *
345
     * @return bool Whether the directory was created.
346
     */
347 171
    private function createDirectoryIfNotExists(string $path): bool
348
    {
349 171
        return is_dir($path) || (mkdir($path, $this->dirMode, true) && is_dir($path));
350
    }
351
352
    /**
353
     * Returns the cache file path given the cache key.
354
     *
355
     * @param string $key The cache key.
356
     *
357
     * @return string The cache file path.
358
     */
359 90
    private function getCacheFile(string $key): string
360
    {
361 90
        if ($this->directoryLevel < 1) {
362 1
            return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->fileSuffix;
363
        }
364
365 89
        $base = $this->cachePath;
366
367 89
        for ($i = 0; $i < $this->directoryLevel; ++$i) {
368 89
            if (($prefix = substr($key, $i + $i, 2)) !== false) {
369 89
                $base .= DIRECTORY_SEPARATOR . $prefix;
370
            }
371
        }
372
373 89
        return $base . DIRECTORY_SEPARATOR . $key . $this->fileSuffix;
374
    }
375
376
    /**
377
     * Recursively removing expired cache files under a directory. This method is mainly used by {@see gc()}.
378
     *
379
     * @param string $path The directory under which expired cache files are removed.
380
     * @param bool $expiredOnly Whether to only remove expired cache files.
381
     * If false, all files under `$path` will be removed.
382
     */
383 13
    private function removeCacheFiles(string $path, bool $expiredOnly): void
384
    {
385 13
        if (($handle = @opendir($path)) === false) {
386
            return;
387
        }
388
389 13
        while (($file = readdir($handle)) !== false) {
390 13
            if (strncmp($file, '.', 1) === 0) {
391 13
                continue;
392
            }
393
394 13
            $fullPath = $path . DIRECTORY_SEPARATOR . $file;
395
396 13
            if (is_dir($fullPath)) {
397 13
                $this->removeCacheFiles($fullPath, $expiredOnly);
398
399 13
                if (!$expiredOnly && !@rmdir($fullPath)) {
400
                    $errorMessage = error_get_last()['message'] ?? '';
401 13
                    throw new CacheException("Unable to remove directory '{$fullPath}': {$errorMessage}");
402
                }
403 13
            } elseif ((!$expiredOnly || @filemtime($fullPath) < time()) && !@unlink($fullPath)) {
404
                $errorMessage = error_get_last()['message'] ?? '';
405
                throw new CacheException("Unable to remove file '{$fullPath}': {$errorMessage}");
406
            }
407
        }
408
409 13
        closedir($handle);
410 13
    }
411
412
    /**
413
     * Removes expired cache files.
414
     *
415
     * @throws Exception
416
     */
417 89
    private function gc(): void
418
    {
419 89
        if (random_int(0, 1000000) < $this->gcProbability) {
420 1
            $this->removeCacheFiles($this->cachePath, true);
421
        }
422 89
    }
423
424
    /**
425
     * @param mixed $key
426
     */
427 138
    private function validateKey($key): void
428
    {
429 138
        if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) {
430 48
            throw new InvalidArgumentException('Invalid key value.');
431
        }
432 90
    }
433
434
    /**
435
     * @param array $keys
436
     */
437 26
    private function validateKeys(array $keys): void
438
    {
439 26
        foreach ($keys as $key) {
440 26
            $this->validateKey($key);
441
        }
442 10
    }
443
444
    /**
445
     * @param string $file
446
     *
447
     * @return bool
448
     */
449 78
    private function existsAndNotExpired(string $file): bool
450
    {
451 78
        return file_exists($file) && @filemtime($file) > time();
452
    }
453
454
    /**
455
     * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
456
     *
457
     * @param mixed $iterable
458
     *
459
     * @return array
460
     */
461 50
    private function iterableToArray($iterable): array
462
    {
463 50
        if (!is_iterable($iterable)) {
464 24
            throw new InvalidArgumentException('Iterable is expected, got ' . gettype($iterable));
465
        }
466
467
        /** @psalm-suppress RedundantCast */
468 26
        return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
469
    }
470
}
471