1
1
<?php /** @noinspection UnknownInspectionInspection */
2
2
3
-
4
-
5
3
/** @noinspection TypeUnsafeComparisonInspection */
6
4
7
-
8
-
9
-
10
-
11
5
namespace eftec \bladeone ;
12
6
13
7
use Exception ;
44
38
* </code>
45
39
*
46
40
* @package BladeOneCache
47
- * @version 3.43 2024-03-02
41
+ * @version 3.43.1 2025-09-03
48
42
* @link https://github.com/EFTEC/BladeOne
49
43
* @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
50
44
*/
@@ -65,7 +59,7 @@ trait BladeOneCache
65
59
* @var string=['get','post','getpost','request',null][$i]
66
60
*/
67
61
private string $ cacheStrategy ;
68
- /** @var array|null */
62
+ /** @var array|null */
69
63
private ?array $ cacheStrategyIndex ;
70
64
71
65
/**
@@ -84,7 +78,7 @@ public function getCacheStrategy(): ?string
84
78
*/
85
79
public function setCacheLog ($ file ): void
86
80
{
87
- $ this ->cacheLog = $ file ;
81
+ $ this ->cacheLog = $ file ;
88
82
}
89
83
90
84
/**
@@ -100,22 +94,22 @@ public function writeCacheLog($txt, $nivel): void
100
94
$ txt = print_r ($ txt , true );
101
95
}
102
96
// Rewrite file if more than 100000 bytes
103
- $ mode= ($ fz > 100000 ) ? 'w ' : 'a ' ;
97
+ $ mode = ($ fz > 100000 ) ? 'w ' : 'a ' ;
104
98
$ fp = fopen ($ this ->cacheLog , $ mode );
105
99
if ($ fp === false ) {
106
100
return ;
107
101
}
108
102
switch ($ nivel ) {
109
103
case 1 :
110
- $ txtNivel= 'expired ' ;
104
+ $ txtNivel = 'expired ' ;
111
105
break ;
112
106
case 2 :
113
- $ txtNivel= 'new ' ;
107
+ $ txtNivel = 'new ' ;
114
108
break ;
115
109
default :
116
- $ txtNivel= 'other ' ;
110
+ $ txtNivel = 'other ' ;
117
111
}
118
- $ txtarg = json_encode ($ this ->cacheUniqueGUID (false ), JSON_THROW_ON_ERROR );
112
+ $ txtarg = json_encode ($ this ->cacheUniqueGUID (false ), JSON_THROW_ON_ERROR );
119
113
fwrite ($ fp , date ('c ' ) . "\t$ txt \t$ txtNivel \t$ txtarg \n" );
120
114
fclose ($ fp );
121
115
}
@@ -124,7 +118,7 @@ public function writeCacheLog($txt, $nivel): void
124
118
* It sets the strategy of the cache page.
125
119
*
126
120
* @param null|string $cacheStrategy =['get','post','getpost','request',null][$i]
127
- * @param array|null $index if null then it reads all indexes. If not, it reads an indexes.
121
+ * @param array|null $index if null then it reads all indexes. If not, it reads an indexes.
128
122
*/
129
123
public function setCacheStrategy ($ cacheStrategy , $ index = null ): void
130
124
{
@@ -164,17 +158,14 @@ private function cacheUniqueGUID($serialize = true): ?string
164
158
$ r = null ;
165
159
}
166
160
if ($ this ->cacheStrategyIndex === null || !is_array ($ r )) {
167
- $ r = serialize ($ r );
161
+ $ r = serialize ($ r );
168
162
} else {
169
- $ copy =[];
170
- foreach ($ r as $ key => $ item ) {
171
- if (in_array ($ key , $ this ->cacheStrategyIndex , true )) {
172
- $ copy [$ key ]=$ item ;
173
- }
174
- }
175
- $ r =serialize ($ copy );
163
+ $ copy = array_filter ($ r , function ($ key ) {
164
+ return in_array ($ key , $ this ->cacheStrategyIndex , true );
165
+ }, ARRAY_FILTER_USE_KEY );
166
+ $ r = serialize ($ copy );
176
167
}
177
- return $ serialize ===true ? md5 ($ r ): $ r ;
168
+ return $ serialize === true ? md5 ($ r ) : $ r ;
178
169
}
179
170
180
171
public function compileCache ($ expression ): string
@@ -185,7 +176,6 @@ public function compileCache($expression): string
185
176
// if the date is too old then re-save.
186
177
// else
187
178
// save for the first time.
188
-
189
179
return $ this ->phpTag . "echo \$this->cacheStart $ expression; if(! \$this->cacheRunning) { ?> " ;
190
180
}
191
181
@@ -223,12 +213,12 @@ private function getCompiledFileCache($view): string
223
213
public function runCache ($ view , $ variables = [], $ ttl = 86400 ): string
224
214
{
225
215
$ this ->cachePageRunning = true ;
226
- $ cacheStatus= $ this ->cachePageExpired ($ view , $ ttl );
227
- if ($ cacheStatus !==0 ) {
216
+ $ cacheStatus = $ this ->cachePageExpired ($ view , $ ttl );
217
+ if ($ cacheStatus !== 0 ) {
228
218
$ this ->writeCacheLog ($ view , $ cacheStatus );
229
219
$ this ->cacheStart ('_page_ ' , $ ttl );
230
220
$ content = $ this ->run ($ view , $ variables ); // if no cache, then it runs normally.
231
- $ this ->fileName = $ view ; // sometimes the filename is replaced (@ include), so we restore it
221
+ $ this ->fileName = $ view ; // sometimes the filename is replaced (using the tag include), so we restore it
232
222
$ this ->cacheEnd ($ content ); // and it stores as a cache paged.
233
223
} else {
234
224
$ content = $ this ->getFile ($ this ->getCompiledFileCache ($ view ));
@@ -240,9 +230,9 @@ public function runCache($view, $variables = [], $ttl = 86400): string
240
230
/**
241
231
* Returns true if the block cache expired (or doesn't exist), otherwise false.
242
232
*
243
- * @param string $templateName name of the template to use (such hello for template hello.blade.php)
244
- * @param string $id (id of cache, optional, if not id then it adds automatically a number)
245
- * @param int $cacheDuration (duration of the cache in seconds)
233
+ * @param string $templateName name of the template to use (such hello for template hello.blade.php)
234
+ * @param string $id (id of cache, optional, if not id then it adds automatically a number)
235
+ * @param int $cacheDuration (duration of the cache in seconds)
246
236
* @return int 0=cache exists, 1= cache expired, 2=not exists, string= the cache file (if any)
247
237
*/
248
238
public function cacheExpired ($ templateName , $ id , $ cacheDuration ): int
@@ -258,7 +248,7 @@ public function cacheExpired($templateName, $id, $cacheDuration): int
258
248
* It returns true if the whole page expired.
259
249
*
260
250
* @param string $templateName
261
- * @param int $cacheDuration is seconds.
251
+ * @param int $cacheDuration is seconds.
262
252
* @return int 0=cache exists, 1= cache expired, 2=not exists, string= the cache content (if any)
263
253
*/
264
254
public function cachePageExpired ($ templateName , $ cacheDuration ): int
@@ -274,7 +264,7 @@ public function cachePageExpired($templateName, $cacheDuration): int
274
264
* This method is used by cacheExpired() and cachePageExpired()
275
265
*
276
266
* @param string $compiledFile
277
- * @param int $cacheDuration is seconds.
267
+ * @param int $cacheDuration is seconds.
278
268
* @return int|mixed 0=cache exists, 1= cache expired, 2=not exists, string= the cache content (if any)
279
269
*/
280
270
private function cacheExpiredInt ($ compiledFile , $ cacheDuration )
@@ -307,8 +297,7 @@ public function cacheStart($id = '', $cacheDuration = 86400): void
307
297
} else {
308
298
$ compiledFile = $ this ->getCompiledFile () . '_cache ' . $ this ->curCacheId ;
309
299
}
310
-
311
- if ($ this ->cacheExpired ('' , $ id , $ cacheDuration ) !==0 ) {
300
+ if ($ this ->cacheExpired ('' , $ id , $ cacheDuration ) !== 0 ) {
312
301
$ this ->cacheRunning = false ;
313
302
} else {
314
303
$ this ->cacheRunning = true ;
0 commit comments