-
-
Notifications
You must be signed in to change notification settings - Fork 708
/
Copy pathFilesFinder.php
193 lines (158 loc) · 5.16 KB
/
FilesFinder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
declare(strict_types=1);
namespace Rector\Core\FileSystem;
use Nette\Utils\Strings;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symplify\PackageBuilder\FileSystem\FileSystem;
use Symplify\SmartFileSystem\Finder\FinderSanitizer;
use Symplify\SmartFileSystem\SmartFileInfo;
/**
* @see \Rector\Core\Tests\FileSystem\FilesFinder\FilesFinderTest
*/
final class FilesFinder
{
/**
* @var SmartFileInfo[][]
*/
private $fileInfosBySourceAndSuffixes = [];
/**
* @var string[]
*/
private $excludePaths = [];
/**
* @var FilesystemTweaker
*/
private $filesystemTweaker;
/**
* @var FinderSanitizer
*/
private $finderSanitizer;
/**
* @var FileSystem
*/
private $fileSystem;
/**
* @param string[] $excludePaths
*/
public function __construct(
array $excludePaths,
FilesystemTweaker $filesystemTweaker,
FinderSanitizer $finderSanitizer,
FileSystem $fileSystem
) {
$this->excludePaths = $excludePaths;
$this->filesystemTweaker = $filesystemTweaker;
$this->finderSanitizer = $finderSanitizer;
$this->fileSystem = $fileSystem;
}
/**
* @param string[] $source
* @param string[] $suffixes
* @return SmartFileInfo[]
*/
public function findInDirectoriesAndFiles(array $source, array $suffixes, bool $matchDiff = false): array
{
$cacheKey = md5(serialize($source) . serialize($suffixes) . (int) $matchDiff);
if (isset($this->fileInfosBySourceAndSuffixes[$cacheKey])) {
return $this->fileInfosBySourceAndSuffixes[$cacheKey];
}
[$files, $directories] = $this->fileSystem->separateFilesAndDirectories($source);
$smartFileInfos = [];
foreach ($files as $file) {
$smartFileInfos[] = new SmartFileInfo($file);
}
$smartFileInfos = array_merge($smartFileInfos, $this->findInDirectories($directories, $suffixes));
if ($matchDiff) {
$gitDiffFiles = $this->getGitDiff();
$smartFileInfos = array_filter($smartFileInfos, function (SmartFileInfo $fileInfo) use (
$gitDiffFiles
): bool {
return in_array($fileInfo->getRealPath(), $gitDiffFiles, true);
});
$smartFileInfos = array_values($smartFileInfos);
}
return $this->fileInfosBySourceAndSuffixes[$cacheKey] = $smartFileInfos;
}
/**
* @param string[] $directories
* @param string[] $suffixes
* @return SmartFileInfo[]
*/
private function findInDirectories(array $directories, array $suffixes): array
{
if (count($directories) === 0) {
return [];
}
$absoluteDirectories = $this->filesystemTweaker->resolveDirectoriesWithFnmatch($directories);
if ($absoluteDirectories === []) {
return [];
}
$suffixesPattern = $this->normalizeSuffixesToPattern($suffixes);
$finder = Finder::create()
->files()
->in($absoluteDirectories)
->name($suffixesPattern)
->sortByName();
$this->addFilterWithExcludedPaths($finder);
return $this->finderSanitizer->sanitize($finder);
}
/**
* @return string[] The absolute path to the file matching the git diff shell command.
*/
private function getGitDiff(): array
{
$plainDiff = shell_exec('git diff --name-only') ?: '';
$relativePaths = explode(PHP_EOL, trim($plainDiff));
return array_values(array_filter(array_map('realpath', $relativePaths)));
}
/**
* @param string[] $suffixes
*/
private function normalizeSuffixesToPattern(array $suffixes): string
{
$suffixesPattern = implode('|', $suffixes);
return '#\.(' . $suffixesPattern . ')$#';
}
private function addFilterWithExcludedPaths(Finder $finder): void
{
if ($this->excludePaths === []) {
return;
}
$finder->filter(function (SplFileInfo $splFileInfo): bool {
/** @var string|false $realPath */
$realPath = $splFileInfo->getRealPath();
if (! $realPath) {
//dead symlink
return false;
}
// return false to remove file
foreach ($this->excludePaths as $excludePath) {
if (Strings::match($realPath, '#' . preg_quote($excludePath, '#') . '#')) {
return false;
}
$excludePath = $this->normalizeForFnmatch($excludePath);
if (fnmatch($excludePath, $realPath)) {
return false;
}
}
return true;
});
}
/**
* "value*" → "*value*"
* "*value" → "*value*"
*/
private function normalizeForFnmatch(string $path): string
{
// ends with *
if (Strings::match($path, '#^[^*](.*?)\*$#')) {
return '*' . $path;
}
// starts with *
if (Strings::match($path, '#^\*(.*?)[^*]$#')) {
return $path . '*';
}
return $path;
}
}