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 cab3c11

Browse filesBrowse files
feature #43796 [Filesystem] Add third argument $lockFile to Filesystem::appendToFile() (fwolfsjaeger)
This PR was merged into the 5.4 branch. Discussion ---------- [Filesystem] Add third argument `$lockFile` to `Filesystem::appendToFile()` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a I have to set the LOCK_EX flag for file_put_contents() as I'm writing to the same file from parallel threads. Therefor I've added a third argument to `Filesystem::appendToFile()` named `$flags`. Commits ------- 1519828 [Filesystem] Add third argument `$lockFile` to `Filesystem::appendToFile()`
2 parents 20801bc + 1519828 commit cab3c11
Copy full SHA for cab3c11

File tree

2 files changed

+6
-2
lines changed
Filter options

2 files changed

+6
-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+
* Add `$lock` argument to `Filesystem::appendToFile()`
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
+5-2Lines changed: 5 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 $lock Whether the file should be locked 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 $lock = 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,9 @@ 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+
$lock = \func_num_args() > 2 && func_get_arg(2);
712+
713+
if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND | ($lock ? \LOCK_EX : 0))) {
711714
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
712715
}
713716
}

0 commit comments

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