Skip to content

Commit 84569d6

Browse files
committed
refactor: deprecate redundant FileHandler cache methods
1 parent 73718f4 commit 84569d6

File tree

6 files changed

+35
-127
lines changed

6 files changed

+35
-127
lines changed

system/Cache/Handlers/FileHandler.php

Lines changed: 21 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

system/Helpers/filesystem_helper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,12 @@ function write_file(string $path, string $data, string $mode = 'wb'): bool
123123

124124
flock($fp, LOCK_EX);
125125

126-
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result) {
127-
if (($result = fwrite($fp, substr($data, $written))) === false) {
126+
$result = 0;
127+
128+
for ($written = 0, $length = strlen($data); $written < $length; $written += $result) {
129+
$result = fwrite($fp, substr($data, $written));
130+
131+
if ($result === false) {
128132
break;
129133
}
130134
}

user_guide_src/source/changelogs/v4.6.1.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ Changes
2626
Deprecations
2727
************
2828

29+
- **Cache:** The ``FileHandler::writeFile()`` method is deprecated. Use ``write_file()`` instead.
30+
- **Cache:** The ``FileHandler::deleteFiles()`` method is deprecated. Use ``delete_files()`` instead.
31+
- **Cache:** The ``FileHandler::getDirFileInfo()`` method is deprecated. Use ``get_dir_file_info()`` instead.
32+
- **Cache:** The ``FileHandler::getFileInfo()`` method is deprecated. Use ``get_file_info()`` instead.
33+
2934
**********
3035
Bugs Fixed
3136
**********

utils/phpstan-baseline/loader.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 3760 errors
1+
# total 3757 errors
22
includes:
33
- argument.type.neon
44
- assign.propertyType.neon

utils/phpstan-baseline/ternary.shortNotAllowed.neon

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# total 37 errors
1+
# total 36 errors
22

33
parameters:
44
ignoreErrors:
@@ -7,11 +7,6 @@ parameters:
77
count: 2
88
path: ../../system/CLI/CLI.php
99

10-
-
11-
message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#'
12-
count: 1
13-
path: ../../system/Cache/Handlers/FileHandler.php
14-
1510
-
1611
message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#'
1712
count: 1

utils/phpstan-baseline/variable.undefined.neon

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,7 @@
1-
# total 21 errors
1+
# total 19 errors
22

33
parameters:
44
ignoreErrors:
5-
-
6-
message: '#^Variable \$result might not be defined\.$#'
7-
count: 1
8-
path: ../../system/Cache/Handlers/FileHandler.php
9-
10-
-
11-
message: '#^Variable \$result might not be defined\.$#'
12-
count: 1
13-
path: ../../system/Helpers/filesystem_helper.php
14-
155
-
166
message: '#^Variable \$written might not be defined\.$#'
177
count: 1

0 commit comments

Comments
 (0)