10000 fix(Finder): ExcludeDirectoryFilterIterator relative to "root" if `/` · phil-davis/symfony@01045da · GitHub
[go: up one dir, main page]

Skip to content

Commit 01045da

Browse files
joshtrichardsphil-davis
authored andcommitted
fix(Finder): ExcludeDirectoryFilterIterator relative to "root" if /
Fixes symfony#28158 Fixes symfony#47431 Related: symfony#26396 Related: symfony#9158 Related: symfony#28410
1 parent c54809e commit 01045da

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
@@ -30,6 +30,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi
3030
/** @var array<string, true> */
3131
private array $excludedDirs = [];
3232
private ?string $excludedPattern = null;
33+
private ?string $excludedPatternAbsolute = null;
3334
/** @var list<callable(SplFileInfo):bool> */
3435
private array $pruneFilters = [];
3536

@@ -42,6 +43,7 @@ public function __construct(\Iterator $iterator, array $directories)
4243
$this->iterator = $iterator;
4344
$this->isRecursive = $iterator instanceof \RecursiveIterator;
4445
$patterns = [];
46+
$patternsAbsolute = [];
4547
foreach ($directories as $directory) {
4648
if (!\is_string($directory)) {
4749
if (!\is_callable($directory)) {
@@ -54,7 +56,13 @@ public function __construct(\Iterator $iterator, array $directories)
5456
}
5557

5658
$directory = rtrim($directory, '/');
57-
if (!$this->isRecursive || str_contains($directory, '/')) {
59+
$slashPos = strpos($directory, '/');
60+
if (false !== $slashPos && \strlen($directory) - 1 !== $slashPos) {
61+
if (0 === $slashPos) {
62+
$directory = substr($directory, 1);
63+
}
64+
$patternsAbsolute[] = preg_quote($directory, '#');
65+
} elseif (!$this->isRecursive || str_contains($directory, '/')) {
5866
$patterns[] = preg_quote($directory, '#');
5967
} else {
6068
$this->excludedDirs[$directory] = true;
@@ -64,6 +72,10 @@ public function __construct(\Iterator $iterator, array $directories)
6472
$this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
6573
}
6674

75+
if ($patternsAbsolute) {
76+
$this->excludedPatternAbsolute = '#^('.implode('|', $patternsAbsolute).')$#';
77+
}
78+
6779
parent::__construct($iterator);
6880
}
6981

@@ -76,11 +88,15 @@ public function accept(): bool
7688
return false;
7789
}
7890

79-
if ($this->excludedPattern) {
91+
if ($this->excludedPattern || $this->excludedPatternAbsolute) {
8092
$path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
8193
$path = str_replace('\\', '/', $path);
82-
83-
return !preg_match($this->excludedPattern, $path);
94+
}
95+
if ($this->excludedPattern && preg_match($this->excludedPattern, $path)) {
96+
return false;
97+
}
98+
if ($this->excludedPatternAbsolute && preg_match($this->excludedPatternAbsolute, $path)) {
99+
return false;
84100
}
85101

86102
if ($this->pruneFilters && $this->hasChildren()) {

0 commit comments

Comments
 (0)
0