Skip to content

Commit 6144727

Browse files
Jean-Berunicolas-grekas
authored andcommitted
[Config] Improve GlobResource performance
1 parent e80af4f commit 6144727

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Resource/GlobResource.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -167,28 +167,31 @@ function (\SplFileInfo $file, $path) {
167167
throw new \LogicException(sprintf('Extended glob pattern "%s" cannot be used as the Finder component is not installed.', $this->pattern));
168168
}
169169

170-
$finder = new Finder();
171170
$regex = Glob::toRegex($this->pattern);
172171
if ($this->recursive) {
173172
$regex = substr_replace($regex, '(/|$)', -2, 1);
174173
}
175174

176175
$prefixLen = \strlen($this->prefix);
177-
foreach ($finder->followLinks()->sortByName()->in($this->prefix) as $path => $info) {
178-
$normalizedPath = str_replace('\\', '/', $path);
179-
if (!preg_match($regex, substr($normalizedPath, $prefixLen)) || !$info->isFile()) {
180-
continue;
181-
}
182-
if ($this->excludedPrefixes) {
183-
do {
184-
if (isset($this->excludedPrefixes[$dirPath = $normalizedPath])) {
185-
continue 2;
186-
}
187-
} while ($prefix !== $dirPath && $dirPath !== $normalizedPath = \dirname($dirPath));
188-
}
189176

190-
yield $path => $info;
191-
}
177+
yield from (new Finder())
178+
->followLinks()
179+
->filter(function (\SplFileInfo $info) use ($regex, $prefixLen, $prefix) {
180+
$normalizedPath = str_replace('\\', '/', $info->getPathname());
181+
if (!preg_match($regex, substr($normalizedPath, $prefixLen)) || !$info->isFile()) {
182+
return false;
183+
}
184+
if ($this->excludedPrefixes) {
185+
do {
186+
if (isset($this->excludedPrefixes[$dirPath = $normalizedPath])) {
187+
return false;
188+
}
189+
} while ($prefix !== $dirPath && $dirPath !== $normalizedPath = \dirname($dirPath));
190+
}
191+
})
192+
->sortByName()
193+
->in($this->prefix)
194+
;
192195
}
193196

194197
private function computeHash(): string

0 commit comments

Comments
 (0)