Skip to content

[Finder] Allow finder to set the UNIX_PATH flag when recursing directories #58732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Symfony/Component/Finder/Finder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Finder/Tests/FinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading