Skip to content

Commit

Permalink
[5.2] add moveDirectory method (#14198)
Browse files Browse the repository at this point in the history
* add moveDirectory method

* StyleCI fix

* StyleCI fix 2

* newline for StyleCI
  • Loading branch information
jordonbrill authored and taylorotwell committed Jul 1, 2016
1 parent e6dd517 commit 917dfbc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,31 @@ 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.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 917dfbc

Please sign in to comment.