11<?php /** @noinspection UnknownInspectionInspection */
22
3-
4-
53/** @noinspection TypeUnsafeComparisonInspection */
64
7-
8-
9-
10-
115namespace eftec \bladeone ;
126
137use Exception ;
4438 * </code>
4539 *
4640 * @package BladeOneCache
47- * @version 3.43 2024-03-02
41+ * @version 3.43.1 2025-09-03
4842 * @link https://github.com/EFTEC/BladeOne
4943 * @author Jorge Patricio Castro Castillo <jcastro arroba eftec dot cl>
5044 */
@@ -65,7 +59,7 @@ trait BladeOneCache
6559 * @var string=['get','post','getpost','request',null][$i]
6660 */
6761 private string $ cacheStrategy ;
68- /** @var array|null */
62+ /** @var array|null */
6963 private ?array $ cacheStrategyIndex ;
7064
7165 /**
@@ -84,7 +78,7 @@ public function getCacheStrategy(): ?string
8478 */
8579 public function setCacheLog ($ file ): void
8680 {
87- $ this ->cacheLog = $ file ;
81+ $ this ->cacheLog = $ file ;
8882 }
8983
9084 /**
@@ -100,22 +94,22 @@ public function writeCacheLog($txt, $nivel): void
10094 $ txt = print_r ($ txt , true );
10195 }
10296 // Rewrite file if more than 100000 bytes
103- $ mode= ($ fz > 100000 ) ? 'w ' : 'a ' ;
97+ $ mode = ($ fz > 100000 ) ? 'w ' : 'a ' ;
10498 $ fp = fopen ($ this ->cacheLog , $ mode );
10599 if ($ fp === false ) {
106100 return ;
107101 }
108102 switch ($ nivel ) {
109103 case 1 :
110- $ txtNivel= 'expired ' ;
104+ $ txtNivel = 'expired ' ;
111105 break ;
112106 case 2 :
113- $ txtNivel= 'new ' ;
107+ $ txtNivel = 'new ' ;
114108 break ;
115109 default :
116- $ txtNivel= 'other ' ;
110+ $ txtNivel = 'other ' ;
117111 }
118- $ txtarg = json_encode ($ this ->cacheUniqueGUID (false ), JSON_THROW_ON_ERROR );
112+ $ txtarg = json_encode ($ this ->cacheUniqueGUID (false ), JSON_THROW_ON_ERROR );
119113 fwrite ($ fp , date ('c ' ) . "\t$ txt \t$ txtNivel \t$ txtarg \n" );
120114 fclose ($ fp );
121115 }
@@ -124,7 +118,7 @@ public function writeCacheLog($txt, $nivel): void
124118 * It sets the strategy of the cache page.
125119 *
126120 * @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.
128122 */
129123 public function setCacheStrategy ($ cacheStrategy , $ index = null ): void
130124 {
@@ -164,17 +158,14 @@ private function cacheUniqueGUID($serialize = true): ?string
164158 $ r = null ;
165159 }
166160 if ($ this ->cacheStrategyIndex === null || !is_array ($ r )) {
167- $ r = serialize ($ r );
161+ $ r = serialize ($ r );
168162 } 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 );
176167 }
177- return $ serialize ===true ? md5 ($ r ): $ r ;
168+ return $ serialize === true ? md5 ($ r ) : $ r ;
178169 }
179170
180171 public function compileCache ($ expression ): string
@@ -185,7 +176,6 @@ public function compileCache($expression): string
185176 // if the date is too old then re-save.
186177 // else
187178 // save for the first time.
188-
189179 return $ this ->phpTag . "echo \$this->cacheStart $ expression; if(! \$this->cacheRunning) { ?> " ;
190180 }
191181
@@ -223,12 +213,12 @@ private function getCompiledFileCache($view): string
223213 public function runCache ($ view , $ variables = [], $ ttl = 86400 ): string
224214 {
225215 $ this ->cachePageRunning = true ;
226- $ cacheStatus= $ this ->cachePageExpired ($ view , $ ttl );
227- if ($ cacheStatus !==0 ) {
216+ $ cacheStatus = $ this ->cachePageExpired ($ view , $ ttl );
217+ if ($ cacheStatus !== 0 ) {
228218 $ this ->writeCacheLog ($ view , $ cacheStatus );
229219 $ this ->cacheStart ('_page_ ' , $ ttl );
230220 $ 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
232222 $ this ->cacheEnd ($ content ); // and it stores as a cache paged.
233223 } else {
234224 $ content = $ this ->getFile ($ this ->getCompiledFileCache ($ view ));
@@ -240,9 +230,9 @@ public function runCache($view, $variables = [], $ttl = 86400): string
240230 /**
241231 * Returns true if the block cache expired (or doesn't exist), otherwise false.
242232 *
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)
246236 * @return int 0=cache exists, 1= cache expired, 2=not exists, string= the cache file (if any)
247237 */
248238 public function cacheExpired ($ templateName , $ id , $ cacheDuration ): int
@@ -258,7 +248,7 @@ public function cacheExpired($templateName, $id, $cacheDuration): int
258248 * It returns true if the whole page expired.
259249 *
260250 * @param string $templateName
261- * @param int $cacheDuration is seconds.
251+ * @param int $cacheDuration is seconds.
262252 * @return int 0=cache exists, 1= cache expired, 2=not exists, string= the cache content (if any)
263253 */
264254 public function cachePageExpired ($ templateName , $ cacheDuration ): int
@@ -274,7 +264,7 @@ public function cachePageExpired($templateName, $cacheDuration): int
274264 * This method is used by cacheExpired() and cachePageExpired()
275265 *
276266 * @param string $compiledFile
277- * @param int $cacheDuration is seconds.
267+ * @param int $cacheDuration is seconds.
278268 * @return int|mixed 0=cache exists, 1= cache expired, 2=not exists, string= the cache content (if any)
279269 */
280270 private function cacheExpiredInt ($ compiledFile , $ cacheDuration )
@@ -307,8 +297,7 @@ public function cacheStart($id = '', $cacheDuration = 86400): void
307297 } else {
308298 $ compiledFile = $ this ->getCompiledFile () . '_cache ' . $ this ->curCacheId ;
309299 }
310-
311- if ($ this ->cacheExpired ('' , $ id , $ cacheDuration ) !==0 ) {
300+ if ($ this ->cacheExpired ('' , $ id , $ cacheDuration ) !== 0 ) {
312301 $ this ->cacheRunning = false ;
313302 } else {
314303 $ this ->cacheRunning = true ;
0 commit comments