Skip to content

Commit

Permalink
Inject provided default values in the repository level
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Dec 3, 2020
1 parent b94a2c0 commit 13d6708
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;

Expand Down Expand Up @@ -52,6 +53,14 @@ public function default(string $key, $value)

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,
Expand Down
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;
}
}
15 changes: 10 additions & 5 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];
} elseif (! $this->isCached) {
return Arr::get($this->all(), $key, $default);
$value = $this->cache[$key];
} else if (! $this->isCached) {
$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
41 changes: 41 additions & 0 deletions tests/integration/extenders/SettingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,47 @@ public function custom_setting_callback_works_on_default_value()
$this->assertArrayHasKey('customPrefix.unavailableCustomSetting2', $payload['data']['attributes']);
$this->assertEquals('defaultModified', $payload['data']['attributes']['customPrefix.unavailableCustomSetting2']);
}

/**
* @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')
);

$this->prepDb();

$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')
);

$this->prepDb();

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

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

class CustomInvokableClass
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/Settings/MemoryCacheSettingsRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Flarum\Tests\unit\Settings;

use Flarum\Settings\DefaultSettingsManager;
use Flarum\Settings\MemoryCacheSettingsRepository;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tests\unit\TestCase;
Expand All @@ -17,12 +18,14 @@
class MemoryCacheSettingsRepositoryTest extends TestCase
{
private $baseRepository;
private $defaultSettingsManager;
private $repository;

protected function setUp(): void
{
$this->baseRepository = m::mock(SettingsRepositoryInterface::class);
$this->repository = new MemoryCacheSettingsRepository($this->baseRepository);
$this->defaultSettingsManager = m::mock(DefaultSettingsManager::class);
$this->repository = new MemoryCacheSettingsRepository($this->baseRepository, $this->defaultSettingsManager);
}

public function test_it_should_return_all_settings_when_not_cached()
Expand All @@ -36,6 +39,7 @@ public function test_it_should_return_all_settings_when_not_cached()
public function test_it_should_retrieve_a_specific_value()
{
$this->baseRepository->shouldReceive('all')->once()->andReturn(['key1' => 'value1', 'key2' => 'value2']);
$this->defaultSettingsManager->shouldReceive('get')->twice();

$this->assertEquals('value2', $this->repository->get('key2'));
$this->assertEquals('value2', $this->repository->get('key2')); // Assert twice to ensure we hit the cache
Expand All @@ -44,6 +48,7 @@ public function test_it_should_retrieve_a_specific_value()
public function test_it_should_set_a_key_value_pair()
{
$this->baseRepository->shouldReceive('set')->once();
$this->defaultSettingsManager->shouldReceive('get')->once();

$this->repository->set('key', 'value');

Expand Down

0 comments on commit 13d6708

Please sign in to comment.