Skip to content
This repository
Browse code

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...
  • Loading branch information...
commit b3c58e7b90cf1dca76bf5687f7b804f277bfaa83 2 parents fdb11be + 2b13760
Fabien Potencier authored December 11, 2012
22  src/Symfony/Component/Filesystem/Filesystem.php
@@ -336,11 +336,30 @@ public function makePathRelative($endPath, $startPath)
336 336
      *                               Valid options are:
337 337
      *                                 - $options['override'] Whether to override an existing file on copy or not (see copy())
338 338
      *                                 - $options['copy_on_windows'] Whether to copy files instead of links on Windows (see symlink())
  339
+     *                                 - $options['delete'] Default false Whether to delete files that are not in the source directory
339 340
      *
340 341
      * @throws IOException When file type is unknown
341 342
      */
342 343
     public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
343 344
     {
  345
+        $targetDir = rtrim($targetDir, '/\\');
  346
+        $originDir = rtrim($originDir, '/\\');
  347
+
  348
+        // Iterate in destination folder to remove obsolete entries
  349
+        if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
  350
+            $deleteIterator = $iterator;
  351
+            if (null === $deleteIterator) {
  352
+                $flags = \FilesystemIterator::SKIP_DOTS;
  353
+                $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
  354
+            }
  355
+            foreach ($deleteIterator as $file) {
  356
+                $origin = str_replace($targetDir, $originDir, $file->getPathname());
  357
+                if (!$this->exists($origin)) {
  358
+                    $this->remove($file);
  359
+                }
  360
+            }
  361
+        }
  362
+
344 363
         $copyOnWindows = false;
345 364
         if (isset($options['copy_on_windows']) && !function_exists('symlink')) {
346 365
             $copyOnWindows = $options['copy_on_windows'];
@@ -351,9 +370,6 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
351 370
             $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
352 371
         }
353 372
 
354  
-        $targetDir = rtrim($targetDir, '/\\');
355  
-        $originDir = rtrim($originDir, '/\\');
356  
-
357 373
         foreach ($iterator as $file) {
358 374
             $target = str_replace($originDir, $targetDir, $file->getPathname());
359 375
 
19  src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
@@ -798,6 +798,25 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively()
798 798
         $this->assertTrue(is_dir($targetPath.'directory'));
799 799
         $this->assertFileEquals($file1, $targetPath.'directory'.DIRECTORY_SEPARATOR.'file1');
800 800
         $this->assertFileEquals($file2, $targetPath.'file2');
  801
+
  802
+        $this->filesystem->remove($file1);
  803
+
  804
+        $this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => FALSE));
  805
+        $this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  806
+
  807
+        $this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => TRUE));
  808
+        $this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  809
+
  810
+        file_put_contents($file1, 'FILE1');
  811
+
  812
+        $this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => TRUE));
  813
+        $this->assertTrue($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  814
+
  815
+        $this->filesystem->remove($directory);
  816
+        $this->filesystem->mirror($sourcePath, $targetPath, null, array("delete" => TRUE));
  817
+        $this->assertFalse($this->filesystem->exists($targetPath.'directory'));
  818
+        $this->assertFalse($this->filesystem->exists($targetPath.'directory'.DIRECTORY_SEPARATOR.'file1'));
  819
+
801 820
     }
802 821
 
803 822
     public function testMirrorCopiesLinks()

0 notes on commit b3c58e7

Please sign in to comment.
Something went wrong with that request. Please try again.