1
0
mirror of https://github.com/flarum/core.git synced 2025-04-22 16:12:05 +02:00

Add a helper class for managing low-level config

This commit is contained in:
Franz Liedke 2020-08-21 14:41:45 +02:00
parent f885cebdc5
commit f869999011
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
2 changed files with 230 additions and 0 deletions

75
src/Foundation/Config.php Normal file
View File

@ -0,0 +1,75 @@
<?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\Foundation;
use ArrayAccess;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Laminas\Diactoros\Uri;
use Psr\Http\Message\UriInterface;
use RuntimeException;
class Config implements ArrayAccess
{
private $data;
public function __construct(array $data)
{
$this->data = $data;
$this->requireKeys('url');
}
public function url(): UriInterface
{
return new Uri(rtrim($this->data['url'], '/'));
}
public function inDebugMode(): bool
{
return $this->data['debug'] ?? false;
}
public function inMaintenanceMode(): bool
{
return $this->data['offline'] ?? false;
}
private function requireKeys(...$keys)
{
foreach ($keys as $key) {
if (! array_key_exists($key, $this->data)) {
throw new InvalidArgumentException(
"Configuration is invalid without a $key key"
);
}
}
}
public function offsetGet($offset)
{
return Arr::get($this->data, $offset);
}
public function offsetExists($offset)
{
return Arr::has($this->data, $offset);
}
public function offsetSet($offset, $value)
{
throw new RuntimeException('The Config is immutable');
}
public function offsetUnset($offset)
{
throw new RuntimeException('The Config is immutable');
}
}

View File

@ -0,0 +1,155 @@
<?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\Tests\unit\Foundation;
use Flarum\Foundation\Config;
use Flarum\Tests\unit\TestCase;
use InvalidArgumentException;
use RuntimeException;
class ConfigTest extends TestCase
{
/** @test */
public function it_complains_when_base_url_is_missing()
{
$this->expectException(InvalidArgumentException::class);
new Config([]);
}
/** @test */
public function it_wraps_base_url_in_value_object()
{
$config = new Config([
'url' => 'https://flarum.local/myforum/',
]);
$url = $config->url();
$this->assertEquals('https', $url->getScheme());
$this->assertEquals('/myforum', $url->getPath()); // Note that trailing slashes are removed
$this->assertEquals('https://flarum.local/myforum', (string) $url);
}
/** @test */
public function it_has_a_helper_for_debug_mode()
{
$config = new Config([
'url' => 'https://flarum.local',
'debug' => false,
]);
$this->assertFalse($config->inDebugMode());
$config = new Config([
'url' => 'https://flarum.local',
'debug' => true,
]);
$this->assertTrue($config->inDebugMode());
}
/** @test */
public function it_turns_off_debug_mode_by_default()
{
$config = new Config([
'url' => 'https://flarum.local',
]);
$this->assertFalse($config->inDebugMode());
}
/** @test */
public function it_has_a_helper_for_maintenance_mode()
{
$config = new Config([
'url' => 'https://flarum.local',
'offline' => false,
]);
$this->assertFalse($config->inMaintenanceMode());
$config = new Config([
'url' => 'https://flarum.local',
'offline' => true,
]);
$this->assertTrue($config->inMaintenanceMode());
}
/** @test */
public function it_turns_off_maintenance_mode_by_default()
{
$config = new Config([
'url' => 'https://flarum.local',
]);
$this->assertFalse($config->inMaintenanceMode());
}
/** @test */
public function it_exposes_additional_keys_via_array_access()
{
$config = new Config([
'url' => 'https://flarum.local',
'custom_a' => 'b',
]);
$this->assertEquals('b', $config['custom_a']);
}
/** @test */
public function it_exposes_nested_keys_via_dot_syntax()
{
$config = new Config([
'url' => 'https://flarum.local',
'nested' => [
'first' => '1',
'second' => '2',
],
]);
$this->assertEquals('1', $config['nested.first']);
$this->assertEquals('2', $config['nested.second']);
}
/** @test */
public function it_does_not_allow_mutation_via_array_access()
{
$config = new Config([
'url' => 'https://flarum.local',
'custom_a' => 'b',
]);
try {
$config['custom_a'] = 'c';
} catch (RuntimeException $_) {
}
// Ensure the value was not changed
$this->assertEquals('b', $config['custom_a']);
}
/** @test */
public function it_does_not_allow_removal_via_array_access()
{
$config = new Config([
'url' => 'https://flarum.local',
'custom_a' => 'b',
]);
try {
unset($config['custom_a']);
} catch (RuntimeException $_) {
}
// Ensure the value was not changed
$this->assertEquals('b', $config['custom_a']);
}
}