Skip to content

Commit

Permalink
Forget settings in SetSettingsController
Browse files Browse the repository at this point in the history
  • Loading branch information
OrdinaryJellyfish committed Nov 29, 2023
1 parent 35823ad commit 577bd9a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
18 changes: 16 additions & 2 deletions framework/core/src/Api/Controller/SetSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
use Flarum\Settings\Event;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\EmptyResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Illuminate\Support\Arr;

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

public function __construct(
protected SettingsRepositoryInterface $settings,
protected Dispatcher $dispatcher
Expand All @@ -36,8 +40,18 @@ public function handle(ServerRequestInterface $request): ResponseInterface

foreach ($settings as $k => $v) {
$this->dispatcher->dispatch(new Event\Serializing($k, $v));

$this->settings->set($k, $v);
$forgetCallback = Arr::get(static::$forget, $k);
$shouldForget = false;

if (! is_null($forgetCallback)) {
$shouldForget = $forgetCallback($v);
}

if ($shouldForget) {
$this->settings->delete($k);
} else {
$this->settings->set($k, $v);
}
}

$this->dispatcher->dispatch(new Event\Saved($settings));
Expand Down
16 changes: 7 additions & 9 deletions framework/core/src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

namespace Flarum\Extend;

use Flarum\Api\Controller\SetSettingsController;
use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\Collection;
use Illuminate\Support\Arr;

class Settings implements ExtenderInterface
{
Expand Down Expand Up @@ -112,16 +114,12 @@ public function extend(Container $container, Extension $extension = null): void
}

if (! empty($this->forget)) {
$settings = $container->make(SettingsRepositoryInterface::class);

foreach ($this->forget as $key => $callback) {
$value = $settings->get($key);
$callback = ContainerUtil::wrapCallback($callback, $container);
$shouldForget = $callback($value);

if ($shouldForget) {
$settings->delete($key);
}
Arr::set(
SetSettingsController::$forget,
$key,
ContainerUtil::wrapCallback($callback, $container)
);
}
}

Expand Down
11 changes: 9 additions & 2 deletions framework/core/tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,6 @@ public function null_custom_setting_returns_null()
*/
public function forgetting_setting_returns_default_value()
{
$this->setting('custom-prefix.forget_this_setting', '');

$this->extend(
(new Extend\Settings())
->default('custom-prefix.forget_this_setting', 'extenderDefault')
Expand All @@ -192,6 +190,15 @@ public function forgetting_setting_returns_default_value()
})
);

$this->send(
$this->request('POST', '/api/settings', [
'authenticatedAs' => 1,
'json' => [
'custom-prefix.forget_this_setting' => ''
]
])
);

$value = $this->app()
->getContainer()
->make('flarum.settings')
Expand Down

0 comments on commit 577bd9a

Please sign in to comment.