Skip to content

Commit

Permalink
[Filesystem] [mirror] added "delete" option
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Melard authored and fabpot committed Dec 11, 2012
1 parent 225e3e5 commit 2b13760
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -336,11 +336,30 @@ public function makePathRelative($endPath, $startPath)
* Valid options are: * Valid options are:
* - $options['override'] Whether to override an existing file on copy or not (see copy()) * - $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['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 * @throws IOException When file type is unknown
*/ */
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array()) 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) && isset($options['delete']) && $options['delete']) {
$deleteIterator = $iterator;
if (null === $deleteIterator) {
$flags = \FilesystemIterator::SKIP_DOTS;
$deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
}
foreach ($deleteIterator as $file) {
$origin = str_replace($targetDir, $originDir, $file->getPathname());
if (!$this->exists($origin)) {
$this->remove($file);
}
}
}

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


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

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


Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Expand Up @@ -798,6 +798,25 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively()
$this->assertTrue(is_dir($targetPath.'directory')); $this->assertTrue(is_dir($targetPath.'directory'));
$this->assertFileEquals($file1, $targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'); $this->assertFileEquals($file1, $targetPath.'directory'.DIRECTORY_SEPARATOR.'file1');
$this->assertFileEquals($file2, $targetPath.'file2'); $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() public function testMirrorCopiesLinks()
Expand Down

0 comments on commit 2b13760

Please sign in to comment.