function Filesystem::mirror
Mirrors a directory to another.
Copies files and directories from the origin directory into the target directory. By default:
- existing files in the target directory will be overwritten, except if they are newer (see the `override` option)
- files in the target directory that do not exist in the source directory will not be deleted (see the `delete` option)
Parameters
\Traversable|null $iterator Iterator that filters which files and directories to copy, if null a recursive iterator is created:
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['delete'] Whether to delete files that are not in the source directory (defaults to false)
Throws
IOException When file type is unknown
2 calls to Filesystem::mirror()
- Filesystem::rename in vendor/
symfony/ filesystem/ Filesystem.php - Renames a file or a directory.
- Filesystem::symlink in vendor/
symfony/ filesystem/ Filesystem.php - Creates a symbolic link or copy a directory.
File
-
vendor/
symfony/ filesystem/ Filesystem.php, line 522
Class
- Filesystem
- Provides basic utility to manipulate the file system.
Namespace
Symfony\Component\FilesystemCode
public function mirror(string $originDir, string $targetDir, ?\Traversable $iterator = null, array $options = []) : void {
$targetDir = rtrim($targetDir, '/\\');
$originDir = rtrim($originDir, '/\\');
$originDirLen = \strlen($originDir);
if (!$this->exists($originDir)) {
throw new IOException(\sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
}
// Iterate in destination folder to remove obsolete entries
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
$deleteIterator = $iterator;
if (null === $deleteIterator) {
$flags = \FilesystemIterator::SKIP_DOTS;
$deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
}
$targetDirLen = \strlen($targetDir);
foreach ($deleteIterator as $file) {
$origin = $originDir . substr($file->getPathname(), $targetDirLen);
if (!$this->exists($origin)) {
$this->remove($file);
}
}
}
$copyOnWindows = $options['copy_on_windows'] ?? false;
if (null === $iterator) {
$flags = $copyOnWindows ? \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS : \FilesystemIterator::SKIP_DOTS;
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
}
$this->mkdir($targetDir);
$filesCreatedWhileMirroring = [];
foreach ($iterator as $file) {
if ($file->getPathname() === $targetDir || $file->getRealPath() === $targetDir || isset($filesCreatedWhileMirroring[$file->getRealPath()])) {
continue;
}
$target = $targetDir . substr($file->getPathname(), $originDirLen);
$filesCreatedWhileMirroring[$target] = true;
if (!$copyOnWindows && is_link($file)) {
$this->symlink($file->getLinkTarget(), $target);
}
elseif (is_dir($file)) {
$this->mkdir($target);
}
elseif (is_file($file)) {
$this->copy($file, $target, $options['override'] ?? false);
}
else {
throw new IOException(\sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
}
}