merged branch mylen/master (PR #6014)

This PR was squashed before being merged into the master branch (closes #6014).

Commits
-------

2b13760 [Filesystem] [mirror] added "delete" option

Discussion
----------

[Filesystem] [mirror] added "delete" option

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets:
Todo: -
License of the code: MIT
Documentation PR: symfony/symfony-docs#123

I added a "delete" option to the mirror function. If set to true, then
files present in target dir and not in origin dir will be removed.

Added also unit test for these feature.

---------------------------------------------------------------------------

by pborreli at 2012-11-16T00:58:19Z

Symfony2 code standard use lowercase `true` and `false`

---------------------------------------------------------------------------

by mylen at 2012-11-16T20:25:19Z

I have problem to believe that the last commit (merging two if together) was to blame for the segfault on  travis...
when I run the unit testing on my machine, I get:

I'm using PHP 5.4.7 by the way...

Time: 2 seconds, Memory: 3.25Mb

OK, but incomplete or skipped tests!
Tests: 80, Assertions: 106, Skipped: 14.

---------------------------------------------------------------------------

by pborreli at 2012-11-16T20:38:40Z

Can you fix end-of-lines ?

---------------------------------------------------------------------------

by mylen at 2012-11-16T20:52:37Z

I put UNIX line feed and UTF8 charset

---------------------------------------------------------------------------

by mylen at 2012-11-16T20:53:59Z

Sorry, I add to clone the symfony repo with github and did small editing using pspad, I forgot to setup charset and line feed...
This commit is contained in:
Fabien Potencier 2012-12-11 13:55:45 +01:00
commit b3c58e7b90
2 changed files with 38 additions and 3 deletions

View File

@ -336,11 +336,30 @@ class Filesystem
* 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) && 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;
if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
$copyOnWindows = $options['copy_on_windows'];
@ -351,9 +370,6 @@ class Filesystem
$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());

View File

@ -798,6 +798,25 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
$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()