Skip to content

Commit

Permalink
fix(Finder): ExcludeDirectoryFilterIterator relative to "root" if /
Browse files Browse the repository at this point in the history
Fixes #28158
Fixes #47431

Related: #26396
Related: #9158
Related: #28410
  • Loading branch information
joshtrichards authored and phil-davis committed Apr 27, 2024
1 parent c54809e commit 01045da
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi
/** @var array<string, true> */
private array $excludedDirs = [];
private ?string $excludedPattern = null;
private ?string $excludedPatternAbsolute = null;
/** @var list<callable(SplFileInfo):bool> */
private array $pruneFilters = [];

Expand All @@ -42,6 +43,7 @@ public function __construct(\Iterator $iterator, array $directories)
$this->iterator = $iterator;
$this->isRecursive = $iterator instanceof \RecursiveIterator;
$patterns = [];
$patternsAbsolute = [];
foreach ($directories as $directory) {
if (!\is_string($directory)) {
if (!\is_callable($directory)) {
Expand All @@ -54,7 +56,13 @@ public function __construct(\Iterator $iterator, array $directories)
}

$directory = rtrim($directory, '/');
if (!$this->isRecursive || str_contains($directory, '/')) {
$slashPos = strpos($directory, '/');
if (false !== $slashPos && \strlen($directory) - 1 !== $slashPos) {
if (0 === $slashPos) {
$directory = substr($directory, 1);
}
$patternsAbsolute[] = preg_quote($directory, '#');
} elseif (!$this->isRecursive || str_contains($directory, '/')) {
$patterns[] = preg_quote($directory, '#');
} else {
$this->excludedDirs[$directory] = true;
Expand All @@ -64,6 +72,10 @@ public function __construct(\Iterator $iterator, array $directories)
$this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
}

if ($patternsAbsolute) {
$this->excludedPatternAbsolute = '#^('.implode('|', $patternsAbsolute).')$#';
}

parent::__construct($iterator);
}

Expand All @@ -76,11 +88,15 @@ public function accept(): bool
return false;
}

if ($this->excludedPattern) {
if ($this->excludedPattern || $this->excludedPatternAbsolute) {
$path = $this->isDir() ? $this->current()->getRelativePathname() : $this->current()->getRelativePath();
$path = str_replace('\\', '/', $path);

return !preg_match($this->excludedPattern, $path);
}
if ($this->excludedPattern && preg_match($this->excludedPattern, $path)) {
return false;
}
if ($this->excludedPatternAbsolute && preg_match($this->excludedPatternAbsolute, $path)) {
return false;
}

if ($this->pruneFilters && $this->hasChildren()) {
Expand Down

0 comments on commit 01045da

Please sign in to comment.