From 131423ebeac9779d0d62258e15b57e8e0cc16130 Mon Sep 17 00:00:00 2001 From: Jordon Brill Date: Fri, 1 Jul 2016 09:05:54 -0500 Subject: [PATCH 1/4] add moveDirectory method --- src/Illuminate/Filesystem/Filesystem.php | 22 +++++++++++++ tests/Filesystem/FilesystemTest.php | 41 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index e503230fed3b..0c20a6ef12f8 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -388,6 +388,28 @@ public function makeDirectory($path, $mode = 0755, $recursive = false, $force = return mkdir($path, $mode, $recursive); } + /** + * Move a directory. + * + * @param string $directory + * @param string $destination + * @param bool $overwrite + * @return bool + */ + public function moveDirectory($directory, $destination, $overwrite = false) + { + if($overwrite && $this->isDirectory($destination)) { + $this->deleteDirectory($destination); + $this->copyDirectory($directory, $destination); + $this->deleteDirectory($directory); + return true; + } + if (@rename($directory, $destination) !== true) { + return false; + } + return true; + } + /** * Copy a directory from one location to another. * diff --git a/tests/Filesystem/FilesystemTest.php b/tests/Filesystem/FilesystemTest.php index 02a0a05b7e43..017ea9e1ae5d 100755 --- a/tests/Filesystem/FilesystemTest.php +++ b/tests/Filesystem/FilesystemTest.php @@ -120,6 +120,47 @@ public function testCopyDirectoryMovesEntireDirectory() $this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt'); } + public function testMoveDirectoryMovesEntireDirectory() + { + mkdir($this->tempDir.'/tmp', 0777, true); + file_put_contents($this->tempDir.'/tmp/foo.txt', ''); + file_put_contents($this->tempDir.'/tmp/bar.txt', ''); + mkdir($this->tempDir.'/tmp/nested', 0777, true); + file_put_contents($this->tempDir.'/tmp/nested/baz.txt', ''); + + $files = new Filesystem(); + $files->moveDirectory($this->tempDir.'/tmp', $this->tempDir.'/tmp2'); + $this->assertTrue(is_dir($this->tempDir.'/tmp2')); + $this->assertFileExists($this->tempDir.'/tmp2/foo.txt'); + $this->assertFileExists($this->tempDir.'/tmp2/bar.txt'); + $this->assertTrue(is_dir($this->tempDir.'/tmp2/nested')); + $this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt'); + $this->assertFalse(is_dir($this->tempDir.'/tmp')); + } + + public function testMoveDirectoryMovesEntireDirectoryAndOverwrites() + { + mkdir($this->tempDir.'/tmp', 0777, true); + file_put_contents($this->tempDir.'/tmp/foo.txt', ''); + file_put_contents($this->tempDir.'/tmp/bar.txt', ''); + mkdir($this->tempDir.'/tmp/nested', 0777, true); + file_put_contents($this->tempDir.'/tmp/nested/baz.txt', ''); + mkdir($this->tempDir.'/tmp2', 0777, true); + file_put_contents($this->tempDir.'/tmp2/foo2.txt', ''); + file_put_contents($this->tempDir.'/tmp2/bar2.txt', ''); + + $files = new Filesystem(); + $files->moveDirectory($this->tempDir.'/tmp', $this->tempDir.'/tmp2', true); + $this->assertTrue(is_dir($this->tempDir.'/tmp2')); + $this->assertFileExists($this->tempDir.'/tmp2/foo.txt'); + $this->assertFileExists($this->tempDir.'/tmp2/bar.txt'); + $this->assertTrue(is_dir($this->tempDir.'/tmp2/nested')); + $this->assertFileExists($this->tempDir.'/tmp2/nested/baz.txt'); + $this->assertFileNotExists($this->tempDir.'/tmp2/foo2.txt'); + $this->assertFileNotExists($this->tempDir.'/tmp2/bar2.txt'); + $this->assertFalse(is_dir($this->tempDir.'/tmp')); + } + /** * @expectedException Illuminate\Contracts\Filesystem\FileNotFoundException */ From b6ab3c84de5af03532f4dbd38f7a75310951cf96 Mon Sep 17 00:00:00 2001 From: Jordon Brill Date: Fri, 1 Jul 2016 09:20:21 -0500 Subject: [PATCH 2/4] StyleCI fix --- src/Illuminate/Filesystem/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index 0c20a6ef12f8..71fc7cdf0daf 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -398,7 +398,7 @@ public function makeDirectory($path, $mode = 0755, $recursive = false, $force = */ public function moveDirectory($directory, $destination, $overwrite = false) { - if($overwrite && $this->isDirectory($destination)) { + if ($overwrite && $this->isDirectory($destination)) { $this->deleteDirectory($destination); $this->copyDirectory($directory, $destination); $this->deleteDirectory($directory); From e44db216bce40cc87e015e14bac2d083bc115a4e Mon Sep 17 00:00:00 2001 From: Jordon Brill Date: Fri, 1 Jul 2016 09:23:15 -0500 Subject: [PATCH 3/4] StyleCI fix 2 --- src/Illuminate/Filesystem/Filesystem.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index 71fc7cdf0daf..5daba5e09a9a 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -402,11 +402,13 @@ public function moveDirectory($directory, $destination, $overwrite = false) $this->deleteDirectory($destination); $this->copyDirectory($directory, $destination); $this->deleteDirectory($directory); + return true; } if (@rename($directory, $destination) !== true) { return false; } + return true; } From 383c16246591fbda90cd79c2c4e58f9fcf547a8f Mon Sep 17 00:00:00 2001 From: Jordon Brill Date: Fri, 1 Jul 2016 09:46:51 -0500 Subject: [PATCH 4/4] newline for StyleCI --- src/Illuminate/Filesystem/Filesystem.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index 5daba5e09a9a..d749b36be2e2 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -405,10 +405,11 @@ public function moveDirectory($directory, $destination, $overwrite = false) return true; } + if (@rename($directory, $destination) !== true) { return false; } - + return true; }