@@ -63,6 +63,8 @@ public function __construct(Cache $config)
63
63
64
64
$ this ->mode = $ config ->file ['mode ' ] ?? 0640 ;
65
65
$ this ->prefix = $ config ->prefix ;
66
+
67
+ helper ('filesystem ' );
66
68
}
67
69
68
70
/**
@@ -96,7 +98,7 @@ public function save(string $key, $value, int $ttl = 60)
96
98
'data ' => $ value ,
97
99
];
98
100
99
- if ($ this -> writeFile ($ this ->path . $ key , serialize ($ contents ))) {
101
+ if (write_file ($ this ->path . $ key , serialize ($ contents ))) {
100
102
try {
101
103
chmod ($ this ->path . $ key , $ this ->mode );
102
104
@@ -176,15 +178,15 @@ public function decrement(string $key, int $offset = 1)
176
178
*/
177
179
public function clean ()
178
180
{
179
- return $ this -> deleteFiles ($ this ->path , false , true );
181
+ return delete_files ($ this ->path , false , true );
180
182
}
181
183
182
184
/**
183
185
* {@inheritDoc}
184
186
*/
185
187
public function getCacheInfo ()
186
188
{
187
- return $ this -> getDirFileInfo ($ this ->path );
189
+ return get_dir_file_info ($ this ->path );
188
190
}
189
191
190
192
/**
@@ -251,6 +253,8 @@ protected function getItem(string $filename)
251
253
/**
252
254
* Writes a file to disk, or returns false if not successful.
253
255
*
256
+ * @deprecated Use `write_file()` instead.
257
+ *
254
258
* @param string $path
255
259
* @param string $data
256
260
* @param string $mode
@@ -259,22 +263,7 @@ protected function getItem(string $filename)
259
263
*/
260
264
protected function writeFile ($ path , $ data , $ mode = 'wb ' )
261
265
{
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 );
278
267
}
279
268
280
269
/**
@@ -283,33 +272,17 @@ protected function writeFile($path, $data, $mode = 'wb')
283
272
* If the second parameter is set to TRUE, any directories contained
284
273
* within the supplied base directory will be nuked as well.
285
274
*
275
+ * @deprecated Use `delete_files()` instead.
276
+ *
286
277
* @param string $path File path
287
278
* @param bool $delDir Whether to delete any directories found in the path
288
279
* @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 )
290
281
*/
291
282
protected function deleteFiles (string $ path , bool $ delDir = false , bool $ htdocs = false , int $ _level = 0 ): bool
292
283
{
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 );
313
286
}
314
287
315
288
/**
@@ -318,6 +291,8 @@ protected function deleteFiles(string $path, bool $delDir = false, bool $htdocs
318
291
*
319
292
* Any sub-folders contained within the specified path are read as well.
320
293
*
294
+ * @deprecated Use `get_dir_file_info()` instead.
295
+ *
321
296
* @param string $sourceDir Path to source
322
297
* @param bool $topLevelOnly Look only at the top level directory specified?
323
298
* @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
326
301
*/
327
302
protected function getDirFileInfo (string $ sourceDir , bool $ topLevelOnly = true , bool $ _recursion = false )
328
303
{
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 );
348
305
306
+ if ($ fp !== false ) {
349
307
closedir ($ fp );
350
308
351
- return $ _filedata ;
309
+ return get_dir_file_info ( $ sourceDir , $ topLevelOnly , $ _recursion ) ;
352
310
}
353
311
354
312
return false ;
@@ -360,59 +318,15 @@ protected function getDirFileInfo(string $sourceDir, bool $topLevelOnly = true,
360
318
* Options are: name, server_path, size, date, readable, writable, executable, fileperms
361
319
* Returns FALSE if the file cannot be found.
362
320
*
321
+ * @deprecated Use `get_file_info()` instead.
322
+ *
363
323
* @param string $file Path to file
364
324
* @param array|string $returnedValues Array or comma separated string of information returned
365
325
*
366
326
* @return array|false
367
327
*/
368
328
protected function getFileInfo (string $ file , $ returnedValues = ['name ' , 'server_path ' , 'size ' , 'date ' ])
369
329
{
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 ;
417
331
}
418
332
}
0 commit comments