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