1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Part of the Joomla Framework Cache Package |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\Cache; |
10
|
|
|
|
11
|
|
|
use Joomla\Cache\Exception\RuntimeException; |
12
|
|
|
use Joomla\Cache\Item\HasExpirationDateInterface; |
13
|
|
|
use Psr\Cache\CacheItemInterface; |
14
|
|
|
use Joomla\Cache\Exception\InvalidArgumentException; |
15
|
|
|
use Joomla\Cache\Item\Item; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Filesystem cache driver for the Joomla Framework. |
19
|
|
|
* |
20
|
|
|
* Supported options: |
21
|
|
|
* - file.locking (boolean) : |
22
|
|
|
* - file.path : The path for cache files. |
23
|
|
|
* |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
|
|
class File extends Cache |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param mixed $options An options array, or an object that implements \ArrayAccess |
32
|
|
|
* |
33
|
|
|
* @since 1.0 |
34
|
|
|
* @throws \RuntimeException |
35
|
|
|
*/ |
36
|
10 |
|
public function __construct($options = array()) |
37
|
|
|
{ |
38
|
10 |
|
if (!isset($options['file.locking'])) |
39
|
10 |
|
{ |
40
|
10 |
|
$options['file.locking'] = true; |
41
|
10 |
|
} |
42
|
|
|
|
43
|
10 |
|
if (!isset($options['file.path'])) |
44
|
10 |
|
{ |
45
|
|
|
throw new InvalidArgumentException('The file.path option must be set.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
10 |
|
$this->checkFilePath($options['file.path']); |
49
|
|
|
|
50
|
10 |
|
parent::__construct($options); |
51
|
10 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This will wipe out the entire cache's keys.... |
55
|
|
|
* |
56
|
|
|
* @return boolean The result of the clear operation. |
57
|
|
|
* |
58
|
|
|
* @since 1.0 |
59
|
|
|
*/ |
60
|
10 |
|
public function clear() |
61
|
|
|
{ |
62
|
10 |
|
$filePath = $this->options['file.path']; |
63
|
10 |
|
$this->checkFilePath($filePath); |
64
|
|
|
|
65
|
10 |
|
$iterator = new \RegexIterator( |
66
|
10 |
|
new \RecursiveIteratorIterator( |
67
|
10 |
|
new \RecursiveDirectoryIterator($filePath) |
68
|
10 |
|
), |
69
|
|
|
'/\.data$/i' |
70
|
10 |
|
); |
71
|
|
|
|
72
|
|
|
/* @var \RecursiveDirectoryIterator $file */ |
73
|
10 |
|
foreach ($iterator as $file) |
74
|
|
|
{ |
75
|
6 |
|
if ($file->isFile()) |
76
|
6 |
|
{ |
77
|
6 |
|
@unlink($file->getRealPath()); |
|
|
|
|
78
|
6 |
|
} |
79
|
10 |
|
} |
80
|
|
|
|
81
|
10 |
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Method to get a storage entry value from a key. |
86
|
|
|
* |
87
|
|
|
* @param string $key The storage entry identifier. |
88
|
|
|
* |
89
|
|
|
* @return CacheItemInterface |
90
|
|
|
* |
91
|
|
|
* @since 1.0 |
92
|
|
|
* @throws \RuntimeException |
93
|
|
|
*/ |
94
|
5 |
|
public function getItem($key) |
95
|
|
|
{ |
96
|
5 |
|
if (!$this->hasItem($key)) |
97
|
5 |
|
{ |
98
|
3 |
|
return new Item($key); |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
$resource = @fopen($this->fetchStreamUri($key), 'rb'); |
102
|
|
|
|
103
|
5 |
|
if (!$resource) |
104
|
5 |
|
{ |
105
|
|
|
throw new RuntimeException(sprintf('Unable to fetch cache entry for %s. Connot open the resource.', $key)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// If locking is enabled get a shared lock for reading on the resource. |
109
|
5 |
View Code Duplication |
if ($this->options['file.locking'] && !flock($resource, LOCK_SH)) |
|
|
|
|
110
|
5 |
|
{ |
111
|
|
|
throw new RuntimeException(sprintf('Unable to fetch cache entry for %s. Connot obtain a lock.', $key)); |
112
|
|
|
} |
113
|
|
|
|
114
|
5 |
|
$data = stream_get_contents($resource); |
115
|
|
|
|
116
|
|
|
// If locking is enabled release the lock on the resource. |
117
|
5 |
View Code Duplication |
if ($this->options['file.locking'] && !flock($resource, LOCK_UN)) |
|
|
|
|
118
|
5 |
|
{ |
119
|
|
|
throw new RuntimeException(sprintf('Unable to fetch cache entry for %s. Connot release the lock.', $key)); |
120
|
|
|
} |
121
|
|
|
|
122
|
5 |
|
fclose($resource); |
123
|
|
|
|
124
|
5 |
|
$item = new Item($key); |
125
|
5 |
|
$information = unserialize($data); |
126
|
|
|
|
127
|
|
|
// If the cached data has expired remove it and return. |
128
|
5 |
|
if ($information[1] !== null && time() > $information[1]) |
129
|
5 |
|
{ |
130
|
1 |
|
if (!$this->deleteItem($key)) |
131
|
1 |
|
{ |
132
|
|
|
throw new RuntimeException(sprintf('Unable to clean expired cache entry for %s.', $key), null); |
133
|
|
|
} |
134
|
|
|
|
135
|
1 |
|
return $item; |
136
|
|
|
} |
137
|
|
|
|
138
|
4 |
|
$item->set($information[0]); |
139
|
|
|
|
140
|
4 |
|
return $item; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Method to remove a storage entry for a key. |
145
|
|
|
* |
146
|
|
|
* @param string $key The storage entry identifier. |
147
|
|
|
* |
148
|
|
|
* @return boolean True on success |
149
|
|
|
* |
150
|
|
|
* @since 1.0 |
151
|
|
|
*/ |
152
|
3 |
|
public function deleteItem($key) |
153
|
|
|
{ |
154
|
3 |
|
if ($this->hasItem($key)) |
155
|
3 |
|
{ |
156
|
3 |
|
return (bool) @unlink($this->fetchStreamUri($key)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// If the item doesn't exist, no error |
160
|
2 |
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Persists a cache item immediately. |
165
|
|
|
* |
166
|
|
|
* @param CacheItemInterface $item The cache item to save. |
167
|
|
|
* |
168
|
|
|
* @return bool True if the item was successfully persisted. False if there was an error. |
169
|
|
|
*/ |
170
|
8 |
|
public function save(CacheItemInterface $item) |
171
|
|
|
{ |
172
|
8 |
|
$fileName = $this->fetchStreamUri($item->getKey()); |
173
|
8 |
|
$filePath = pathinfo($fileName, PATHINFO_DIRNAME); |
174
|
|
|
|
175
|
8 |
|
if (!is_dir($filePath)) |
176
|
8 |
|
{ |
177
|
6 |
|
mkdir($filePath, 0770, true); |
178
|
6 |
|
} |
179
|
|
|
|
180
|
8 |
|
if ($item instanceof HasExpirationDateInterface) |
181
|
8 |
|
{ |
182
|
1 |
|
$contents = serialize(array($item->get(), time() + $this->convertItemExpiryToSeconds($item))); |
183
|
1 |
|
} |
184
|
|
|
else |
185
|
|
|
{ |
186
|
7 |
|
$contents = serialize(array($item->get(), null)); |
187
|
|
|
} |
188
|
|
|
|
189
|
8 |
|
$success = (bool) file_put_contents( |
190
|
8 |
|
$fileName, |
191
|
8 |
|
$contents, |
192
|
8 |
|
($this->options['file.locking'] ? LOCK_EX : null) |
193
|
8 |
|
); |
194
|
|
|
|
195
|
8 |
|
return $success; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Method to determine whether a storage entry has been set for a key. |
200
|
|
|
* |
201
|
|
|
* @param string $key The storage entry identifier. |
202
|
|
|
* |
203
|
|
|
* @return boolean |
204
|
|
|
* |
205
|
|
|
* @since 1.0 |
206
|
|
|
*/ |
207
|
8 |
|
public function hasItem($key) |
208
|
|
|
{ |
209
|
8 |
|
return is_file($this->fetchStreamUri($key)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Test to see if the CacheItemPoolInterface is available |
214
|
|
|
* |
215
|
|
|
* @return boolean True on success, false otherwise |
216
|
|
|
* |
217
|
|
|
* @since __DEPLOY_VERSION__ |
218
|
|
|
*/ |
219
|
|
|
public static function isSupported() |
220
|
|
|
{ |
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Check that the file path is a directory and writable. |
226
|
|
|
* |
227
|
|
|
* @param string $filePath A file path. |
228
|
|
|
* |
229
|
|
|
* @return boolean The method will always return true, if it returns. |
230
|
|
|
* |
231
|
|
|
* @since 1.0 |
232
|
|
|
* @throws \RuntimeException if the file path is invalid. |
233
|
|
|
*/ |
234
|
10 |
|
private function checkFilePath($filePath) |
235
|
|
|
{ |
236
|
10 |
|
if (!is_dir($filePath)) |
237
|
10 |
|
{ |
238
|
|
|
throw new RuntimeException(sprintf('The base cache path `%s` does not exist.', $filePath)); |
239
|
|
|
} |
240
|
10 |
|
elseif (!is_writable($filePath)) |
241
|
|
|
{ |
242
|
|
|
throw new RuntimeException(sprintf('The base cache path `%s` is not writable.', $filePath)); |
243
|
|
|
} |
244
|
|
|
|
245
|
10 |
|
return true; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the full stream URI for the cache entry. |
250
|
|
|
* |
251
|
|
|
* @param string $key The storage entry identifier. |
252
|
|
|
* |
253
|
|
|
* @return string The full stream URI for the cache entry. |
254
|
|
|
* |
255
|
|
|
* @since 1.0 |
256
|
|
|
* @throws \RuntimeException if the cache path is invalid. |
257
|
|
|
*/ |
258
|
8 |
|
private function fetchStreamUri($key) |
259
|
|
|
{ |
260
|
8 |
|
$filePath = $this->options['file.path']; |
261
|
8 |
|
$this->checkFilePath($filePath); |
262
|
|
|
|
263
|
8 |
|
return sprintf( |
264
|
8 |
|
'%s/~%s/%s.data', |
265
|
8 |
|
$filePath, |
266
|
8 |
|
substr(hash('md5', $key), 0, 4), |
267
|
8 |
|
hash('sha1', $key) |
268
|
8 |
|
); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: