Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4f4ec76

Browse files
committed
[Filesystem] Made sure Filesystem::dumpFile() overwrites an existing file.
1 parent 61c56fc commit 4f4ec76

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,16 +224,17 @@ public function chgrp($files, $group, $recursive = false)
224224
/**
225225
* Renames a file.
226226
*
227-
* @param string $origin The origin filename
228-
* @param string $target The new filename
227+
* @param string $origin The origin filename
228+
* @param string $target The new filename
229+
* @param Boolean $overwrite Whether to overwrite the target if it already exists
229230
*
230231
* @throws IOException When target file already exists
231232
* @throws IOException When origin cannot be renamed
232233
*/
233-
public function rename($origin, $target)
234+
public function rename($origin, $target, $overwrite = false)
234235
{
235236
// we check that target does not exist
236-
if (is_readable($target)) {
237+
if (!$overwrite && is_readable($target)) {
237238
throw new IOException(sprintf('Cannot rename because the target "%s" already exist.', $target));
238239
}
239240

@@ -452,7 +453,7 @@ public function dumpFile($filename, $content, $mode = 0666)
452453
throw new IOException(sprintf('Failed to write file "%s".', $filename));
453454
}
454455

455-
$this->rename($tmpFile, $filename);
456+
$this->rename($tmpFile, $filename, true);
456457
$this->chmod($filename, $mode);
457458
}
458459
}

src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,20 @@ public function testRenameThrowsExceptionIfTargetAlreadyExists()
639639
$this->filesystem->rename($file, $newPath);
640640
}
641641

642+
public function testRenameOverwritesTheTargetIfItAlreadyExists()
643+
{
644+
$file = $this->workspace.DIRECTORY_SEPARATOR.'file';
645+
$newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
646+
647+
touch($file);
648+
touch($newPath);
649+
650+
$this->filesystem->rename($file, $newPath, true);
651+
652+
$this->assertFileNotExists($file);
653+
$this->assertFileExists($newPath);
654+
}
655+
642656
/**
643657
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
644658
*/
@@ -894,6 +908,17 @@ public function testDumpFile()
894908
$this->assertEquals(753, $this->getFilePermissions($filename));
895909
}
896910

911+
public function testDumpFileOverwritesAnExistingFile()
912+
{
913+
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo.txt';
914+
file_put_contents($filename, 'FOO BAR');
915+
916+
$this->filesystem->dumpFile($filename, 'bar');
917+
918+
$this->assertFileExists($filename);
919+
$this->assertSame('bar', file_get_contents($filename));
920+
}
921+
897922
/**
898923
* Returns file permissions as three digits (i.e. 755)
899924
*

0 commit comments

Comments
 (0)