From 731f917dc31edcffec2c6a777f3698c33bea8f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Wolfsj=C3=A4ger?= Date: Thu, 28 Oct 2021 10:31:13 +0200 Subject: [PATCH] [Filesystem] Add third argument `$lockFile` to `Filesystem::appendToFile()` --- CHANGELOG.md | 1 + Filesystem.php | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41e6405ee..fcb7170ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG --- * Add `Path` class + * Add `$lock` argument to `Filesystem::appendToFile()` 5.0.0 ----- diff --git a/Filesystem.php b/Filesystem.php index 4ec9cb24c..524d17c2b 100644 --- a/Filesystem.php +++ b/Filesystem.php @@ -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__)); @@ -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); } }