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 str_replace; |
26
|
|
|
use function unlink; |
27
|
|
|
use function unserialize; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* A cache driver class provided by local file system. |
31
|
|
|
*/ |
32
|
|
|
class File extends CacheProvider |
33
|
|
|
{ |
34
|
|
|
protected $type = 'file'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The absolute path of the storage's directory. |
38
|
|
|
* It must be writable. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $storage = '/tmp/simple-cache'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param array $setting The settings. |
48
|
|
|
* |
49
|
|
|
* @throws CacheException |
50
|
|
|
*/ |
51
|
36 |
|
public function __construct(array $setting = []) |
52
|
|
|
{ |
53
|
36 |
|
if (isset($setting['storage'])) { |
54
|
36 |
|
$this->storage = rtrim($setting['storage'], '/'); |
55
|
|
|
} |
56
|
|
|
|
57
|
36 |
|
$this->assertDirectoryWritable($this->storage); |
58
|
34 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Fetch a cache by an extended Cache Driver. |
62
|
|
|
* |
63
|
|
|
* @param string $key The key of a cache. |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
16 |
|
protected function doGet(string $key): array |
68
|
|
|
{ |
69
|
16 |
|
$filePath = $this->getFilePath($key); |
70
|
|
|
|
71
|
16 |
|
if (!is_file($filePath)) { |
72
|
12 |
|
return []; |
73
|
|
|
} |
74
|
|
|
|
75
|
16 |
|
$data = unserialize(file_get_contents($filePath)); |
76
|
|
|
|
77
|
16 |
|
return $data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set a cache by an extended Cache Driver. |
82
|
|
|
* |
83
|
|
|
* @param string $key The key of a cache. |
84
|
|
|
* @param mixed $value The value of a cache. (serialized) |
85
|
|
|
* @param int $ttl The time to live for a cache. |
86
|
|
|
* @param int $timestamp The time to store a cache. |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
16 |
|
protected function doSet(string $key, $value, int $ttl, int $timestamp): bool |
91
|
|
|
{ |
92
|
|
|
$contents = [ |
93
|
16 |
|
'timestamp' => $timestamp, |
94
|
16 |
|
'ttl' => $ttl, |
95
|
16 |
|
'value' => $value, |
96
|
|
|
]; |
97
|
|
|
|
98
|
16 |
|
$filePath = $this->getFilePath($key); |
99
|
|
|
|
100
|
16 |
|
if (file_put_contents($filePath, serialize($contents))) { |
101
|
16 |
|
chmod($filePath, 0640); |
102
|
16 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// @codeCoverageIgnoreStart |
106
|
|
|
return false; |
107
|
|
|
// @codeCoverageIgnoreEnd |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete a cache by an extended Cache Driver. |
112
|
|
|
* |
113
|
|
|
* @param string $key The key of a cache. |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
12 |
|
protected function doDelete(string $key): bool |
118
|
|
|
{ |
119
|
12 |
|
$filePath = $this->getFilePath($key); |
120
|
|
|
|
121
|
12 |
|
return is_file($filePath) ? unlink($filePath) : false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Delete all caches by an extended Cache Driver. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
12 |
|
protected function doClear(): bool |
130
|
|
|
{ |
131
|
12 |
|
$directory = new DirectoryIterator($this->storage); |
132
|
|
|
|
133
|
12 |
|
foreach ($directory as $file) { |
134
|
12 |
|
if ($file->isFile() && $file->getExtension() === 'cache') { |
135
|
12 |
|
unlink($file->getRealPath()); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
12 |
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if the cache exists or not. |
144
|
|
|
* |
145
|
|
|
* @param string $key The key of a cache. |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
6 |
|
protected function doHas(string $key): bool |
150
|
|
|
{ |
151
|
6 |
|
return file_exists($this->getFilePath($key)); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Fetch all cache items. |
156
|
|
|
* |
157
|
|
|
* @return attay |
|
|
|
|
158
|
|
|
*/ |
159
|
8 |
|
protected function getAll(): array |
160
|
|
|
{ |
161
|
8 |
|
$directory = new DirectoryIterator($this->storage); |
162
|
8 |
|
$list = []; |
163
|
|
|
|
164
|
8 |
|
foreach ($directory as $file) { |
165
|
8 |
|
$ext = $file->getExtension(); |
166
|
|
|
|
167
|
8 |
|
if ($file->isFile() && $ext === 'cache') { |
168
|
8 |
|
$key = str_replace('.' . $ext, '', $file->getFilename()); |
169
|
8 |
|
$value = $this->doGet($key); |
170
|
|
|
|
171
|
8 |
|
$list[$key] = $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
8 |
|
return $list; |
|
|
|
|
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get the path of a cache file. |
179
|
|
|
* |
180
|
|
|
* @param string $key The key of a cache. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
16 |
|
private function getFilePath(string $key): string |
185
|
|
|
{ |
186
|
16 |
|
return $this->storage . '/' . $key . '.cache'; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
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