Skip to content

Commit

Permalink
Switch to an array of defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Oct 27, 2021
1 parent d30cb39 commit 6aea45f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 48 deletions.
8 changes: 6 additions & 2 deletions src/Extend/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ public function default(string $key, $value): self
public function extend(Container $container, Extension $extension = null)
{
if (! empty($this->defaults)) {
$container->extend(DefaultSettingsManager::class, function (DefaultSettingsManager $manager) {
$container->extend('flarum.settings.default', function (array $settings) {
foreach ($this->defaults as $key => $default) {
$manager->set($key, $default);
if (! isset($settings[$key])) {
$settings[$key] = $default;
}
}

return $settings;
});
}

Expand Down
27 changes: 0 additions & 27 deletions src/Settings/DefaultSettingsManager.php

This file was deleted.

16 changes: 7 additions & 9 deletions src/Settings/MemoryCacheSettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ class MemoryCacheSettingsRepository implements SettingsRepositoryInterface

protected $cache = [];

protected $defaultSettingsManager;
protected $defaultSettings = [];

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

public function all(): array
{
if (! $this->isCached) {
$this->cache = $this->inner->all();
$this->cache = array_merge($this->defaultSettings, $this->inner->all());
$this->isCached = true;
}

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

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

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

return $value;
return $default;
}

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

$this->container->alias(DefaultSettingsManager::class, 'flarum.default_settings');
$this->container->singleton('flarum.settings.default', function () {
return [];
});

$this->container->singleton(SettingsRepositoryInterface::class, function (Container $container) {
return new MemoryCacheSettingsRepository(
new DatabaseSettingsRepository(
$container->make(ConnectionInterface::class)
),
$this->container->make(DefaultSettingsManager::class)
$this->container->make('flarum.settings.default')
);
});

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

namespace Flarum\Tests\unit\Settings;

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

/**
Expand All @@ -27,8 +25,7 @@ class MemoryCacheSettingsRepositoryTest extends TestCase
protected function setUp(): void
{
$this->baseRepository = m::mock(SettingsRepositoryInterface::class);
$this->defaultSettingsManager = m::mock(DefaultSettingsManager::class);
$this->repository = new MemoryCacheSettingsRepository($this->baseRepository, $this->defaultSettingsManager);
$this->repository = new MemoryCacheSettingsRepository($this->baseRepository, []);
}

public function test_it_should_return_all_settings_when_not_cached()
Expand All @@ -42,7 +39,6 @@ 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 @@ -51,7 +47,6 @@ 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 6aea45f

Please sign in to comment.