mirror of
https://github.com/flarum/core.git
synced 2025-08-17 05:44:13 +02:00
feat: Default Settings Extender (#3127)
Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com>
This commit is contained in:
@@ -15,10 +15,12 @@ use Flarum\Extension\Extension;
|
||||
use Flarum\Foundation\ContainerUtil;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class Settings implements ExtenderInterface
|
||||
{
|
||||
private $settings = [];
|
||||
private $defaults = [];
|
||||
|
||||
/**
|
||||
* Serialize a setting value to the ForumSerializer attributes.
|
||||
@@ -33,7 +35,8 @@ class Settings implements ExtenderInterface
|
||||
* The callable should return:
|
||||
* - mixed $value: The modified value.
|
||||
*
|
||||
* @param mixed $default: Optional default serialized value. Will be run through the optional callback.
|
||||
* @todo remove $default in 2.0
|
||||
* @param mixed $default: Deprecated optional default serialized value. Will be run through the optional callback.
|
||||
* @return self
|
||||
*/
|
||||
public function serializeToForum(string $attributeName, string $key, $callback = null, $default = null): self
|
||||
@@ -43,8 +46,35 @@ class Settings implements ExtenderInterface
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a default value for a setting.
|
||||
* Replaces inserting the default value with a migration.
|
||||
*
|
||||
* @param string $key: The setting key, must be unique. Namespace it with the extension ID (example: 'my-extension-id.setting_key').
|
||||
* @param mixed $value: The setting value.
|
||||
* @return self
|
||||
*/
|
||||
public function default(string $key, $value): self
|
||||
{
|
||||
$this->defaults[$key] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function extend(Container $container, Extension $extension = null)
|
||||
{
|
||||
if (! empty($this->defaults)) {
|
||||
$container->extend('flarum.settings.default', function (Collection $defaults) {
|
||||
foreach ($this->defaults as $key => $value) {
|
||||
if ($defaults->has($key)) {
|
||||
throw new \RuntimeException("Cannot modify immutable default setting $key.");
|
||||
}
|
||||
|
||||
$defaults->put($key, $value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (! empty($this->settings)) {
|
||||
AbstractSerializer::addAttributeMutator(
|
||||
ForumSerializer::class,
|
||||
|
Reference in New Issue
Block a user