|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* FileUtility. |
|
5
|
|
|
*/ |
|
6
|
|
|
declare(strict_types = 1); |
|
7
|
|
|
|
|
8
|
|
|
namespace HDNET\Autoloader\Utility; |
|
9
|
|
|
|
|
10
|
|
|
use HDNET\Autoloader\Exception; |
|
11
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
12
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* FileUtility. |
|
16
|
|
|
*/ |
|
17
|
|
|
class FileUtility |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Write a file and create the target folder, if the folder do not exists. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $absoluteFileName |
|
23
|
|
|
* @param string $content |
|
24
|
|
|
* |
|
25
|
|
|
* @throws Exception |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
|
|
public static function writeFileAndCreateFolder($absoluteFileName, $content) |
|
30
|
|
|
{ |
|
31
|
|
|
$dir = PathUtility::dirname($absoluteFileName) . '/'; |
|
32
|
|
|
if (!is_dir($dir)) { |
|
33
|
|
|
GeneralUtility::mkdir_deep($dir); |
|
34
|
|
|
} |
|
35
|
|
|
if (is_file($absoluteFileName) && !is_writable($absoluteFileName)) { |
|
36
|
|
|
throw new Exception('The autoloader try to add same content to ' . $absoluteFileName . ' but the file is not writable for the autoloader. Please fix it!', 234627835); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return GeneralUtility::writeFile($absoluteFileName, $content); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get all base file names in the given directory with the given file extension |
|
44
|
|
|
* Check also if the directory exists. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $dirPath |
|
47
|
|
|
* @param string $fileExtension |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getBaseFilesInDir($dirPath, $fileExtension) |
|
52
|
|
|
{ |
|
53
|
|
|
return self::getFileInformationInDir($dirPath, $fileExtension, PATHINFO_FILENAME); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get all base file names in the given directory with the given file extension |
|
58
|
|
|
* Check also if the directory exists. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $dirPath |
|
61
|
|
|
* @param string $fileExtension |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function getBaseFilesWithExtensionInDir($dirPath, $fileExtension) |
|
66
|
|
|
{ |
|
67
|
|
|
return self::getFileInformationInDir($dirPath, $fileExtension, PATHINFO_BASENAME); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get all base file names in the given directory with the given file extension |
|
72
|
|
|
* Check also if the directory exists. If you scan the dir recursively you get |
|
73
|
|
|
* also the folder name. The filename is also "basename" only. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $dirPath |
|
76
|
|
|
* @param string $fileExtensions |
|
77
|
|
|
* @param bool $recursively |
|
78
|
|
|
* |
|
79
|
|
|
* @return array |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function getBaseFilesRecursivelyInDir($dirPath, $fileExtensions, $recursively = true) |
|
82
|
|
|
{ |
|
83
|
|
|
if (!is_dir($dirPath)) { |
|
84
|
|
|
return []; |
|
85
|
|
|
} |
|
86
|
|
|
$recursively = $recursively ? 99 : 0; |
|
87
|
|
|
$files = GeneralUtility::getAllFilesAndFoldersInPath([], $dirPath, $fileExtensions, false, $recursively); |
|
88
|
|
|
foreach ($files as $key => $file) { |
|
89
|
|
|
$pathInfo = PathUtility::pathinfo($file); |
|
90
|
|
|
$files[$key] = $pathInfo['dirname'] . '/' . $pathInfo['filename']; |
|
91
|
|
|
} |
|
92
|
|
|
$files = GeneralUtility::removePrefixPathFromList($files, $dirPath); |
|
93
|
|
|
|
|
94
|
|
|
return array_values($files); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get file information in the given folder. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $dirPath |
|
101
|
|
|
* @param string $fileExtension |
|
102
|
|
|
* @param int $informationType |
|
103
|
|
|
* |
|
104
|
|
|
* @return array |
|
105
|
|
|
*/ |
|
106
|
|
|
protected static function getFileInformationInDir($dirPath, $fileExtension, $informationType) |
|
107
|
|
|
{ |
|
108
|
|
|
if (!is_dir($dirPath)) { |
|
109
|
|
|
return []; |
|
110
|
|
|
} |
|
111
|
|
|
$files = GeneralUtility::getFilesInDir($dirPath, $fileExtension); |
|
112
|
|
|
foreach ($files as $key => $file) { |
|
|
|
|
|
|
113
|
|
|
$files[$key] = PathUtility::pathinfo($file, $informationType); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return array_values($files); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.