8000 fix(Finder): ExcludeDirectoryFilterIterator relative to "root" if `/` · symfony/symfony@d685583 · GitHub
[go: up one dir, main page]

Skip to content

Commit d685583

Browse files
committed
fix(Finder): ExcludeDirectoryFilterIterator relative to "root" if /
Fixes #28158 Fixes #47431 Related: #26396 Related: #9158 Related: #28410
1 parent b5e67f7 commit d685583

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/Symfony/Component/Finder/Iterator/ExcludeDirectoryFilterIterator.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi
2626
private $isRecursive;
2727
private $excludedDirs = [];
2828
private $excludedPattern;
29+
private $excludedPatternAbsolute;
2930

3031
/**
3132
* @param \Iterator $iterator The Iterator to filter
@@ -36,9 +37,16 @@ public function __construct(\Iterator $iterator, array $directories)
3637
$this->iterator = $iterator;
3738
$this->isRecursive = $iterator instanceof \RecursiveIterator;
3839
$patterns = [];
40+
$patternsAbsolute = [];
3941
foreach ($directories as $directory) {
4042
$directory = rtrim($directory, '/');
41-
if (!$this->isRecursive || str_contains($directory, '/')) {
43+
$slashPos = strpos($directory, '/');
44+
if (false !== $slashPos && \strlen($directory) - 1 !== $slashPos) {
45+
if (0 === $slashPos) {
46+
$directory = substr($directory, 1);
47+
}
48+
$patternsAbsolute[] = preg_quote($directory, '#');
49+
} elseif (!$this->isRecursive || str_contains($directory, '/')) {
4250
$patterns[] = preg_quote($directory, '#');
4351
} else {
4452
$this->excludedDirs[$directory] = true;
@@ -48,6 +56,10 @@ public function __construct(\Iterator $iterator, array $directories)
4856
$this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
4957
}
5058

59+
if ($patternsAbsolute) {
60+
$this->excludedPatternAbsolute = '#^('.implode('|', $patternsAbsolute).')$#';
61+
}
62+
5163
parent::__construct($iterator);
5264
}
5365

@@ -63,11 +75,15 @@ public function accept()
6375
return false;
6476
}
6577

66-
if ($this->excludedPattern) {
78+
if ($this->excludedPattern || $this->excludedPatternAbsolute) {
6779
$path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
6880
$path = str_replace('\\', '/', $path);
69-
70-
return !preg_match($this->excludedPattern, $path);
81+
}
82+
if ($this->excludedPattern && preg_match($this->excludedPattern, $path)) {
83+
return false;
84+
}
85+
if ($this->excludedPatternAbsolute && preg_match($this->excludedPatternAbsolute, $path)) {
86+
return false;
7187
}
7288

7389
return true;

0 commit comments

Comments
 (0)
0