1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeBlog\RateLimit; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class CodeBlog VariableCache |
7
|
|
|
* |
8
|
|
|
* @author Whallysson Avelino <https://github.com/whallysson> |
9
|
|
|
* @package CodeBlog\RateLimit |
10
|
|
|
*/ |
11
|
|
|
class VariableCache { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Directory where the data will be saved |
15
|
|
|
* @access private |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $cacheFolder; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Extent that will save the file |
22
|
|
|
* @access private |
23
|
|
|
* @var type string |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
private $ext = "cache"; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Variable Array |
29
|
|
|
* @access private |
30
|
|
|
* @var type array |
31
|
|
|
*/ |
32
|
|
|
private $variables = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* VariableCache constructor. |
36
|
|
|
* @param string|null $cacheFolder |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
|
|
function __construct(string $cacheFolder = null) { |
40
|
|
|
$folder = (substr($cacheFolder, "-1") == "/" ? $cacheFolder : "{$cacheFolder}/"); |
41
|
|
|
$this->cacheFolder = $cacheFolder ? $folder : $_SERVER['DOCUMENT_ROOT'] . '/cache-variable/'; |
42
|
|
|
|
43
|
|
|
if (!file_exists($this->cacheFolder) && !is_dir($this->cacheFolder)): |
44
|
|
|
if (!mkdir($this->cacheFolder, 0755)) { |
45
|
|
|
throw new \Exception("Could not create cache folder"); |
46
|
|
|
} |
47
|
|
|
endif; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Saves a variable in the cache |
52
|
|
|
* |
53
|
|
|
* @param string $key |
54
|
|
|
* @param array $value |
55
|
|
|
* @param string $expire |
56
|
|
|
*/ |
57
|
|
|
public function store(string $key, array $value, $expire = 0) { |
58
|
|
|
$this->variables = array_merge($this->variables, ['expire' => time() + $expire]); |
|
|
|
|
59
|
|
|
$this->variables = array_merge($this->variables, ['data' => $value]); |
60
|
|
|
file_put_contents($this->cacheFolder . "{$key}.{$this->ext}", json_encode($this->variables)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the saved cache variables |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @return boolean |
68
|
|
|
*/ |
69
|
|
|
public function fetch(string $key) { |
70
|
|
|
$archive = $this->cacheFolder . "{$key}.{$this->ext}"; |
71
|
|
|
if (file_exists($archive)) { |
72
|
|
|
$content = json_decode(file_get_contents($archive), true); |
73
|
|
|
|
74
|
|
|
if ($content['expire'] > time()): |
75
|
|
|
return $content['data']; |
76
|
|
|
endif; |
77
|
|
|
unlink($archive); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Delete the last cache on $key |
85
|
|
|
* |
86
|
|
|
* @param type $key |
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
public function delete(string $key) { |
90
|
|
|
$archive = $this->cacheFolder . "{$key}.{$this->ext}"; |
91
|
|
|
if (file_exists($archive)) { |
92
|
|
|
unlink($archive); |
93
|
|
|
} |
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns all saved cache variables |
99
|
|
|
* |
100
|
|
|
* @param string $key |
101
|
|
|
* @return boolean |
102
|
|
|
*/ |
103
|
|
|
public function cacheInfo(string $key) { |
104
|
|
|
$archive = $this->cacheFolder . "{$key}.{$this->ext}"; |
105
|
|
|
if (file_exists($archive)) { |
106
|
|
|
$content = json_decode(file_get_contents($archive), true); |
107
|
|
|
return $content; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
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