1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon Simple Cache package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\SimpleCache\Driver; |
14
|
|
|
|
15
|
|
|
use Shieldon\SimpleCache\CacheProvider; |
16
|
|
|
use Shieldon\SimpleCache\Exception\CacheException; |
17
|
|
|
use DirectoryIterator; |
18
|
|
|
use function chmod; |
19
|
|
|
use function file_exists; |
20
|
|
|
use function file_get_contents; |
21
|
|
|
use function file_put_contents; |
22
|
|
|
use function is_file; |
23
|
|
|
use function rtrim; |
24
|
|
|
use function serialize; |
25
|
|
|
use function unlink; |
26
|
|
|
use function unserialize; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A cache driver class provided by local file system. |
30
|
|
|
*/ |
31
|
|
|
class File extends CacheProvider |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* The absolute path of the storage's directory. |
35
|
|
|
* It must be writable. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $storage = '/tmp/simple-cache'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param array $setting The settings. |
45
|
|
|
* |
46
|
|
|
* @throws CacheException |
47
|
|
|
*/ |
48
|
34 |
|
public function __construct(array $setting = []) |
49
|
|
|
{ |
50
|
34 |
|
if (isset($setting['storage'])) { |
51
|
34 |
|
$this->storage = rtrim($setting['storage'], '/'); |
52
|
|
|
} |
53
|
|
|
|
54
|
34 |
|
$this->assertDirectoryWritable($this->storage); |
55
|
32 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Fetch a cache by an extended Cache Driver. |
59
|
|
|
* |
60
|
|
|
* @param string $key The key of a cache. |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
14 |
|
protected function doGet(string $key): array |
65
|
|
|
{ |
66
|
14 |
|
$filePath = $this->getFilePath($key); |
67
|
|
|
|
68
|
14 |
|
if (!is_file($filePath)) { |
69
|
10 |
|
return []; |
70
|
|
|
} |
71
|
|
|
|
72
|
14 |
|
$data = unserialize(file_get_contents($filePath)); |
73
|
|
|
|
74
|
14 |
|
return $data; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set a cache by an extended Cache Driver. |
79
|
|
|
* |
80
|
|
|
* @param string $key The key of a cache. |
81
|
|
|
* @param mixed $value The value of a cache. (serialized) |
82
|
|
|
* @param int $ttl The time to live for a cache. |
83
|
|
|
* @param int $timestamp The time to store a cache. |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
14 |
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
88
|
|
|
{ |
89
|
|
|
$contents = [ |
90
|
14 |
|
'timestamp' => $timestamp, |
91
|
14 |
|
'ttl' => $ttl, |
92
|
14 |
|
'value' => $value |
93
|
|
|
]; |
94
|
|
|
|
95
|
14 |
|
$filePath = $this->getFilePath($key); |
96
|
|
|
|
97
|
14 |
|
if (file_put_contents($filePath, serialize($contents))) { |
98
|
14 |
|
chmod($filePath, 0640); |
99
|
14 |
|
return true; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// @codeCoverageIgnoreStart |
103
|
|
|
return false; |
104
|
|
|
// @codeCoverageIgnoreEnd |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete a cache by an extended Cache Driver. |
109
|
|
|
* |
110
|
|
|
* @param string $key The key of a cache. |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
10 |
|
protected function doDelete(string $key): bool |
115
|
|
|
{ |
116
|
10 |
|
$filePath = $this->getFilePath($key); |
117
|
|
|
|
118
|
10 |
|
return is_file($filePath) ? unlink($filePath) : false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Delete all caches by an extended Cache Driver. |
123
|
|
|
* |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
10 |
|
protected function doClear(): bool |
127
|
|
|
{ |
128
|
10 |
|
$directory = new DirectoryIterator($this->storage); |
129
|
|
|
|
130
|
10 |
|
foreach ($directory as $file) { |
131
|
10 |
|
if ($file->isFile() && $file->getExtension() === 'cache') { |
132
|
10 |
|
unlink($file->getRealPath()); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
10 |
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check if the cache exists or not. |
141
|
|
|
* |
142
|
|
|
* @param string $key The key of a cache. |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
6 |
|
protected function doHas(string $key): bool |
147
|
|
|
{ |
148
|
6 |
|
return file_exists($this->getFilePath($key)); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Fetch all cache items. |
153
|
|
|
* |
154
|
|
|
* @return attay |
|
|
|
|
155
|
|
|
*/ |
156
|
6 |
|
protected function getAll(): array |
157
|
|
|
{ |
158
|
6 |
|
$directory = new DirectoryIterator($this->storage); |
159
|
6 |
|
$list = []; |
160
|
|
|
|
161
|
6 |
|
foreach ($directory as $file) { |
162
|
6 |
|
$ext = $file->getExtension(); |
163
|
|
|
|
164
|
6 |
|
if ($file->isFile() && $ext === 'cache') { |
165
|
6 |
|
$key = str_replace('.' . $ext, '', $file->getFilename()); |
166
|
6 |
|
$value = $this->doGet($key); |
167
|
|
|
|
168
|
6 |
|
$list[$key] = $value; |
169
|
|
|
} |
170
|
|
|
} |
171
|
6 |
|
return $list; |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the path of a cache file. |
176
|
|
|
* |
177
|
|
|
* @param string $key The key of a cache. |
178
|
|
|
* |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
14 |
|
private function getFilePath(string $key): string |
182
|
|
|
{ |
183
|
14 |
|
return $this->storage . '/' . $key . '.cache'; |
184
|
|
|
} |
185
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths