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

Skip to content

Commit 0e207b0

Browse files
committed
[Filesystem] Add appendToFile()
1 parent a4edafb commit 0e207b0

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,35 @@ public function dumpFile($filename, $content)
648648
$this->rename($tmpFile, $filename, true);
649649
}
650650

651+
/**
652+
* Atomically appends content to an existing file.
653+
*
654+
* @param string $filename The file for which to append content
655+
* @param string $content The content to append
656+
*
657+
* @throws IOException If the file is not readable
658+
* @throws IOException If the file is not writable
659+
*/
660+
public function appendToFile($filename, $content)
661+
{
662+
if (false === $originalContent = @file_get_contents($filename)) {
663+
throw new IOException(sprintf('The file "%s" doesn\'t exist or is not readable.', $filename), 0, null, $filename);
664+
}
665+
666+
if (!is_writable($filename)) {
667+
throw new IOException(sprintf('Unable to write to the "%s" file.', $filename), 0, null, $filename);
668+
}
669+
670+
$tmpFile = $this->tempnam(dirname($filename), basename($filename));
671+
672+
if (false === @file_put_contents($tmpFile, $originalContent.$content)) {
673+
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
674+
}
675+
676+
@chmod($tmpFile, 0666 & ~umask());
677+
$this->rename($tmpFile, $filename, true);
678+
}
679+
651680
/**
652681
* @param mixed $files
653682
*

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,70 @@ public function testDumpFileWithZlibScheme()
14061406
$this->assertSame('bar', file_get_contents($filename));
14071407
}
14081408

1409+
public function testAppendToFile()
1410+
{
1411+
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.txt';
1412+
$this->filesystem->dumpFile($filename, 'foo');
1413+
1414+
// skip mode check on Windows
1415+
if ('\\' !== DIRECTORY_SEPARATOR) {
1416+
$oldMask = umask(0002);
1417+
}
1418+
1419+
$this->filesystem->appendToFile($filename, 'bar');
1420+
1421+
$this->assertFileExists($filename);
1422+
$this->assertSame('foobar', file_get_contents($filename));
1423+
1424+
// skip mode check on Windows
1425+
if ('\\' !== DIRECTORY_SEPARATOR) {
1426+
$this->assertFilePermissions(664, $filename);
1427+
umask($oldMask);
1428+
}
1429+
}
1430+
1431+
public function testAppendToFileWithScheme()
1432+
{
1433+
if (defined('HHVM_VERSION')) {
1434+
$this->markTestSkipped('HHVM does not handle the file:// scheme correctly');
1435+
}
1436+
1437+
$scheme = 'file://';
1438+
$filename = $scheme.$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
1439+
$this->filesystem->dumpFile($filename, 'foo');
1440+
1441+
$this->filesystem->appendToFile($filename, 'bar');
1442+
1443+
$this->assertFileExists($filename);
1444+
$this->assertSame('foobar', file_get_contents($filename));
1445+
}
1446+
1447+
public function testAppendToFileWithZlibScheme()
1448+
{
1449+
$scheme = 'compress.zlib://';
1450+
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
1451+
$this->filesystem->dumpFile($filename, 'foo');
1452+
1453+
// Zlib stat uses file:// wrapper so remove it
1454+
$this->assertSame('foo', file_get_contents(str_replace($scheme, '', $filename)));
1455+
1456+
$this->filesystem->appendToFile($filename, 'bar');
1457+
1458+
$this->assertFileExists($filename);
1459+
$this->assertSame('foobar', file_get_contents($filename));
1460+
}
1461+
1462+
/**
1463+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1464+
* @expectedExceptionMessage doesn't exist or is not readable
1465+
*/
1466+
public function testAppendToFileThrowsExceptionIfNotExists()
1467+
{
1468+
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.txt';
1469+
1470+
$this->filesystem->appendToFile($filename, 'bar');
1471+
}
1472+
14091473
public function testCopyShouldKeepExecutionPermission()
14101474
{
14111475
$this->markAsSkippedIfChmodIsMissing();

0 commit comments

Comments
 (0)