Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5681f39

Browse files
committed
[Filesystem] Add appendToFile()
1 parent6f6100a commit5681f39

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

‎src/Symfony/Component/Filesystem/Filesystem.php‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ public function tempnam($dir, $prefix)
624624
* @param string $filename The file to be written to
625625
* @param string $content The data to write into the file
626626
*
627-
* @throws IOExceptionIf the file cannot be written to.
627+
* @throws IOExceptionif the file cannot be written to
628628
*/
629629
publicfunctiondumpFile($filename,$content)
630630
{
@@ -648,6 +648,29 @@ public function dumpFile($filename, $content)
648648
$this->rename($tmpFile,$filename,true);
649649
}
650650

651+
/**
652+
* 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 writable
658+
*/
659+
publicfunctionappendToFile($filename,$content)
660+
{
661+
$dir =dirname($filename);
662+
663+
if (!is_dir($dir)) {
664+
$this->mkdir($dir);
665+
}elseif (!is_writable($dir)) {
666+
thrownewIOException(sprintf('Unable to write to the "%s" directory.',$dir),0,null,$dir);
667+
}
668+
669+
if (false === @file_put_contents($filename,$content,FILE_APPEND)) {
670+
thrownewIOException(sprintf('Failed to write file "%s".',$filename),0,null,$filename);
671+
}
672+
}
673+
651674
/**
652675
* @param mixed $files
653676
*

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

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

1409+
publicfunctiontestAppendToFile()
1410+
{
1411+
$filename =$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.txt';
1412+
1413+
// skip mode check on Windows
1414+
if ('\\' !==DIRECTORY_SEPARATOR) {
1415+
$oldMask =umask(0002);
1416+
}
1417+
1418+
$this->filesystem->dumpFile($filename,'foo');
1419+
1420+
$this->filesystem->appendToFile($filename,'bar');
1421+
1422+
$this->assertFileExists($filename);
1423+
$this->assertSame('foobar',file_get_contents($filename));
1424+
1425+
// skip mode check on Windows
1426+
if ('\\' !==DIRECTORY_SEPARATOR) {
1427+
$this->assertFilePermissions(664,$filename,'The written file should keep the same permissions as before.');
1428+
umask($oldMask);
1429+
}
1430+
}
1431+
1432+
publicfunctiontestAppendToFileWithScheme()
1433+
{
1434+
if (defined('HHVM_VERSION')) {
1435+
$this->markTestSkipped('HHVM does not handle the file:// scheme correctly');
1436+
}
1437+
1438+
$scheme ='file://';
1439+
$filename =$scheme.$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
1440+
$this->filesystem->dumpFile($filename,'foo');
1441+
1442+
$this->filesystem->appendToFile($filename,'bar');
1443+
1444+
$this->assertFileExists($filename);
1445+
$this->assertSame('foobar',file_get_contents($filename));
1446+
}
1447+
1448+
publicfunctiontestAppendToFileWithZlibScheme()
1449+
{
1450+
$scheme ='compress.zlib://';
1451+
$filename =$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
1452+
$this->filesystem->dumpFile($filename,'foo');
1453+
1454+
// Zlib stat uses file:// wrapper so remove it
1455+
$this->assertSame('foo',file_get_contents(str_replace($scheme,'',$filename)));
1456+
1457+
$this->filesystem->appendToFile($filename,'bar');
1458+
1459+
$this->assertFileExists($filename);
1460+
$this->assertSame('foobar',file_get_contents($filename));
1461+
}
1462+
1463+
publicfunctiontestAppendToFileCreateTheFileIfNotExists()
1464+
{
1465+
$filename =$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.txt';
1466+
1467+
// skip mode check on Windows
1468+
if ('\\' !==DIRECTORY_SEPARATOR) {
1469+
$oldMask =umask(0002);
1470+
}
1471+
1472+
$this->filesystem->appendToFile($filename,'bar');
1473+
1474+
// skip mode check on Windows
1475+
if ('\\' !==DIRECTORY_SEPARATOR) {
1476+
$this->assertFilePermissions(664,$filename);
1477+
umask($oldMask);
1478+
}
1479+
1480+
$this->assertFileExists($filename);
1481+
$this->assertSame('bar',file_get_contents($filename));
1482+
}
1483+
14091484
publicfunctiontestCopyShouldKeepExecutionPermission()
14101485
{
14111486
$this->markAsSkippedIfChmodIsMissing();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp