Skip to content

Commit 1f7dbe5

Browse files
authored
Moved globbing to FileSystemUtils implementing the GlobIterator
1 parent aebcb06 commit 1f7dbe5

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/Patch/SourceLoaders/PatchesSearch.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,13 @@ public function load(\Composer\Package\PackageInterface $package, $source)
102102
$basePath = $this->getInstallPath($package);
103103
$results = array();
104104

105-
foreach ($source as $sourceGlob) {
106-
foreach (glob($sourceGlob) as $item) {
107-
$paths = $this->fileSystemUtils->collectFilePathsRecursively(
108-
$basePath . DIRECTORY_SEPARATOR . $item,
109-
sprintf('/%s/i', PluginConfig::PATCH_FILE_REGEX_MATCHER)
110-
);
105+
foreach ($source as $item) {
106+
$paths = $this->fileSystemUtils->collectFilePathsRecursively(
107+
$basePath . DIRECTORY_SEPARATOR . $item,
108+
sprintf('/%s/i', PluginConfig::PATCH_FILE_REGEX_MATCHER)
109+
);
111110

112-
$results[] = $this->createPatchDefinitions($basePath, $paths);
113-
}
111+
$results[] = $this->createPatchDefinitions($basePath, $paths);
114112
}
115113

116114
return $results;

src/Utils/FileSystemUtils.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,47 @@ class FileSystemUtils
99
{
1010
public function collectFilePathsRecursively($rootPath, $pattern)
1111
{
12-
$paths = $this->collectPathsRecursively($rootPath, $pattern);
12+
if (strpos($rootPath, '*') !== false || strpos($rootPath, '?') !== false) {
13+
$paths = $this->collectGlobs($rootPath, $pattern);
14+
} else {
15+
$paths = $this->collectPathsRecursively($rootPath, $pattern);
16+
}
1317

1418
return array_filter($paths, function ($item) {
1519
return is_file($item);
1620
});
1721
}
1822

23+
public function collectGlobs($rootPath, $pattern)
24+
{
25+
$iterator = new \GlobIterator($rootPath);
26+
27+
$files = array();
28+
29+
foreach ($iterator as $info) {
30+
if($info->isDir()) {
31+
$files = [...$files, ...$this->collectPathsRecursively($info->getRealPath(), $pattern)];
32+
continue;
33+
}
34+
35+
if (!$info->isFile() || !preg_match($pattern, $info->getRealPath())) {
36+
continue;
37+
}
38+
39+
$path = $info->getRealPath();
40+
$files[$path] = $path;
41+
}
42+
43+
$sequence = array_keys($files);
44+
45+
natsort($sequence);
46+
47+
return array_replace(
48+
array_flip($sequence),
49+
$files
50+
);
51+
}
52+
1953
public function collectPathsRecursively($rootPath, $pattern)
2054
{
2155
if (!is_dir($rootPath)) {

0 commit comments

Comments
 (0)