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

Add Settings Extender #2452

Merged
merged 7 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
87 changes: 87 additions & 0 deletions src/Extend/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Extend;

use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Api\Serializer\ForumSerializer;
use Flarum\Extension\Extension;
use Flarum\Foundation\ContainerUtil;
use Flarum\Settings\DefaultSettingsManager;
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.
*
* @param string $attributeName: The attribute name to be used in the ForumSerializer attributes array.
* @param string $key: The key of the setting.
* @param string|callable|null $callback: Optional callback to modify the value before serialization.
* @return $this
*/
public function serializeToForum(string $attributeName, string $key, $callback = null)
{
$this->settings[$key] = compact('attributeName', 'callback');

return $this;
}

/**
* Set a default value for a setting selected for serialization.
*
* @param string $key
* @param $value
* @return $this
*/
public function default(string $key, $value)
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
{
$this->defaults[$key] = $value;

return $this;
}

public function extend(Container $container, Extension $extension = null)
{
if (! empty($this->defaults)) {
$container->extend(DefaultSettingsManager::class, function (DefaultSettingsManager $manager) {
foreach ($this->defaults as $key => $default) {
$manager->set($key, $default);
}
});
}

if (! empty($this->settings)) {
AbstractSerializer::addMutator(
ForumSerializer::class,
function () use ($container) {
$settings = $container->make(SettingsRepositoryInterface::class);
$attributes = [];

foreach ($this->settings as $key => $setting) {
$value = $settings->get($key, null);

if (isset($setting['callback'])) {
$setting['callback'] = ContainerUtil::wrapCallback($setting['callback'], $container);
SychO9 marked this conversation as resolved.
Show resolved Hide resolved
$value = $setting['callback']($value);
}

$attributes[$setting['attributeName']] = $value;
}

return $attributes;
}
);
}
}
}
27 changes: 27 additions & 0 deletions src/Settings/DefaultSettingsManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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 Illuminate\Support\Arr;

class DefaultSettingsManager
{
protected $defaults = [];

public function get($key, $default = null)
{
return Arr::get($this->defaults, $key, $default);
}

public function set($key, $value)
{
$this->defaults[$key] = $value;
}
}
13 changes: 9 additions & 4 deletions src/Settings/MemoryCacheSettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ class MemoryCacheSettingsRepository implements SettingsRepositoryInterface
{
protected $inner;

protected $defaultSettingsManager;

protected $isCached;

protected $cache = [];

public function __construct(SettingsRepositoryInterface $inner)
public function __construct(SettingsRepositoryInterface $inner, DefaultSettingsManager $defaultSettingsManager)
{
$this->inner = $inner;
$this->defaultSettingsManager = $defaultSettingsManager;
}

public function all(): array
Expand All @@ -36,13 +39,15 @@ public function all(): array

public function get($key, $default = null)
{
$value = $this->defaultSettingsManager->get($key, $default);

if (array_key_exists($key, $this->cache)) {
return $this->cache[$key];
$value = $this->cache[$key];
} elseif (! $this->isCached) {
return Arr::get($this->all(), $key, $default);
$value = Arr::get($this->all(), $key, $value);
}

return $default;
return $value;
}

public function set($key, $value)
Expand Down
5 changes: 4 additions & 1 deletion src/Settings/SettingsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ class SettingsServiceProvider extends AbstractServiceProvider
*/
public function register()
{
$this->app->singleton(DefaultSettingsManager::class);

$this->app->singleton(SettingsRepositoryInterface::class, function () {
return new MemoryCacheSettingsRepository(
new DatabaseSettingsRepository(
$this->app->make(ConnectionInterface::class)
)
),
$this->app->make(DefaultSettingsManager::class)
);
});

Expand Down
9 changes: 0 additions & 9 deletions tests/integration/extenders/ApiSerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ protected function prepDb()
]);
}

protected function prepSettingsDb()
{
$this->prepareDatabase([
'settings' => [
['key' => 'customPrefix.customSetting', 'value' => 'customValue']
],
]);
}

/**
* @test
*/
Expand Down
Loading