Skip to content

Commit b1f7377

Browse files
committed
Finder: removed slashes normalization
1 parent 4520494 commit b1f7377

File tree

3 files changed

+18
-21
lines changed

3 files changed

+18
-21
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
}
@@ -328,7 +326,6 @@ public function getIterator(): \Generator
328326
if ($item instanceof self) {
329327
yield from $item->getIterator();
330328
} else {
331-
$item = FileSystem::platformSlashes($item);
332329
yield $item => new FileInfo($item);
333330
}
334331
}
@@ -349,7 +346,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
349346
}
350347

351348
try {
352-
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::UNIX_PATHS);
349+
$pathNames = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME);
353350
} catch (\UnexpectedValueException $e) {
354351
if ($this->ignoreUnreadableDirs) {
355352
return;
@@ -358,7 +355,7 @@ private function traverseDir(string $dir, array $searches, array $subdirs = []):
358355
}
359356
}
360357

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

363360
if ($this->sort) {
364361
$files = iterator_to_array($files);
@@ -404,9 +401,8 @@ private function convertToFiles(iterable $pathNames, string $relativePath, bool
404401
{
405402
foreach ($pathNames as $pathName) {
406403
if (!$absolute) {
407-
$pathName = preg_replace('~\.?/~A', '', $pathName);
404+
$pathName = preg_replace('~\.?[\\\\/]~A', '', $pathName);
408405
}
409-
$pathName = FileSystem::platformSlashes($pathName);
410406
yield new FileInfo($pathName, $relativePath);
411407
}
412408
}
@@ -440,7 +436,7 @@ private function buildPlan(): array
440436
} else {
441437
foreach ($this->in ?: ['.'] as $in) {
442438
$in = strtr($in, ['[' => '[[]', ']' => '[]]']); // in path, do not treat [ and ] as a pattern by glob()
443-
$splits[] = self::splitRecursivePart($in . '/' . $mask);
439+
$splits[] = self::splitRecursivePart($in . DIRECTORY_SEPARATOR . $mask);
444440
}
445441
}
446442

@@ -470,11 +466,11 @@ private function buildPlan(): array
470466
*/
471467
private static function splitRecursivePart(string $path): array
472468
{
473-
$a = strrpos($path, '/');
474-
$parts = preg_split('~(?<=^|/)\*\*($|/)~', substr($path, 0, $a + 1), 2);
469+
preg_match('~(.*[\\\\/])(.*)$~A', $path, $m);
470+
$parts = preg_split('~(?<=^|[\\\\/])\*\*($|[\\\\/])~', $m[1], 2);
475471
return isset($parts[1])
476-
? [$parts[0], $parts[1] . substr($path, $a + 1), true]
477-
: [$parts[0], substr($path, $a + 1), false];
472+
? [$parts[0], $parts[1] . $m[2], true]
473+
: [$parts[0], $m[2], false];
478474
}
479475

480476

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

tests/Utils/Finder.append.phpt

+2-2
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_keys($finder->collect()));
3535
});
3636

@@ -41,6 +41,6 @@ test('append files', function () {
4141

4242
Assert::equal([
4343
__FILE__ => new Nette\Utils\FileInfo(__FILE__),
44-
__DIR__ => new Nette\Utils\FileInfo(__DIR__),
44+
FileSystem::unixSlashes(__DIR__) => new Nette\Utils\FileInfo(__DIR__),
4545
], $finder->collect());
4646
});

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 '.\\fixtures.finder/*/unknown' does not exist.",
3939
);
4040
});

0 commit comments

Comments
 (0)