Browse Source

Merge pull request #6545 from daniel-lang/copy-recursive

3.x - Folder::copy can now copy non-recursively
José Lorenzo Rodríguez 11 years ago
parent
commit
a1d97df04c
2 changed files with 40 additions and 2 deletions
  1. 9 2
      src/Filesystem/Folder.php
  2. 31 0
      tests/TestCase/Filesystem/FolderTest.php

+ 9 - 2
src/Filesystem/Folder.php

@@ -661,6 +661,7 @@ class Folder
      * - `mode` The mode to copy the files/directories with as integer, e.g. 0775.
      * - `skip` Files/directories to skip.
      * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
+     * - `recursive` Whether to copy recursively or not (default: true - recursive)
      *
      * @param array|string $options Either an array of options (see above) or a string of the destination directory.
      * @return bool Success.
@@ -680,7 +681,8 @@ class Folder
             'from' => $this->path,
             'mode' => $this->mode,
             'skip' => [],
-            'scheme' => Folder::MERGE
+            'scheme' => Folder::MERGE,
+            'recursive' => true
         ];
 
         $fromDir = $options['from'];
@@ -723,6 +725,10 @@ class Folder
                         $this->delete($to);
                     }
 
+                    if (is_dir($from) && $options['recursive'] === false) {
+                        continue;
+                    }
+
                     if (is_dir($from) && !file_exists($to)) {
                         $old = umask(0);
                         if (mkdir($to, $mode, true)) {
@@ -763,6 +769,7 @@ class Folder
      * - `chmod` The mode to copy the files/directories with.
      * - `skip` Files/directories to skip.
      * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP
+     * - `recursive` Whether to copy recursively or not (default: true - recursive)
      *
      * @param array|string $options (to, from, chmod, skip, scheme)
      * @return bool Success
@@ -774,7 +781,7 @@ class Folder
             $to = $options;
             $options = (array)$options;
         }
-        $options += ['to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => []];
+        $options += ['to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => [], 'recursive' => true];
 
         if ($this->copy($options)) {
             if ($this->delete($options['from'])) {

+ 31 - 0
tests/TestCase/Filesystem/FolderTest.php

@@ -1031,6 +1031,25 @@ class FolderTest extends TestCase
     }
 
     /**
+     * testCopyWithoutResursive
+     *
+     * Verify that only the files exist in the target directory.
+     *
+     * @return void
+     */
+    public function testCopyWithoutRecursive()
+    {
+        extract($this->_setupFilesystem());
+
+        $Folder = new Folder($folderOne);
+        $result = $Folder->copy(['to' => $folderThree, 'recursive' => false]);
+
+        $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
+        $this->assertFalse(is_dir($folderThree . DS . 'folderA'));
+        $this->assertFalse(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
+    }
+
+    /**
      * Setup filesystem for copy tests
      * $path: folder_test/
      * - folder1/file1.php
@@ -1216,4 +1235,16 @@ class FolderTest extends TestCase
         $Folder = new Folder($path);
         $Folder->delete();
     }
+    
+    public function testMoveWithoutRecursive()
+    {
+        extract($this->_setupFilesystem());
+
+        $Folder = new Folder($folderOne);
+        $result = $Folder->move(['to' => $folderTwo, 'recursive' => false]);
+        $this->assertTrue($result);
+        $this->assertTrue(file_exists($folderTwo . '/file1.php'));
+        $this->assertFalse(is_dir($folderTwo . '/folderA'));
+        $this->assertFalse(file_exists($folderTwo . '/folderA/fileA.php'));
+    }
 }