Skip to content

Commit

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

Related: symfony#26396
Related: symfony#9158
Related: symfony#28410
  • Loading branch information
joshtrichards committed Apr 27, 2024
1 parent b5e67f7 commit d685583
Showing 1 changed file with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi
private $isRecursive;
private $excludedDirs = [];
private $excludedPattern;
private $excludedPatternAbsolute;

/**
* @param \Iterator $iterator The Iterator to filter
Expand All @@ -36,9 +37,16 @@ public function __construct(\Iterator $iterator, array $directories)
$this->iterator = $iterator;
$this->isRecursive = $iterator instanceof \RecursiveIterator;
$patterns = [];
$patternsAbsolute = [];
foreach ($directories as $directory) {
$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 @@ -48,6 +56,10 @@ public function __construct(\Iterator $iterator, array $directories)
$this->excludedPattern = '#(?:^|/)(?:'.implode('|', $patterns).')(?:/|$)#';
}

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

parent::__construct($iterator);
}

Expand All @@ -63,11 +75,15 @@ public function accept()
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;
}

return true;
Expand Down

0 comments on commit d685583

Please sign in to comment.