Skip to content

Commit

Permalink
[Filesystem] Add third argument $lockFile to `Filesystem::appendToF…
Browse files Browse the repository at this point in the history
…ile()`
  • Loading branch information
fwolfsjaeger authored and nicolas-grekas committed Oct 28, 2021
1 parent e5e81a6 commit 731f917
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `Path` class
* Add `$lock` argument to `Filesystem::appendToFile()`

5.0.0
-----
Expand Down
7 changes: 5 additions & 2 deletions Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,10 +692,11 @@ public function dumpFile(string $filename, $content)
* Appends content to an existing file.
*
* @param string|resource $content The content to append
* @param bool $lock Whether the file should be locked when writing to it
*
* @throws IOException If the file is not writable
*/
public function appendToFile(string $filename, $content)
public function appendToFile(string $filename, $content/*, bool $lock = false*/)
{
if (\is_array($content)) {
throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be string or resource, array given.', __METHOD__));
Expand All @@ -707,7 +708,9 @@ public function appendToFile(string $filename, $content)
$this->mkdir($dir);
}

if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND)) {
$lock = \func_num_args() > 2 && func_get_arg(2);

if (false === self::box('file_put_contents', $filename, $content, \FILE_APPEND | ($lock ? \LOCK_EX : 0))) {
throw new IOException(sprintf('Failed to write file "%s": ', $filename).self::$lastError, 0, null, $filename);
}
}
Expand Down

0 comments on commit 731f917

Please sign in to comment.