Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow resetting settings to default #3935

Merged
merged 8 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
use Flarum\Settings\Event;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Arr;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class SetSettingsController implements RequestHandlerInterface
{
public static array $resetWhen = [];

public function __construct(
protected SettingsRepositoryInterface $settings,
protected Dispatcher $dispatcher
Expand All @@ -37,7 +40,11 @@ public function handle(ServerRequestInterface $request): ResponseInterface
foreach ($settings as $k => $v) {
$this->dispatcher->dispatch(new Event\Serializing($k, $v));

$this->settings->set($k, $v);
if (! is_null($resetWhen = Arr::get(static::$resetWhen, $k)) && $resetWhen($v)) {
$this->settings->delete($k);
} else {
$this->settings->set($k, $v);
}
}

$this->dispatcher->dispatch(new Event\Saved($settings));
Expand Down
27 changes: 27 additions & 0 deletions framework/core/src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
namespace Flarum\Extend;

use Flarum\Admin\WhenSavingSettings;
use Flarum\Api\Controller\SetSettingsController;
use Flarum\Api\Resource\ForumResource;
use Flarum\Api\Schema\Attribute;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class Settings implements ExtenderInterface
Expand All @@ -24,6 +26,7 @@ class Settings implements ExtenderInterface
private array $defaults = [];
private array $lessConfigs = [];
private array $resetJsCacheFor = [];
private array $resetWhen = [];

/**
* Serialize a setting value to the ForumSerializer attributes.
Expand Down Expand Up @@ -62,6 +65,20 @@ public function default(string $key, mixed $value): self
return $this;
}

/**
* 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) $callback: The callback to determine if the setting should be reset.
*/
public function resetWhen(string $key, callable|string $callback): self
{
$this->resetWhen[$key] = $callback;

return $this;
}

/**
* Register a setting as a LESS configuration variable.
*
Expand Down Expand Up @@ -113,6 +130,16 @@ public function extend(Container $container, Extension $extension = null): void
});
}

if (! empty($this->resetWhen)) {
foreach ($this->resetWhen as $key => $callback) {
Arr::set(
SetSettingsController::$resetWhen,
$key,
ContainerUtil::wrapCallback($callback, $container)
);
}
}

if (! empty($this->settings)) {
(new ApiResource(ForumResource::class))
->fields(function () use ($container) {
Expand Down
48 changes: 48 additions & 0 deletions framework/core/tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,54 @@ public function null_custom_setting_returns_null()
$this->assertEquals(null, $value);
}

#[Test]
public function resetting_setting_returns_default_value()
{
$this->extend(
(new Extend\Settings())
->default('custom-prefix.filter_this_setting', 'extenderDefault')
->resetWhen('custom-prefix.filter_this_setting', function (mixed $value): bool {
return $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('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()
{
Expand Down
Loading