Skip to content

Commit 2d60daf

Browse files
committed
Finder: removed slashes normalization
1 parent 2717279 commit 2d60daf

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

src/Utils/Finder.php

+15-18
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,14 @@ public function directories(string|array $masks = ['*']): static
9999
private function addMask(array $masks, string $mode): static
100100
{
101101
foreach ($masks as $mask) {
102-
$mask = FileSystem::unixSlashes($mask);
102+
$orig = $mask;
103103
if ($mode === 'dir') {
104-
$mask = rtrim($mask, '/');
104+
$mask = rtrim($mask, '/\\');
105105
}
106-
if ($mask === '' || ($mode === 'file' && str_ends_with($mask, '/'))) {
106+
if ($mask === '' || ($mode === 'file' && $mask !== $orig)) {
107107
throw new Nette\InvalidArgumentException("Invalid mask '$mask'");
108108
}
109-
if (str_starts_with($mask, '**/')) {
110-
$mask = substr($mask, 3);
111-
}
109+
$mask = preg_replace('~\*\*[/\\\\]~A', '', $mask);
112110
$this->find[] = [$mask, $mode];
113111
}
114112
return $this;
@@ -132,7 +130,7 @@ public function in(string|array $paths): static
132130
public function from(string|array $paths): static
133131
{
134132
$paths = is_array($paths) ? $paths : func_get_args(); // compatibility with variadic
135-
$this->addLocation($paths, '/**');
133+
$this->addLocation($paths, DIRECTORY_SEPARATOR . '**');
136134
return $this;
137135
}
138136

@@ -143,7 +141,7 @@ private function addLocation(array $paths, string $ext): void
143141
if ($path === '') {
144142
throw new Nette\InvalidArgumentException("Invalid directory '$path'");
145143
}
146-
$path = rtrim(FileSystem::unixSlashes($path), '/');
144+
$path = rtrim($path, '/\\');
147145
$this->in[] = $path . $ext;
148146
}
149147
}
@@ -329,7 +327,6 @@ public function getIterator(): \Generator
329327
if ($item instanceof self) {
330328
yield from $item->getIterator();
331329
} else {
332-
$item = FileSystem::platformSlashes($item);
333330
yield $item => new FileInfo($item);
334331
}
335332
}
@@ -350,7 +347,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
350347
}
351348

352349
try {
353-
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::UNIX_PATHS);
350+
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME);
354351
} catch (\UnexpectedValueException $e) {
355352
if ($this->ignoreUnreadableDirs) {
356353
return;
@@ -359,7 +356,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
359356
}
360357
}
361358

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

364361
if ($this->sort) {
365362
$files = iterator_to_array($files);
@@ -405,9 +402,8 @@ private function convertToFiles(iterable $pathNames, string $relativePath, bool
405402
{
406403
foreach ($pathNames as $pathName) {
407404
if (!$absolute) {
408-
$pathName = preg_replace('~\.?/~A', '', $pathName);
405+
$pathName = preg_replace('~\.?[\\\\/]~A', '', $pathName);
409406
}
410-
$pathName = FileSystem::platformSlashes($pathName);
411407
yield new FileInfo($pathName, $relativePath);
412408
}
413409
}
@@ -441,7 +437,7 @@ private function buildPlan(): array
441437
} else {
442438
foreach ($this->in ?: ['.'] as $in) {
443439
$in = strtr($in, ['[' => '[[]', ']' => '[]]']); // in path, do not treat [ and ] as a pattern by glob()
444-
$splits[] = self::splitRecursivePart($in . '/' . $mask);
440+
$splits[] = self::splitRecursivePart($in . DIRECTORY_SEPARATOR . $mask);
445441
}
446442
}
447443

@@ -471,11 +467,11 @@ private function buildPlan(): array
471467
*/
472468
private static function splitRecursivePart(string $path): array
473469
{
474-
$a = strrpos($path, '/');
475-
$parts = preg_split('~(?<=^|/)\*\*($|/)~', substr($path, 0, $a + 1), 2);
470+
preg_match('~(.*[\\\\/])(.*)$~A', $path, $m);
471+
$parts = preg_split('~(?<=^|[\\\\/])\*\*($|[\\\\/])~', $m[1], 2);
476472
return isset($parts[1])
477-
? [$parts[0], $parts[1] . substr($path, $a + 1), true]
478-
: [$parts[0], substr($path, $a + 1), false];
473+
? [$parts[0], $parts[1] . $m[2], true]
474+
: [$parts[0], $m[2], false];
479475
}
480476

481477

@@ -484,6 +480,7 @@ private static function splitRecursivePart(string $path): array
484480
*/
485481
private function buildPattern(string $mask): string
486482
{
483+
$mask = FileSystem::unixSlashes($mask);
487484
if ($mask === '*') {
488485
return '##';
489486
} elseif (str_starts_with($mask, './')) {

tests/Utils/Finder.append.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ test('append finder', function () {
3030
"fixtures.finder{$ds}file.txt",
3131
"fixtures.finder{$ds}subdir",
3232
"fixtures.finder{$ds}subdir{$ds}subdir2",
33-
"fixtures.finder{$ds}subdir{$ds}subdir2{$ds}file.txt",
33+
"fixtures.finder/subdir/subdir2{$ds}file.txt",
3434
], array_map('strval', $finder->collect()));
3535
});
3636

tests/Utils/Finder.errors.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ test('globing', function () {
3535
Assert::exception(
3636
fn() => iterator_to_array(Finder::findFiles('fixtures.finder/*/unknown/*')),
3737
Nette\InvalidStateException::class,
38-
"Directory './fixtures.finder/*/unknown' does not exist.",
38+
"Directory '.%ds%fixtures.finder/*/unknown' does not exist.",
3939
);
4040
});

0 commit comments

Comments
 (0)