1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Cache\Adapters\File;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Cache\Item;
|
19
|
|
|
use Psr\Cache\CacheItemInterface;
|
|
|
|
|
20
|
|
|
use Psr\Cache\CacheItemPoolInterface;
|
|
|
|
|
21
|
|
|
use O2System\Spl\Exceptions\Logic\InvalidArgumentException;
|
22
|
|
|
|
23
|
|
|
/**
|
24
|
|
|
* Class ItemPool
|
25
|
|
|
*
|
26
|
|
|
* @package O2System\Cache\Adapters\File
|
27
|
|
|
*/
|
28
|
|
|
class ItemPool extends Adapter implements CacheItemPoolInterface
|
29
|
|
|
{
|
30
|
|
|
/**
|
31
|
|
|
* ItemPool::getItems
|
32
|
|
|
*
|
33
|
|
|
* Returns a traversable set of cache items.
|
34
|
|
|
*
|
35
|
|
|
* @param string[] $keys
|
36
|
|
|
* An indexed array of keys of items to retrieve.
|
37
|
|
|
*
|
38
|
|
|
* @throws InvalidArgumentException
|
39
|
|
|
* If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
|
40
|
|
|
* MUST be thrown.
|
41
|
|
|
*
|
42
|
|
|
* @return array|\Traversable
|
43
|
|
|
* A traversable collection of Cache Items keyed by the cache keys of
|
44
|
|
|
* each item. A Cache item will be returned for each key, even if that
|
45
|
|
|
* key is not found. However, if no keys are specified then an empty
|
46
|
|
|
* traversable MUST be returned instead.
|
47
|
|
|
*/
|
48
|
|
|
public function getItems(array $keys = [])
|
49
|
|
|
{
|
50
|
|
|
if ( ! is_array($keys)) {
|
|
|
|
|
51
|
|
|
throw new InvalidArgumentException('CACHE_E_INVALID_ARGUMENT_ARRAY');
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
$items = [];
|
55
|
|
|
|
56
|
|
|
if (empty($keys)) {
|
57
|
|
|
$directory = new \RecursiveIteratorIterator(
|
58
|
|
|
new \RecursiveDirectoryIterator($this->path),
|
59
|
|
|
\RecursiveIteratorIterator::SELF_FIRST
|
60
|
|
|
);
|
61
|
|
|
|
62
|
|
|
$cacheIterator = new \RegexIterator($directory, '/^.+\.cache/i', \RecursiveRegexIterator::GET_MATCH);
|
63
|
|
|
|
64
|
|
|
foreach ($cacheIterator as $cacheFiles) {
|
65
|
|
|
foreach ($cacheFiles as $cacheFile) {
|
66
|
|
|
$items[] = $this->getItem(pathinfo($cacheFile, PATHINFO_FILENAME));
|
67
|
|
|
}
|
68
|
|
|
}
|
69
|
|
|
} elseif (count($keys)) {
|
70
|
|
|
foreach ($keys as $key) {
|
71
|
|
|
$items[] = $this->getItem($key);
|
72
|
|
|
}
|
73
|
|
|
}
|
74
|
|
|
|
75
|
|
|
return $items;
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
// ------------------------------------------------------------------------
|
79
|
|
|
|
80
|
|
|
/**
|
81
|
|
|
* ItemPool::getKey
|
82
|
|
|
*
|
83
|
|
|
* Returns a Cache Item representing the specified key.
|
84
|
|
|
*
|
85
|
|
|
* This method must always return a CacheItemInterface object, even in case of
|
86
|
|
|
* a cache miss. It MUST NOT return null.
|
87
|
|
|
*
|
88
|
|
|
* @param string $key
|
89
|
|
|
* The key for which to return the corresponding Cache Item.
|
90
|
|
|
*
|
91
|
|
|
* @throws InvalidArgumentException
|
92
|
|
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
|
93
|
|
|
* MUST be thrown.
|
94
|
|
|
*
|
95
|
|
|
* @return CacheItemInterface
|
96
|
|
|
* The corresponding Cache Item.
|
97
|
|
|
*/
|
98
|
|
|
public function getItem($key)
|
99
|
|
|
{
|
100
|
|
|
if ( ! is_string($key)) {
|
|
|
|
|
101
|
|
|
throw new InvalidArgumentException('CACHE_E_INVALID_ARGUMENT_STRING');
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
$filename = $this->path . $key . '.cache';
|
105
|
|
|
|
106
|
|
|
if (is_file($filename)) {
|
107
|
|
|
$metadata = unserialize(file_get_contents($filename));
|
108
|
|
|
|
109
|
|
|
if ($metadata[ 'ttl' ] > 0 AND time() > $metadata[ 'ctime' ] + $metadata[ 'ttl' ]) {
|
110
|
|
|
unlink($filename);
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
return new Item($key, $metadata);
|
114
|
|
|
}
|
115
|
|
|
|
116
|
|
|
return new Item($key);
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
// ------------------------------------------------------------------------
|
120
|
|
|
|
121
|
|
|
/**
|
122
|
|
|
* ItemPool::hasItem
|
123
|
|
|
*
|
124
|
|
|
* Confirms if the cache contains specified cache item.
|
125
|
|
|
*
|
126
|
|
|
* Note: This method MAY avoid retrieving the cached value for performance reasons.
|
127
|
|
|
* This could result in a race condition with CacheItemInterface::get(). To avoid
|
128
|
|
|
* such situation use CacheItemInterface::isHit() instead.
|
129
|
|
|
*
|
130
|
|
|
* @param string $key
|
131
|
|
|
* The key for which to check existence.
|
132
|
|
|
*
|
133
|
|
|
* @throws InvalidArgumentException
|
134
|
|
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
|
135
|
|
|
* MUST be thrown.
|
136
|
|
|
*
|
137
|
|
|
* @return bool
|
138
|
|
|
* True if item exists in the cache, false otherwise.
|
139
|
|
|
*/
|
140
|
|
|
public function hasItem($key)
|
141
|
|
|
{
|
142
|
|
|
if ( ! is_string($key)) {
|
|
|
|
|
143
|
|
|
throw new InvalidArgumentException('CACHE_E_INVALID_ARGUMENT_STRING');
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
$filename = $this->path . $key . '.cache';
|
147
|
|
|
|
148
|
|
|
return (bool)is_file($filename);
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
// ------------------------------------------------------------------------
|
152
|
|
|
|
153
|
|
|
/**
|
154
|
|
|
* ItemPool::clear
|
155
|
|
|
*
|
156
|
|
|
* Deletes all items in the pool.
|
157
|
|
|
*
|
158
|
|
|
* @return bool
|
159
|
|
|
* True if the pool was successfully cleared. False if there was an error.
|
160
|
|
|
*/
|
161
|
|
|
public function clear()
|
162
|
|
|
{
|
163
|
|
|
$directory = new \RecursiveIteratorIterator(
|
164
|
|
|
new \RecursiveDirectoryIterator($this->path),
|
165
|
|
|
\RecursiveIteratorIterator::SELF_FIRST
|
166
|
|
|
);
|
167
|
|
|
|
168
|
|
|
$cacheIterator = new \RegexIterator($directory, '/^.+\.cache/i', \RecursiveRegexIterator::GET_MATCH);
|
169
|
|
|
|
170
|
|
|
$isCleared = false;
|
171
|
|
|
|
172
|
|
|
foreach ($cacheIterator as $cacheFiles) {
|
173
|
|
|
foreach ($cacheFiles as $cacheFile) {
|
174
|
|
|
if (false === ($isCleared = unlink($cacheFile))) {
|
175
|
|
|
return $isCleared;
|
176
|
|
|
break;
|
|
|
|
|
177
|
|
|
}
|
178
|
|
|
}
|
179
|
|
|
}
|
180
|
|
|
|
181
|
|
|
return $isCleared;
|
182
|
|
|
}
|
183
|
|
|
|
184
|
|
|
// ------------------------------------------------------------------------
|
185
|
|
|
|
186
|
|
|
/**
|
187
|
|
|
* ItemPool::deleteItem
|
188
|
|
|
*
|
189
|
|
|
* Removes the item from the pool.
|
190
|
|
|
*
|
191
|
|
|
* @param string $key
|
192
|
|
|
* The key to delete.
|
193
|
|
|
*
|
194
|
|
|
* @throws InvalidArgumentException
|
195
|
|
|
* If the $key string is not a legal value a \Psr\Cache\InvalidArgumentException
|
196
|
|
|
* MUST be thrown.
|
197
|
|
|
*
|
198
|
|
|
* @return bool
|
199
|
|
|
* True if the item was successfully removed. False if there was an error.
|
200
|
|
|
*/
|
201
|
|
|
public function deleteItem($key)
|
202
|
|
|
{
|
203
|
|
|
if ( ! is_string($key)) {
|
|
|
|
|
204
|
|
|
throw new InvalidArgumentException('CACHE_E_INVALID_ARGUMENT_STRING');
|
205
|
|
|
}
|
206
|
|
|
|
207
|
|
|
$filename = $this->path . $key . '.cache';
|
208
|
|
|
|
209
|
|
|
if (is_file($filename)) {
|
210
|
|
|
return unlink($filename);
|
211
|
|
|
}
|
212
|
|
|
|
213
|
|
|
return false;
|
214
|
|
|
}
|
215
|
|
|
|
216
|
|
|
// ------------------------------------------------------------------------
|
217
|
|
|
|
218
|
|
|
/**
|
219
|
|
|
* ItemPool::save
|
220
|
|
|
*
|
221
|
|
|
* Persists a cache item immediately.
|
222
|
|
|
*
|
223
|
|
|
* @param CacheItemInterface $item
|
224
|
|
|
* The cache item to save.
|
225
|
|
|
*
|
226
|
|
|
* @return bool
|
227
|
|
|
* True if the item was successfully persisted. False if there was an error.
|
228
|
|
|
*/
|
229
|
|
|
public function save(CacheItemInterface $item)
|
230
|
|
|
{
|
231
|
|
|
$metadata = $item->getMetadata();
|
232
|
|
|
$metadata[ 'data' ] = $item->get();
|
233
|
|
|
$metadata = serialize($metadata);
|
234
|
|
|
|
235
|
|
|
$path = $this->path . $this->prefixKey . $item->getKey() . '.cache';
|
236
|
|
|
|
237
|
|
|
if ( ! $fp = @fopen($path, 'wb')) {
|
238
|
|
|
return false;
|
239
|
|
|
}
|
240
|
|
|
|
241
|
|
|
flock($fp, LOCK_EX);
|
242
|
|
|
|
243
|
|
|
for ($result = $written = 0, $length = strlen($metadata); $written < $length; $written += $result) {
|
244
|
|
|
if (($result = fwrite($fp, substr($metadata, $written))) === false) {
|
245
|
|
|
break;
|
246
|
|
|
}
|
247
|
|
|
}
|
248
|
|
|
|
249
|
|
|
flock($fp, LOCK_UN);
|
250
|
|
|
fclose($fp);
|
251
|
|
|
|
252
|
|
|
return (bool)is_int($result);
|
253
|
|
|
}
|
254
|
|
|
} |
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