Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Finder] Exclude relative to content root if prefixed / #54754

Open
wants to merge 4 commits into
base: 7.2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,10 @@ public function size(string|int|array $sizes): static
*
* $finder->in(__DIR__)->exclude('ruby');
*
* If the directory is preceded by '/' then it is only excluded at the top level. For example:
*
* $finder->in(__DIR__)->exclude('/vendor');
*
* @param string|array $dirs A directory path or an array of directories
*
* @return $this
Expand Down
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