Skip to content

[Filesystem] Add appendToFile() #20612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.3.0
-----

* added `appendToFile()` to append contents to existing files

3.2.0
-----

Expand Down
27 changes: 26 additions & 1 deletion src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public function tempnam($dir, $prefix)
* @param string $filename The file to be written to
* @param string $content The data to write into the file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For using file_put_contents we technically allow string|array|resource.

Copy link
Member Author

@chalasr chalasr Dec 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, updated on the two newly added methods.
Since this targets master, would you mind to open a PR against 2.8 (it doesn't handle streams in 2.7, see 0fc513e) for changing this docblock? I think it's worth it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

*
* @throws IOException If the file cannot be written to.
* @throws IOException If the file cannot be written to
*/
public function dumpFile($filename, $content)
{
Expand All @@ -648,6 +648,31 @@ public function dumpFile($filename, $content)
$this->rename($tmpFile, $filename, true);
}

/**
* Appends content to an existing file.
*
* @param string $filename The file to which to append content
* @param string $content The content to append
*
* @throws IOException If the file is not writable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you remove the capital letter above, you should do the same here, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted the removal of the capital letter

*/
public function appendToFile($filename, $content)
{
$dir = dirname($filename);

if (!is_dir($dir)) {
$this->mkdir($dir);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have to check here too that the directory is writable after we created it (the directory might have been created by another process)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, done. The same could be applied on dumpFile() I think

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think you are right. Should be done on 2.7 then as bug fix IMO.

}

if (!is_writable($dir)) {
throw new IOException(sprintf('Unable to write to the "%s" directory.', $dir), 0, null, $dir);
}

if (false === @file_put_contents($filename, $content, FILE_APPEND)) {
throw new IOException(sprintf('Failed to write file "%s".', $filename), 0, null, $filename);
}
}

/**
* @param mixed $files
*
Expand Down
75 changes: 75 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,81 @@ public function testDumpFileWithZlibScheme()
$this->assertSame('bar', file_get_contents($filename));
}

public function testAppendToFile()
{
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.txt';

// skip mode check on Windows
if ('\\' !== DIRECTORY_SEPARATOR) {
$oldMask = umask(0002);
}

$this->filesystem->dumpFile($filename, 'foo');

$this->filesystem->appendToFile($filename, 'bar');

$this->assertFileExists($filename);
$this->assertSame('foobar', file_get_contents($filename));

// skip mode check on Windows
if ('\\' !== DIRECTORY_SEPARATOR) {
$this->assertFilePermissions(664, $filename, 'The written file should keep the same permissions as before.');
umask($oldMask);
}
}

public function testAppendToFileWithScheme()
{
if (defined('HHVM_VERSION')) {
$this->markTestSkipped('HHVM does not handle the file:// scheme correctly');
}

$scheme = 'file://';
$filename = $scheme.$this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
$this->filesystem->dumpFile($filename, 'foo');

$this->filesystem->appendToFile($filename, 'bar');

$this->assertFileExists($filename);
$this->assertSame('foobar', file_get_contents($filename));
}

public function testAppendToFileWithZlibScheme()
{
$scheme = 'compress.zlib://';
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'baz.txt';
$this->filesystem->dumpFile($filename, 'foo');

// Zlib stat uses file:// wrapper so remove it
$this->assertSame('foo', file_get_contents(str_replace($scheme, '', $filename)));

$this->filesystem->appendToFile($filename, 'bar');

$this->assertFileExists($filename);
$this->assertSame('foobar', file_get_contents($filename));
}

public function testAppendToFileCreateTheFileIfNotExists()
{
$filename = $this->workspace.DIRECTORY_SEPARATOR.'foo'.DIRECTORY_SEPARATOR.'bar.txt';

// skip mode check on Windows
if ('\\' !== DIRECTORY_SEPARATOR) {
$oldMask = umask(0002);
}

$this->filesystem->appendToFile($filename, 'bar');

// skip mode check on Windows
if ('\\' !== DIRECTORY_SEPARATOR) {
$this->assertFilePermissions(664, $filename);
umask($oldMask);
}

$this->assertFileExists($filename);
$this->assertSame('bar', file_get_contents($filename));
}

public function testCopyShouldKeepExecutionPermission()
{
$this->markAsSkippedIfChmodIsMissing();
Expand Down