Skip to content

Commit

Permalink
added Finder::ignoreUnreadableDirs()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 2, 2023
1 parent d51381f commit d422dd0
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Finder implements \IteratorAggregate
private array $descentFilters = [];
private bool $childFirst = false;
private int $maxDepth = -1;
private bool $ignoreUnreadableDirs = true;


/**
Expand Down Expand Up @@ -152,6 +153,16 @@ public function childFirst(bool $state = true): static
}


/**
* Ignores unreadable directories. By default, this is enabled.
*/
public function ignoreUnreadableDirs(bool $state = true): static
{
$this->ignoreUnreadableDirs = $state;
return $this;
}


/********************* filtering ****************d*g**/


Expand Down Expand Up @@ -287,10 +298,19 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
throw new Nette\InvalidStateException("Directory '$dir' not found.");
}

$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::UNIX_PATHS);
$relativePath = implode('/', $subdirs);
try {
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::UNIX_PATHS);
} catch (\UnexpectedValueException $e) {
if ($this->ignoreUnreadableDirs) {
return;
} else {
throw new Nette\InvalidStateException($e->getMessage());
}
}
$absolute = FileSystem::isAbsolute($dir);

$relativePath = implode(DIRECTORY_SEPARATOR, $subdirs);

foreach ($pathNames as $pathName) {
if (!$absolute) {
$pathName = preg_replace('~\.?/~A', '', $pathName);
Expand Down

0 comments on commit d422dd0

Please sign in to comment.