|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Helper; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Helper for common file system functions. |
|
7
|
|
|
*/ |
|
8
|
|
|
final class FileSystem |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param string $userProvidedPath |
|
12
|
|
|
* @return string|null |
|
13
|
|
|
*/ |
|
14
|
|
|
public static function getRealPathToReadableAndWritableFile($userProvidedPath) |
|
15
|
|
|
{ |
|
16
|
|
|
$realPath = realpath(dirname($userProvidedPath)) . '/' . basename($userProvidedPath); |
|
17
|
|
|
return is_file($realPath) && is_readable($realPath) && is_writable($realPath) ? $realPath : null; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $path |
|
22
|
|
|
* @return string[] |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function readFileIntoArray($path) |
|
25
|
|
|
{ |
|
26
|
|
|
$array = file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
27
|
|
|
if ($array === false) { |
|
28
|
|
|
throw new \RuntimeException($path . ' could not be read.'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return $array; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param string[] $lines |
|
36
|
|
|
* @param string $path |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function writeArrayToFile(array $lines, $path) |
|
39
|
|
|
{ |
|
40
|
|
|
$handle = fopen($path, 'wb'); |
|
41
|
|
|
fwrite($handle, implode(PHP_EOL, $lines)); |
|
42
|
|
|
fclose($handle); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string|null $userProvidedPathToBlacklist |
|
47
|
|
|
* @return string[] |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getBlacklistingRegExps($userProvidedPathToBlacklist = null) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($userProvidedPathToBlacklist === null) { |
|
52
|
|
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!is_file($userProvidedPathToBlacklist) || !is_readable($userProvidedPathToBlacklist)) { |
|
56
|
|
|
throw new \InvalidArgumentException($userProvidedPathToBlacklist . ' is no readable file'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return self::readFileIntoArray($userProvidedPathToBlacklist); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param \SplFileInfo[] $foundFilesInfos |
|
64
|
|
|
* @param string[] $blacklistRegExps |
|
65
|
|
|
* @return string[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function filterFilesIn(array $foundFilesInfos, array $blacklistRegExps) |
|
68
|
|
|
{ |
|
69
|
|
|
$filteredFiles = []; |
|
70
|
|
|
|
|
71
|
|
|
/** @var \Iterator $foundFilesInfos */ |
|
72
|
|
|
foreach ($foundFilesInfos as $foundFileInfo) { |
|
73
|
|
|
/** @var $foundFileInfo \SplFileInfo */ |
|
74
|
|
|
foreach ($blacklistRegExps as $blacklistRegExp) { |
|
75
|
|
|
if (preg_match($blacklistRegExp, $foundFileInfo->getRealPath()) === 1) { |
|
76
|
|
|
continue 2; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$filteredFiles[] = $foundFileInfo->getRealPath(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $filteredFiles; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string|null $userProvidedPathToOutput |
|
88
|
|
|
* @param string $fallbackPath |
|
89
|
|
|
* @param string $fallbackFileName |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
public static function getPathToOutput($userProvidedPathToOutput = null, $fallbackPath, $fallbackFileName) |
|
93
|
|
|
{ |
|
94
|
|
|
$pathToOutput = ($userProvidedPathToOutput !== null) |
|
95
|
|
|
? realpath(dirname($userProvidedPathToOutput)) . '/' . basename($userProvidedPathToOutput) |
|
96
|
|
|
: realpath(dirname($fallbackPath)) . '/' . $fallbackFileName; |
|
97
|
|
|
|
|
98
|
|
|
if (is_file($pathToOutput) && !is_writable($pathToOutput)) { |
|
99
|
|
|
throw new \InvalidArgumentException('Output file ' . $pathToOutput . ' is not writable'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!is_writable(dirname($pathToOutput))) { |
|
103
|
|
|
throw new \InvalidArgumentException('Output path ' . dirname($pathToOutput) . ' is not writable'); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $pathToOutput; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|