From 8c6d672a2b85bde15e005d15b6dd995c7d7b584f Mon Sep 17 00:00:00 2001 From: Summer Date: Mon, 7 Mar 2022 19:42:53 +0800 Subject: [PATCH] when setting file not exists, not crashing --- src/helpers.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/helpers.php b/src/helpers.php index c03c3af..325d770 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -6,7 +6,12 @@ function setting($key, $default = '', $setting_name = 'site') { if ( ! config()->get($setting_name)) { // Decode the settings to an associative array. - $site_settings = json_decode(file_get_contents(storage_path("/administrator_settings/$setting_name.json")), true); + $file_path = storage_path("/administrator_settings/$setting_name.json"); + if (file_exists($file_path)) { + $site_settings = json_decode(file_get_contents($file_path), true); + } else { + $site_settings = []; + } // Add the site settings to the application configuration config()->set($setting_name, $site_settings); } @@ -21,7 +26,12 @@ function admin_setting($key, $default = '', $setting_name = 'site') { if ( ! config()->get($setting_name)) { // Decode the settings to an associative array. - $site_settings = json_decode(file_get_contents(storage_path("/administrator_settings/$setting_name.json")), true); + $file_path = storage_path("/administrator_settings/$setting_name.json"); + if (file_exists($file_path)) { + $site_settings = json_decode(file_get_contents($file_path), true); + } else { + $site_settings = []; + } // Add the site settings to the application configuration config()->set($setting_name, $site_settings); }