diff --git a/framework/core/src/Api/Controller/SetSettingsController.php b/framework/core/src/Api/Controller/SetSettingsController.php index f587ec92c9..eca891eacb 100644 --- a/framework/core/src/Api/Controller/SetSettingsController.php +++ b/framework/core/src/Api/Controller/SetSettingsController.php @@ -21,7 +21,7 @@ class SetSettingsController implements RequestHandlerInterface { - public static array $filter = []; + public static array $resetWhen = []; public function __construct( protected SettingsRepositoryInterface $settings, @@ -39,14 +39,8 @@ public function handle(ServerRequestInterface $request): ResponseInterface foreach ($settings as $k => $v) { $this->dispatcher->dispatch(new Event\Serializing($k, $v)); - $filterCallback = Arr::get(static::$filter, $k); - $shouldFilter = false; - if (! is_null($filterCallback)) { - $shouldFilter = $filterCallback($v); - } - - if ($shouldFilter) { + if (! is_null($resetWhen = Arr::get(static::$resetWhen, $k)) && $resetWhen($v)) { $this->settings->delete($k); } else { $this->settings->set($k, $v); diff --git a/framework/core/src/Extend/Settings.php b/framework/core/src/Extend/Settings.php index 2a6d803944..e177146761 100644 --- a/framework/core/src/Extend/Settings.php +++ b/framework/core/src/Extend/Settings.php @@ -26,7 +26,7 @@ class Settings implements ExtenderInterface private array $defaults = []; private array $lessConfigs = []; private array $resetJsCacheFor = []; - private array $filter = []; + private array $resetWhen = []; /** * Serialize a setting value to the ForumSerializer attributes. @@ -66,13 +66,15 @@ public function default(string $key, mixed $value): self } /** - * Reset a setting to default when callback returns true. + * Delete a custom setting value when the callback returns true. + * This allows the setting to be reset to its default value. + * * @param string $key: The key of the setting. - * @param (callable(mixed $value): bool)|bool $callback: Boolean to determine whether the setting needs deleted. + * @param (callable(mixed $value): bool) $callback: The callback to determine if the setting should be reset. */ - public function filter(string $key, callable|bool $callback): self + public function resetWhen(string $key, callable|string $callback): self { - $this->filter[$key] = $callback; + $this->resetWhen[$key] = $callback; return $this; } @@ -128,10 +130,10 @@ public function extend(Container $container, Extension $extension = null): void }); } - if (! empty($this->filter)) { - foreach ($this->filter as $key => $callback) { + if (! empty($this->resetWhen)) { + foreach ($this->resetWhen as $key => $callback) { Arr::set( - SetSettingsController::$filter, + SetSettingsController::$resetWhen, $key, ContainerUtil::wrapCallback($callback, $container) ); diff --git a/framework/core/tests/integration/extenders/SettingsTest.php b/framework/core/tests/integration/extenders/SettingsTest.php index 0787cb0c0f..c96a882c7d 100644 --- a/framework/core/tests/integration/extenders/SettingsTest.php +++ b/framework/core/tests/integration/extenders/SettingsTest.php @@ -188,12 +188,12 @@ public function null_custom_setting_returns_null() } #[Test] - public function filtering_setting_returns_default_value() + public function resetting_setting_returns_default_value() { $this->extend( (new Extend\Settings()) ->default('custom-prefix.filter_this_setting', 'extenderDefault') - ->filter('custom-prefix.filter_this_setting', function (mixed $value): bool { + ->resetWhen('custom-prefix.filter_this_setting', function (mixed $value): bool { return $value === ''; }) ); @@ -215,6 +215,26 @@ public function filtering_setting_returns_default_value() $this->assertEquals('extenderDefault', $value); } + #[Test] + public function not_resetting_setting_returns_value() + { + $this->send( + $this->request('POST', '/api/settings', [ + 'authenticatedAs' => 1, + 'json' => [ + 'custom-prefix.filter_this_setting' => '' + ] + ]) + ); + + $value = $this->app() + ->getContainer() + ->make('flarum.settings') + ->get('custom-prefix.filter_this_setting'); + + $this->assertEquals('', $value); + } + #[Test] public function custom_less_var_does_not_work_by_default() {