1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the m1\vars library |
5
|
|
|
* |
6
|
|
|
* (c) m1 <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @package m1/vars |
12
|
|
|
* @version 0.1.0 |
13
|
|
|
* @author Miles Croxford <[email protected]> |
14
|
|
|
* @copyright Copyright (c) Miles Croxford <[email protected]> |
15
|
|
|
* @license http://github.com/m1/vars/blob/master/LICENSE |
16
|
|
|
* @link http://github.com/m1/vars/blob/master/README.MD Documentation |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace M1\Vars\Cache; |
20
|
|
|
|
21
|
|
|
use M1\Vars\Vars; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The CacheProvider provides the cached file if one exists and is requested |
25
|
|
|
* |
26
|
|
|
* @since 0.1.0 |
27
|
|
|
*/ |
28
|
|
|
class CacheProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Has the cache been attempted |
32
|
|
|
* |
33
|
|
|
* @var bool $attempted |
34
|
|
|
*/ |
35
|
|
|
private $attempted = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* How long for the cache to be fresh |
39
|
|
|
* |
40
|
|
|
* @var int $expire |
41
|
|
|
*/ |
42
|
|
|
private $expire; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Has the cache been found |
46
|
|
|
* |
47
|
|
|
* @var bool $hit |
48
|
|
|
*/ |
49
|
|
|
private $hit = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The loaded Vars object from cache |
53
|
|
|
* |
54
|
|
|
* @var \M1\Vars\Vars $loaded_vars |
55
|
|
|
*/ |
56
|
|
|
private $loaded_vars; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The cache file name |
60
|
|
|
* |
61
|
|
|
* @var string $name |
62
|
|
|
*/ |
63
|
|
|
private $name; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The specific path for the cache folder |
67
|
|
|
* |
68
|
|
|
* @var string $path |
69
|
|
|
*/ |
70
|
|
|
private $path; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Is the cache turned on |
74
|
|
|
* |
75
|
|
|
* @var boolean $provide |
76
|
|
|
*/ |
77
|
|
|
private $provide; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* If cached, the time when the class was cached |
81
|
|
|
* |
82
|
|
|
* @var int $time |
83
|
|
|
*/ |
84
|
|
|
private $time; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Creates a new instance of the cacheProvider for Vars |
88
|
|
|
* |
89
|
|
|
* @param string|array $resource The main configuration resource |
90
|
|
|
* @param array $options The options being used for Vars |
91
|
|
|
*/ |
92
|
|
|
public function __construct($resource, $options) |
93
|
|
|
{ |
94
|
|
|
$this->setProvide($options['cache']); |
95
|
|
|
$this->setPath($options['cache_path']); |
96
|
|
|
|
97
|
|
|
$this->expire = $options['cache_expire']; |
98
|
|
|
$this->name = md5(serialize($resource)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Checks the cache to see if there is a valid cache available |
103
|
|
|
* |
104
|
|
|
* @return bool Returns true if has the cached resource |
105
|
|
|
*/ |
106
|
|
|
public function checkCache() |
107
|
|
|
{ |
108
|
|
|
if ($this->provide && $this->path && !$this->getAttempted()) { |
109
|
|
|
$file = sprintf('%s/%s.php', $this->path, $this->name); |
110
|
|
|
$this->attempted = true; |
111
|
|
|
|
112
|
|
|
if (is_file($file) && |
113
|
|
|
filemtime($file) >= (time() - $this->expire)) { |
114
|
|
|
$this->hit = true; |
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Load the cached file into $this->loaded_vars |
123
|
|
|
*/ |
124
|
|
|
public function load() |
125
|
|
|
{ |
126
|
|
|
$cached_file = sprintf('%s/%s.php', $this->path, $this->name); |
127
|
|
|
$this->loaded_vars = unserialize(file_get_contents($cached_file)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Transfer the contents of the parent Vars object into a file for cache |
132
|
|
|
* |
133
|
|
|
* @param \M1\Vars\Vars $provide Does the cache want to be on or off |
|
|
|
|
134
|
|
|
*/ |
135
|
|
|
public function makeCache(Vars $vars) |
136
|
|
|
{ |
137
|
|
|
if ($this->provide) { |
138
|
|
|
$cache_file = sprintf('%s/%s.php', $this->path, $this->name); |
139
|
|
|
file_put_contents($cache_file, serialize($vars)); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Returns if cache is on or off |
145
|
|
|
* |
146
|
|
|
* @return bool Is cache on or off |
147
|
|
|
*/ |
148
|
|
|
public function getProvide() |
149
|
|
|
{ |
150
|
|
|
return $this->provide; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Set the cache on or off |
155
|
|
|
* |
156
|
|
|
* @param bool $provide Does the cache want to be on or off |
157
|
|
|
* |
158
|
|
|
* @return \M1\Vars\Cache\CacheProvider |
159
|
|
|
*/ |
160
|
|
|
public function setProvide($provide) |
161
|
|
|
{ |
162
|
|
|
$this->provide = $provide; |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns if the cache has been attempted |
168
|
|
|
* |
169
|
|
|
* @return bool Has the cache been attempted |
170
|
|
|
*/ |
171
|
|
|
public function getAttempted() |
172
|
|
|
{ |
173
|
|
|
return $this->attempted; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns the cache path |
178
|
|
|
* |
179
|
|
|
* @return string The cache path |
180
|
|
|
*/ |
181
|
|
|
public function getPath() |
182
|
|
|
{ |
183
|
|
|
return $this->path; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sets the cache path |
188
|
|
|
* |
189
|
|
|
* @param string $cache_path The cache path to set |
|
|
|
|
190
|
|
|
* |
191
|
|
|
* @throws \InvalidArgumentException If the cache path does not exist or is not writable |
192
|
|
|
* |
193
|
|
|
* @return \M1\Vars\Cache\CacheProvider |
194
|
|
|
*/ |
195
|
|
View Code Duplication |
public function setPath($path) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
if (is_null($path)) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (!is_dir($path) || !is_writable($path)) { |
202
|
|
|
throw new \InvalidArgumentException(sprintf( |
203
|
|
|
"'%s' cache path does not exist or is not writable", |
204
|
|
|
$path |
205
|
|
|
)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$this->path = realpath($path); |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns how long the cache lasts for |
214
|
|
|
* |
215
|
|
|
* @return int Cache expire time |
216
|
|
|
*/ |
217
|
|
|
public function getExpire() |
218
|
|
|
{ |
219
|
|
|
return $this->expire; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Returns how long the cache lasts for |
224
|
|
|
* |
225
|
|
|
* @return int Cache expire time |
226
|
|
|
*/ |
227
|
|
|
public function isHit() |
228
|
|
|
{ |
229
|
|
|
return $this->hit; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Returns when the cache was made |
234
|
|
|
* |
235
|
|
|
* @return int Cache creation time |
236
|
|
|
*/ |
237
|
|
|
public function getTime() |
238
|
|
|
{ |
239
|
|
|
return $this->time; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Sets the time when the Vars was cached |
244
|
|
|
* |
245
|
|
|
* @param int $time Time when vars cached was created |
246
|
|
|
* |
247
|
|
|
* @return \M1\Vars\Cache\CacheProvider The cacheProvider object |
248
|
|
|
*/ |
249
|
|
|
public function setTime($time) |
250
|
|
|
{ |
251
|
|
|
$this->time = $time; |
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Returns the loaded Vars object |
257
|
|
|
* |
258
|
|
|
* @return \M1\Vars\Vars The loaded Vars object |
259
|
|
|
*/ |
260
|
|
|
public function getLoadedVars() |
261
|
|
|
{ |
262
|
|
|
return $this->loaded_vars; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.