@@ -63,6 +63,8 @@ public function __construct(Cache $config)
6363
6464 $ this ->mode = $ config ->file ['mode ' ] ?? 0640 ;
6565 $ this ->prefix = $ config ->prefix ;
66+
67+ helper ('filesystem ' );
6668 }
6769
6870 /**
@@ -96,7 +98,7 @@ public function save(string $key, $value, int $ttl = 60)
9698 'data ' => $ value ,
9799 ];
98100
99- if ($ this -> writeFile ($ this ->path . $ key , serialize ($ contents ))) {
101+ if (write_file ($ this ->path . $ key , serialize ($ contents ))) {
100102 try {
101103 chmod ($ this ->path . $ key , $ this ->mode );
102104
@@ -176,15 +178,15 @@ public function decrement(string $key, int $offset = 1)
176178 */
177179 public function clean ()
178180 {
179- return $ this -> deleteFiles ($ this ->path , false , true );
181+ return delete_files ($ this ->path , false , true );
180182 }
181183
182184 /**
183185 * {@inheritDoc}
184186 */
185187 public function getCacheInfo ()
186188 {
187- return $ this -> getDirFileInfo ($ this ->path );
189+ return get_dir_file_info ($ this ->path );
188190 }
189191
190192 /**
@@ -251,6 +253,8 @@ protected function getItem(string $filename)
251253 /**
252254 * Writes a file to disk, or returns false if not successful.
253255 *
256+ * @deprecated 4.6.1 Use `write_file()` instead.
257+ *
254258 * @param string $path
255259 * @param string $data
256260 * @param string $mode
@@ -259,22 +263,7 @@ protected function getItem(string $filename)
259263 */
260264 protected function writeFile ($ path , $ data , $ mode = 'wb ' )
261265 {
262- if (($ fp = @fopen ($ path , $ mode )) === false ) {
263- return false ;
264- }
265-
266- flock ($ fp , LOCK_EX );
267-
268- for ($ result = $ written = 0 , $ length = strlen ($ data ); $ written < $ length ; $ written += $ result ) {
269- if (($ result = fwrite ($ fp , substr ($ data , $ written ))) === false ) {
270- break ;
271- }
272- }
273-
274- flock ($ fp , LOCK_UN );
275- fclose ($ fp );
276-
277- return is_int ($ result );
266+ return write_file ($ path , $ data , $ mode );
278267 }
279268
280269 /**
@@ -283,33 +272,17 @@ protected function writeFile($path, $data, $mode = 'wb')
283272 * If the second parameter is set to TRUE, any directories contained
284273 * within the supplied base directory will be nuked as well.
285274 *
275+ * @deprecated 4.6.1 Use `delete_files()` instead.
276+ *
286277 * @param string $path File path
287278 * @param bool $delDir Whether to delete any directories found in the path
288279 * @param bool $htdocs Whether to skip deleting .htaccess and index page files
289- * @param int $_level Current directory depth level (default: 0; internal use only)
280+ * @param int $_level Current directory depth level (default: 0; internal use only; unused )
290281 */
291282 protected function deleteFiles (string $ path , bool $ delDir = false , bool $ htdocs = false , int $ _level = 0 ): bool
292283 {
293- // Trim the trailing slash
294- $ path = rtrim ($ path , '/ \\' );
295-
296- if (! $ currentDir = @opendir ($ path )) {
297- return false ;
298- }
299-
300- while (false !== ($ filename = @readdir ($ currentDir ))) {
301- if ($ filename !== '. ' && $ filename !== '.. ' ) {
302- if (is_dir ($ path . DIRECTORY_SEPARATOR . $ filename ) && $ filename [0 ] !== '. ' ) {
303- $ this ->deleteFiles ($ path . DIRECTORY_SEPARATOR . $ filename , $ delDir , $ htdocs , $ _level + 1 );
304- } elseif (! $ htdocs || preg_match ('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i ' , $ filename ) !== 1 ) {
305- @unlink ($ path . DIRECTORY_SEPARATOR . $ filename );
306- }
307- }
308- }
309-
310- closedir ($ currentDir );
311-
312- return ($ delDir && $ _level > 0 ) ? @rmdir ($ path ) : true ;
284+ // $hidden is set to default false as original implementation skips hidden files
285+ return delete_files ($ path , $ delDir , $ htdocs );
313286 }
314287
315288 /**
@@ -318,6 +291,8 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
318291 *
319292 * Any sub-folders contained within the specified path are read as well.
320293 *
294+ * @deprecated 4.6.1 Use `get_dir_file_info()` instead.
295+ *
321296 * @param string $sourceDir Path to source
322297 * @param bool $topLevelOnly Look only at the top level directory specified?
323298 * @param bool $_recursion Internal variable to determine recursion status - do not use in calls
@@ -326,29 +301,12 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
326301 */
327302 protected function getDirFileInfo (string $ sourceDir , bool $ topLevelOnly = true , bool $ _recursion = false )
328303 {
329- static $ _filedata = [];
330- $ relativePath = $ sourceDir ;
331-
332- if ($ fp = @opendir ($ sourceDir )) {
333- // reset the array and make sure $sourceDir has a trailing slash on the initial call
334- if ($ _recursion === false ) {
335- $ _filedata = [];
336- $ sourceDir = rtrim (realpath ($ sourceDir ) ?: $ sourceDir , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ;
337- }
338-
339- // Used to be foreach (scandir($sourceDir, 1) as $file), but scandir() is simply not as fast
340- while (false !== ($ file = readdir ($ fp ))) {
341- if (is_dir ($ sourceDir . $ file ) && $ file [0 ] !== '. ' && $ topLevelOnly === false ) {
342- $ this ->getDirFileInfo ($ sourceDir . $ file . DIRECTORY_SEPARATOR , $ topLevelOnly , true );
343- } elseif (! is_dir ($ sourceDir . $ file ) && $ file [0 ] !== '. ' ) {
344- $ _filedata [$ file ] = $ this ->getFileInfo ($ sourceDir . $ file );
345- $ _filedata [$ file ]['relative_path ' ] = $ relativePath ;
346- }
347- }
304+ $ fp = @opendir ($ sourceDir );
348305
306+ if ($ fp !== false ) {
349307 closedir ($ fp );
350308
351- return $ _filedata ;
309+ return get_dir_file_info ( $ sourceDir , $ topLevelOnly , $ _recursion ) ;
352310 }
353311
354312 return false ;
@@ -360,59 +318,15 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
360318 * Options are: name, server_path, size, date, readable, writable, executable, fileperms
361319 * Returns FALSE if the file cannot be found.
362320 *
321+ * @deprecated 4.6.1 Use `get_file_info()` instead.
322+ *
363323 * @param string $file Path to file
364324 * @param array|string $returnedValues Array or comma separated string of information returned
365325 *
366326 * @return array|false
367327 */
368328 protected function getFileInfo (string $ file , $ returnedValues = ['name ' , 'server_path ' , 'size ' , 'date ' ])
369329 {
370- if (! is_file ($ file )) {
371- return false ;
372- }
373-
374- if (is_string ($ returnedValues )) {
375- $ returnedValues = explode (', ' , $ returnedValues );
376- }
377-
378- $ fileInfo = [];
379-
380- foreach ($ returnedValues as $ key ) {
381- switch ($ key ) {
382- case 'name ' :
383- $ fileInfo ['name ' ] = basename ($ file );
384- break ;
385-
386- case 'server_path ' :
387- $ fileInfo ['server_path ' ] = $ file ;
388- break ;
389-
390- case 'size ' :
391- $ fileInfo ['size ' ] = filesize ($ file );
392- break ;
393-
394- case 'date ' :
395- $ fileInfo ['date ' ] = filemtime ($ file );
396- break ;
397-
398- case 'readable ' :
399- $ fileInfo ['readable ' ] = is_readable ($ file );
400- break ;
401-
402- case 'writable ' :
403- $ fileInfo ['writable ' ] = is_writable ($ file );
404- break ;
405-
406- case 'executable ' :
407- $ fileInfo ['executable ' ] = is_executable ($ file );
408- break ;
409-
410- case 'fileperms ' :
411- $ fileInfo ['fileperms ' ] = fileperms ($ file );
412- break ;
413- }
414- }
415-
416- return $ fileInfo ;
330+ return get_file_info ($ file , $ returnedValues ) ?? false ;
417331 }
418332}
0 commit comments