Skip to content

mirror: added "delete" option #2

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

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 21 additions & 3 deletions Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,32 @@ public function makePathRelative($endPath, $startPath)
* Valid options are:
* - $options['override'] Whether to override an existing file on copy or not (see copy())
* - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink())
* - $options['delete'] Default FALSE Whether to delete files that are not in the source directory
*
* @throws IOException When file type is unknown
*/
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
{
$targetDir = rtrim($targetDir, '/\\');
$originDir = rtrim($originDir, '/\\');

// Iterate in destination folder to remove obsolete entries
if ($this->exists($targetDir)) {
if (isset($options['delete']) and $options['delete']) {
$l_iterator = $iterator;
if (null === $l_iterator) {
$flags = \FilesystemIterator::SKIP_DOTS;
$l_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST );
}
foreach ($l_iterator as $file) {
$origin = str_replace($targetDir, $originDir, $file->getPathname());
if (!$this->exists($origin)) {
$this->remove($file);
}
}
}
}

$copyOnWindows = false;
if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
$copyOnWindows = $options['copy_on_windows'];
Expand All @@ -351,9 +372,6 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
}

$targetDir = rtrim($targetDir, '/\\');
$originDir = rtrim($originDir, '/\\');

foreach ($iterator as $file) {
$target = str_replace($originDir, $targetDir, $file->getPathname());

Expand Down
19 changes: 19 additions & 0 deletions Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,25 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively()
$this->assertTrue(is_dir($targetPath.'directory'));
$this->assertFileEquals($file1, $targetPath.'directory'.DIRECTORY_SEPARATOR.'file1');
$this->assertFileEquals($file2, $targetPath.'file2');

$this->filesystem->remove($file1);

$this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => FALSE));
$this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));

$this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => TRUE));
$this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));

file_put_contents($file1, 'FILE1');

$this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => TRUE));
$this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));

$this->filesystem->remove($directory);
$this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => TRUE));
$this->assertFalse($this->filesystem->exists($targetPath.'directory'));
$this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));

}

public function testMirrorCopiesLinks()
Expand Down