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 dfadcc39c..8df9641f3 100644 --- a/Filesystem.php +++ b/Filesystem.php @@ -665,10 +665,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__)); @@ -680,7 +681,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); } }