Skip to content

[Filesystem] rename Filesystem::mirror() option copy_on_windows to follow_symlinks #47969

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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Deprecate calling `Filesystem::mirror()` with option `copy_on_windows`, use option `follow_symlinks` instead.

7.1
---

Expand Down
13 changes: 9 additions & 4 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,18 @@ public function makePathRelative(string $endPath, string $startPath): string
* @param array $options An array of boolean options
* Valid options are:
* - $options['override'] If true, target files newer than origin files are overwritten (see copy(), defaults to false)
* - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink(), defaults to false)
* - $options['copy_on_windows'] (deprecated) Whether to copy files instead of links on Windows (see symlink(), defaults to false)
* - $options['follow_symlinks'] Whether to copy files instead of links, where using symlinks in not working: windows, different drive, docker context not supporting symlinks (see symlink(), defaults to false)
* - $options['delete'] Whether to delete files that are not in the source directory (defaults to false)
*
* @throws IOException When file type is unknown
*/
public function mirror(string $originDir, string $targetDir, ?\Traversable $iterator = null, array $options = []): void
{
if (isset($options['copy_on_windows'])) {
trigger_deprecation('symfony/filesystem', '7.3', 'Calling "%s()" with option "copy_on_windows" is deprecated, use "follow_symlinks" option instead.', __METHOD__);
}

$targetDir = rtrim($targetDir, '/\\');
$originDir = rtrim($originDir, '/\\');
$originDirLen = \strlen($originDir);
Expand All @@ -545,10 +550,10 @@ public function mirror(string $originDir, string $targetDir, ?\Traversable $iter
}
}

$copyOnWindows = $options['copy_on_windows'] ?? false;
$followSymlinks = $options['follow_symlinks'] ?? $options['copy_on_windows'] ?? false;

if (null === $iterator) {
$flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
$flags = $followSymlinks ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
}

Expand All @@ -563,7 +568,7 @@ public function mirror(string $originDir, string $targetDir, ?\Traversable $iter
$target = $targetDir.substr($file->getPathname(), $originDirLen);
$filesCreatedWhileMirroring[$target] = true;

if (!$copyOnWindows && is_link($file)) {
if (!$followSymlinks && is_link($file)) {
$this->symlink($file->getLinkTarget(), $target);
} elseif (is_dir($file)) {
$this->mkdir($target);
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,24 @@ public function testMirrorCopiesLinks()
$this->assertTrue(is_link($targetPath.\DIRECTORY_SEPARATOR.'link1'));
}

public function testMirrorCopiesLinksByFollowingSymlinks()
{
$this->markAsSkippedIfSymlinkIsMissing();

$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;

mkdir($sourcePath);
file_put_contents($sourcePath.'file1', 'FILE1');
symlink($sourcePath.'file1', $sourcePath.'link1');

$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;

$this->filesystem->mirror($sourcePath, $targetPath, null, ['follow_symlinks' => true]);

$this->assertDirectoryExists($targetPath);
$this->assertFalse(is_link($targetPath.\DIRECTORY_SEPARATOR.'link1'));
}

public function testMirrorCopiesLinkedDirectoryContents()
{
$this->markAsSkippedIfSymlinkIsMissing(true);
Expand Down
Loading