2121class FileHandler extends BaseHandler
2222{
2323 /**
24- * Prefixed to all cache names.
25- *
26- * @var string
24+ * Maximum key length.
2725 */
28- protected $ prefix ;
26+ public const MAX_KEY_LENGTH = 255 ;
2927
3028 /**
3129 * Where to store cached files on the disk.
@@ -94,8 +92,7 @@ public function initialize()
9492 */
9593 public function get (string $ key )
9694 {
97- $ key = $ this ->prefix . $ key ;
98-
95+ $ key = static ::validateKey ($ key , $ this ->prefix );
9996 $ data = $ this ->getItem ($ key );
10097
10198 return is_array ($ data ) ? $ data ['data ' ] : null ;
@@ -114,7 +111,7 @@ public function get(string $key)
114111 */
115112 public function save (string $ key , $ value , int $ ttl = 60 )
116113 {
117- $ key = $ this ->prefix . $ key ;
114+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
118115
119116 $ contents = [
120117 'time ' => time (),
@@ -152,7 +149,7 @@ public function save(string $key, $value, int $ttl = 60)
152149 */
153150 public function delete (string $ key )
154151 {
155- $ key = $ this ->prefix . $ key ;
152+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
156153
157154 return is_file ($ this ->path . $ key ) && unlink ($ this ->path . $ key );
158155 }
@@ -170,7 +167,7 @@ public function deleteMatching(string $pattern)
170167 {
171168 $ deleted = 0 ;
172169
173- foreach (glob ($ this ->path . $ pattern , GLOB_NOSORT ) as $ filename )
170+ foreach (glob ($ this ->path . $ pattern , GLOB_NOSORT ) as $ filename )
174171 {
175172 if (is_file ($ filename ) && @unlink ($ filename ))
176173 {
@@ -193,8 +190,7 @@ public function deleteMatching(string $pattern)
193190 */
194191 public function increment (string $ key , int $ offset = 1 )
195192 {
196- $ key = $ this ->prefix . $ key ;
197-
193+ $ key = static ::validateKey ($ key , $ this ->prefix );
198194 $ data = $ this ->getItem ($ key );
199195
200196 if ($ data === false )
@@ -222,12 +218,11 @@ public function increment(string $key, int $offset = 1)
222218 * @param string $key Cache ID
223219 * @param integer $offset Step/value to increase by
224220 *
225- * @return bool
221+ * @return boolean
226222 */
227223 public function decrement (string $ key , int $ offset = 1 )
228224 {
229- $ key = $ this ->prefix . $ key ;
230-
225+ $ key = static ::validateKey ($ key , $ this ->prefix );
231226 $ data = $ this ->getItem ($ key );
232227
233228 if ($ data === false )
@@ -288,7 +283,7 @@ public function getCacheInfo()
288283 */
289284 public function getMetaData (string $ key )
290285 {
291- $ key = $ this ->prefix . $ key ;
286+ $ key = static :: validateKey ( $ key , $ this ->prefix ) ;
292287
293288 if (! is_file ($ this ->path . $ key ))
294289 {
@@ -342,26 +337,26 @@ public function isSupported(): bool
342337 * Does the heavy lifting of actually retrieving the file and
343338 * verifying it's age.
344339 *
345- * @param string $key
340+ * @param string $filename
346341 *
347342 * @return boolean|mixed
348343 */
349- protected function getItem (string $ key )
344+ protected function getItem (string $ filename )
350345 {
351- if (! is_file ($ this ->path . $ key ))
346+ if (! is_file ($ this ->path . $ filename ))
352347 {
353348 return false ;
354349 }
355350
356- $ data = unserialize (file_get_contents ($ this ->path . $ key ));
351+ $ data = unserialize (file_get_contents ($ this ->path . $ filename ));
357352
358353 // @phpstan-ignore-next-line
359354 if ($ data ['ttl ' ] > 0 && time () > $ data ['time ' ] + $ data ['ttl ' ])
360355 {
361356 // If the file is still there then remove it
362- if (is_file ($ this ->path . $ key ))
357+ if (is_file ($ this ->path . $ filename ))
363358 {
364- unlink ($ this ->path . $ key );
359+ unlink ($ this ->path . $ filename );
365360 }
366361
367362 return false ;
0 commit comments