Skip to content

Commit

Permalink
Finder: removed slashes normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 20, 2023
1 parent dd9b21f commit 072c303
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 20 deletions.
33 changes: 15 additions & 18 deletions src/Utils/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,14 @@ public function directories(string|array $masks = ['*']): static
private function addMask(array $masks, string $mode): static
{
foreach ($masks as $mask) {
$mask = FileSystem::unixSlashes($mask);
$orig = $mask;
if ($mode === 'dir') {
$mask = rtrim($mask, '/');
$mask = rtrim($mask, '/\\');
}
if ($mask === '' || ($mode === 'file' && str_ends_with($mask, '/'))) {
if ($mask === '' || ($mode === 'file' && $mask !== $orig)) {
throw new Nette\InvalidArgumentException("Invalid mask '$mask'");
}
if (str_starts_with($mask, '**/')) {
$mask = substr($mask, 3);
}
$mask = preg_replace('~\*\*[/\\\\]~A', '', $mask);
$this->find[] = [$mask, $mode];
}
return $this;
Expand All @@ -132,7 +130,7 @@ public function in(string|array $paths): static
public function from(string|array $paths): static
{
$paths = is_array($paths) ? $paths : func_get_args(); // compatibility with variadic
$this->addLocation($paths, '/**');
$this->addLocation($paths, DIRECTORY_SEPARATOR . '**');
return $this;
}

Expand All @@ -143,7 +141,7 @@ private function addLocation(array $paths, string $ext): void
if ($path === '') {
throw new Nette\InvalidArgumentException("Invalid directory '$path'");
}
$path = rtrim(FileSystem::unixSlashes($path), '/');
$path = rtrim($path, '/\\');
$this->in[] = $path . $ext;
}
}
Expand Down Expand Up @@ -329,7 +327,6 @@ public function getIterator(): \Generator
if ($item instanceof self) {
yield from $item->getIterator();
} else {
$item = FileSystem::platformSlashes($item);
yield $item => new FileInfo($item);
}
}
Expand All @@ -350,7 +347,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
}

try {
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::UNIX_PATHS);
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME);
} catch (\UnexpectedValueException $e) {
if ($this->ignoreUnreadableDirs) {
return;
Expand All @@ -359,7 +356,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
}
}

$files = $this->convertToFiles($pathNames, implode('/', $subdirs), FileSystem::isAbsolute($dir));
$files = $this->convertToFiles($pathNames, implode(DIRECTORY_SEPARATOR, $subdirs), FileSystem::isAbsolute($dir));

if ($this->sort) {
$files = iterator_to_array($files);
Expand Down Expand Up @@ -405,9 +402,8 @@ private function convertToFiles(iterable $pathNames, string $relativePath, bool
{
foreach ($pathNames as $pathName) {
if (!$absolute) {
$pathName = preg_replace('~\.?/~A', '', $pathName);
$pathName = preg_replace('~\.?[\\\\/]~A', '', $pathName);
}
$pathName = FileSystem::platformSlashes($pathName);
yield new FileInfo($pathName, $relativePath);
}
}
Expand Down Expand Up @@ -441,7 +437,7 @@ private function buildPlan(): array
} else {
foreach ($this->in ?: ['.'] as $in) {
$in = strtr($in, ['[' => '[[]', ']' => '[]]']); // in path, do not treat [ and ] as a pattern by glob()
$splits[] = self::splitRecursivePart($in . '/' . $mask);
$splits[] = self::splitRecursivePart($in . DIRECTORY_SEPARATOR . $mask);
}
}

Expand Down Expand Up @@ -471,11 +467,11 @@ private function buildPlan(): array
*/
private static function splitRecursivePart(string $path): array
{
$a = strrpos($path, '/');
$parts = preg_split('~(?<=^|/)\*\*($|/)~', substr($path, 0, $a + 1), 2);
preg_match('~(.*[\\\\/])(.*)$~A', $path, $m);
$parts = preg_split('~(?<=^|[\\\\/])\*\*($|[\\\\/])~', $m[1], 2);
return isset($parts[1])
? [$parts[0], $parts[1] . substr($path, $a + 1), true]
: [$parts[0], substr($path, $a + 1), false];
? [$parts[0], $parts[1] . $m[2], true]
: [$parts[0], $m[2], false];
}


Expand All @@ -484,6 +480,7 @@ private static function splitRecursivePart(string $path): array
*/
private function buildPattern(string $mask): string
{
$mask = FileSystem::unixSlashes($mask);
if ($mask === '*') {
return '##';
} elseif (str_starts_with($mask, './')) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Finder.append.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('append finder', function () {
"fixtures.finder{$ds}file.txt",
"fixtures.finder{$ds}subdir",
"fixtures.finder{$ds}subdir{$ds}subdir2",
"fixtures.finder{$ds}subdir{$ds}subdir2{$ds}file.txt",
"fixtures.finder/subdir/subdir2{$ds}file.txt",
], array_map('strval', $finder->collect()));
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Finder.errors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ test('globing', function () {
Assert::exception(
fn() => iterator_to_array(Finder::findFiles('fixtures.finder/*/unknown/*')),
Nette\InvalidStateException::class,
"Directory './fixtures.finder/*/unknown' does not exist.",
"Directory '.%ds%fixtures.finder/*/unknown' does not exist.",
);
});

0 comments on commit 072c303

Please sign in to comment.