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

Appearance settings

Commit fb498a3

Browse filesBrowse files
committed
[Filesystem] Add third argument $lockFile to Filesystem::appendToFile()
1 parent ee6bbb2 commit fb498a3
Copy full SHA for fb498a3

File tree

2 files changed

+11
-2
lines changed
Filter options

2 files changed

+11
-2
lines changed

‎src/Symfony/Component/Filesystem/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `Path` class
8+
* `Filesystem::appendToFile()` now accepts a third argument `$lockFile`
89

910
5.0.0
1011
-----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Filesystem.php
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -692,10 +692,11 @@ public function dumpFile(string $filename, $content)
692692
* Appends content to an existing file.
693693
*
694694
* @param string|resource $content The content to append
695+
* @param bool $lockFile Lock the file when writing to it
695696
*
696697
* @throws IOException If the file is not writable
697698
*/
698-
public function appendToFile(string $filename, $content)
699+
public function appendToFile(string $filename, $content/*,bool $lockFile = false*/)
699700
{
700701
if (\is_array($content)) {
701702
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
@@ -707,7 +708,14 @@ public function appendToFile(string $filename, $content)
707708
$this->mkdir($dir);
708709
}
709710

710-
if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND)) {
711+
$flags = \FILE_APPEND;
712+
$lockFile = \func_num_args() > 2 ? \func_get_arg(2) : '';
713+
714+
if ($lockFile === true) {
715+
$flags |= \LOCK_EX;
716+
}
717+
718+
if (false === self::box('file_put_contents', $filename, $content, $flags)) {
711719
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
712720
}
713721
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.