diff --git a/src/Symfony/Component/Finder/Finder.php b/src/Symfony/Component/Finder/Finder.php index 78673af66a9bd..9a7cf836b4308 100644 --- a/src/Symfony/Component/Finder/Finder.php +++ b/src/Symfony/Component/Finder/Finder.php @@ -54,6 +54,7 @@ class Finder implements \IteratorAggregate, \Countable private array $depths = []; private array $sizes = []; private bool $followLinks = false; + private bool $unixPaths = false; private bool $reverseSorting = false; private \Closure|int|false $sort = false; private int $ignore = 0; @@ -609,6 +610,18 @@ public function followLinks(): static return $this; } + /** + * Force the use of UNIX paths when recursing directories. + * + * @return $this + */ + public function useUnixPaths(): static + { + $this->unixPaths = true; + + return $this; + } + /** * Tells finder to ignore unreadable directories. * @@ -783,6 +796,10 @@ private function searchInDirectory(string $dir): \Iterator $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS; } + if ($this->unixPaths) { + $flags |= \RecursiveDirectoryIterator::UNIX_PATHS; + } + $iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs); if ($exclude) { diff --git a/src/Symfony/Component/Finder/Tests/FinderTest.php b/src/Symfony/Component/Finder/Tests/FinderTest.php index 1ca2e783adcec..042c968f29095 100644 --- a/src/Symfony/Component/Finder/Tests/FinderTest.php +++ b/src/Symfony/Component/Finder/Tests/FinderTest.php @@ -1086,6 +1086,20 @@ public function testFollowLinks() ]), $finder->in(self::$tmpDir)->getIterator()); } + public function testUseUnixPaths() + { + $fixturesDirectory = __DIR__.\DIRECTORY_SEPARATOR.'Fixtures'; + + // Fix __DIR__ on Windows giving us backslashes. + $fixturesDirectory = str_replace('\\', '/', $fixturesDirectory); + + $finder = $this->buildFinder(); + $this->assertSame($finder, $finder->useUnixPaths()); + foreach ($finder->in($fixturesDirectory) as $file) { + $this->assertStringNotContainsString('\\', $file->getPathname(), 'Paths should be in UNIX style.'); + } + } + public function testIn() { $finder = $this->buildFinder();