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: Default Settings Repository #3127

Merged
merged 18 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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
4 changes: 4 additions & 0 deletions src/Database/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Database;

use Flarum\Extend\Settings;
use Flarum\Settings\DatabaseSettingsRepository;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
Expand Down Expand Up @@ -120,6 +121,9 @@ public static function renameColumns($tableName, array $columnNames)

/**
* Add default values for config values.
*
* @deprecated Use the Settings extender's `default` method instead to register settings.
* @see Settings::default()
*/
public static function addSettings(array $defaults)
{
Expand Down
28 changes: 27 additions & 1 deletion src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Flarum\Settings\DefaultSettingsRepository;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;

class Settings implements ExtenderInterface
{
private $settings = [];
private $defaults = [];

/**
* Serialize a setting value to the ForumSerializer attributes.
Expand All @@ -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
Expand All @@ -43,8 +46,31 @@ public function serializeToForum(string $attributeName, string $key, $callback =
return $this;
}

/**
* Set a default value for a setting.
* Replaces inserting the default value with a migration.
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
*
* @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(DefaultSettingsRepository::class, function (DefaultSettingsRepository $defaultSettings) {
foreach ($this->defaults as $key => $default) {
$defaultSettings->default($key, $default);
}
});
}

if (! empty($this->settings)) {
AbstractSerializer::addAttributeMutator(
ForumSerializer::class,
Expand Down
53 changes: 53 additions & 0 deletions src/Settings/DefaultSettingsRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Settings;

use RuntimeException;

class DefaultSettingsRepository implements SettingsRepositoryInterface
{
protected $defaults = [];

private $inner;

public function setInner(SettingsRepositoryInterface $inner): void
{
$this->inner = $inner;
}

public function default(string $key, $value): void
{
if (isset($this->defaults[$key])) {
throw new RuntimeException("Cannot modify immutable default setting $key.");
}

$this->defaults[$key] = $value;
}

public function get($key, $default = null)
{
return $this->inner->get($key, $this->defaults[$key] ?? $default);
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
}

public function set($key, $value)
{
$this->inner->set($key, $value);
}

public function delete($keyLike)
{
$this->inner->delete($keyLike);
}

public function all(): array
{
return array_merge($this->defaults, $this->inner->all());
}
}
7 changes: 7 additions & 0 deletions src/Settings/SettingsRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ interface SettingsRepositoryInterface
{
public function all(): array;

/**
* @todo remove $default in 2.0
*
* @param $key
* @param mixed $default: Deprecated
* @return mixed
*/
public function get($key, $default = null);

public function set($key, $value);
Expand Down
15 changes: 12 additions & 3 deletions src/Settings/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,21 @@ class SettingsServiceProvider extends AbstractServiceProvider
*/
public function register()
{
$this->container->singleton(DefaultSettingsRepository::class);

$this->container->alias(DefaultSettingsRepository::class, 'flarum.settings.default');

$this->container->singleton(SettingsRepositoryInterface::class, function (Container $container) {
return new MemoryCacheSettingsRepository(
new DatabaseSettingsRepository(
$container->make(ConnectionInterface::class)
$defaults = $container->make(DefaultSettingsRepository::class);
$defaults->setInner(
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
new MemoryCacheSettingsRepository(
new DatabaseSettingsRepository(
$container->make(ConnectionInterface::class)
)
)
);

return $defaults;
});

$this->container->alias(SettingsRepositoryInterface::class, 'flarum.settings');
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,43 @@ public function custom_setting_default_passed_to_callback()
$this->assertArrayHasKey('customPrefix.noCustomSetting', $payload['data']['attributes']);
$this->assertEquals('customDefaultModified2', $payload['data']['attributes']['customPrefix.noCustomSetting']);
}

/**
* @test
*/
public function custom_setting_default_prioritizes_extender()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting3', 'custom-prefix.unavailable_custom_setting3')
->default('custom-prefix.unavailable_custom_setting3', 'extenderDefault')
);

$value = $this->app()
->getContainer()
->make('flarum.settings')
->get('custom-prefix.unavailable_custom_setting3', 'defaultParameterValue');

$this->assertEquals('extenderDefault', $value);
}

/**
* @test
*/
public function custom_setting_default_falls_back_to_parameter()
{
$this->extend(
(new Extend\Settings())
->serializeToForum('customPrefix.unavailableCustomSetting4', 'custom-prefix.unavailable_custom_setting4')
);

$value = $this->app()
->getContainer()
->make('flarum.settings')
->get('custom-prefix.unavailable_custom_setting4', 'defaultParameterValue');

$this->assertEquals('defaultParameterValue', $value);
}
}

class CustomInvokableClass
Expand Down