From c82ed9dbf335ba9434d35b98becde3dfeaeaae96 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Wed, 24 Apr 2024 23:28:33 +0200 Subject: [PATCH] Remove a metadata file if empty when saving --- app/Classes/MetadataManager.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/Classes/MetadataManager.php b/app/Classes/MetadataManager.php index 229a011..32cc7c4 100644 --- a/app/Classes/MetadataManager.php +++ b/app/Classes/MetadataManager.php @@ -65,11 +65,15 @@ public function isDirty(): bool */ public function save(): bool { - if (!$this->isDirty() || empty($this->content)) { + if (!$this->isDirty()) { return false; } - $this->writeToDisk($this->content); + if (empty($this->content)) { + $this->disk->delete($this->filename); + } else { + $this->writeToDisk($this->content); + } $this->originalContent = $this->content; @@ -94,10 +98,14 @@ public function set($key, $value, bool $respectDots = true) /** * Set many values at once */ - public function setMany(array $array) + public function setMany(array $array, bool $replace = false) { - foreach ($array as $key => $value) { - $this->set($key, $value); + if (!$replace) { + foreach ($array as $key => $value) { + $this->set($key, $value); + } + } else { + $this->content = $array; } }