aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Inverle <inverle@proton.me> 2025-11-16 18:42:27 +0100
committerGravatar GitHub <noreply@github.com> 2025-11-16 18:42:27 +0100
commitcf4d8043d2076ba346e9890a41984d458e94c8dc (patch)
tree20f259058273be6e5855b806e493536916e5a288 /lib
parent9050598b08fd426aafe1a27ccea0ba445134a7ea (diff)
Rework saving of configuration files (#8220)
* Attempt to restore user config if `file_put_contents()` fails * Second approach * Minor preference: `.tmp.php` Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr> * Change boolean compare syntax Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr> * fix?
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Configuration.php14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/Minz/Configuration.php b/lib/Minz/Configuration.php
index 0d6c8b1f3..e0480b414 100644
--- a/lib/Minz/Configuration.php
+++ b/lib/Minz/Configuration.php
@@ -210,14 +210,22 @@ class Minz_Configuration {
* Save the current configuration in the configuration file.
*/
public function save(): bool {
+ $tmp_filename = $this->config_filename . '.tmp.php';
$back_filename = $this->config_filename . '.bak.php';
- @rename($this->config_filename, $back_filename);
- if (file_put_contents($this->config_filename,
- "<?php\nreturn " . var_export($this->data, true) . ';', LOCK_EX) === false) {
+ if (!file_put_contents($tmp_filename,
+ "<?php\nreturn " . var_export($this->data, true) . ';', LOCK_EX)) {
+ @unlink($tmp_filename);
return false;
}
+ if (!copy($this->config_filename, $back_filename)) {
+ @unlink($tmp_filename);
+ return false;
+ }
+
+ @rename($tmp_filename, $this->config_filename);
+
// Clear PHP cache for include
if (function_exists('opcache_invalidate')) {
opcache_invalidate($this->config_filename);