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( |
37
|
|
|
'The autoloader try to add same content to ' . $absoluteFileName . ' but the file is not writable for the autoloader. Please fix it!', |
38
|
|
|
234627835 |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return GeneralUtility::writeFile($absoluteFileName, $content); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all base file names in the given directory with the given file extension |
47
|
|
|
* Check also if the directory exists. |
48
|
|
|
* |
49
|
|
|
* @param string $dirPath |
50
|
|
|
* @param string $fileExtension |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public static function getBaseFilesInDir($dirPath, $fileExtension) |
55
|
|
|
{ |
56
|
|
|
return self::getFileInformationInDir($dirPath, $fileExtension, PATHINFO_FILENAME); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get all base file names in the given directory with the given file extension |
61
|
|
|
* Check also if the directory exists. |
62
|
|
|
* |
63
|
|
|
* @param string $dirPath |
64
|
|
|
* @param string $fileExtension |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
public static function getBaseFilesWithExtensionInDir($dirPath, $fileExtension) |
69
|
|
|
{ |
70
|
|
|
return self::getFileInformationInDir($dirPath, $fileExtension, PATHINFO_BASENAME); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get all base file names in the given directory with the given file extension |
75
|
|
|
* Check also if the directory exists. If you scan the dir recursively you get |
76
|
|
|
* also the folder name. The filename is also "basename" only. |
77
|
|
|
* |
78
|
|
|
* @param string $dirPath |
79
|
|
|
* @param string $fileExtensions |
80
|
|
|
* @param bool $recursively |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public static function getBaseFilesRecursivelyInDir($dirPath, $fileExtensions, $recursively = true) |
85
|
|
|
{ |
86
|
|
|
if (!\is_dir($dirPath)) { |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
$recursively = $recursively ? 99 : 0; |
90
|
|
|
$files = GeneralUtility::getAllFilesAndFoldersInPath([], $dirPath, $fileExtensions, false, $recursively); |
91
|
|
|
foreach ($files as $key => $file) { |
92
|
|
|
$pathInfo = PathUtility::pathinfo($file); |
93
|
|
|
$files[$key] = $pathInfo['dirname'] . '/' . $pathInfo['filename']; |
94
|
|
|
} |
95
|
|
|
$files = GeneralUtility::removePrefixPathFromList($files, $dirPath); |
96
|
|
|
|
97
|
|
|
return \array_values($files); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get file information in the given folder. |
102
|
|
|
* |
103
|
|
|
* @param string $dirPath |
104
|
|
|
* @param string $fileExtension |
105
|
|
|
* @param int $informationType |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
protected static function getFileInformationInDir($dirPath, $fileExtension, $informationType) |
110
|
|
|
{ |
111
|
|
|
if (!\is_dir($dirPath)) { |
112
|
|
|
return []; |
113
|
|
|
} |
114
|
|
|
$files = GeneralUtility::getFilesInDir($dirPath, $fileExtension); |
115
|
|
|
foreach ($files as $key => $file) { |
|
|
|
|
116
|
|
|
$files[$key] = PathUtility::pathinfo($file, $informationType); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return \array_values($files); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.